Compare commits

...

1 commit

Author SHA1 Message Date
世界
255ab5a070
Update handler usages 2024-11-15 16:34:19 +08:00
7 changed files with 17 additions and 40 deletions

2
go.mod
View file

@ -5,7 +5,7 @@ go 1.20
require ( require (
github.com/gofrs/uuid/v5 v5.3.0 github.com/gofrs/uuid/v5 v5.3.0
github.com/sagernet/quic-go v0.48.1-beta.1 github.com/sagernet/quic-go v0.48.1-beta.1
github.com/sagernet/sing v0.5.0 github.com/sagernet/sing v0.6.0-alpha.14
golang.org/x/crypto v0.28.0 golang.org/x/crypto v0.28.0
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842
) )

4
go.sum
View file

@ -21,8 +21,8 @@ github.com/quic-go/qtls-go1-20 v0.4.1 h1:D33340mCNDAIKBqXuAvexTNMUByrYmFYVfKfDN5
github.com/quic-go/qtls-go1-20 v0.4.1/go.mod h1:X9Nh97ZL80Z+bX/gUXMbipO6OxdiDi58b/fMC9mAL+k= github.com/quic-go/qtls-go1-20 v0.4.1/go.mod h1:X9Nh97ZL80Z+bX/gUXMbipO6OxdiDi58b/fMC9mAL+k=
github.com/sagernet/quic-go v0.48.1-beta.1 h1:ElPaV5yzlXIKZpqFMAcUGax6vddi3zt4AEpT94Z0vwo= github.com/sagernet/quic-go v0.48.1-beta.1 h1:ElPaV5yzlXIKZpqFMAcUGax6vddi3zt4AEpT94Z0vwo=
github.com/sagernet/quic-go v0.48.1-beta.1/go.mod h1:1WgdDIVD1Gybp40JTWketeSfKA/+or9YMLaG5VeTk4k= github.com/sagernet/quic-go v0.48.1-beta.1/go.mod h1:1WgdDIVD1Gybp40JTWketeSfKA/+or9YMLaG5VeTk4k=
github.com/sagernet/sing v0.5.0 h1:soo2wVwLcieKWWKIksFNK6CCAojUgAppqQVwyRYGkEM= github.com/sagernet/sing v0.6.0-alpha.14 h1:ORh6yQwLL+/nv+rklrO2W4k+zgf3ZzaOl/83vQbJUl4=
github.com/sagernet/sing v0.5.0/go.mod h1:ARkL0gM13/Iv5VCZmci/NuoOlePoIsW0m7BWfln/Hak= github.com/sagernet/sing v0.6.0-alpha.14/go.mod h1:ARkL0gM13/Iv5VCZmci/NuoOlePoIsW0m7BWfln/Hak=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=

View file

@ -46,8 +46,8 @@ type ServiceOptions struct {
} }
type ServerHandler interface { type ServerHandler interface {
N.TCPConnectionHandler N.TCPConnectionHandlerEx
N.UDPConnectionHandler N.UDPConnectionHandlerEx
} }
type Service[U comparable] struct { type Service[U comparable] struct {
@ -244,10 +244,7 @@ func (s *serverSession[U]) handleStream(stream quic.Stream) error {
} }
ctx := auth.ContextWithUser(s.ctx, s.authUser) ctx := auth.ContextWithUser(s.ctx, s.authUser)
if !request.UDP { if !request.UDP {
_ = s.handler.NewConnection(ctx, &serverConn{Stream: stream}, M.Metadata{ s.handler.NewConnectionEx(ctx, &serverConn{Stream: stream}, s.source, M.ParseSocksaddrHostPort(request.Host, request.Port), nil)
Source: s.source,
Destination: M.ParseSocksaddrHostPort(request.Host, request.Port),
})
} else { } else {
if s.udpDisabled { if s.udpDisabled {
return WriteServerResponse(stream, ServerResponse{ return WriteServerResponse(stream, ServerResponse{
@ -278,10 +275,7 @@ func (s *serverSession[U]) handleStream(stream quic.Stream) error {
return err return err
} }
newCtx, newConn := canceler.NewPacketConn(udpConn.ctx, udpConn, s.udpTimeout) newCtx, newConn := canceler.NewPacketConn(udpConn.ctx, udpConn, s.udpTimeout)
go s.handler.NewPacketConnection(newCtx, newConn, M.Metadata{ go s.handler.NewPacketConnectionEx(newCtx, newConn, s.source, M.ParseSocksaddrHostPort(request.Host, request.Port), nil)
Source: s.source,
Destination: M.ParseSocksaddrHostPort(request.Host, request.Port),
})
holdBuffer := make([]byte, 1024) holdBuffer := make([]byte, 1024)
for { for {
_, hErr := stream.Read(holdBuffer) _, hErr := stream.Read(holdBuffer)
@ -330,7 +324,7 @@ type serverConn struct {
func (c *serverConn) HandshakeFailure(err error) error { func (c *serverConn) HandshakeFailure(err error) error {
if c.responseWritten { if c.responseWritten {
return os.ErrClosed return os.ErrInvalid
} }
c.responseWritten = true c.responseWritten = true
return WriteServerResponse(c.Stream, ServerResponse{ return WriteServerResponse(c.Stream, ServerResponse{

View file

@ -47,8 +47,8 @@ type ServiceOptions struct {
} }
type ServerHandler interface { type ServerHandler interface {
N.TCPConnectionHandler N.TCPConnectionHandlerEx
N.UDPConnectionHandler N.UDPConnectionHandlerEx
} }
type Service[U comparable] struct { type Service[U comparable] struct {
@ -250,8 +250,6 @@ func (s *serverSession[U]) handleStream0(frameType http3.FrameType, id quic.Conn
} }
go func() { go func() {
hErr := s.handleStream(stream) hErr := s.handleStream(stream)
stream.CancelRead(0)
stream.Close()
if hErr != nil { if hErr != nil {
stream.CancelRead(0) stream.CancelRead(0)
stream.Close() stream.Close()
@ -266,11 +264,7 @@ func (s *serverSession[U]) handleStream(stream quic.Stream) error {
if err != nil { if err != nil {
return E.New("read TCP request") return E.New("read TCP request")
} }
ctx := auth.ContextWithUser(s.ctx, s.authUser) s.handler.NewConnectionEx(auth.ContextWithUser(s.ctx, s.authUser), &serverConn{Stream: stream}, s.source, M.ParseSocksaddr(destinationString), nil)
_ = s.handler.NewConnection(ctx, &serverConn{Stream: stream}, M.Metadata{
Source: s.source,
Destination: M.ParseSocksaddr(destinationString),
})
return nil return nil
} }
@ -299,7 +293,7 @@ type serverConn struct {
func (c *serverConn) HandshakeFailure(err error) error { func (c *serverConn) HandshakeFailure(err error) error {
if c.responseWritten { if c.responseWritten {
return os.ErrClosed return os.ErrInvalid
} }
c.responseWritten = true c.responseWritten = true
buffer := protocol.WriteTCPResponse(false, err.Error(), nil) buffer := protocol.WriteTCPResponse(false, err.Error(), nil)

View file

@ -49,10 +49,7 @@ func (s *serverSession[U]) handleUDPMessage(message *udpMessage) {
s.udpConnMap[message.sessionID] = udpConn s.udpConnMap[message.sessionID] = udpConn
s.udpAccess.Unlock() s.udpAccess.Unlock()
newCtx, newConn := canceler.NewPacketConn(udpConn.ctx, udpConn, s.udpTimeout) newCtx, newConn := canceler.NewPacketConn(udpConn.ctx, udpConn, s.udpTimeout)
go s.handler.NewPacketConnection(newCtx, newConn, M.Metadata{ go s.handler.NewPacketConnectionEx(newCtx, newConn, s.source, M.ParseSocksaddr(message.destination), nil)
Source: s.source,
Destination: M.ParseSocksaddr(message.destination),
})
} }
udpConn.inputPacket(message) udpConn.inputPacket(message)
} }

View file

@ -40,8 +40,8 @@ type ServiceOptions struct {
} }
type ServiceHandler interface { type ServiceHandler interface {
N.TCPConnectionHandler N.TCPConnectionHandlerEx
N.UDPConnectionHandler N.UDPConnectionHandlerEx
} }
type Service[U comparable] struct { type Service[U comparable] struct {
@ -362,11 +362,7 @@ func (s *serverSession[U]) handleStream(stream quic.Stream) error {
} else { } else {
conn = bufio.NewCachedConn(conn, buffer) conn = bufio.NewCachedConn(conn, buffer)
} }
ctx := auth.ContextWithUser(s.ctx, s.authUser) s.handler.NewConnectionEx(auth.ContextWithUser(s.ctx, s.authUser), conn, s.source, destination, nil)
_ = s.handler.NewConnection(ctx, conn, M.Metadata{
Source: s.source,
Destination: destination,
})
return nil return nil
} }

View file

@ -5,7 +5,6 @@ import (
"github.com/sagernet/sing/common/auth" "github.com/sagernet/sing/common/auth"
"github.com/sagernet/sing/common/canceler" "github.com/sagernet/sing/common/canceler"
E "github.com/sagernet/sing/common/exceptions" E "github.com/sagernet/sing/common/exceptions"
M "github.com/sagernet/sing/common/metadata"
) )
func (s *serverSession[U]) loopMessages() { func (s *serverSession[U]) loopMessages() {
@ -67,10 +66,7 @@ func (s *serverSession[U]) handleUDPMessage(message *udpMessage, udpStream bool)
s.udpConnMap[message.sessionID] = udpConn s.udpConnMap[message.sessionID] = udpConn
s.udpAccess.Unlock() s.udpAccess.Unlock()
newCtx, newConn := canceler.NewPacketConn(udpConn.ctx, udpConn, s.udpTimeout) newCtx, newConn := canceler.NewPacketConn(udpConn.ctx, udpConn, s.udpTimeout)
go s.handler.NewPacketConnection(newCtx, newConn, M.Metadata{ go s.handler.NewPacketConnectionEx(newCtx, newConn, s.source, message.destination, nil)
Source: s.source,
Destination: message.destination,
})
} }
udpConn.inputPacket(message) udpConn.inputPacket(message)
} }