Repository navigation
Expand file tree
/
Copy pathdirect_handler_integration_test.go
More file actions
299 lines (271 loc) · 8.59 KB
/
Copy pathdirect_handler_integration_test.go
File metadata and controls
299 lines (271 loc) · 8.59 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package cloudflared
import (
"bytes"
"context"
"net"
"net/netip"
"strings"
"sync"
"testing"
"time"
internalicmp "github.com/sagernet/sing-cloudflared/internal/icmp"
"github.com/sagernet/sing-cloudflared/internal/icmptest"
pkgicmp "github.com/sagernet/sing-cloudflared/internal/pkg/icmp"
internalprotocol "github.com/sagernet/sing-cloudflared/internal/protocol"
tun "github.com/sagernet/sing-tun"
"github.com/sagernet/sing-tun/gtcpip/header"
"github.com/sagernet/sing/common/buf"
"github.com/sagernet/sing/common/logger"
)
type captureRouteContext struct {
packetCh chan []byte
}
func (c *captureRouteContext) WritePacket(packet []byte) error {
copied := append([]byte(nil), packet...)
select {
case c.packetCh <- copied:
default:
}
return nil
}
type syncDatagramSender struct {
version string
packetCh chan []byte
access sync.Mutex
sent [][]byte
}
func newSyncDatagramSender(version string) *syncDatagramSender {
return &syncDatagramSender{
version: version,
packetCh: make(chan []byte, 1),
}
}
func (s *syncDatagramSender) SendDatagram(data []byte) error {
copied := append([]byte(nil), data...)
s.access.Lock()
s.sent = append(s.sent, copied)
s.access.Unlock()
select {
case s.packetCh <- copied:
default:
}
return nil
}
func (s *syncDatagramSender) DatagramVersion() string {
return s.version
}
func (s *syncDatagramSender) waitForPacket(t *testing.T, timeout time.Duration) []byte {
t.Helper()
select {
case packet := <-s.packetCh:
return packet
case <-time.After(timeout):
t.Fatal("timed out waiting for ICMP reply datagram")
return nil
}
}
func requireIPv6Loopback(t *testing.T) {
t.Helper()
listener, err := net.ListenPacket("udp6", "[::1]:0")
if err != nil {
t.Skipf("IPv6 loopback unavailable: %v", err)
}
_ = listener.Close()
}
func shouldSkipICMPError(err error) bool {
if err == nil {
return false
}
lower := strings.ToLower(err.Error())
return strings.Contains(lower, "permission denied") ||
strings.Contains(lower, "operation not permitted") ||
strings.Contains(lower, "network is unreachable") ||
strings.Contains(lower, "cannot assign requested address")
}
func buildLoopbackICMPRequest(source, destination netip.Addr, identifier, sequence uint16) []byte {
if destination.Is6() {
return icmptest.BuildIPv6ICMPPacket(source, destination, header.ICMPv6EchoRequest, 0, identifier, sequence)
}
return icmptest.BuildIPv4ICMPPacket(source, destination, header.ICMPv4Echo, 0, identifier, sequence)
}
func requireDirectHandlerLoopbackCapability(t *testing.T, source, destination netip.Addr) {
t.Helper()
if destination.Is6() {
requireIPv6Loopback(t)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
handler := pkgicmp.NewDirectHandler(logger.NOP())
routeContext := &captureRouteContext{packetCh: make(chan []byte, 1)}
destinationConn, err := handler.RouteICMPConnection(ctx, tun.DirectRouteSession{
Source: source,
Destination: destination,
}, routeContext, 2*time.Second)
if shouldSkipICMPError(err) {
t.Skipf("ICMP loopback unavailable for %s: %v", destination, err)
}
if err != nil {
t.Fatalf("RouteICMPConnection(%s): %v", destination, err)
}
defer destinationConn.Close()
err = destinationConn.WritePacket(buf.As(buildLoopbackICMPRequest(source, destination, 0x40, 1)).ToOwned())
if shouldSkipICMPError(err) {
t.Skipf("ICMP loopback write unavailable for %s: %v", destination, err)
}
if err != nil {
t.Fatalf("WritePacket(%s): %v", destination, err)
}
select {
case packet := <-routeContext.packetCh:
info, err := ParseICMPPacket(packet)
if err != nil {
t.Fatalf("parse capability probe reply: %v", err)
}
if !info.IsEchoReply() {
t.Fatalf("expected echo reply from capability probe, got type=%d code=%d", info.ICMPType, info.ICMPCode)
}
case <-time.After(3 * time.Second):
t.Fatalf("timed out waiting for ICMP loopback capability probe reply to %s", destination)
}
}
func newICMPIntegrationService(t *testing.T) *Service {
t.Helper()
serviceInstance := newLimitedService(t, 0)
serviceInstance.logger = logger.NOP()
serviceInstance.icmpHandler = pkgicmp.NewDirectHandler(serviceInstance.logger)
serviceInstance.datagramV2Muxers = make(map[DatagramSender]*DatagramV2Muxer)
serviceInstance.datagramV3Muxers = make(map[DatagramSender]*DatagramV3Muxer)
return serviceInstance
}
func encodeInboundICMPDatagram(t *testing.T, version string, packet []byte, traced bool) []byte {
t.Helper()
switch version {
case datagramVersionV3:
datagram, err := encodeV3ICMPDatagram(packet)
if err != nil {
t.Fatalf("encode V3 ICMP datagram: %v", err)
}
return datagram
default:
if traced {
traceIdentity := bytes.Repeat([]byte{0x7a}, icmpTraceIdentityLength)
payload := append(append([]byte(nil), packet...), traceIdentity...)
return append(payload, byte(DatagramV2TypeIPWithTrace))
}
return append(append([]byte(nil), packet...), byte(DatagramV2TypeIP))
}
}
func roundTripServiceICMP(t *testing.T, version string, source, destination netip.Addr, identifier, sequence uint16, traced bool) []byte {
t.Helper()
requireDirectHandlerLoopbackCapability(t, source, destination)
serviceInstance := newICMPIntegrationService(t)
sender := newSyncDatagramSender(version)
request := buildLoopbackICMPRequest(source, destination, identifier, sequence)
serviceInstance.handleDatagram(serviceInstance.ctx, encodeInboundICMPDatagram(t, version, request, traced), sender)
return sender.waitForPacket(t, 3*time.Second)
}
func parseICMPReplyDatagram(t *testing.T, version string, datagram []byte) (internalprotocol.DatagramV2Type, internalprotocol.DatagramV3Type, internalicmp.PacketInfo) {
t.Helper()
switch version {
case datagramVersionV3:
if len(datagram) == 0 {
t.Fatal("empty V3 reply datagram")
}
replyType := internalprotocol.DatagramV3Type(datagram[0])
info, err := ParseICMPPacket(datagram[1:])
if err != nil {
t.Fatalf("parse V3 ICMP reply: %v", err)
}
return 0, replyType, info
default:
if len(datagram) == 0 {
t.Fatal("empty V2 reply datagram")
}
replyType := internalprotocol.DatagramV2Type(datagram[len(datagram)-1])
info, err := ParseICMPPacket(datagram[:len(datagram)-1])
if err != nil {
t.Fatalf("parse V2 ICMP reply: %v", err)
}
return replyType, 0, info
}
}
func TestDirectHandlerICMPRoundTrip(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
version string
source netip.Addr
destination netip.Addr
identifier uint16
sequence uint16
traced bool
}{
{
name: "v2 ipv4",
version: defaultDatagramVersion,
source: netip.MustParseAddr("127.0.0.2"),
destination: netip.MustParseAddr("127.0.0.1"),
identifier: 0x101,
sequence: 1,
},
{
name: "v2 traced ipv4",
version: defaultDatagramVersion,
source: netip.MustParseAddr("127.0.0.2"),
destination: netip.MustParseAddr("127.0.0.1"),
identifier: 0x102,
sequence: 2,
traced: true,
},
{
name: "v3 ipv4",
version: datagramVersionV3,
source: netip.MustParseAddr("127.0.0.2"),
destination: netip.MustParseAddr("127.0.0.1"),
identifier: 0x103,
sequence: 3,
},
{
name: "v2 ipv6",
version: defaultDatagramVersion,
source: netip.MustParseAddr("::1"),
destination: netip.MustParseAddr("::1"),
identifier: 0x201,
sequence: 4,
},
{
name: "v3 ipv6",
version: datagramVersionV3,
source: netip.MustParseAddr("::1"),
destination: netip.MustParseAddr("::1"),
identifier: 0x202,
sequence: 5,
},
}
for _, testCase := range testCases {
testCase := testCase
t.Run(testCase.name, func(t *testing.T) {
reply := roundTripServiceICMP(t, testCase.version, testCase.source, testCase.destination, testCase.identifier, testCase.sequence, testCase.traced)
v2Type, v3Type, info := parseICMPReplyDatagram(t, testCase.version, reply)
switch testCase.version {
case datagramVersionV3:
if v3Type != internalprotocol.DatagramV3TypeICMP {
t.Fatalf("expected V3 ICMP reply type, got %d", v3Type)
}
default:
if v2Type != internalprotocol.DatagramV2TypeIP {
t.Fatalf("expected plain V2 IP reply type, got %d", v2Type)
}
}
if !info.IsEchoReply() {
t.Fatalf("expected echo reply, got type=%d code=%d", info.ICMPType, info.ICMPCode)
}
if info.Identifier != testCase.identifier || info.Sequence != testCase.sequence {
t.Fatalf("unexpected reply id/seq %d/%d", info.Identifier, info.Sequence)
}
if info.SourceIP != testCase.destination || info.Destination != testCase.source {
t.Fatalf("unexpected reply routing src=%s dst=%s", info.SourceIP, info.Destination)
}
})
}
}