forked from SagerNet/sing-quic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
382 lines (356 loc) · 9.76 KB
/
service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
package hysteria
import (
"context"
"errors"
"io"
"math"
"net"
"os"
"runtime"
"sync"
"time"
qtls "github.com/minjunlan/sing-quic"
hyCC "github.com/minjunlan/sing-quic/hysteria/congestion"
"github.com/sagernet/quic-go"
"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/auth"
"github.com/sagernet/sing/common/baderror"
"github.com/sagernet/sing/common/canceler"
E "github.com/sagernet/sing/common/exceptions"
"github.com/sagernet/sing/common/logger"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
aTLS "github.com/sagernet/sing/common/tls"
)
type ServiceOptions struct {
Context context.Context
Logger logger.Logger
BrutalDebug bool
SendBPS uint64
ReceiveBPS uint64
XPlusPassword string
TLSConfig aTLS.ServerConfig
UDPDisabled bool
UDPTimeout time.Duration
Handler ServerHandler
// Legacy options
ConnReceiveWindow uint64
StreamReceiveWindow uint64
MaxIncomingStreams int64
DisableMTUDiscovery bool
}
type ServerHandler interface {
N.TCPConnectionHandler
N.UDPConnectionHandler
}
type Service[U comparable] struct {
ctx context.Context
logger logger.Logger
brutalDebug bool
sendBPS uint64
receiveBPS uint64
xplusPassword string
tlsConfig aTLS.ServerConfig
quicConfig *quic.Config
userMap map[string]U
udpDisabled bool
udpTimeout time.Duration
handler ServerHandler
quicListener io.Closer
}
func NewService[U comparable](options ServiceOptions) (*Service[U], error) {
quicConfig := &quic.Config{
DisablePathMTUDiscovery: !(runtime.GOOS == "windows" || runtime.GOOS == "linux" || runtime.GOOS == "android" || runtime.GOOS == "darwin"),
EnableDatagrams: !options.UDPDisabled,
MaxIncomingStreams: 1 << 60,
InitialStreamReceiveWindow: DefaultStreamReceiveWindow,
MaxStreamReceiveWindow: DefaultStreamReceiveWindow,
InitialConnectionReceiveWindow: DefaultConnReceiveWindow,
MaxConnectionReceiveWindow: DefaultConnReceiveWindow,
MaxIdleTimeout: DefaultMaxIdleTimeout,
KeepAlivePeriod: DefaultKeepAlivePeriod,
}
if options.StreamReceiveWindow != 0 {
quicConfig.InitialStreamReceiveWindow = options.StreamReceiveWindow
quicConfig.MaxStreamReceiveWindow = options.StreamReceiveWindow
}
if options.ConnReceiveWindow != 0 {
quicConfig.InitialConnectionReceiveWindow = options.ConnReceiveWindow
quicConfig.MaxConnectionReceiveWindow = options.ConnReceiveWindow
}
if options.MaxIncomingStreams > 0 {
quicConfig.MaxIncomingStreams = int64(options.MaxIncomingStreams)
}
if options.DisableMTUDiscovery {
quicConfig.DisablePathMTUDiscovery = true
}
if len(options.TLSConfig.NextProtos()) == 0 {
options.TLSConfig.SetNextProtos([]string{DefaultALPN})
}
if options.SendBPS == 0 {
return nil, E.New("missing upload speed configuration")
}
if options.ReceiveBPS == 0 {
return nil, E.New("missing download speed configuration")
}
return &Service[U]{
ctx: options.Context,
logger: options.Logger,
brutalDebug: options.BrutalDebug,
sendBPS: options.SendBPS,
receiveBPS: options.ReceiveBPS,
xplusPassword: options.XPlusPassword,
tlsConfig: options.TLSConfig,
quicConfig: quicConfig,
userMap: make(map[string]U),
handler: options.Handler,
udpDisabled: options.UDPDisabled,
udpTimeout: options.UDPTimeout,
}, nil
}
func (s *Service[U]) UpdateUsers(userList []U, passwordList []string) {
userMap := make(map[string]U)
for i, user := range userList {
userMap[passwordList[i]] = user
}
s.userMap = userMap
}
func (s *Service[U]) Start(conn net.PacketConn) error {
if s.xplusPassword != "" {
conn = NewXPlusPacketConn(conn, []byte(s.xplusPassword))
}
listener, err := qtls.Listen(conn, s.tlsConfig, s.quicConfig)
if err != nil {
return err
}
s.quicListener = listener
go s.loopConnections(listener)
return nil
}
func (s *Service[U]) Close() error {
return common.Close(
s.quicListener,
)
}
func (s *Service[U]) loopConnections(listener qtls.Listener) {
for {
connection, err := listener.Accept(s.ctx)
if err != nil {
if E.IsClosedOrCanceled(err) || errors.Is(err, quic.ErrServerClosed) {
s.logger.Debug(E.Cause(err, "listener closed"))
} else {
s.logger.Error(E.Cause(err, "listener closed"))
}
return
}
session := &serverSession[U]{
Service: s,
ctx: s.ctx,
quicConn: connection,
source: M.SocksaddrFromNet(connection.RemoteAddr()),
connDone: make(chan struct{}),
udpConnMap: make(map[uint32]*udpPacketConn),
}
go session.handleConnection()
}
}
type serverSession[U comparable] struct {
*Service[U]
ctx context.Context
quicConn quic.Connection
source M.Socksaddr
connAccess sync.Mutex
connDone chan struct{}
connErr error
authUser U
udpAccess sync.RWMutex
udpConnMap map[uint32]*udpPacketConn
udpSessionID uint32
}
func (s *serverSession[U]) handleConnection() {
ctx, cancel := context.WithTimeout(s.ctx, ProtocolTimeout)
controlStream, err := s.quicConn.AcceptStream(ctx)
cancel()
if err != nil {
s.closeWithError0(ErrorCodeProtocolError, err)
return
}
clientHello, err := ReadClientHello(controlStream)
if err != nil {
s.closeWithError0(ErrorCodeProtocolError, E.Cause(err, "read client hello"))
return
}
user, loaded := s.userMap[clientHello.Auth]
if !loaded {
WriteServerHello(controlStream, ServerHello{
OK: false,
Message: "Wrong password",
})
s.closeWithError0(ErrorCodeAuthError, E.Cause(err, "authentication failed, auth_str=", clientHello.Auth))
return
}
err = WriteServerHello(controlStream, ServerHello{
OK: true,
SendBPS: s.sendBPS,
RecvBPS: s.receiveBPS,
})
if err != nil {
s.closeWithError(err)
return
}
s.authUser = user
s.quicConn.SetCongestionControl(hyCC.NewBrutalSender(uint64(math.Min(float64(s.sendBPS), float64(clientHello.RecvBPS))), s.brutalDebug, s.logger))
if !s.udpDisabled {
go s.loopMessages()
}
s.loopStreams()
}
func (s *serverSession[U]) loopStreams() {
for {
stream, err := s.quicConn.AcceptStream(s.ctx)
if err != nil {
return
}
go func() {
err = s.handleStream(stream)
if err != nil {
stream.CancelRead(0)
stream.Close()
s.logger.Error(E.Cause(err, "handle stream request"))
}
}()
}
}
func (s *serverSession[U]) handleStream(stream quic.Stream) error {
request, err := ReadClientRequest(stream)
if err != nil {
return E.New("read TCP request")
}
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),
})
} else {
if s.udpDisabled {
return WriteServerResponse(stream, ServerResponse{
OK: false,
Message: "UDP disabled by server",
})
}
var sessionID uint32
udpConn := newUDPPacketConn(ctx, s.quicConn, func() {
stream.CancelRead(0)
stream.Close()
s.udpAccess.Lock()
delete(s.udpConnMap, sessionID)
s.udpAccess.Unlock()
})
s.udpAccess.Lock()
s.udpSessionID++
sessionID = s.udpSessionID
udpConn.sessionID = sessionID
s.udpConnMap[sessionID] = udpConn
s.udpAccess.Unlock()
err = WriteServerResponse(stream, ServerResponse{
OK: true,
UDPSessionID: sessionID,
})
if err != nil {
udpConn.closeWithError(E.Cause(err, "write server response"))
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),
})
holdBuffer := make([]byte, 1024)
for {
_, hErr := stream.Read(holdBuffer)
if hErr != nil {
break
}
}
udpConn.closeWithError(E.Cause(net.ErrClosed, "hold stream closed"))
}
return nil
}
func (s *serverSession[U]) closeWithError(err error) {
s.closeWithError0(ErrorCodeGeneric, err)
}
func (s *serverSession[U]) closeWithError0(errorCode int, err error) {
s.connAccess.Lock()
defer s.connAccess.Unlock()
select {
case <-s.connDone:
return
default:
s.connErr = err
close(s.connDone)
}
if E.IsClosedOrCanceled(err) {
s.logger.Debug(E.Cause(err, "connection failed"))
} else {
s.logger.Error(E.Cause(err, "connection failed"))
}
switch errorCode {
case ErrorCodeProtocolError:
_ = s.quicConn.CloseWithError(quic.ApplicationErrorCode(errorCode), "protocol error")
case ErrorCodeAuthError:
_ = s.quicConn.CloseWithError(quic.ApplicationErrorCode(errorCode), "auth error")
default:
_ = s.quicConn.CloseWithError(quic.ApplicationErrorCode(errorCode), "")
}
}
type serverConn struct {
quic.Stream
responseWritten bool
}
func (c *serverConn) HandshakeFailure(err error) error {
if c.responseWritten {
return os.ErrClosed
}
c.responseWritten = true
return WriteServerResponse(c.Stream, ServerResponse{
OK: false,
Message: err.Error(),
})
}
func (c *serverConn) HandshakeSuccess() error {
if c.responseWritten {
return nil
}
c.responseWritten = true
return WriteServerResponse(c.Stream, ServerResponse{
OK: true,
})
}
func (c *serverConn) Read(p []byte) (n int, err error) {
n, err = c.Stream.Read(p)
return n, baderror.WrapQUIC(err)
}
func (c *serverConn) Write(p []byte) (n int, err error) {
if !c.responseWritten {
c.responseWritten = true
err = WriteServerResponse(c.Stream, ServerResponse{
OK: true,
})
if err != nil {
return 0, baderror.WrapQUIC(err)
}
}
n, err = c.Stream.Write(p)
return n, baderror.WrapQUIC(err)
}
func (c *serverConn) LocalAddr() net.Addr {
return M.Socksaddr{}
}
func (c *serverConn) RemoteAddr() net.Addr {
return M.Socksaddr{}
}
func (c *serverConn) Close() error {
c.Stream.CancelRead(0)
return c.Stream.Close()
}