Skip to content

Commit

Permalink
Update handler usages
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Jan 1, 2025
1 parent 8cd5f17 commit fa2ae2a
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 37 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.20
require (
github.com/gofrs/uuid/v5 v5.3.0
github.com/sagernet/reality v0.0.0-20230406110435-ee17307e7691
github.com/sagernet/sing v0.5.1
github.com/sagernet/sing v0.6.0-alpha.18
github.com/sagernet/utls v1.6.7
golang.org/x/crypto v0.31.0
)
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ github.com/klauspost/compress v1.17.4/go.mod h1:/dCuZOvVtNoHsyb+cuJD3itjs3NbnF6K
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/sagernet/reality v0.0.0-20230406110435-ee17307e7691 h1:5Th31OC6yj8byLGkEnIYp6grlXfo1QYUfiYFGjewIdc=
github.com/sagernet/reality v0.0.0-20230406110435-ee17307e7691/go.mod h1:B8lp4WkQ1PwNnrVMM6KyuFR20pU8jYBD+A4EhJovEXU=
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.18 h1:ih4CurU8KvbhfagYjSqVrE2LR0oBSXSZTNH2sAGPGiM=
github.com/sagernet/sing v0.6.0-alpha.18/go.mod h1:ARkL0gM13/Iv5VCZmci/NuoOlePoIsW0m7BWfln/Hak=
github.com/sagernet/utls v1.6.7 h1:Ep3+aJ8FUGGta+II2IEVNUc3EDhaRCZINWkj/LloIA8=
github.com/sagernet/utls v1.6.7/go.mod h1:Uua1TKO/FFuAhLr9rkaVnnrTmmiItzDjv1BUb2+ERwM=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
Expand Down
22 changes: 9 additions & 13 deletions mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ import (
N "github.com/sagernet/sing/common/network"
)

func HandleMuxConnection(ctx context.Context, conn net.Conn, handler Handler) error {
func HandleMuxConnection(ctx context.Context, conn net.Conn, source M.Socksaddr, handler Handler) error {
ctx, cancel := context.WithCancelCause(ctx)
session := &serverSession{
ctx: ctx,
source: source,
conn: conn,
directWriter: bufio.NewExtendedWriter(conn),
handler: handler,
Expand All @@ -38,6 +39,7 @@ func HandleMuxConnection(ctx context.Context, conn net.Conn, handler Handler) er

type serverSession struct {
ctx context.Context
source M.Socksaddr
conn net.Conn
directWriter N.ExtendedWriter
handler Handler
Expand Down Expand Up @@ -135,27 +137,21 @@ func (c *serverSession) recv() error {
return E.New("bad network: ", network)
}
go func() {
var hErr error
if network == NetworkTCP {
hErr = c.handler.NewConnection(c.ctx, &serverMuxConn{
conn := &serverMuxConn{
sessionID,
pipeIn,
c,
}, M.Metadata{
Destination: destination,
})
}
c.handler.NewConnectionEx(c.ctx, conn, c.source, destination, nil)
} else {
hErr = c.handler.NewPacketConnection(c.ctx, &serverMuxPacketConn{
conn := &serverMuxPacketConn{
sessionID,
pipeIn,
c,
destination,
}, M.Metadata{
Destination: destination,
})
}
if hErr != nil {
c.handler.NewError(c.ctx, hErr)
}
c.handler.NewPacketConnectionEx(c.ctx, conn, c.source, destination, nil)
}
}()
case StatusKeep:
Expand Down
19 changes: 9 additions & 10 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,9 @@ import (
"github.com/gofrs/uuid/v5"
)

var _ N.TCPConnectionHandler = (*Service[string])(nil)

type Handler interface {
N.TCPConnectionHandler
N.UDPConnectionHandler
E.Handler
N.TCPConnectionHandlerEx
N.UDPConnectionHandlerEx
}

var (
Expand Down Expand Up @@ -171,7 +168,7 @@ func (s *Service[U]) generateLegacyKeys() {
s.alterIdMap = userAlterIdMap
}

func (s *Service[U]) NewConnection(ctx context.Context, conn net.Conn, metadata M.Metadata) error {
func (s *Service[U]) NewConnection(ctx context.Context, conn net.Conn, source M.Socksaddr, onClose N.CloseHandlerFunc) error {
const headerLenBufferLen = 2 + CipherOverhead
const aeadMinHeaderLen = 16 + headerLenBufferLen + 8 + CipherOverhead + 42
minHeaderLen := aeadMinHeaderLen
Expand Down Expand Up @@ -322,8 +319,9 @@ func (s *Service[U]) NewConnection(ctx context.Context, conn net.Conn, metadata
if command == CommandUDP && option == 0 {
return E.New("bad packet connection")
}
var destination M.Socksaddr
if command != CommandMux {
metadata.Destination, err = AddressSerializer.ReadAddrPort(headerReader)
destination, err = AddressSerializer.ReadAddrPort(headerReader)
if err != nil {
return err
}
Expand Down Expand Up @@ -358,14 +356,15 @@ func (s *Service[U]) NewConnection(ctx context.Context, conn net.Conn, metadata

switch command {
case CommandTCP:
return s.handler.NewConnection(ctx, &serverConn{rawConn}, metadata)
s.handler.NewConnectionEx(ctx, &serverConn{rawConn}, source, destination, onClose)
case CommandUDP:
return s.handler.NewPacketConnection(ctx, &serverPacketConn{rawConn, metadata.Destination}, metadata)
s.handler.NewPacketConnectionEx(ctx, &serverPacketConn{rawConn, destination}, source, destination, onClose)
case CommandMux:
return HandleMuxConnection(ctx, &serverConn{rawConn}, s.handler)
return HandleMuxConnection(ctx, &serverConn{rawConn}, source, s.handler)
default:
return E.New("unknown command: ", command)
}
return nil
}

type rawServerConn struct {
Expand Down
19 changes: 8 additions & 11 deletions vless/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ type Service[T comparable] struct {
}

type Handler interface {
N.TCPConnectionHandler
N.UDPConnectionHandler
E.Handler
N.TCPConnectionHandlerEx
N.UDPConnectionHandlerEx
}

func NewService[T comparable](logger logger.Logger, handler Handler) *Service[T] {
Expand All @@ -53,9 +52,7 @@ func (s *Service[T]) UpdateUsers(userList []T, userUUIDList []string, userFlowLi
s.userFlow = userFlowMap
}

var _ N.TCPConnectionHandler = (*Service[int])(nil)

func (s *Service[T]) NewConnection(ctx context.Context, conn net.Conn, metadata M.Metadata) error {
func (s *Service[T]) NewConnection(ctx context.Context, conn net.Conn, source M.Socksaddr, onClose N.CloseHandlerFunc) error {
request, err := ReadRequest(conn)
if err != nil {
return err
Expand All @@ -65,8 +62,6 @@ func (s *Service[T]) NewConnection(ctx context.Context, conn net.Conn, metadata
return E.New("unknown UUID: ", uuid.FromBytesOrNil(request.UUID[:]))
}
ctx = auth.ContextWithUser(ctx, user)
metadata.Destination = request.Destination

userFlow := s.userFlow[user]
if request.Flow == FlowVision && request.Command == vmess.NetworkUDP {
return E.New(FlowVision, " flow does not support UDP")
Expand All @@ -75,7 +70,8 @@ func (s *Service[T]) NewConnection(ctx context.Context, conn net.Conn, metadata
}

if request.Command == vmess.CommandUDP {
return s.handler.NewPacketConnection(ctx, &serverPacketConn{ExtendedConn: bufio.NewExtendedConn(conn), destination: request.Destination}, metadata)
s.handler.NewPacketConnectionEx(ctx, &serverPacketConn{ExtendedConn: bufio.NewExtendedConn(conn), destination: request.Destination}, source, request.Destination, onClose)
return nil
}
responseConn := &serverConn{ExtendedConn: bufio.NewExtendedConn(conn), writer: bufio.NewVectorisedWriter(conn)}
switch userFlow {
Expand All @@ -91,9 +87,10 @@ func (s *Service[T]) NewConnection(ctx context.Context, conn net.Conn, metadata
}
switch request.Command {
case vmess.CommandTCP:
return s.handler.NewConnection(ctx, conn, metadata)
s.handler.NewConnectionEx(ctx, conn, source, request.Destination, onClose)
return nil
case vmess.CommandMux:
return vmess.HandleMuxConnection(ctx, conn, s.handler)
return vmess.HandleMuxConnection(ctx, conn, source, s.handler)
default:
return E.New("unknown command: ", request.Command)
}
Expand Down

0 comments on commit fa2ae2a

Please sign in to comment.