@@ -13,15 +13,24 @@ import (
1313 "github.com/sagernet/sing/common/task"
1414)
1515
16+ // Deprecated: Use ServiceHandlerEx instead.
17+ //
18+ //nolint:staticcheck
1619type ServiceHandler interface {
1720 N.TCPConnectionHandler
1821 N.UDPConnectionHandler
1922}
2023
24+ type ServiceHandlerEx interface {
25+ N.TCPConnectionHandlerEx
26+ N.UDPConnectionHandlerEx
27+ }
28+
2129type Service struct {
2230 newStreamContext func (context.Context , net.Conn ) context.Context
2331 logger logger.ContextLogger
2432 handler ServiceHandler
33+ handlerEx ServiceHandlerEx
2534 padding bool
2635 brutal BrutalOptions
2736}
@@ -30,6 +39,7 @@ type ServiceOptions struct {
3039 NewStreamContext func (context.Context , net.Conn ) context.Context
3140 Logger logger.ContextLogger
3241 Handler ServiceHandler
42+ HandlerEx ServiceHandlerEx
3343 Padding bool
3444 Brutal BrutalOptions
3545}
@@ -42,12 +52,26 @@ func NewService(options ServiceOptions) (*Service, error) {
4252 newStreamContext : options .NewStreamContext ,
4353 logger : options .Logger ,
4454 handler : options .Handler ,
55+ handlerEx : options .HandlerEx ,
4556 padding : options .Padding ,
4657 brutal : options .Brutal ,
4758 }, nil
4859}
4960
61+ // Deprecated: Use NewConnectionEx instead.
5062func (s * Service ) NewConnection (ctx context.Context , conn net.Conn , metadata M.Metadata ) error {
63+ return s .newConnection (ctx , conn , metadata .Source )
64+ }
65+
66+ func (s * Service ) NewConnectionEx (ctx context.Context , conn net.Conn , source M.Socksaddr , destination M.Socksaddr , onClose N.CloseHandler ) {
67+ err := s .newConnection (ctx , conn , source )
68+ if err != nil {
69+ N .CloseOnHandshakeFailure (conn , onClose , err )
70+ s .logger .ErrorContext (ctx , E .Cause (err , "process multiplex connection from " , source ))
71+ }
72+ }
73+
74+ func (s * Service ) newConnection (ctx context.Context , conn net.Conn , source M.Socksaddr ) error {
5175 request , err := ReadRequest (conn )
5276 if err != nil {
5377 return err
@@ -71,9 +95,10 @@ func (s *Service) NewConnection(ctx context.Context, conn net.Conn, metadata M.M
7195 }
7296 streamCtx := s .newStreamContext (ctx , stream )
7397 go func () {
74- hErr := s .newConnection (streamCtx , conn , stream , metadata )
98+ hErr := s .newSession (streamCtx , conn , stream , source )
7599 if hErr != nil {
76- s .logger .ErrorContext (streamCtx , E .Cause (hErr , "handle connection" ))
100+ stream .Close ()
101+ s .logger .ErrorContext (streamCtx , E .Cause (hErr , "process multiplex stream" ))
77102 }
78103 }()
79104 }
@@ -84,13 +109,13 @@ func (s *Service) NewConnection(ctx context.Context, conn net.Conn, metadata M.M
84109 return group .Run (ctx )
85110}
86111
87- func (s * Service ) newConnection (ctx context.Context , sessionConn net.Conn , stream net.Conn , metadata M.Metadata ) error {
112+ func (s * Service ) newSession (ctx context.Context , sessionConn net.Conn , stream net.Conn , source M.Socksaddr ) error {
88113 stream = & wrapStream {stream }
89114 request , err := ReadStreamRequest (stream )
90115 if err != nil {
91116 return E .Cause (err , "read multiplex stream request" )
92117 }
93- metadata . Destination = request .Destination
118+ destination : = request .Destination
94119 if request .Network == N .NetworkTCP {
95120 conn := & serverConn {ExtendedConn : bufio .NewExtendedConn (stream )}
96121 if request .Destination .Fqdn == BrutalExchangeDomain {
@@ -128,20 +153,28 @@ func (s *Service) newConnection(ctx context.Context, sessionConn net.Conn, strea
128153 }
129154 return nil
130155 }
131- s .logger .InfoContext (ctx , "inbound multiplex connection to " , metadata .Destination )
132- s .handler .NewConnection (ctx , conn , metadata )
133- stream .Close ()
156+ s .logger .InfoContext (ctx , "inbound multiplex connection to " , destination )
157+ if s .handler != nil {
158+ //nolint:staticcheck
159+ s .handler .NewConnection (ctx , conn , M.Metadata {Source : source , Destination : destination })
160+ } else {
161+ s .handlerEx .NewConnectionEx (ctx , conn , source , destination , nil )
162+ }
134163 } else {
135164 var packetConn N.PacketConn
136165 if ! request .PacketAddr {
137- s .logger .InfoContext (ctx , "inbound multiplex packet connection to " , metadata . Destination )
166+ s .logger .InfoContext (ctx , "inbound multiplex packet connection to " , destination )
138167 packetConn = & serverPacketConn {ExtendedConn : bufio .NewExtendedConn (stream ), destination : request .Destination }
139168 } else {
140169 s .logger .InfoContext (ctx , "inbound multiplex packet connection" )
141170 packetConn = & serverPacketAddrConn {ExtendedConn : bufio .NewExtendedConn (stream )}
142171 }
143- s .handler .NewPacketConnection (ctx , packetConn , metadata )
144- stream .Close ()
172+ if s .handler != nil {
173+ //nolint:staticcheck
174+ s .handler .NewPacketConnection (ctx , packetConn , M.Metadata {Source : source , Destination : destination })
175+ } else {
176+ s .handlerEx .NewPacketConnectionEx (ctx , packetConn , source , destination , nil )
177+ }
145178 }
146179 return nil
147180}
0 commit comments