forked from SagerNet/sing-tun
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_go.go
More file actions
315 lines (296 loc) · 8.46 KB
/
Copy pathstack_go.go
File metadata and controls
315 lines (296 loc) · 8.46 KB
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
package tun
import (
"context"
"errors"
"net/netip"
"runtime"
"sync"
"sync/atomic"
"time"
"github.com/sagernet/sing-tun/gtcpip/header"
"github.com/sagernet/sing/common/buf"
E "github.com/sagernet/sing/common/exceptions"
"github.com/sagernet/sing/common/logger"
)
type Go struct {
ctx context.Context
tun Tun
mtu int
handler Handler
logger logger.Logger
broadcastAddr netip.Addr
inet4LoopbackAddress []netip.Addr
inet6LoopbackAddress []netip.Addr
udpTimeout time.Duration
icmpTimeout time.Duration
udpNATOptions UDPNatOptions
memoryPressure func() MemoryPressure
udpNats []*UDPNat
fragmentIdentification atomic.Uint32
dispatcher *ForwardDispatcher
directory goFlowDirectory
congestion *goCongestionOps
queueFactory func(stack *Go) ([]goPlatformIO, error)
access sync.Mutex
engines []*goEngine
closed atomic.Bool
validateChecksum bool
}
func NewGo(options StackOptions) (*Go, error) {
congestion, err := goLookupCongestionControl(options.TCPCongestionControl)
if err != nil {
return nil, err
}
stack := &Go{
congestion: congestion,
queueFactory: newGoPlatformQueues,
ctx: options.Context,
tun: options.Tun,
mtu: int(options.TunOptions.MTU),
handler: options.Handler,
logger: options.Logger,
broadcastAddr: BroadcastAddr(options.TunOptions.Inet4Address),
inet4LoopbackAddress: options.TunOptions.Inet4LoopbackAddress,
inet6LoopbackAddress: options.TunOptions.Inet6LoopbackAddress,
udpTimeout: options.UDPTimeout,
icmpTimeout: options.ICMPTimeout,
udpNATOptions: UDPNatOptions{
Timeout: options.UDPTimeout,
Mapping: options.UDPMapping,
Filtering: options.UDPFiltering,
MaxSize: options.UDPNATMax,
InterfaceFinder: options.InterfaceFinder,
},
memoryPressure: options.MemoryPressure,
directory: goFlowDirectory{
flows: make(map[flowKey]*GoConn),
udpSockets: make(map[netip.AddrPort]*GoUDPConn),
listeners: make(map[netip.AddrPort]*GoListener),
},
}
memoryTun, isMemoryTun := options.Tun.(*MemoryTun)
if isMemoryTun {
stack.validateChecksum = true
stack.queueFactory = func(current *Go) ([]goPlatformIO, error) {
return []goPlatformIO{&goMemoryIO{stack: current, tun: memoryTun, handed: make([]*buf.Buffer, 0, goReadBatch), blocked: make(map[*OutboundQueue]*goBlockedWriters)}}, nil
}
}
return stack, nil
}
func (s *Go) Start() error {
s.access.Lock()
defer s.access.Unlock()
queues, err := s.queueFactory(s)
if err != nil {
return err
}
for index, queue := range queues {
err = queue.start()
if err == nil {
continue
}
if index > 0 && errors.Is(err, errGoQueueUnavailable) {
queues = queues[:index]
break
}
return E.Errors(err, goCloseQueues(queues[:index]))
}
natCount := uint32(1)
var udpNats []*UDPNat
if s.handler != nil {
udpNATOptions := s.udpNATOptions
udpNATOptions.Handler = s.handler
udpNATOptions.Prepare = s.prepareUDPConnection
maxSize := udpNATOptions.MaxSize
if maxSize == 0 {
if runtime.GOOS == "ios" {
maxSize = 4096
} else {
maxSize = 16384
}
}
if udpNATOptions.Mapping == NATMappingAddressAndPortDependent {
natCount = min(uint32(len(queues)), maxSize)
}
udpNats = make([]*UDPNat, natCount)
for index := range udpNats {
udpNATOptions.Shared = index+int(natCount) < len(queues)
udpNATOptions.MaxSize = maxSize / natCount
if uint32(index) < maxSize%natCount {
udpNATOptions.MaxSize++
}
udpNat := NewUDPNat(udpNATOptions)
err = udpNat.Start()
if err != nil {
udpNat.Close()
for _, started := range udpNats[:index] {
started.Close()
}
return E.Errors(err, goCloseQueues(queues))
}
udpNats[index] = udpNat
}
s.dispatcher = NewForwardDispatcher(s.handler, &goWriteback{platformIO: queues[0]}, s.logger, s.udpTimeout, s.icmpTimeout)
}
s.udpNats = udpNats
if natCount > 1 {
s.directory.udpFlows = make(map[udpNatSessionKey]*GoPacketConn)
}
s.engines = make([]*goEngine, len(queues))
for index, queue := range queues {
s.engines[index] = newGoEngine(s, queue, len(queues))
if udpNats != nil {
s.engines[index].udpNat = udpNats[index%int(natCount)]
}
}
for _, engine := range s.engines {
go engine.run()
}
return nil
}
func goCloseQueues(queues []goPlatformIO) error {
var err error
for _, queue := range queues {
err = E.Errors(err, queue.close())
}
return err
}
func (s *Go) HasEndpoint(protocol uint8, local, remote netip.AddrPort) bool {
if s.closed.Load() || !local.IsValid() || !remote.IsValid() {
return false
}
local = netip.AddrPortFrom(local.Addr().Unmap(), local.Port())
remote = netip.AddrPortFrom(remote.Addr().Unmap(), remote.Port())
if local.Addr().Is4() != remote.Addr().Is4() {
return false
}
s.directory.access.RLock()
defer s.directory.access.RUnlock()
switch protocol {
case uint8(header.TCPProtocolNumber):
return s.directory.flows[flowKey{protocol: protocol, source: remote, destination: local}] != nil || s.directory.listeners[local] != nil
case uint8(header.UDPProtocolNumber):
socket := s.directory.udpSockets[local]
return socket != nil && (!socket.connected || socket.remote == remote)
default:
return false
}
}
type goFlowDirectory struct {
access sync.RWMutex
flows map[flowKey]*GoConn
udpFlows map[udpNatSessionKey]*GoPacketConn
udpSockets map[netip.AddrPort]*GoUDPConn
listeners map[netip.AddrPort]*GoListener
}
func (d *goFlowDirectory) insertListener(listener *GoListener) {
d.access.Lock()
if len(listener.engine.stack.engines) > 1 {
listener.engine.tcpListeners[listener.local] = listener
}
d.listeners[listener.local] = listener
d.access.Unlock()
}
func (d *goFlowDirectory) removeListener(listener *GoListener) {
d.access.Lock()
if len(listener.engine.stack.engines) > 1 && listener.engine.tcpListeners[listener.local] == listener {
delete(listener.engine.tcpListeners, listener.local)
}
if d.listeners[listener.local] == listener {
delete(d.listeners, listener.local)
}
d.access.Unlock()
}
func (d *goFlowDirectory) lookupListener(local netip.AddrPort) *GoListener {
d.access.RLock()
listener := d.listeners[local]
d.access.RUnlock()
return listener
}
func (d *goFlowDirectory) insertUDPSocket(socket *GoUDPConn) {
d.access.Lock()
if len(socket.engine.stack.engines) > 1 {
socket.engine.udpSockets[socket.local] = socket
}
d.udpSockets[socket.local] = socket
d.access.Unlock()
}
func (d *goFlowDirectory) removeUDPSocket(socket *GoUDPConn) {
d.access.Lock()
if len(socket.engine.stack.engines) > 1 && socket.engine.udpSockets[socket.local] == socket {
delete(socket.engine.udpSockets, socket.local)
}
if d.udpSockets[socket.local] == socket {
delete(d.udpSockets, socket.local)
}
d.access.Unlock()
}
func (d *goFlowDirectory) lookupUDPSocket(local netip.AddrPort) *GoUDPConn {
d.access.RLock()
socket := d.udpSockets[local]
d.access.RUnlock()
return socket
}
func (d *goFlowDirectory) insert(key flowKey, conn *GoConn) {
d.access.Lock()
if len(conn.engine.stack.engines) > 1 {
conn.engine.flows[key] = conn
}
d.flows[key] = conn
d.access.Unlock()
}
func (d *goFlowDirectory) remove(key flowKey, conn *GoConn) {
d.access.Lock()
if len(conn.engine.stack.engines) > 1 && conn.engine.flows[key] == conn {
delete(conn.engine.flows, key)
}
if d.flows[key] == conn {
delete(d.flows, key)
}
d.access.Unlock()
}
func (d *goFlowDirectory) lookup(key flowKey) *GoConn {
d.access.RLock()
conn := d.flows[key]
d.access.RUnlock()
return conn
}
func (s *Go) ResetNetwork() {
s.access.Lock()
defer s.access.Unlock()
if s.closed.Load() {
return
}
for _, udpNat := range s.udpNats {
udpNat.Purge()
}
s.dispatcher.ResetNetwork()
for _, engine := range s.engines {
engine.postMessage(&engine.resetMessage)
}
}
func (s *Go) Close() error {
s.access.Lock()
if !s.closed.CompareAndSwap(false, true) {
s.access.Unlock()
return nil
}
for _, engine := range s.engines {
engine.postMessage(&engine.closeMessage)
}
s.access.Unlock()
for _, engine := range s.engines {
<-engine.exitSignal
}
if s.dispatcher != nil {
s.dispatcher.Close()
}
for _, udpNat := range s.udpNats {
udpNat.Close()
}
var err error
for _, engine := range s.engines {
err = E.Errors(err, engine.platformIO.close())
}
return err
}