diff --git a/go.mod b/go.mod index 57ef9d0..4acdd79 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.20 require ( github.com/gofrs/uuid/v5 v5.3.0 github.com/sagernet/quic-go v0.48.1-beta.1 - 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/exp v0.0.0-20240506185415-9bf2ced13842 ) diff --git a/go.sum b/go.sum index a11e82f..cf11c9b 100644 --- a/go.sum +++ b/go.sum @@ -21,26 +21,20 @@ 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/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/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.5.1 h1:mhL/MZVq0TjuvHcpYcFtmSD1BFOxZ/+8ofbNZcg1k1Y= -github.com/sagernet/sing v0.5.1/go.mod h1:ARkL0gM13/Iv5VCZmci/NuoOlePoIsW0m7BWfln/Hak= +github.com/sagernet/sing v0.6.0-alpha.17 h1:y//jVrBjJMW6tRpA/ElT7+Snp3DHEJvO60D+DByg/Es= +github.com/sagernet/sing v0.6.0-alpha.17/go.mod h1:ARkL0gM13/Iv5VCZmci/NuoOlePoIsW0m7BWfln/Hak= 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.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= -golang.org/x/crypto v0.28.0 h1:GBDwsMXVQi34v5CCYUm2jkJvu4cbtru2U4TN2PSyQnw= -golang.org/x/crypto v0.28.0/go.mod h1:rmgy+3RHxRZMyY0jjAJShp2zgEdOqj2AO7U0pYmeQ7U= golang.org/x/crypto v0.29.0 h1:L5SG1JTTXupVV3n6sUqMTeWbjAyfPwoda2DLX8J8FrQ= golang.org/x/crypto v0.29.0/go.mod h1:+F4F4N5hv6v38hfeYwTdx20oUvLLc+QfrE9Ax9HtgRg= golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 h1:vr/HnozRka3pE4EsMEg1lgkXJkTFJCVUX+S/ZT6wYzM= golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842/go.mod h1:XtvwrStGgqGPLc4cjQfWqZHG1YFdYs6swckp8vpsjnc= golang.org/x/net v0.30.0 h1:AcW1SDZMkb8IpzCdQUaIq2sP4sZ4zw+55h6ynffypl4= golang.org/x/net v0.30.0/go.mod h1:2wGyMJ5iFasEhkwi13ChkO/t1ECNC4X4eBKkVFyYFlU= -golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo= -golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +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/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM= -golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= +golang.org/x/text v0.20.0 h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug= golang.org/x/text v0.20.0/go.mod h1:D4IsuqiFMhST5bX19pQ9ikHC2GsaKyk/oF+pn3ducp4= golang.org/x/tools v0.24.0 h1:J1shsA93PJUEVaUSaay7UXAyE8aimq3GW0pjlolpa24= golang.org/x/tools v0.24.0/go.mod h1:YhNqVBIfWHdzvTLs0d8LCuMhkKUgSUKldakyV7W/WDQ= diff --git a/hysteria/service.go b/hysteria/service.go index 725ffc3..7953411 100644 --- a/hysteria/service.go +++ b/hysteria/service.go @@ -46,8 +46,8 @@ type ServiceOptions struct { } type ServerHandler interface { - N.TCPConnectionHandler - N.UDPConnectionHandler + N.TCPConnectionHandlerEx + N.UDPConnectionHandlerEx } 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) if !request.UDP { - _ = s.handler.NewConnection(ctx, &serverConn{Stream: stream}, M.Metadata{ - Source: s.source, - Destination: M.ParseSocksaddrHostPort(request.Host, request.Port), - }) + s.handler.NewConnectionEx(ctx, &serverConn{Stream: stream}, s.source, M.ParseSocksaddrHostPort(request.Host, request.Port), nil) } else { if s.udpDisabled { return WriteServerResponse(stream, ServerResponse{ @@ -278,10 +275,7 @@ func (s *serverSession[U]) handleStream(stream quic.Stream) error { return err } newCtx, newConn := canceler.NewPacketConn(udpConn.ctx, udpConn, s.udpTimeout) - go s.handler.NewPacketConnection(newCtx, newConn, M.Metadata{ - Source: s.source, - Destination: M.ParseSocksaddrHostPort(request.Host, request.Port), - }) + go s.handler.NewPacketConnectionEx(newCtx, newConn, s.source, M.ParseSocksaddrHostPort(request.Host, request.Port), nil) holdBuffer := make([]byte, 1024) for { _, hErr := stream.Read(holdBuffer) @@ -330,7 +324,7 @@ type serverConn struct { func (c *serverConn) HandshakeFailure(err error) error { if c.responseWritten { - return os.ErrClosed + return os.ErrInvalid } c.responseWritten = true return WriteServerResponse(c.Stream, ServerResponse{ diff --git a/hysteria2/service.go b/hysteria2/service.go index d9c6eb6..c94a738 100644 --- a/hysteria2/service.go +++ b/hysteria2/service.go @@ -47,8 +47,8 @@ type ServiceOptions struct { } type ServerHandler interface { - N.TCPConnectionHandler - N.UDPConnectionHandler + N.TCPConnectionHandlerEx + N.UDPConnectionHandlerEx } type Service[U comparable] struct { @@ -250,8 +250,6 @@ func (s *serverSession[U]) handleStream0(frameType http3.FrameType, id quic.Conn } go func() { hErr := s.handleStream(stream) - stream.CancelRead(0) - stream.Close() if hErr != nil { stream.CancelRead(0) stream.Close() @@ -266,11 +264,7 @@ func (s *serverSession[U]) handleStream(stream quic.Stream) error { if err != nil { return E.New("read TCP request") } - ctx := auth.ContextWithUser(s.ctx, s.authUser) - _ = s.handler.NewConnection(ctx, &serverConn{Stream: stream}, M.Metadata{ - Source: s.source, - Destination: M.ParseSocksaddr(destinationString), - }) + s.handler.NewConnectionEx(auth.ContextWithUser(s.ctx, s.authUser), &serverConn{Stream: stream}, s.source, M.ParseSocksaddr(destinationString), nil) return nil } @@ -299,7 +293,7 @@ type serverConn struct { func (c *serverConn) HandshakeFailure(err error) error { if c.responseWritten { - return os.ErrClosed + return os.ErrInvalid } c.responseWritten = true buffer := protocol.WriteTCPResponse(false, err.Error(), nil) diff --git a/hysteria2/service_packet.go b/hysteria2/service_packet.go index 0349fbb..bf7524d 100644 --- a/hysteria2/service_packet.go +++ b/hysteria2/service_packet.go @@ -49,10 +49,7 @@ func (s *serverSession[U]) handleUDPMessage(message *udpMessage) { s.udpConnMap[message.sessionID] = udpConn s.udpAccess.Unlock() newCtx, newConn := canceler.NewPacketConn(udpConn.ctx, udpConn, s.udpTimeout) - go s.handler.NewPacketConnection(newCtx, newConn, M.Metadata{ - Source: s.source, - Destination: M.ParseSocksaddr(message.destination), - }) + go s.handler.NewPacketConnectionEx(newCtx, newConn, s.source, M.ParseSocksaddr(message.destination), nil) } udpConn.inputPacket(message) } diff --git a/tuic/service.go b/tuic/service.go index b57f9af..8a0d07a 100644 --- a/tuic/service.go +++ b/tuic/service.go @@ -40,8 +40,8 @@ type ServiceOptions struct { } type ServiceHandler interface { - N.TCPConnectionHandler - N.UDPConnectionHandler + N.TCPConnectionHandlerEx + N.UDPConnectionHandlerEx } type Service[U comparable] struct { @@ -362,11 +362,7 @@ func (s *serverSession[U]) handleStream(stream quic.Stream) error { } else { conn = bufio.NewCachedConn(conn, buffer) } - ctx := auth.ContextWithUser(s.ctx, s.authUser) - _ = s.handler.NewConnection(ctx, conn, M.Metadata{ - Source: s.source, - Destination: destination, - }) + s.handler.NewConnectionEx(auth.ContextWithUser(s.ctx, s.authUser), conn, s.source, destination, nil) return nil } diff --git a/tuic/service_packet.go b/tuic/service_packet.go index 5493d8a..46aba71 100644 --- a/tuic/service_packet.go +++ b/tuic/service_packet.go @@ -5,7 +5,6 @@ import ( "github.com/sagernet/sing/common/auth" "github.com/sagernet/sing/common/canceler" E "github.com/sagernet/sing/common/exceptions" - M "github.com/sagernet/sing/common/metadata" ) func (s *serverSession[U]) loopMessages() { @@ -67,10 +66,7 @@ func (s *serverSession[U]) handleUDPMessage(message *udpMessage, udpStream bool) s.udpConnMap[message.sessionID] = udpConn s.udpAccess.Unlock() newCtx, newConn := canceler.NewPacketConn(udpConn.ctx, udpConn, s.udpTimeout) - go s.handler.NewPacketConnection(newCtx, newConn, M.Metadata{ - Source: s.source, - Destination: message.destination, - }) + go s.handler.NewPacketConnectionEx(newCtx, newConn, s.source, message.destination, nil) } udpConn.inputPacket(message) }