-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathudp.go
145 lines (127 loc) · 3.25 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
package vmnet
import (
"context"
"fmt"
"log/slog"
"net"
"time"
"gvisor.dev/gvisor/pkg/tcpip"
"gvisor.dev/gvisor/pkg/tcpip/adapters/gonet"
"gvisor.dev/gvisor/pkg/tcpip/network/ipv4"
"gvisor.dev/gvisor/pkg/tcpip/stack"
"gvisor.dev/gvisor/pkg/tcpip/transport/udp"
"gvisor.dev/gvisor/pkg/waiter"
)
func (nt *Network) setUDPForwarder(ctx context.Context) {
udpForwarder := udp.NewForwarder(nt.stack, func(fr *udp.ForwarderRequest) {
id := fr.ID()
relay := fmt.Sprintf(
"%s:%d <-> %s:%d",
id.LocalAddress.String(), id.LocalPort,
id.RemoteAddress.String(), id.RemotePort,
)
nt.logger.Info(
"start UDP relay",
slog.String("between", relay),
)
var wq waiter.Queue
ep, tcpipErr := fr.CreateEndpoint(&wq)
if tcpipErr != nil {
nt.logger.Info(
"failed to create TCP end",
slog.Any("tcpiperr", tcpipErr.String()),
slog.String("between", relay),
)
return
}
clientAddr := &net.UDPAddr{
IP: net.IP([]byte(id.RemoteAddress.AsSlice())),
Port: int(id.RemotePort),
}
remoteAddr := &net.UDPAddr{
IP: net.IP([]byte(id.LocalAddress.AsSlice())),
Port: int(id.LocalPort),
}
proxyConn, err := nt.listenUDP(id.LocalAddress, id.LocalPort)
if err != nil {
if err != nil {
nt.logger.Warn(
"failed to bind local port",
err,
slog.String("between", relay),
)
return
}
}
client := gonet.NewUDPConn(&wq, ep)
ctx, cancel := context.WithCancel(ctx)
idleTimeout := time.Minute
timer := time.AfterFunc(idleTimeout, func() {
cancel()
})
go func() {
<-ctx.Done()
client.Close()
proxyConn.Close()
}()
extend := func() { timer.Reset(idleTimeout) }
go func() {
defer cancel()
nt.pool.udpRelay(ctx, nt.logger, client, clientAddr, proxyConn, cancel, extend) // loc <- remote
}()
go func() {
defer cancel()
nt.pool.udpRelay(ctx, nt.logger, proxyConn, remoteAddr, client, cancel, extend) // remote <- loc
}()
})
nt.stack.SetTransportProtocolHandler(udp.ProtocolNumber, udpForwarder.HandlePacket)
}
func (nt *Network) listenUDP(addr tcpip.Address, port uint16) (net.PacketConn, error) {
proxyAddr := &net.UDPAddr{
IP: net.IPv4zero,
Port: int(port),
}
if nt.subnet.Contains(addr) {
return gonet.DialUDP(nt.stack, &tcpip.FullAddress{
Addr: tcpip.AddrFromSlice(net.IPv4zero),
Port: 0, // random port
}, nil, ipv4.ProtocolNumber)
}
proxyConn, err := net.ListenUDP("udp", proxyAddr)
if err != nil {
proxyAddr.Port = 0
proxyConn, err = net.ListenUDP("udp", proxyAddr)
if err != nil {
return nil, err
}
}
return proxyConn, err
}
func dialUDPConn(
s *stack.Stack,
laddr *tcpip.FullAddress,
network tcpip.NetworkProtocolNumber,
opts ...func(*tcpip.SocketOptions),
) (*gonet.UDPConn, error) {
var wq waiter.Queue
ep, err := s.NewEndpoint(udp.ProtocolNumber, network, &wq)
if err != nil {
return nil, fmt.Errorf(err.String())
}
for _, opt := range opts {
opt(ep.SocketOptions())
}
if err := ep.Bind(*laddr); err != nil {
ep.Close()
return nil, &net.OpError{
Op: "bind",
Net: "udp",
Addr: fullToUDPAddr(*laddr),
Err: fmt.Errorf(err.String()),
}
}
return gonet.NewUDPConn(&wq, ep), nil
}
func fullToUDPAddr(addr tcpip.FullAddress) *net.UDPAddr {
return &net.UDPAddr{IP: net.IP(addr.Addr.AsSlice()), Port: int(addr.Port)}
}