This commit is contained in:
ginuerzh
2021-11-15 12:55:05 +08:00
parent 3fe5084629
commit ce3d62759a
38 changed files with 788 additions and 291 deletions

View File

@ -6,7 +6,6 @@ import (
"time"
"github.com/go-gost/gosocks4"
"github.com/go-gost/gost/pkg/auth"
"github.com/go-gost/gost/pkg/bypass"
"github.com/go-gost/gost/pkg/chain"
"github.com/go-gost/gost/pkg/handler"
@ -108,7 +107,7 @@ func (h *socks4Handler) handleConnect(ctx context.Context, conn net.Conn, req *g
return
}
r := (&handler.Router{}).
r := (&chain.Router{}).
WithChain(h.chain).
WithRetry(h.md.retryCount).
WithLogger(h.logger)
@ -142,19 +141,3 @@ func (h *socks4Handler) handleConnect(ctx context.Context, conn net.Conn, req *g
func (h *socks4Handler) handleBind(ctx context.Context, conn net.Conn, req *gosocks4.Request) {
// TODO: bind
}
func (h *socks4Handler) parseMetadata(md md.Metadata) (err error) {
if v, _ := md.Get(authsKey).([]interface{}); len(v) > 0 {
authenticator := auth.NewLocalAuthenticator(nil)
for _, auth := range v {
if v, _ := auth.(string); v != "" {
authenticator.Add(v, "")
}
}
h.md.authenticator = authenticator
}
h.md.readTimeout = md.GetDuration(readTimeout)
h.md.retryCount = md.GetInt(retryCount)
return
}

View File

@ -4,12 +4,7 @@ import (
"time"
"github.com/go-gost/gost/pkg/auth"
)
const (
authsKey = "auths"
readTimeout = "readTimeout"
retryCount = "retry"
md "github.com/go-gost/gost/pkg/metadata"
)
type metadata struct {
@ -17,3 +12,25 @@ type metadata struct {
readTimeout time.Duration
retryCount int
}
func (h *socks4Handler) 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 v, _ := auth.(string); v != "" {
authenticator.Add(v, "")
}
}
h.md.authenticator = authenticator
}
h.md.readTimeout = md.GetDuration(readTimeout)
h.md.retryCount = md.GetInt(retryCount)
return
}

View File

@ -6,6 +6,7 @@ import (
"time"
"github.com/go-gost/gosocks5"
"github.com/go-gost/gost/pkg/chain"
"github.com/go-gost/gost/pkg/handler"
)
@ -19,12 +20,20 @@ func (h *socks5Handler) handleBind(ctx context.Context, conn net.Conn, req *goso
h.logger.Infof("%s >> %s", conn.RemoteAddr(), addr)
if !h.md.enableBind {
reply := gosocks5.NewReply(gosocks5.NotAllowed, nil)
reply.Write(conn)
h.logger.Debug(reply)
h.logger.Error("BIND is diabled")
return
}
if h.chain.IsEmpty() {
h.bindLocal(ctx, conn, addr)
return
}
r := (&handler.Router{}).
r := (&chain.Router{}).
WithChain(h.chain).
WithRetry(h.md.retryCount).
WithLogger(h.logger)
@ -83,7 +92,7 @@ func (h *socks5Handler) bindLocal(ctx context.Context, conn net.Conn, addr strin
h.logger = h.logger.WithFields(map[string]interface{}{
"bind": socksAddr.String(),
})
h.logger.Infof("bind on %s OK", socksAddr.String())
h.logger.Debugf("bind on %s OK", &socksAddr)
h.serveBind(ctx, conn, ln)
}
@ -127,10 +136,19 @@ func (h *socks5Handler) serveBind(ctx context.Context, conn net.Conn, ln net.Lis
case err := <-accept():
if err != nil {
h.logger.Error(err)
reply := gosocks5.NewReply(gosocks5.Failure, nil)
if err := reply.Write(pc2); err != nil {
h.logger.Error(err)
}
h.logger.Debug(reply)
return
}
defer rc.Close()
h.logger.Debugf("peer %s accepted", rc.RemoteAddr())
raddr := gosocks5.Addr{}
raddr.ParseFrom(rc.RemoteAddr().String())
reply := gosocks5.NewReply(gosocks5.Succeeded, &raddr)
@ -138,7 +156,6 @@ func (h *socks5Handler) serveBind(ctx context.Context, conn net.Conn, ln net.Lis
h.logger.Error(err)
}
h.logger.Debug(reply)
h.logger.Infof("peer accepted: %s", raddr.String())
start := time.Now()
h.logger.Infof("%s <-> %s", conn.RemoteAddr(), raddr.String())

View File

@ -6,6 +6,7 @@ import (
"time"
"github.com/go-gost/gosocks5"
"github.com/go-gost/gost/pkg/chain"
"github.com/go-gost/gost/pkg/handler"
)
@ -24,7 +25,7 @@ func (h *socks5Handler) handleConnect(ctx context.Context, conn net.Conn, addr s
return
}
r := (&handler.Router{}).
r := (&chain.Router{}).
WithChain(h.chain).
WithRetry(h.md.retryCount).
WithLogger(h.logger)

View File

@ -90,29 +90,13 @@ func (h *socks5Handler) Handle(ctx context.Context, conn net.Conn) {
case gosocks5.CmdConnect:
h.handleConnect(ctx, conn, req.Addr.String())
case gosocks5.CmdBind:
if h.md.enableBind {
h.handleBind(ctx, conn, req)
} else {
h.logger.Error("BIND is diabled")
}
h.handleBind(ctx, conn, req)
case socks.CmdMuxBind:
if h.md.enableBind {
h.handleMuxBind(ctx, conn, req)
} else {
h.logger.Error("BIND is diabled")
}
h.handleMuxBind(ctx, conn, req)
case gosocks5.CmdUdp:
if h.md.enableUDP {
h.handleUDP(ctx, conn, req)
} else {
h.logger.Error("UDP relay is diabled")
}
h.handleUDP(ctx, conn, req)
case socks.CmdUDPTun:
if h.md.enableUDP {
h.handleUDPTun(ctx, conn, req)
} else {
h.logger.Error("UDP relay is diabled")
}
h.handleUDPTun(ctx, conn, req)
default:
h.logger.Errorf("unknown cmd: %d", req.Cmd)
resp := gosocks5.NewReply(gosocks5.CmdUnsupported, nil)

View File

@ -6,6 +6,7 @@ import (
"time"
"github.com/go-gost/gosocks5"
"github.com/go-gost/gost/pkg/chain"
"github.com/go-gost/gost/pkg/handler"
"github.com/go-gost/gost/pkg/internal/utils/mux"
)
@ -20,12 +21,20 @@ func (h *socks5Handler) handleMuxBind(ctx context.Context, conn net.Conn, req *g
h.logger.Infof("%s >> %s", conn.RemoteAddr(), addr)
if !h.md.enableBind {
reply := gosocks5.NewReply(gosocks5.NotAllowed, nil)
reply.Write(conn)
h.logger.Debug(reply)
h.logger.Error("BIND is diabled")
return
}
if h.chain.IsEmpty() {
h.muxBindLocal(ctx, conn, addr)
return
}
r := (&handler.Router{}).
r := (&chain.Router{}).
WithChain(h.chain).
WithRetry(h.md.retryCount).
WithLogger(h.logger)
@ -90,14 +99,14 @@ func (h *socks5Handler) muxBindLocal(ctx context.Context, conn net.Conn, addr st
h.logger = h.logger.WithFields(map[string]interface{}{
"bind": socksAddr.String(),
})
h.logger.Infof("bind on: %s OK", socksAddr.String())
h.logger.Debugf("bind on %s OK", &socksAddr)
h.serveMuxBind(ctx, conn, ln)
}
func (h *socks5Handler) serveMuxBind(ctx context.Context, conn net.Conn, ln net.Listener) {
// Upgrade connection to multiplex stream.
session, err := mux.NewMuxSession(conn)
session, err := mux.ClientSession(conn)
if err != nil {
h.logger.Error(err)
return
@ -122,7 +131,7 @@ func (h *socks5Handler) serveMuxBind(ctx context.Context, conn net.Conn, ln net.
h.logger.Error(err)
return
}
h.logger.Infof("peer accepted: %s", rc.RemoteAddr().String())
h.logger.Debugf("peer %s accepted", rc.RemoteAddr())
go func(c net.Conn) {
defer c.Close()
@ -134,6 +143,18 @@ func (h *socks5Handler) serveMuxBind(ctx context.Context, conn net.Conn, ln net.
}
defer sc.Close()
// incompatible with GOST v2.x
if !h.md.compatibilityMode {
addr := gosocks5.Addr{}
addr.ParseFrom(c.RemoteAddr().String())
reply := gosocks5.NewReply(gosocks5.Succeeded, &addr)
if err := reply.Write(sc); err != nil {
h.logger.Error(err)
return
}
h.logger.Debug(reply)
}
t := time.Now()
h.logger.Infof("%s <-> %s", conn.RemoteAddr(), c.RemoteAddr().String())
handler.Transport(sc, c)

View File

@ -11,30 +11,32 @@ import (
)
type metadata struct {
tlsConfig *tls.Config
authenticator auth.Authenticator
timeout time.Duration
readTimeout time.Duration
retryCount int
noTLS bool
enableBind bool
enableUDP bool
udpBufferSize int
tlsConfig *tls.Config
authenticator auth.Authenticator
timeout time.Duration
readTimeout time.Duration
retryCount int
noTLS bool
enableBind bool
enableUDP bool
udpBufferSize int
compatibilityMode bool
}
func (h *socks5Handler) parseMetadata(md md.Metadata) error {
const (
certFile = "certFile"
keyFile = "keyFile"
caFile = "caFile"
authsKey = "auths"
readTimeout = "readTimeout"
timeout = "timeout"
retryCount = "retry"
noTLS = "notls"
enableBind = "bind"
enableUDP = "udp"
udpBufferSize = "udpBufferSize"
certFile = "certFile"
keyFile = "keyFile"
caFile = "caFile"
authsKey = "auths"
readTimeout = "readTimeout"
timeout = "timeout"
retryCount = "retry"
noTLS = "notls"
enableBind = "bind"
enableUDP = "udp"
udpBufferSize = "udpBufferSize"
compatibilityMode = "comp"
)
var err error
@ -81,5 +83,7 @@ func (h *socks5Handler) parseMetadata(md md.Metadata) error {
h.md.udpBufferSize = 4096 // default buffer size
}
h.md.compatibilityMode = md.GetBool(compatibilityMode)
return nil
}

View File

@ -9,7 +9,7 @@ import (
"time"
"github.com/go-gost/gosocks5"
"github.com/go-gost/gost/pkg/handler"
"github.com/go-gost/gost/pkg/chain"
"github.com/go-gost/gost/pkg/internal/bufpool"
"github.com/go-gost/gost/pkg/internal/utils/socks"
)
@ -19,6 +19,14 @@ func (h *socks5Handler) handleUDP(ctx context.Context, conn net.Conn, req *gosoc
"cmd": "udp",
})
if !h.md.enableUDP {
reply := gosocks5.NewReply(gosocks5.NotAllowed, nil)
reply.Write(conn)
h.logger.Debug(reply)
h.logger.Error("UDP relay is diabled")
return
}
relay, err := net.ListenUDP("udp", nil)
if err != nil {
h.logger.Error(err)
@ -43,7 +51,7 @@ func (h *socks5Handler) handleUDP(ctx context.Context, conn net.Conn, req *gosoc
h.logger = h.logger.WithFields(map[string]interface{}{
"bind": saddr.String(),
})
h.logger.Infof("bind on %s OK", saddr.String())
h.logger.Debugf("bind on %s OK", &saddr)
if h.chain.IsEmpty() {
// serve as standard socks5 udp relay.
@ -81,7 +89,7 @@ func (h *socks5Handler) handleUDP(ctx context.Context, conn net.Conn, req *gosoc
}
func (h *socks5Handler) getUDPTun(ctx context.Context) (conn net.Conn, err error) {
r := (&handler.Router{}).
r := (&chain.Router{}).
WithChain(h.chain).
WithRetry(h.md.retryCount).
WithLogger(h.logger)

View File

@ -6,6 +6,7 @@ import (
"time"
"github.com/go-gost/gosocks5"
"github.com/go-gost/gost/pkg/chain"
"github.com/go-gost/gost/pkg/handler"
"github.com/go-gost/gost/pkg/internal/bufpool"
"github.com/go-gost/gost/pkg/internal/utils/socks"
@ -16,6 +17,14 @@ func (h *socks5Handler) handleUDPTun(ctx context.Context, conn net.Conn, req *go
"cmd": "udp-tun",
})
if !h.md.enableUDP {
reply := gosocks5.NewReply(gosocks5.NotAllowed, nil)
reply.Write(conn)
h.logger.Debug(reply)
h.logger.Error("UDP relay is diabled")
return
}
if h.chain.IsEmpty() {
addr := req.Addr.String()
@ -56,7 +65,7 @@ func (h *socks5Handler) handleUDPTun(ctx context.Context, conn net.Conn, req *go
return
}
r := (&handler.Router{}).
r := (&chain.Router{}).
WithChain(h.chain).
WithRetry(h.md.retryCount).
WithLogger(h.logger)