|
1 | 1 | package wg |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | "encoding/base64" |
5 | 6 | "errors" |
6 | 7 | "net" |
| 8 | + "strconv" |
7 | 9 | "testing" |
8 | 10 | "time" |
9 | 11 |
|
@@ -208,3 +210,138 @@ func TestTokenPeerName(t *testing.T) { |
208 | 210 | assert.Equal(t, "token-01020304", TokenPeerName(base64.StdEncoding.EncodeToString(raw))) |
209 | 211 | assert.Equal(t, "token-peer", TokenPeerName("not base64!")) |
210 | 212 | } |
| 213 | + |
| 214 | +// sendingTo returns where the tunnel's WireGuard device currently sends. |
| 215 | +func (t *Tunnel) sendingTo() string { |
| 216 | + t.mu.RLock() |
| 217 | + defer t.mu.RUnlock() |
| 218 | + |
| 219 | + return t.endpointAddr |
| 220 | +} |
| 221 | + |
| 222 | +func directUDP(ip string) string { |
| 223 | + return net.JoinHostPort(ip, strconv.Itoa(tokenGatewayUDPPort)) |
| 224 | +} |
| 225 | + |
| 226 | +func TestConnectTokenEndpointChoice(t *testing.T) { |
| 227 | + for _, tc := range []struct { |
| 228 | + name string |
| 229 | + udp bool |
| 230 | + direct string |
| 231 | + want string // "" means the websocket proxy |
| 232 | + }{ |
| 233 | + {name: "udp with direct ip", udp: true, direct: "192.0.2.10", want: directUDP("192.0.2.10")}, |
| 234 | + {name: "udp without direct ip", udp: true}, |
| 235 | + {name: "websockets only", udp: false, direct: "192.0.2.10"}, |
| 236 | + } { |
| 237 | + t.Run(tc.name, func(t *testing.T) { |
| 238 | + gw, _ := testKeypair(t) |
| 239 | + gateway := newFakeTokenGateway(t, []string{gw}, []tokenResult{ |
| 240 | + {Type: "ok", PeerIP: "fdaa:0:18:a7b:1:1111:2222:3302", ExpiresAt: time.Now().Add(time.Hour).Unix(), Direct: tc.direct}, |
| 241 | + }) |
| 242 | + |
| 243 | + pub, priv := testKeypair(t) |
| 244 | + state := &WireGuardState{Org: "test-org", LocalPublic: pub, LocalPrivate: priv} |
| 245 | + |
| 246 | + tunnel, err := ConnectToken(context.Background(), state, gateway.endpoint(), &TokenAuth{ |
| 247 | + Token: func() string { return "FlyV1 fm2_test" }, |
| 248 | + OrgSlug: "test-org", |
| 249 | + }, tc.udp) |
| 250 | + require.NoError(t, err) |
| 251 | + defer tunnel.Close() |
| 252 | + |
| 253 | + <-gateway.ready |
| 254 | + |
| 255 | + want := tc.want |
| 256 | + if want == "" { |
| 257 | + want = tunnel.wsProxyAddr |
| 258 | + } |
| 259 | + assert.Equal(t, want, tunnel.sendingTo()) |
| 260 | + }) |
| 261 | + } |
| 262 | +} |
| 263 | + |
| 264 | +func TestConnectTokenUDPSendsWireGuardToDirectIP(t *testing.T) { |
| 265 | + gw, _ := testKeypair(t) |
| 266 | + gateway := newFakeTokenGateway(t, []string{gw}, []tokenResult{ |
| 267 | + {Type: "ok", PeerIP: "fdaa:0:18:a7b:1:1111:2222:3302", DNS: "fdaa:0:18::3", ExpiresAt: time.Now().Add(time.Hour).Unix(), Direct: "127.0.0.1"}, |
| 268 | + }) |
| 269 | + |
| 270 | + pub, priv := testKeypair(t) |
| 271 | + state := &WireGuardState{Org: "test-org", LocalPublic: pub, LocalPrivate: priv} |
| 272 | + |
| 273 | + tunnel, err := ConnectToken(context.Background(), state, gateway.endpoint(), &TokenAuth{ |
| 274 | + Token: func() string { return "FlyV1 fm2_test" }, |
| 275 | + OrgSlug: "test-org", |
| 276 | + }, true) |
| 277 | + require.NoError(t, err) |
| 278 | + defer tunnel.Close() |
| 279 | + |
| 280 | + <-gateway.ready |
| 281 | + |
| 282 | + // traffic into the tunnel makes the device start a handshake, which |
| 283 | + // should arrive at the gateway's direct IP over UDP |
| 284 | + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 285 | + defer cancel() |
| 286 | + go func() { |
| 287 | + if c, err := tunnel.DialContext(ctx, "tcp", "[fdaa:0:18::3]:53"); err == nil { |
| 288 | + c.Close() |
| 289 | + } |
| 290 | + }() |
| 291 | + |
| 292 | + buf := make([]byte, 2048) |
| 293 | + require.NoError(t, udpGateway.SetReadDeadline(time.Now().Add(5*time.Second))) |
| 294 | + n, _, err := udpGateway.ReadFromUDP(buf) |
| 295 | + require.NoError(t, err, "no WireGuard packet at the direct IP") |
| 296 | + |
| 297 | + // a handshake initiation: message type 1, 148 bytes |
| 298 | + assert.Equal(t, 148, n) |
| 299 | + assert.Equal(t, byte(1), buf[0]) |
| 300 | +} |
| 301 | + |
| 302 | +func TestConnectTokenUDPFollowsReprovision(t *testing.T) { |
| 303 | + gw1, _ := testKeypair(t) |
| 304 | + gw2, _ := testKeypair(t) |
| 305 | + |
| 306 | + const ( |
| 307 | + peerA = "fdaa:0:18:a7b:1:1111:2222:3302" |
| 308 | + peerB = "fdaa:0:18:c9d:2:3333:4444:5502" |
| 309 | + ) |
| 310 | + |
| 311 | + // the first gateway's direct IP isn't routed, so the reconnect falls |
| 312 | + // back to anycast, which lands on a second gateway with its own |
| 313 | + gateway := newFakeTokenGateway(t, []string{gw1, gw2}, []tokenResult{ |
| 314 | + {Type: "ok", PeerIP: peerA, DNS: "fdaa:0:18::3", ExpiresAt: time.Now().Add(time.Hour).Unix(), Direct: "192.0.2.10"}, |
| 315 | + {Type: "ok", PeerIP: peerB, DNS: "fdaa:0:18::3", ExpiresAt: time.Now().Add(time.Hour).Unix(), Direct: "192.0.2.20"}, |
| 316 | + }) |
| 317 | + |
| 318 | + pub, priv := testKeypair(t) |
| 319 | + state := &WireGuardState{Org: "test-org", LocalPublic: pub, LocalPrivate: priv} |
| 320 | + |
| 321 | + tunnel, err := ConnectToken(context.Background(), state, gateway.endpoint(), &TokenAuth{ |
| 322 | + Token: func() string { return "FlyV1 fm2_test" }, |
| 323 | + OrgSlug: "test-org", |
| 324 | + }, true) |
| 325 | + require.NoError(t, err) |
| 326 | + defer tunnel.Close() |
| 327 | + |
| 328 | + <-gateway.ready |
| 329 | + assert.Equal(t, directUDP("192.0.2.10"), tunnel.sendingTo()) |
| 330 | + |
| 331 | + gateway.drop(0) |
| 332 | + <-gateway.ready |
| 333 | + |
| 334 | + waitFor(t, "tunnel rebuild", func() bool { |
| 335 | + st, _ := tunnel.StateAndConfig() |
| 336 | + |
| 337 | + return st.Peer.Peerip == peerB |
| 338 | + }) |
| 339 | + |
| 340 | + assert.Equal(t, directUDP("192.0.2.20"), tunnel.sendingTo()) |
| 341 | + |
| 342 | + select { |
| 343 | + case <-tunnel.Done(): |
| 344 | + t.Fatalf("tunnel died: %v", tunnel.Err()) |
| 345 | + default: |
| 346 | + } |
| 347 | +} |
0 commit comments