Compare commits

...

3 commits

Author SHA1 Message Date
世界
2d5886b68f
Update dependencies 2025-02-06 08:57:26 +08:00
世界
5fdf816d25
Update handler usages 2024-11-18 18:29:14 +08:00
世界
97f9cf2536
Update dependencies 2024-11-18 12:48:12 +08:00
3 changed files with 28 additions and 30 deletions

6
go.mod
View file

@ -3,9 +3,9 @@ module github.com/sagernet/sing-shadowtls
go 1.20
require (
github.com/sagernet/sing v0.5.0
golang.org/x/crypto v0.29.0
github.com/sagernet/sing v0.6.0
golang.org/x/crypto v0.32.0
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56
)
require golang.org/x/sys v0.27.0
require golang.org/x/sys v0.30.0

12
go.sum
View file

@ -1,12 +1,12 @@
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 h1:jT55zAXrG7H3x+s/FlrC15xQy3LcmuZ2GGA9+8IJdt0=
github.com/sagernet/sing v0.6.0/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=
golang.org/x/crypto v0.32.0 h1:euUpcYgM8WcP71gNpTqQCn6rC2t6ULUPiOzfWaXVVfc=
golang.org/x/crypto v0.32.0/go.mod h1:ZnnJkOaASj8g0AjIduWNlq2NRxL0PlBrbKVyZ6V/Ugc=
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 h1:2dVuKD2vS7b0QIHQbpyTISPd0LeHDbnYEryqj5Q1ug8=
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56/go.mod h1:M4RDyNAINzryxdtnbRXRL/OHtkFuWGRjvuhBJpk2IlY=
golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s=
golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

View file

@ -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
}
}