forked from Code-Hex/gvisor-vmnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
udp.go
127 lines (109 loc) · 2.82 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
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),
)
proxyConn, err := nt.dialUDP(ctx, id.LocalAddress, id.LocalPort)
if err != nil {
nt.logger.Warn(
"failed to bind local port",
"err", err,
"between", relay)
return
}
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
}
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, proxyConn, cancel, extend) // loc <- remote
}()
go func() {
defer cancel()
nt.pool.udpRelay(ctx, nt.logger, proxyConn, client, cancel, extend) // remote <- loc
}()
})
nt.stack.SetTransportProtocolHandler(udp.ProtocolNumber, udpForwarder.HandlePacket)
}
func (nt *Network) dialUDP(ctx context.Context, addr tcpip.Address, port uint16) (net.Conn, error) {
if nt.subnet.Contains(addr) {
return gonet.DialUDP(nt.stack, nil, &tcpip.FullAddress{
Addr: addr,
Port: port,
}, ipv4.ProtocolNumber)
}
remoteAddr := fmt.Sprintf("%s:%d", addr, port)
conn, err := nt.dialOut(ctx, "udp", remoteAddr)
if err != nil {
return nil, err
}
return conn, 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)}
}