diff --git a/go.mod b/go.mod index 437fbab..3284a7e 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/sagernet/sing-shadowtls go 1.20 require ( - github.com/sagernet/sing v0.5.0 + github.com/sagernet/sing v0.6.0-alpha.16 golang.org/x/crypto v0.29.0 golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 ) diff --git a/go.sum b/go.sum index c7acb4e..c869cc3 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,7 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= -github.com/sagernet/sing v0.5.0 h1:soo2wVwLcieKWWKIksFNK6CCAojUgAppqQVwyRYGkEM= -github.com/sagernet/sing v0.5.0/go.mod h1:ARkL0gM13/Iv5VCZmci/NuoOlePoIsW0m7BWfln/Hak= +github.com/sagernet/sing v0.6.0-alpha.16 h1:Ml+nJa8J9d+Svqv2pBvJoet+8PF302Snb6wcMUXaoy4= +github.com/sagernet/sing v0.6.0-alpha.16/go.mod h1:ARkL0gM13/Iv5VCZmci/NuoOlePoIsW0m7BWfln/Hak= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= golang.org/x/crypto v0.29.0 h1:L5SG1JTTXupVV3n6sUqMTeWbjAyfPwoda2DLX8J8FrQ= golang.org/x/crypto v0.29.0/go.mod h1:+F4F4N5hv6v38hfeYwTdx20oUvLLc+QfrE9Ax9HtgRg= diff --git a/service.go b/service.go index b2ef5b4..15a1aaa 100644 --- a/service.go +++ b/service.go @@ -20,6 +20,17 @@ import ( "github.com/sagernet/sing/common/task" ) +type Service struct { + version int + password string + users []User + handshake HandshakeConfig + handshakeForServerName map[string]HandshakeConfig + strictMode bool + handler N.TCPConnectionHandlerEx + logger logger.ContextLogger +} + type ServiceConfig struct { Version int Password string // for protocol version 2 @@ -27,7 +38,7 @@ type ServiceConfig struct { Handshake HandshakeConfig HandshakeForServerName map[string]HandshakeConfig // for protocol version 2/3 StrictMode bool // for protocol version 3 - Handler Handler + Handler N.TCPConnectionHandlerEx Logger logger.ContextLogger } @@ -41,22 +52,6 @@ type HandshakeConfig struct { Dialer N.Dialer } -type Handler interface { - N.TCPConnectionHandler - E.Handler -} - -type Service struct { - version int - password string - users []User - handshake HandshakeConfig - handshakeForServerName map[string]HandshakeConfig - strictMode bool - handler Handler - logger logger.ContextLogger -} - func NewService(config ServiceConfig) (*Service, error) { service := &Service{ version: config.Version, @@ -99,7 +94,7 @@ func (s *Service) selectHandshake(clientHelloFrame *buf.Buffer) HandshakeConfig return s.handshake } -func (s *Service) NewConnection(ctx context.Context, conn net.Conn, metadata M.Metadata) error { +func (s *Service) NewConnection(ctx context.Context, conn net.Conn, source M.Socksaddr, destination M.Socksaddr, onClose N.CloseHandlerFunc) error { switch s.version { default: fallthrough @@ -125,7 +120,8 @@ func (s *Service) NewConnection(ctx context.Context, conn net.Conn, metadata M.M return err } s.logger.TraceContext(ctx, "handshake finished") - return s.handler.NewConnection(ctx, conn, metadata) + s.handler.NewConnectionEx(ctx, conn, source, destination, onClose) + return nil case 2: clientHelloFrame, err := extractFrame(conn) if err != nil { @@ -144,7 +140,8 @@ func (s *Service) NewConnection(ctx context.Context, conn net.Conn, metadata M.M if err == nil { s.logger.TraceContext(ctx, "handshake finished") handshakeConn.Close() - return s.handler.NewConnection(ctx, bufio.NewCachedConn(newConn(conn), request), metadata) + s.handler.NewConnectionEx(ctx, bufio.NewCachedConn(newConn(conn), request), source, destination, onClose) + return nil } else if err == os.ErrPermission { s.logger.WarnContext(ctx, "fallback connection") hashConn.Fallback() @@ -247,6 +244,7 @@ func (s *Service) NewConnection(ctx context.Context, conn net.Conn, metadata M.M return E.Cause(err, "handshake relay") } s.logger.TraceContext(ctx, "handshake relay finished") - return s.handler.NewConnection(ctx, bufio.NewCachedConn(newVerifiedConn(conn, hmacAdd, hmacVerify, nil), clientFirstFrame), metadata) + s.handler.NewConnectionEx(ctx, bufio.NewCachedConn(newVerifiedConn(conn, hmacAdd, hmacVerify, nil), clientFirstFrame), source, destination, onClose) + return nil } }