-
Notifications
You must be signed in to change notification settings - Fork 127
/
udp_relay.go
212 lines (183 loc) · 5.04 KB
/
udp_relay.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
//
// date : 2016-05-16
// author: xjdrew
//
package kone
import (
"io"
"net"
"sync"
"time"
"github.com/xjdrew/kone/tcpip"
)
type UDPTunnel struct {
session *NatSession
record *DomainRecord
cliaddr *net.UDPAddr
localConn *net.UDPConn
remoteConn *net.UDPConn
}
func (tunnel *UDPTunnel) SetDeadline(duration time.Duration) error {
return tunnel.remoteConn.SetDeadline(time.Now().Add(duration))
}
func (tunnel *UDPTunnel) Pump() error {
b := make([]byte, MTU)
for {
n, err := tunnel.remoteConn.Read(b)
if err != nil {
if opErr, ok := err.(*net.OpError); ok && opErr.Timeout() {
return nil
}
return err
}
_, err = tunnel.localConn.WriteToUDP(b[:n], tunnel.cliaddr)
if err != nil {
return err
}
}
}
func (tunnel *UDPTunnel) Write(b []byte) (int, error) {
return tunnel.remoteConn.Write(b)
}
type UDPRelay struct {
one *One
nat *Nat
relayIP net.IP
relayPort uint16
lock sync.Mutex
tunnels map[string]*UDPTunnel
}
// bypass udp packet
func (r *UDPRelay) grabTunnel(localConn *net.UDPConn, cliaddr *net.UDPAddr) *UDPTunnel {
r.lock.Lock()
defer r.lock.Unlock()
addr := cliaddr.String()
tunnel := r.tunnels[addr]
if tunnel == nil {
port := uint16(cliaddr.Port)
session := r.nat.getSession(port)
if session == nil {
return nil
}
record := r.one.dnsTable.GetByIP(session.dstIP)
if record == nil { // by IP-CIDR rule
return nil
}
if record.RealIP == nil {
// try resolve real ip
msg, err := r.one.dns.Resolve(record.Hostname)
if err == nil {
record.SetRealIP(msg)
}
if record.RealIP == nil {
// resolve real ip failed
return nil
}
}
srvaddr := &net.UDPAddr{IP: record.RealIP, Port: int(session.dstPort)}
remoteConn, err := net.DialUDP("udp", nil, srvaddr)
if err != nil {
logger.Errorf("[udp relay] connect to %s failed: %v", srvaddr, err)
return nil
}
tunnel = &UDPTunnel{
session: session,
record: record,
cliaddr: cliaddr,
localConn: localConn,
remoteConn: remoteConn,
}
logger.Debugf("[udp relay] %s:%d > %v: new tunnel", session.srcIP, session.srcPort, srvaddr)
r.tunnels[addr] = tunnel
go func() {
err := tunnel.Pump()
if err != nil {
logger.Debugf("[udp relay] pump to %v failed: %v", tunnel.remoteConn.RemoteAddr(), err)
}
tunnel.remoteConn.Close()
logger.Debugf("[udp relay] %s:%d > %v: destroy tunnel", tunnel.session.srcIP, tunnel.session.srcPort, srvaddr)
r.lock.Lock()
delete(r.tunnels, addr)
r.lock.Unlock()
}()
}
tunnel.SetDeadline(NatSessionLifeSeconds * time.Second)
return tunnel
}
func (r *UDPRelay) handlePacket(localConn *net.UDPConn, cliaddr *net.UDPAddr, packet []byte) {
tunnel := r.grabTunnel(localConn, cliaddr)
if tunnel == nil {
logger.Errorf("[udp relay %v > %v: grap tunnel failed", cliaddr, localConn.LocalAddr())
return
}
_, err := tunnel.Write(packet)
if err != nil {
logger.Debugf("[udp relay] tunnel write failed: %v", err)
}
}
func (r *UDPRelay) Serve() error {
addr := &net.UDPAddr{IP: r.relayIP, Port: int(r.relayPort)}
conn, err := net.ListenUDP("udp", addr)
if err != nil {
logger.Errorf("[udp relay] listen failed: %v", err)
return err
}
for {
b := make([]byte, MTU)
n, cliaddr, err := conn.ReadFromUDP(b)
if err != nil {
logger.Errorf("[udp relay] acceept failed temporary: %v", err)
time.Sleep(time.Second) //prevent log storms
continue
}
go r.handlePacket(conn, cliaddr, b[:n])
}
}
// redirect udp packet to relay
func (r *UDPRelay) Filter(wr io.Writer, ipPacket tcpip.IPv4Packet) {
udpPacket := tcpip.UDPPacket(ipPacket.Payload())
srcIP := ipPacket.SourceIP()
dstIP := ipPacket.DestinationIP()
srcPort := udpPacket.SourcePort()
dstPort := udpPacket.DestinationPort()
one := r.one
if net.IP.Equal(srcIP, r.relayIP) && srcPort == r.relayPort {
// from remote
session := r.nat.getSession(dstPort)
if session == nil {
logger.Debugf("[udp filter] %s:%d > %s:%d: no session", srcIP, srcPort, dstIP, dstPort)
return
}
ipPacket.SetSourceIP(session.dstIP)
ipPacket.SetDestinationIP(session.srcIP)
udpPacket.SetSourcePort(session.dstPort)
udpPacket.SetDestinationPort(session.srcPort)
} else if one.dnsTable.Contains(dstIP) {
// redirect to relay
isNew, port := r.nat.allocSession(srcIP, dstIP, srcPort, dstPort)
ipPacket.SetSourceIP(dstIP)
udpPacket.SetSourcePort(port)
ipPacket.SetDestinationIP(r.relayIP)
udpPacket.SetDestinationPort(r.relayPort)
if isNew {
logger.Debugf("[udp filter] reshape packet from [%s:%d > %s:%d] to [%s:%d > %s:%d]",
srcIP, srcPort, dstIP, dstPort, dstIP, port, r.relayIP, r.relayPort)
}
} else {
logger.Errorf("[udp filter] %s:%d > %s:%d: invalid packet", srcIP, srcPort, dstIP, dstPort)
return
}
// write back packet
udpPacket.ResetChecksum(ipPacket.PseudoSum())
ipPacket.ResetChecksum()
wr.Write(ipPacket)
}
func NewUDPRelay(one *One, cfg CoreConfig) *UDPRelay {
r := new(UDPRelay)
r.one = one
r.nat = NewNat(cfg.UdpNatPortStart, cfg.UdpNatPortEnd)
r.relayIP = one.ip
r.relayPort = cfg.UdpListenPort
r.tunnels = make(map[string]*UDPTunnel)
return r
}