forked from SagerNet/sing-tun
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_system_nat.go
More file actions
154 lines (143 loc) · 3.08 KB
/
Copy pathstack_system_nat.go
File metadata and controls
154 lines (143 loc) · 3.08 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
package tun
import (
"context"
"net/netip"
"sync"
"time"
)
type TCPNat struct {
timeout time.Duration
portIndex uint16
portAccess sync.RWMutex
addrAccess sync.RWMutex
addrMap map[tcpNatKey]uint16
portMap map[uint16]*TCPSession
}
type tcpNatKey struct {
Source netip.AddrPort
Destination netip.AddrPort
}
type TCPSession struct {
sync.Mutex
Source netip.AddrPort
Destination netip.AddrPort
LastActive time.Time
}
func NewNat(ctx context.Context, timeout time.Duration) *TCPNat {
natMap := &TCPNat{
timeout: timeout,
portIndex: 10000,
addrMap: make(map[tcpNatKey]uint16),
portMap: make(map[uint16]*TCPSession),
}
go natMap.loopCheckTimeout(ctx)
return natMap
}
func (n *TCPNat) loopCheckTimeout(ctx context.Context) {
ticker := time.NewTicker(n.timeout)
defer ticker.Stop()
for {
select {
case <-ticker.C:
n.checkTimeout()
case <-ctx.Done():
return
}
}
}
func (n *TCPNat) checkTimeout() {
now := time.Now()
type expiredSession struct {
port uint16
session *TCPSession
}
var expired []expiredSession
n.portAccess.RLock()
for natPort, session := range n.portMap {
session.Lock()
timedOut := now.Sub(session.LastActive) > n.timeout
session.Unlock()
if timedOut {
expired = append(expired, expiredSession{port: natPort, session: session})
}
}
n.portAccess.RUnlock()
if len(expired) == 0 {
return
}
n.addrAccess.Lock()
n.portAccess.Lock()
for _, e := range expired {
e.session.Lock()
if now.Sub(e.session.LastActive) > n.timeout {
delete(n.addrMap, tcpNatKey{Source: e.session.Source, Destination: e.session.Destination})
delete(n.portMap, e.port)
}
e.session.Unlock()
}
n.portAccess.Unlock()
n.addrAccess.Unlock()
}
func (n *TCPNat) Purge() {
n.addrAccess.Lock()
n.portAccess.Lock()
clear(n.addrMap)
clear(n.portMap)
n.portAccess.Unlock()
n.addrAccess.Unlock()
}
func (n *TCPNat) LookupBack(port uint16) *TCPSession {
n.portAccess.RLock()
session := n.portMap[port]
n.portAccess.RUnlock()
if session != nil {
session.Lock()
if time.Since(session.LastActive) > time.Second {
session.LastActive = time.Now()
}
session.Unlock()
}
return session
}
func (n *TCPNat) Lookup(source netip.AddrPort, destination netip.AddrPort) uint16 {
key := tcpNatKey{Source: source, Destination: destination}
n.addrAccess.RLock()
port, loaded := n.addrMap[key]
n.addrAccess.RUnlock()
if loaded {
return port
}
n.addrAccess.Lock()
defer n.addrAccess.Unlock()
if port, loaded = n.addrMap[key]; loaded {
return port
}
n.portAccess.Lock()
defer n.portAccess.Unlock()
nextPort, ok := n.allocatePortLocked()
if !ok {
return 0
}
n.portMap[nextPort] = &TCPSession{
Source: source,
Destination: destination,
LastActive: time.Now(),
}
n.addrMap[key] = nextPort
return nextPort
}
func (n *TCPNat) allocatePortLocked() (uint16, bool) {
for range 65535 - 10000 + 1 {
nextPort := n.portIndex
if nextPort == 0 {
nextPort = 10000
n.portIndex = 10001
} else {
n.portIndex++
}
if _, occupied := n.portMap[nextPort]; !occupied {
return nextPort, true
}
}
return 0, false
}