mirror of
https://github.com/SagerNet/sing-shadowtls.git
synced 2025-04-02 19:57:35 +03:00
Update handler usages
This commit is contained in:
parent
97f9cf2536
commit
5fdf816d25
3 changed files with 22 additions and 26 deletions
2
go.mod
2
go.mod
|
@ -3,7 +3,7 @@ module github.com/sagernet/sing-shadowtls
|
||||||
go 1.20
|
go 1.20
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/sagernet/sing v0.5.1
|
github.com/sagernet/sing v0.6.0-alpha.17
|
||||||
golang.org/x/crypto v0.29.0
|
golang.org/x/crypto v0.29.0
|
||||||
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56
|
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56
|
||||||
)
|
)
|
||||||
|
|
6
go.sum
6
go.sum
|
@ -1,9 +1,7 @@
|
||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
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/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
github.com/sagernet/sing v0.5.0 h1:soo2wVwLcieKWWKIksFNK6CCAojUgAppqQVwyRYGkEM=
|
github.com/sagernet/sing v0.6.0-alpha.17 h1:y//jVrBjJMW6tRpA/ElT7+Snp3DHEJvO60D+DByg/Es=
|
||||||
github.com/sagernet/sing v0.5.0/go.mod h1:ARkL0gM13/Iv5VCZmci/NuoOlePoIsW0m7BWfln/Hak=
|
github.com/sagernet/sing v0.6.0-alpha.17/go.mod h1:ARkL0gM13/Iv5VCZmci/NuoOlePoIsW0m7BWfln/Hak=
|
||||||
github.com/sagernet/sing v0.5.1 h1:mhL/MZVq0TjuvHcpYcFtmSD1BFOxZ/+8ofbNZcg1k1Y=
|
|
||||||
github.com/sagernet/sing v0.5.1/go.mod h1:ARkL0gM13/Iv5VCZmci/NuoOlePoIsW0m7BWfln/Hak=
|
|
||||||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
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 h1:L5SG1JTTXupVV3n6sUqMTeWbjAyfPwoda2DLX8J8FrQ=
|
||||||
golang.org/x/crypto v0.29.0/go.mod h1:+F4F4N5hv6v38hfeYwTdx20oUvLLc+QfrE9Ax9HtgRg=
|
golang.org/x/crypto v0.29.0/go.mod h1:+F4F4N5hv6v38hfeYwTdx20oUvLLc+QfrE9Ax9HtgRg=
|
||||||
|
|
40
service.go
40
service.go
|
@ -20,6 +20,17 @@ import (
|
||||||
"github.com/sagernet/sing/common/task"
|
"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 {
|
type ServiceConfig struct {
|
||||||
Version int
|
Version int
|
||||||
Password string // for protocol version 2
|
Password string // for protocol version 2
|
||||||
|
@ -27,7 +38,7 @@ type ServiceConfig struct {
|
||||||
Handshake HandshakeConfig
|
Handshake HandshakeConfig
|
||||||
HandshakeForServerName map[string]HandshakeConfig // for protocol version 2/3
|
HandshakeForServerName map[string]HandshakeConfig // for protocol version 2/3
|
||||||
StrictMode bool // for protocol version 3
|
StrictMode bool // for protocol version 3
|
||||||
Handler Handler
|
Handler N.TCPConnectionHandlerEx
|
||||||
Logger logger.ContextLogger
|
Logger logger.ContextLogger
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -41,22 +52,6 @@ type HandshakeConfig struct {
|
||||||
Dialer N.Dialer
|
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) {
|
func NewService(config ServiceConfig) (*Service, error) {
|
||||||
service := &Service{
|
service := &Service{
|
||||||
version: config.Version,
|
version: config.Version,
|
||||||
|
@ -99,7 +94,7 @@ func (s *Service) selectHandshake(clientHelloFrame *buf.Buffer) HandshakeConfig
|
||||||
return s.handshake
|
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 {
|
switch s.version {
|
||||||
default:
|
default:
|
||||||
fallthrough
|
fallthrough
|
||||||
|
@ -125,7 +120,8 @@ func (s *Service) NewConnection(ctx context.Context, conn net.Conn, metadata M.M
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
s.logger.TraceContext(ctx, "handshake finished")
|
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:
|
case 2:
|
||||||
clientHelloFrame, err := extractFrame(conn)
|
clientHelloFrame, err := extractFrame(conn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -144,7 +140,8 @@ func (s *Service) NewConnection(ctx context.Context, conn net.Conn, metadata M.M
|
||||||
if err == nil {
|
if err == nil {
|
||||||
s.logger.TraceContext(ctx, "handshake finished")
|
s.logger.TraceContext(ctx, "handshake finished")
|
||||||
handshakeConn.Close()
|
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 {
|
} else if err == os.ErrPermission {
|
||||||
s.logger.WarnContext(ctx, "fallback connection")
|
s.logger.WarnContext(ctx, "fallback connection")
|
||||||
hashConn.Fallback()
|
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")
|
return E.Cause(err, "handshake relay")
|
||||||
}
|
}
|
||||||
s.logger.TraceContext(ctx, "handshake relay finished")
|
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue