add relay proxy
This commit is contained in:
@ -76,12 +76,12 @@ func (g *NodeGroup) Next() *Node {
|
||||
return nil
|
||||
}
|
||||
|
||||
selector := g.selector
|
||||
if selector == nil {
|
||||
return g.nodes[0]
|
||||
s := g.selector
|
||||
if s == nil {
|
||||
s = DefaultSelector
|
||||
}
|
||||
|
||||
return selector.Select(g.nodes...)
|
||||
return s.Select(g.nodes...)
|
||||
}
|
||||
|
||||
type FailMarker struct {
|
||||
|
@ -14,6 +14,10 @@ const (
|
||||
DefaultFailTimeout = 30 * time.Second
|
||||
)
|
||||
|
||||
var (
|
||||
DefaultSelector = NewSelector(RoundRobinStrategy())
|
||||
)
|
||||
|
||||
type Selector interface {
|
||||
Select(nodes ...*Node) *Node
|
||||
}
|
||||
|
58
pkg/common/util/relay/conn.go
Normal file
58
pkg/common/util/relay/conn.go
Normal file
@ -0,0 +1,58 @@
|
||||
package relay
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"io"
|
||||
"math"
|
||||
"net"
|
||||
)
|
||||
|
||||
type packetConn struct {
|
||||
net.Conn
|
||||
}
|
||||
|
||||
func UDPTunConn(conn net.Conn) net.Conn {
|
||||
return &packetConn{
|
||||
Conn: conn,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *packetConn) Read(b []byte) (n int, err error) {
|
||||
var bb [2]byte
|
||||
_, err = io.ReadFull(c.Conn, bb[:])
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
dlen := int(binary.BigEndian.Uint16(bb[:]))
|
||||
if len(b) >= dlen {
|
||||
return io.ReadFull(c.Conn, b[:dlen])
|
||||
}
|
||||
buf := make([]byte, dlen)
|
||||
_, err = io.ReadFull(c.Conn, buf)
|
||||
n = copy(b, buf)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (c *packetConn) ReadFrom(b []byte) (n int, addr net.Addr, err error) {
|
||||
n, err = c.Read(b)
|
||||
addr = c.Conn.RemoteAddr()
|
||||
return
|
||||
}
|
||||
|
||||
func (c *packetConn) Write(b []byte) (n int, err error) {
|
||||
if len(b) > math.MaxUint16 {
|
||||
err = errors.New("write: data maximum exceeded")
|
||||
return
|
||||
}
|
||||
|
||||
var bb [2]byte
|
||||
binary.BigEndian.PutUint16(bb[:2], uint16(len(b)))
|
||||
_, err = c.Conn.Write(bb[:])
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return c.Conn.Write(b)
|
||||
}
|
@ -2,9 +2,11 @@ package config
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -19,9 +21,9 @@ func init() {
|
||||
}
|
||||
|
||||
type LogConfig struct {
|
||||
Output string
|
||||
Level string
|
||||
Format string
|
||||
Output string `yaml:",omitempty"`
|
||||
Level string `yaml:",omitempty"`
|
||||
Format string `yaml:",omitempty"`
|
||||
}
|
||||
|
||||
type ProfilingConfig struct {
|
||||
@ -37,25 +39,22 @@ type SelectorConfig struct {
|
||||
|
||||
type BypassConfig struct {
|
||||
Name string
|
||||
Reverse bool
|
||||
Reverse bool `yaml:",omitempty"`
|
||||
Matchers []string
|
||||
}
|
||||
type ListenerConfig struct {
|
||||
Type string
|
||||
Chain string
|
||||
Metadata map[string]interface{}
|
||||
Metadata map[string]interface{} `yaml:",omitempty"`
|
||||
}
|
||||
|
||||
type HandlerConfig struct {
|
||||
Type string
|
||||
Chain string
|
||||
Bypass string
|
||||
Metadata map[string]interface{}
|
||||
Metadata map[string]interface{} `yaml:",omitempty"`
|
||||
}
|
||||
|
||||
type ForwarderConfig struct {
|
||||
Targets []string
|
||||
Selector *SelectorConfig
|
||||
Selector *SelectorConfig `yaml:",omitempty"`
|
||||
}
|
||||
|
||||
type DialerConfig struct {
|
||||
@ -70,40 +69,42 @@ type ConnectorConfig struct {
|
||||
|
||||
type ServiceConfig struct {
|
||||
Name string
|
||||
URL string
|
||||
Addr string
|
||||
Listener *ListenerConfig
|
||||
Handler *HandlerConfig
|
||||
Forwarder *ForwarderConfig
|
||||
URL string `yaml:",omitempty"`
|
||||
Addr string `yaml:",omitempty"`
|
||||
Chain string `yaml:",omitempty"`
|
||||
Bypass string `yaml:",omitempty"`
|
||||
Listener *ListenerConfig `yaml:",omitempty"`
|
||||
Handler *HandlerConfig `yaml:",omitempty"`
|
||||
Forwarder *ForwarderConfig `yaml:",omitempty"`
|
||||
}
|
||||
|
||||
type ChainConfig struct {
|
||||
Name string
|
||||
Selector *SelectorConfig
|
||||
Hops []HopConfig
|
||||
Selector *SelectorConfig `yaml:",omitempty"`
|
||||
Hops []*HopConfig
|
||||
}
|
||||
|
||||
type HopConfig struct {
|
||||
Name string
|
||||
Selector *SelectorConfig
|
||||
Nodes []NodeConfig
|
||||
Selector *SelectorConfig `yaml:",omitempty"`
|
||||
Nodes []*NodeConfig
|
||||
}
|
||||
|
||||
type NodeConfig struct {
|
||||
Name string
|
||||
URL string
|
||||
Addr string
|
||||
Dialer *DialerConfig
|
||||
Connector *ConnectorConfig
|
||||
Bypass string
|
||||
URL string `yaml:",omitempty"`
|
||||
Addr string `yaml:",omitempty"`
|
||||
Dialer *DialerConfig `yaml:",omitempty"`
|
||||
Connector *ConnectorConfig `yaml:",omitempty"`
|
||||
Bypass string `yaml:",omitempty"`
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
Log *LogConfig
|
||||
Profiling *ProfilingConfig
|
||||
Services []ServiceConfig
|
||||
Chains []ChainConfig
|
||||
Bypasses []BypassConfig
|
||||
Log *LogConfig `yaml:",omitempty"`
|
||||
Profiling *ProfilingConfig `yaml:",omitempty"`
|
||||
Services []*ServiceConfig
|
||||
Chains []*ChainConfig `yaml:",omitempty"`
|
||||
Bypasses []*BypassConfig `yaml:",omitempty"`
|
||||
}
|
||||
|
||||
func (c *Config) Load() error {
|
||||
@ -129,3 +130,16 @@ func (c *Config) ReadFile(file string) error {
|
||||
}
|
||||
return v.Unmarshal(c)
|
||||
}
|
||||
|
||||
func (c *Config) WriteFile(file string) error {
|
||||
f, err := os.Create(file)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
enc := yaml.NewEncoder(f)
|
||||
defer enc.Close()
|
||||
|
||||
return enc.Encode(c)
|
||||
}
|
||||
|
@ -52,8 +52,13 @@ func (c *httpConnector) Connect(ctx context.Context, conn net.Conn, network, add
|
||||
|
||||
switch network {
|
||||
case "tcp", "tcp4", "tcp6":
|
||||
if _, ok := conn.(net.PacketConn); ok {
|
||||
err := fmt.Errorf("tcp over udp is unsupported")
|
||||
c.logger.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
default:
|
||||
err := fmt.Errorf("network %s unsupported, should be tcp, tcp4 or tcp6", network)
|
||||
err := fmt.Errorf("network %s is unsupported", network)
|
||||
c.logger.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
|
@ -51,8 +51,13 @@ func (c *socks4Connector) Connect(ctx context.Context, conn net.Conn, network, a
|
||||
|
||||
switch network {
|
||||
case "tcp", "tcp4", "tcp6":
|
||||
if _, ok := conn.(net.PacketConn); ok {
|
||||
err := fmt.Errorf("tcp over udp is unsupported")
|
||||
c.logger.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
default:
|
||||
err := fmt.Errorf("network %s unsupported, should be tcp, tcp4 or tcp6", network)
|
||||
err := fmt.Errorf("network %s is unsupported", network)
|
||||
c.logger.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
|
@ -97,8 +97,13 @@ func (c *socks5Connector) Connect(ctx context.Context, conn net.Conn, network, a
|
||||
case "udp", "udp4", "udp6":
|
||||
return c.connectUDP(ctx, conn, network, address)
|
||||
case "tcp", "tcp4", "tcp6":
|
||||
if _, ok := conn.(net.PacketConn); ok {
|
||||
err := fmt.Errorf("tcp over udp is unsupported")
|
||||
c.logger.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
default:
|
||||
err := fmt.Errorf("network %s unsupported", network)
|
||||
err := fmt.Errorf("network %s is unsupported", network)
|
||||
c.logger.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
|
@ -52,10 +52,15 @@ func (c *ssConnector) Connect(ctx context.Context, conn net.Conn, network, addre
|
||||
|
||||
switch network {
|
||||
case "tcp", "tcp4", "tcp6":
|
||||
if _, ok := conn.(net.PacketConn); ok {
|
||||
err := fmt.Errorf("tcp over udp is unsupported")
|
||||
c.logger.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
case "udp", "udp4", "udp6":
|
||||
return c.connectUDP(ctx, conn, network, address)
|
||||
default:
|
||||
err := fmt.Errorf("network %s unsupported", network)
|
||||
err := fmt.Errorf("network %s is unsupported", network)
|
||||
c.logger.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
@ -99,7 +104,7 @@ func (c *ssConnector) Connect(ctx context.Context, conn net.Conn, network, addre
|
||||
}
|
||||
|
||||
func (c *ssConnector) connectUDP(ctx context.Context, conn net.Conn, network, address string) (net.Conn, error) {
|
||||
if c.md.enableUDP {
|
||||
if !c.md.enableUDP {
|
||||
err := errors.New("UDP relay is disabled")
|
||||
c.logger.Error(err)
|
||||
return nil, err
|
||||
|
@ -22,7 +22,7 @@ func (c *ssConnector) parseMetadata(md md.Metadata) (err error) {
|
||||
password = "password"
|
||||
key = "key"
|
||||
connectTimeout = "timeout"
|
||||
noDelay = "noDelay"
|
||||
noDelay = "nodelay"
|
||||
enableUDP = "udp" // enable UDP relay
|
||||
udpBufferSize = "udpBufferSize" // udp buffer size
|
||||
)
|
||||
|
@ -43,8 +43,9 @@ func (h *forwardHandler) Init(md md.Metadata) (err error) {
|
||||
}
|
||||
|
||||
// Forward implements handler.Forwarder.
|
||||
func (h *forwardHandler) Forward(group *chain.NodeGroup) {
|
||||
func (h *forwardHandler) Forward(group *chain.NodeGroup, chain *chain.Chain) {
|
||||
h.group = group
|
||||
h.chain = chain
|
||||
}
|
||||
|
||||
func (h *forwardHandler) Handle(ctx context.Context, conn net.Conn) {
|
||||
|
@ -14,5 +14,5 @@ type Handler interface {
|
||||
}
|
||||
|
||||
type Forwarder interface {
|
||||
Forward(*chain.NodeGroup)
|
||||
Forward(*chain.NodeGroup, *chain.Chain)
|
||||
}
|
||||
|
49
pkg/handler/relay/forward.go
Normal file
49
pkg/handler/relay/forward.go
Normal file
@ -0,0 +1,49 @@
|
||||
package relay
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/go-gost/gost/pkg/chain"
|
||||
"github.com/go-gost/gost/pkg/handler"
|
||||
)
|
||||
|
||||
func (h *relayHandler) handleForward(ctx context.Context, conn net.Conn, network string) {
|
||||
target := h.group.Next()
|
||||
if target == nil {
|
||||
h.logger.Error("no target available")
|
||||
return
|
||||
}
|
||||
|
||||
h.logger = h.logger.WithFields(map[string]interface{}{
|
||||
"dst": target.Addr(),
|
||||
})
|
||||
|
||||
h.logger.Infof("%s >> %s", conn.RemoteAddr(), target.Addr())
|
||||
|
||||
r := (&chain.Router{}).
|
||||
WithChain(h.chain).
|
||||
WithRetry(h.md.retryCount).
|
||||
WithLogger(h.logger)
|
||||
|
||||
cc, err := r.Dial(ctx, network, target.Addr())
|
||||
if err != nil {
|
||||
h.logger.Error(err)
|
||||
// TODO: the router itself may be failed due to the failed node in the router,
|
||||
// the dead marker may be a wrong operation.
|
||||
target.Marker().Mark()
|
||||
return
|
||||
}
|
||||
defer cc.Close()
|
||||
target.Marker().Reset()
|
||||
|
||||
t := time.Now()
|
||||
h.logger.Infof("%s <-> %s", conn.RemoteAddr(), target.Addr())
|
||||
handler.Transport(conn, cc)
|
||||
h.logger.
|
||||
WithFields(map[string]interface{}{
|
||||
"duration": time.Since(t),
|
||||
}).
|
||||
Infof("%s >-< %s", conn.RemoteAddr(), target.Addr())
|
||||
}
|
143
pkg/handler/relay/handler.go
Normal file
143
pkg/handler/relay/handler.go
Normal file
@ -0,0 +1,143 @@
|
||||
package relay
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/go-gost/gost/pkg/bypass"
|
||||
"github.com/go-gost/gost/pkg/chain"
|
||||
"github.com/go-gost/gost/pkg/handler"
|
||||
"github.com/go-gost/gost/pkg/logger"
|
||||
md "github.com/go-gost/gost/pkg/metadata"
|
||||
"github.com/go-gost/gost/pkg/registry"
|
||||
"github.com/go-gost/relay"
|
||||
)
|
||||
|
||||
func init() {
|
||||
registry.RegisterHandler("relay", NewHandler)
|
||||
}
|
||||
|
||||
type relayHandler struct {
|
||||
group *chain.NodeGroup
|
||||
chain *chain.Chain
|
||||
bypass bypass.Bypass
|
||||
logger logger.Logger
|
||||
md metadata
|
||||
}
|
||||
|
||||
func NewHandler(opts ...handler.Option) handler.Handler {
|
||||
options := &handler.Options{}
|
||||
for _, opt := range opts {
|
||||
opt(options)
|
||||
}
|
||||
|
||||
return &relayHandler{
|
||||
chain: options.Chain,
|
||||
bypass: options.Bypass,
|
||||
logger: options.Logger,
|
||||
}
|
||||
}
|
||||
|
||||
func (h *relayHandler) Init(md md.Metadata) (err error) {
|
||||
return h.parseMetadata(md)
|
||||
}
|
||||
|
||||
// Forward implements handler.Forwarder.
|
||||
func (h *relayHandler) Forward(group *chain.NodeGroup, chain *chain.Chain) {
|
||||
h.group = group
|
||||
h.chain = chain
|
||||
}
|
||||
|
||||
func (h *relayHandler) Handle(ctx context.Context, conn net.Conn) {
|
||||
defer conn.Close()
|
||||
|
||||
start := time.Now()
|
||||
h.logger = h.logger.WithFields(map[string]interface{}{
|
||||
"remote": conn.RemoteAddr().String(),
|
||||
"local": conn.LocalAddr().String(),
|
||||
})
|
||||
|
||||
h.logger.Infof("%s <> %s", conn.RemoteAddr(), conn.LocalAddr())
|
||||
defer func() {
|
||||
h.logger.WithFields(map[string]interface{}{
|
||||
"duration": time.Since(start),
|
||||
}).Infof("%s >< %s", conn.RemoteAddr(), conn.LocalAddr())
|
||||
}()
|
||||
|
||||
if h.md.readTimeout > 0 {
|
||||
conn.SetReadDeadline(time.Now().Add(h.md.readTimeout))
|
||||
}
|
||||
|
||||
req := relay.Request{}
|
||||
if _, err := req.ReadFrom(conn); err != nil {
|
||||
h.logger.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
conn.SetReadDeadline(time.Time{})
|
||||
|
||||
if req.Version != relay.Version1 {
|
||||
h.logger.Error("bad version")
|
||||
return
|
||||
}
|
||||
|
||||
var user, pass string
|
||||
var target string
|
||||
for _, f := range req.Features {
|
||||
if f.Type() == relay.FeatureUserAuth {
|
||||
feature := f.(*relay.UserAuthFeature)
|
||||
user, pass = feature.Username, feature.Password
|
||||
}
|
||||
if f.Type() == relay.FeatureTargetAddr {
|
||||
feature := f.(*relay.TargetAddrFeature)
|
||||
target = net.JoinHostPort(feature.Host, strconv.Itoa(int(feature.Port)))
|
||||
}
|
||||
}
|
||||
|
||||
if user != "" {
|
||||
h.logger = h.logger.WithFields(map[string]interface{}{"user": user})
|
||||
}
|
||||
if target != "" {
|
||||
h.logger = h.logger.WithFields(map[string]interface{}{"dst": target})
|
||||
}
|
||||
|
||||
resp := relay.Response{
|
||||
Version: relay.Version1,
|
||||
Status: relay.StatusOK,
|
||||
}
|
||||
if h.md.authenticator != nil && !h.md.authenticator.Authenticate(user, pass) {
|
||||
resp.Status = relay.StatusUnauthorized
|
||||
resp.WriteTo(conn)
|
||||
h.logger.Error("unauthorized")
|
||||
return
|
||||
}
|
||||
|
||||
network := "tcp"
|
||||
if (req.Flags & relay.FUDP) == relay.FUDP {
|
||||
network = "udp"
|
||||
}
|
||||
|
||||
if h.group != nil {
|
||||
if target != "" {
|
||||
resp.Status = relay.StatusForbidden
|
||||
resp.WriteTo(conn)
|
||||
h.logger.Error("forbidden")
|
||||
return
|
||||
}
|
||||
// forward mode
|
||||
h.handleForward(ctx, conn, network)
|
||||
return
|
||||
}
|
||||
|
||||
if target == "" {
|
||||
resp.Status = relay.StatusBadRequest
|
||||
resp.WriteTo(conn)
|
||||
h.logger.Error("bad request")
|
||||
return
|
||||
}
|
||||
|
||||
// proxy mode
|
||||
h.handleProxy(ctx, conn, network, target)
|
||||
}
|
41
pkg/handler/relay/metadata.go
Normal file
41
pkg/handler/relay/metadata.go
Normal file
@ -0,0 +1,41 @@
|
||||
package relay
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/go-gost/gost/pkg/auth"
|
||||
md "github.com/go-gost/gost/pkg/metadata"
|
||||
)
|
||||
|
||||
type metadata struct {
|
||||
authenticator auth.Authenticator
|
||||
readTimeout time.Duration
|
||||
retryCount int
|
||||
}
|
||||
|
||||
func (h *relayHandler) parseMetadata(md md.Metadata) (err error) {
|
||||
const (
|
||||
authsKey = "auths"
|
||||
readTimeout = "readTimeout"
|
||||
retryCount = "retry"
|
||||
)
|
||||
|
||||
if v, _ := md.Get(authsKey).([]interface{}); len(v) > 0 {
|
||||
authenticator := auth.NewLocalAuthenticator(nil)
|
||||
for _, auth := range v {
|
||||
if s, _ := auth.(string); s != "" {
|
||||
ss := strings.SplitN(s, ":", 2)
|
||||
if len(ss) == 1 {
|
||||
authenticator.Add(ss[0], "")
|
||||
} else {
|
||||
authenticator.Add(ss[0], ss[1])
|
||||
}
|
||||
}
|
||||
}
|
||||
h.md.authenticator = authenticator
|
||||
}
|
||||
h.md.readTimeout = md.GetDuration(readTimeout)
|
||||
h.md.retryCount = md.GetInt(retryCount)
|
||||
return
|
||||
}
|
57
pkg/handler/relay/proxy.go
Normal file
57
pkg/handler/relay/proxy.go
Normal file
@ -0,0 +1,57 @@
|
||||
package relay
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/go-gost/gost/pkg/chain"
|
||||
util_relay "github.com/go-gost/gost/pkg/common/util/relay"
|
||||
"github.com/go-gost/gost/pkg/handler"
|
||||
"github.com/go-gost/relay"
|
||||
)
|
||||
|
||||
func (h *relayHandler) handleProxy(ctx context.Context, conn net.Conn, network, address string) {
|
||||
h.logger.Infof("%s >> %s", conn.RemoteAddr(), address)
|
||||
|
||||
resp := relay.Response{
|
||||
Version: relay.Version1,
|
||||
Status: relay.StatusOK,
|
||||
}
|
||||
|
||||
if h.bypass != nil && h.bypass.Contains(address) {
|
||||
h.logger.Info("bypass: ", address)
|
||||
resp.Status = relay.StatusForbidden
|
||||
resp.WriteTo(conn)
|
||||
return
|
||||
}
|
||||
|
||||
r := (&chain.Router{}).
|
||||
WithChain(h.chain).
|
||||
WithRetry(h.md.retryCount).
|
||||
WithLogger(h.logger)
|
||||
cc, err := r.Dial(ctx, network, address)
|
||||
if err != nil {
|
||||
resp.Status = relay.StatusNetworkUnreachable
|
||||
resp.WriteTo(conn)
|
||||
return
|
||||
}
|
||||
defer cc.Close()
|
||||
|
||||
if _, err := resp.WriteTo(conn); err != nil {
|
||||
h.logger.Error(err)
|
||||
}
|
||||
|
||||
if network == "udp" {
|
||||
conn = util_relay.UDPTunConn(conn)
|
||||
}
|
||||
|
||||
t := time.Now()
|
||||
h.logger.Infof("%s <-> %s", conn.RemoteAddr(), address)
|
||||
handler.Transport(conn, cc)
|
||||
h.logger.
|
||||
WithFields(map[string]interface{}{
|
||||
"duration": time.Since(t),
|
||||
}).
|
||||
Infof("%s >-< %s", conn.RemoteAddr(), address)
|
||||
}
|
@ -64,13 +64,7 @@ func (h *ssHandler) Handle(ctx context.Context, conn net.Conn) {
|
||||
|
||||
// standard UDP relay.
|
||||
if pc, ok := conn.(net.PacketConn); ok {
|
||||
if h.md.enableUDP {
|
||||
h.handleUDP(ctx, conn.RemoteAddr(), pc)
|
||||
return
|
||||
} else {
|
||||
h.logger.Error("UDP relay is diabled")
|
||||
}
|
||||
|
||||
h.handleUDP(ctx, pc, conn.RemoteAddr())
|
||||
return
|
||||
}
|
||||
|
||||
@ -84,21 +78,17 @@ func (h *ssHandler) Handle(ctx context.Context, conn net.Conn) {
|
||||
|
||||
br := bufio.NewReader(conn)
|
||||
data, err := br.Peek(3)
|
||||
conn.SetReadDeadline(time.Time{})
|
||||
if err != nil {
|
||||
h.logger.Error(err)
|
||||
h.discard(conn)
|
||||
return
|
||||
}
|
||||
conn.SetReadDeadline(time.Time{})
|
||||
|
||||
conn = handler.NewBufferReaderConn(conn, br)
|
||||
if data[2] == 0xff {
|
||||
if h.md.enableUDP {
|
||||
// UDP-over-TCP relay
|
||||
h.handleUDPTun(ctx, conn)
|
||||
} else {
|
||||
h.logger.Error("UDP relay is diabled")
|
||||
}
|
||||
// UDP-over-TCP relay
|
||||
h.handleUDPTun(ctx, conn)
|
||||
return
|
||||
}
|
||||
|
||||
@ -110,8 +100,6 @@ func (h *ssHandler) Handle(ctx context.Context, conn net.Conn) {
|
||||
return
|
||||
}
|
||||
|
||||
conn.SetReadDeadline(time.Time{})
|
||||
|
||||
h.logger = h.logger.WithFields(map[string]interface{}{
|
||||
"dst": addr.String(),
|
||||
})
|
||||
|
@ -11,7 +11,12 @@ import (
|
||||
"github.com/go-gost/gost/pkg/common/util/ss"
|
||||
)
|
||||
|
||||
func (h *ssHandler) handleUDP(ctx context.Context, raddr net.Addr, conn net.PacketConn) {
|
||||
func (h *ssHandler) handleUDP(ctx context.Context, conn net.PacketConn, raddr net.Addr) {
|
||||
if !h.md.enableUDP {
|
||||
h.logger.Error("UDP relay is diabled")
|
||||
return
|
||||
}
|
||||
|
||||
if h.md.cipher != nil {
|
||||
conn = h.md.cipher.PacketConn(conn)
|
||||
}
|
||||
@ -50,6 +55,11 @@ func (h *ssHandler) handleUDP(ctx context.Context, raddr net.Addr, conn net.Pack
|
||||
}
|
||||
|
||||
func (h *ssHandler) handleUDPTun(ctx context.Context, conn net.Conn) {
|
||||
if !h.md.enableUDP {
|
||||
h.logger.Error("UDP relay is diabled")
|
||||
return
|
||||
}
|
||||
|
||||
// obtain a udp connection
|
||||
r := (&chain.Router{}).
|
||||
WithChain(h.chain).
|
||||
|
@ -4,6 +4,7 @@ import (
|
||||
"errors"
|
||||
"net"
|
||||
|
||||
"github.com/go-gost/gost/pkg/chain"
|
||||
"github.com/go-gost/gost/pkg/metadata"
|
||||
)
|
||||
|
||||
@ -21,3 +22,7 @@ type Listener interface {
|
||||
type Accepter interface {
|
||||
Accept() (net.Conn, error)
|
||||
}
|
||||
|
||||
type Chainable interface {
|
||||
Chain(chain *chain.Chain)
|
||||
}
|
||||
|
@ -1,13 +1,11 @@
|
||||
package listener
|
||||
|
||||
import (
|
||||
"github.com/go-gost/gost/pkg/chain"
|
||||
"github.com/go-gost/gost/pkg/logger"
|
||||
)
|
||||
|
||||
type Options struct {
|
||||
Addr string
|
||||
Chain *chain.Chain
|
||||
Logger logger.Logger
|
||||
}
|
||||
|
||||
@ -19,12 +17,6 @@ func AddrOption(addr string) Option {
|
||||
}
|
||||
}
|
||||
|
||||
func ChainOption(chain *chain.Chain) Option {
|
||||
return func(opts *Options) {
|
||||
opts.Chain = chain
|
||||
}
|
||||
}
|
||||
|
||||
func LoggerOption(logger logger.Logger) Option {
|
||||
return func(opts *Options) {
|
||||
opts.Logger = logger
|
||||
|
@ -40,12 +40,16 @@ func NewListener(opts ...listener.Option) listener.Listener {
|
||||
}
|
||||
return &rtcpListener{
|
||||
addr: options.Addr,
|
||||
chain: options.Chain,
|
||||
closed: make(chan struct{}),
|
||||
logger: options.Logger,
|
||||
}
|
||||
}
|
||||
|
||||
// implements listener.Chainable interface
|
||||
func (l *rtcpListener) Chain(chain *chain.Chain) {
|
||||
l.chain = chain
|
||||
}
|
||||
|
||||
func (l *rtcpListener) Init(md md.Metadata) (err error) {
|
||||
if err = l.parseMetadata(md); err != nil {
|
||||
return
|
||||
|
@ -39,12 +39,16 @@ func NewListener(opts ...listener.Option) listener.Listener {
|
||||
}
|
||||
return &rudpListener{
|
||||
addr: options.Addr,
|
||||
chain: options.Chain,
|
||||
closed: make(chan struct{}),
|
||||
logger: options.Logger,
|
||||
}
|
||||
}
|
||||
|
||||
// implements listener.Chainable interface
|
||||
func (l *rudpListener) Chain(chain *chain.Chain) {
|
||||
l.chain = chain
|
||||
}
|
||||
|
||||
func (l *rudpListener) Init(md md.Metadata) (err error) {
|
||||
if err = l.parseMetadata(md); err != nil {
|
||||
return
|
||||
|
@ -84,15 +84,15 @@ func NewLogger(opts ...LoggerOption) Logger {
|
||||
}
|
||||
|
||||
switch options.Format {
|
||||
case JSONFormat:
|
||||
case TextFormat:
|
||||
log.SetFormatter(&logrus.TextFormatter{
|
||||
FullTimestamp: true,
|
||||
})
|
||||
default:
|
||||
log.SetFormatter(&logrus.JSONFormatter{
|
||||
DisableHTMLEscape: true,
|
||||
// PrettyPrint: true,
|
||||
})
|
||||
default:
|
||||
log.SetFormatter(&logrus.TextFormatter{
|
||||
FullTimestamp: true,
|
||||
})
|
||||
}
|
||||
|
||||
switch options.Level {
|
||||
|
Reference in New Issue
Block a user