@@ -20,14 +20,25 @@ import (
20
20
"github.com/sagernet/sing/common/task"
21
21
)
22
22
23
+ type Service struct {
24
+ version int
25
+ password string
26
+ users []User
27
+ handshake HandshakeConfig
28
+ handshakeForServerName map [string ]HandshakeConfig
29
+ strictMode bool
30
+ handler N.TCPConnectionHandlerEx
31
+ logger logger.ContextLogger
32
+ }
33
+
23
34
type ServiceConfig struct {
24
35
Version int
25
36
Password string // for protocol version 2
26
37
Users []User // for protocol version 3
27
38
Handshake HandshakeConfig
28
39
HandshakeForServerName map [string ]HandshakeConfig // for protocol version 2/3
29
40
StrictMode bool // for protocol version 3
30
- Handler Handler
41
+ Handler N. TCPConnectionHandlerEx
31
42
Logger logger.ContextLogger
32
43
}
33
44
@@ -41,22 +52,6 @@ type HandshakeConfig struct {
41
52
Dialer N.Dialer
42
53
}
43
54
44
- type Handler interface {
45
- N.TCPConnectionHandler
46
- E.Handler
47
- }
48
-
49
- type Service struct {
50
- version int
51
- password string
52
- users []User
53
- handshake HandshakeConfig
54
- handshakeForServerName map [string ]HandshakeConfig
55
- strictMode bool
56
- handler Handler
57
- logger logger.ContextLogger
58
- }
59
-
60
55
func NewService (config ServiceConfig ) (* Service , error ) {
61
56
service := & Service {
62
57
version : config .Version ,
@@ -99,7 +94,7 @@ func (s *Service) selectHandshake(clientHelloFrame *buf.Buffer) HandshakeConfig
99
94
return s .handshake
100
95
}
101
96
102
- func (s * Service ) NewConnection (ctx context.Context , conn net.Conn , metadata M.Metadata ) error {
97
+ func (s * Service ) NewConnection (ctx context.Context , conn net.Conn , source M.Socksaddr , destination M. Socksaddr , onClose N. CloseHandlerFunc ) error {
103
98
switch s .version {
104
99
default :
105
100
fallthrough
@@ -125,7 +120,8 @@ func (s *Service) NewConnection(ctx context.Context, conn net.Conn, metadata M.M
125
120
return err
126
121
}
127
122
s .logger .TraceContext (ctx , "handshake finished" )
128
- return s .handler .NewConnection (ctx , conn , metadata )
123
+ s .handler .NewConnectionEx (ctx , conn , source , destination , onClose )
124
+ return nil
129
125
case 2 :
130
126
clientHelloFrame , err := extractFrame (conn )
131
127
if err != nil {
@@ -144,7 +140,8 @@ func (s *Service) NewConnection(ctx context.Context, conn net.Conn, metadata M.M
144
140
if err == nil {
145
141
s .logger .TraceContext (ctx , "handshake finished" )
146
142
handshakeConn .Close ()
147
- return s .handler .NewConnection (ctx , bufio .NewCachedConn (newConn (conn ), request ), metadata )
143
+ s .handler .NewConnectionEx (ctx , bufio .NewCachedConn (newConn (conn ), request ), source , destination , onClose )
144
+ return nil
148
145
} else if err == os .ErrPermission {
149
146
s .logger .WarnContext (ctx , "fallback connection" )
150
147
hashConn .Fallback ()
@@ -247,6 +244,7 @@ func (s *Service) NewConnection(ctx context.Context, conn net.Conn, metadata M.M
247
244
return E .Cause (err , "handshake relay" )
248
245
}
249
246
s .logger .TraceContext (ctx , "handshake relay finished" )
250
- return s .handler .NewConnection (ctx , bufio .NewCachedConn (newVerifiedConn (conn , hmacAdd , hmacVerify , nil ), clientFirstFrame ), metadata )
247
+ s .handler .NewConnectionEx (ctx , bufio .NewCachedConn (newVerifiedConn (conn , hmacAdd , hmacVerify , nil ), clientFirstFrame ), source , destination , onClose )
248
+ return nil
251
249
}
252
250
}
0 commit comments