Skip to content

Commit

Permalink
Update handler usages
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Nov 17, 2024
1 parent 320ffbe commit 2711c0c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 24 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down
40 changes: 19 additions & 21 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,25 @@ 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
Users []User // for protocol version 3
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
}

Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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()
Expand Down Expand Up @@ -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
}
}

0 comments on commit 2711c0c

Please sign in to comment.