forked from shadowsocks/go-shadowsocks2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
udp.go
198 lines (164 loc) · 4.11 KB
/
udp.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
package main
import (
"fmt"
"net"
"time"
"sync"
"github.com/shadowsocks/go-shadowsocks2/core"
"github.com/shadowsocks/go-shadowsocks2/socks"
)
const udpBufSize = 64 * 1024
// Listen on laddr for UDP packets, encrypt and send to server to reach target.
func udpLocal(laddr, server, target string, ciph core.PacketConnCipher) {
srvAddr, err := net.ResolveUDPAddr("udp", server)
if err != nil {
logf("UDP server address error: %v", err)
return
}
tgt := socks.ParseAddr(target)
if tgt == nil {
err = fmt.Errorf("invalid target address: %q", target)
logf("UDP target address error: %v", err)
return
}
c, err := net.ListenPacket("udp", laddr)
if err != nil {
logf("UDP local listen error: %v", err)
return
}
defer c.Close()
nm := newNATmap(config.UDPTimeout)
buf := make([]byte, udpBufSize)
copy(buf, tgt)
logf("UDP tunnel %s <-> %s <-> %s", laddr, server, target)
for {
n, raddr, err := c.ReadFrom(buf[len(tgt):])
if err != nil {
logf("UDP local read error: %v", err)
continue
}
pc := nm.Get(raddr.String())
if pc == nil {
pc, err = net.ListenPacket("udp", "")
if err != nil {
logf("UDP local listen error: %v", err)
continue
}
pc = ciph.PacketConn(pc)
nm.Add(raddr, c, pc, false)
}
_, err = pc.WriteTo(buf[:len(tgt)+n], srvAddr)
if err != nil {
logf("UDP local write error: %v", err)
continue
}
}
}
// Listen on addr for encrypted packets and basically do UDP NAT.
func udpRemote(addr string, ciph core.PacketConnCipher) {
c, err := core.ListenPacket("udp", addr, ciph)
if err != nil {
logf("UDP remote listen error: %v", err)
return
}
defer c.Close()
nm := newNATmap(config.UDPTimeout)
buf := make([]byte, udpBufSize)
logf("listening UDP on %s", addr)
for {
n, raddr, err := c.ReadFrom(buf)
if err != nil {
logf("UDP remote read error: %v", err)
continue
}
tgtAddr := socks.SplitAddr(buf[:n])
if tgtAddr == nil {
logf("failed to split target address from packet: %q", buf[:n])
continue
}
tgtUDPAddr, err := net.ResolveUDPAddr("udp", tgtAddr.String())
if err != nil {
logf("failed to resolve target UDP address: %v", err)
continue
}
payload := buf[len(tgtAddr):n]
pc := nm.Get(raddr.String())
if pc == nil {
pc, err = net.ListenPacket("udp", "")
if err != nil {
logf("UDP remote listen error: %v", err)
continue
}
nm.Add(raddr, c, pc, true)
}
_, err = pc.WriteTo(payload, tgtUDPAddr) // accept only UDPAddr despite the signature
if err != nil {
logf("UDP remote write error: %v", err)
continue
}
}
}
// Packet NAT table
type natmap struct {
sync.RWMutex
m map[string]net.PacketConn
timeout time.Duration
}
func newNATmap(timeout time.Duration) *natmap {
m := &natmap{}
m.m = make(map[string]net.PacketConn)
m.timeout = timeout
return m
}
func (m *natmap) Get(key string) net.PacketConn {
m.RLock()
defer m.RUnlock()
return m.m[key]
}
func (m *natmap) Set(key string, pc net.PacketConn) {
m.Lock()
defer m.Unlock()
m.m[key] = pc
}
func (m *natmap) Del(key string) net.PacketConn {
m.Lock()
defer m.Unlock()
pc, ok := m.m[key]
if ok {
delete(m.m, key)
return pc
}
return nil
}
func (m *natmap) Add(peer net.Addr, dst, src net.PacketConn, srcIncluded bool) {
m.Set(peer.String(), src)
go func() {
timedCopy(dst, peer, src, m.timeout, srcIncluded)
if pc := m.Del(peer.String()); pc != nil {
pc.Close()
}
}()
}
// copy from src to dst at target with read timeout
func timedCopy(dst net.PacketConn, target net.Addr, src net.PacketConn, timeout time.Duration, srcIncluded bool) error {
buf := make([]byte, udpBufSize)
for {
src.SetReadDeadline(time.Now().Add(timeout))
n, raddr, err := src.ReadFrom(buf)
if err != nil {
return err
}
if srcIncluded { // server -> client: add original packet source
srcAddr := socks.ParseAddr(raddr.String())
copy(buf[len(srcAddr):], buf[:n])
copy(buf, srcAddr)
_, err = dst.WriteTo(buf[:len(srcAddr)+n], target)
} else { // client -> user: strip original packet source
srcAddr := socks.SplitAddr(buf[:n])
_, err = dst.WriteTo(buf[len(srcAddr):n], target)
}
if err != nil {
return err
}
}
}