-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
udp.go
215 lines (191 loc) · 4.99 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package main
import (
"fmt"
"net"
"sync"
"time"
"github.com/riobard/go-shadowsocks2/socks"
)
const udpBufSize = 64 * 1024
var bufPool = sync.Pool{New: func() interface{} { return make([]byte, udpBufSize) }}
// Listen on laddr for UDP packets, encrypt and send to server to reach target.
func udpLocal(laddr, server, target string, shadow func(net.PacketConn) net.PacketConn) {
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()
m := make(map[string]chan []byte)
var lock sync.Mutex
logf("UDP tunnel %s <-> %s <-> %s", laddr, server, target)
for {
buf := bufPool.Get().([]byte)
copy(buf, tgt)
n, raddr, err := c.ReadFrom(buf[len(tgt):])
if err != nil {
logf("UDP local read error: %v", err)
continue
}
lock.Lock()
k := raddr.String()
ch := m[k]
if ch == nil {
pc, err := net.ListenPacket("udp", "")
if err != nil {
logf("failed to create UDP socket: %v", err)
goto Unlock
}
pc = shadow(pc)
ch = make(chan []byte, 1) // must use buffered chan
m[k] = ch
go func() { // recv from user and send to udpRemote
for buf := range ch {
pc.SetReadDeadline(time.Now().Add(config.UDPTimeout)) // extend read timeout
if _, err := pc.WriteTo(buf, srvAddr); err != nil {
logf("UDP local write error: %v", err)
}
bufPool.Put(buf[:cap(buf)])
}
}()
go func() { // recv from udpRemote and send to user
if err := timedCopy(raddr, c, pc, config.UDPTimeout, false); err != nil {
if err, ok := err.(net.Error); ok && err.Timeout() {
// ignore i/o timeout
} else {
logf("timedCopy error: %v", err)
}
}
pc.Close()
lock.Lock()
if ch := m[k]; ch != nil {
close(ch)
}
delete(m, k)
lock.Unlock()
}()
}
Unlock:
lock.Unlock()
select {
case ch <- buf[:len(tgt)+n]: // send
default: // drop
bufPool.Put(buf)
}
}
}
// Listen on addr for encrypted packets and basically do UDP NAT.
func udpRemote(addr string, shadow func(net.PacketConn) net.PacketConn) {
c, err := net.ListenPacket("udp", addr)
if err != nil {
logf("UDP remote listen error: %v", err)
return
}
defer c.Close()
c = shadow(c)
m := make(map[string]chan []byte)
var lock sync.Mutex
logf("listening UDP on %s", addr)
for {
buf := bufPool.Get().([]byte)
n, raddr, err := c.ReadFrom(buf)
if err != nil {
logf("UDP remote read error: %v", err)
continue
}
lock.Lock()
k := raddr.String()
ch := m[k]
if ch == nil {
pc, err := net.ListenPacket("udp", "")
if err != nil {
logf("failed to create UDP socket: %v", err)
goto Unlock
}
ch = make(chan []byte, 1) // must use buffered chan
m[k] = ch
go func() { // receive from udpLocal and send to target
var tgtUDPAddr *net.UDPAddr
var err error
for buf := range ch {
tgtAddr := socks.SplitAddr(buf)
if tgtAddr == nil {
logf("failed to split target address from packet: %q", buf)
goto End
}
tgtUDPAddr, err = net.ResolveUDPAddr("udp", tgtAddr.String())
if err != nil {
logf("failed to resolve target UDP address: %v", err)
goto End
}
pc.SetReadDeadline(time.Now().Add(config.UDPTimeout))
if _, err = pc.WriteTo(buf[len(tgtAddr):], tgtUDPAddr); err != nil {
logf("UDP remote write error: %v", err)
goto End
}
End:
bufPool.Put(buf[:cap(buf)])
}
}()
go func() { // receive from udpLocal and send to client
if err := timedCopy(raddr, c, pc, config.UDPTimeout, true); err != nil {
if err, ok := err.(net.Error); ok && err.Timeout() {
// ignore i/o timeout
} else {
logf("timedCopy error: %v", err)
}
}
pc.Close()
lock.Lock()
if ch := m[k]; ch != nil {
close(ch)
}
delete(m, k)
lock.Unlock()
}()
}
Unlock:
lock.Unlock()
select {
case ch <- buf[:n]: // sent
default: // drop
bufPool.Put(buf)
}
}
}
// copy from src to dst at target with read timeout
func timedCopy(target net.Addr, dst, src net.PacketConn, timeout time.Duration, prependSrcAddr bool) error {
buf := bufPool.Get().([]byte)
defer bufPool.Put(buf)
for {
src.SetReadDeadline(time.Now().Add(timeout))
n, raddr, err := src.ReadFrom(buf)
if err != nil {
return err
}
if prependSrcAddr { // server -> client: prepend original packet source address
srcAddr := socks.ParseAddr(raddr.String())
copy(buf[len(srcAddr):], buf[:n])
copy(buf, srcAddr)
if _, err = dst.WriteTo(buf[:len(srcAddr)+n], target); err != nil {
return err
}
} else { // client -> user: strip original packet source address
srcAddr := socks.SplitAddr(buf[:n])
if _, err = dst.WriteTo(buf[len(srcAddr):n], target); err != nil {
return err
}
}
}
}