2024-08-07 00:33:45 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
|
|
|
|
package nat
|
|
|
|
|
|
|
|
import (
|
2024-08-08 20:46:55 +00:00
|
|
|
"bytes"
|
|
|
|
"cmp"
|
2024-08-07 00:33:45 +00:00
|
|
|
"context"
|
2024-08-08 04:31:50 +00:00
|
|
|
"errors"
|
2024-08-08 19:47:54 +00:00
|
|
|
"flag"
|
2024-08-07 00:33:45 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/netip"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
2024-08-08 15:34:08 +00:00
|
|
|
"strings"
|
2024-08-07 00:33:45 +00:00
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2024-08-08 15:34:08 +00:00
|
|
|
"golang.org/x/mod/modfile"
|
2024-08-07 00:33:45 +00:00
|
|
|
"golang.org/x/sync/errgroup"
|
2024-08-08 04:31:50 +00:00
|
|
|
"tailscale.com/client/tailscale"
|
2024-08-07 00:33:45 +00:00
|
|
|
"tailscale.com/ipn/ipnstate"
|
2024-08-08 20:46:55 +00:00
|
|
|
"tailscale.com/syncs"
|
2024-08-08 04:31:50 +00:00
|
|
|
"tailscale.com/tailcfg"
|
2024-08-07 00:33:45 +00:00
|
|
|
"tailscale.com/tstest/natlab/vnet"
|
|
|
|
)
|
|
|
|
|
2024-08-09 03:11:50 +00:00
|
|
|
var (
|
|
|
|
logTailscaled = flag.Bool("log-tailscaled", false, "log tailscaled output")
|
|
|
|
pcapFile = flag.String("pcap", "", "write pcap to file")
|
|
|
|
)
|
2024-08-08 19:47:54 +00:00
|
|
|
|
2024-08-07 00:33:45 +00:00
|
|
|
type natTest struct {
|
|
|
|
tb testing.TB
|
|
|
|
base string // base image
|
|
|
|
tempDir string // for qcow2 images
|
|
|
|
vnet *vnet.Server
|
2024-08-08 15:34:08 +00:00
|
|
|
kernel string // linux kernel path
|
2024-08-13 03:36:31 +00:00
|
|
|
|
|
|
|
gotRoute pingRoute
|
2024-08-07 00:33:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newNatTest(tb testing.TB) *natTest {
|
2024-08-08 15:34:08 +00:00
|
|
|
root, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
tb.Fatal(err)
|
|
|
|
}
|
|
|
|
modRoot := filepath.Join(root, "../../..")
|
|
|
|
|
2024-08-07 00:33:45 +00:00
|
|
|
nt := &natTest{
|
|
|
|
tb: tb,
|
|
|
|
tempDir: tb.TempDir(),
|
2024-08-08 15:34:08 +00:00
|
|
|
base: filepath.Join(modRoot, "gokrazy/tsapp.qcow2"),
|
2024-08-07 00:33:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(nt.base); err != nil {
|
|
|
|
tb.Skipf("skipping test; base image %q not found", nt.base)
|
|
|
|
}
|
2024-08-09 14:53:08 +00:00
|
|
|
|
|
|
|
nt.kernel, err = findKernelPath(filepath.Join(modRoot, "gokrazy/tsapp/builddir/github.com/tailscale/gokrazy-kernel/go.mod"))
|
|
|
|
if err != nil {
|
|
|
|
tb.Skipf("skipping test; kernel not found: %v", err)
|
|
|
|
}
|
|
|
|
tb.Logf("found kernel: %v", nt.kernel)
|
|
|
|
|
2024-08-07 00:33:45 +00:00
|
|
|
return nt
|
|
|
|
}
|
|
|
|
|
2024-08-08 15:34:08 +00:00
|
|
|
func findKernelPath(goMod string) (string, error) {
|
|
|
|
b, err := os.ReadFile(goMod)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
mf, err := modfile.Parse("go.mod", b, nil)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
goModB, err := exec.Command("go", "env", "GOMODCACHE").CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
for _, r := range mf.Require {
|
|
|
|
if r.Mod.Path == "github.com/tailscale/gokrazy-kernel" {
|
|
|
|
return strings.TrimSpace(string(goModB)) + "/" + r.Mod.String() + "/vmlinuz", nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", fmt.Errorf("failed to find kernel in %v", goMod)
|
|
|
|
}
|
|
|
|
|
2024-08-08 21:11:07 +00:00
|
|
|
type addNodeFunc func(c *vnet.Config) *vnet.Node // returns nil to omit test
|
2024-08-07 00:33:45 +00:00
|
|
|
|
|
|
|
func easy(c *vnet.Config) *vnet.Node {
|
|
|
|
n := c.NumNodes() + 1
|
|
|
|
return c.AddNode(c.AddNetwork(
|
|
|
|
fmt.Sprintf("2.%d.%d.%d", n, n, n), // public IP
|
|
|
|
fmt.Sprintf("192.168.%d.1/24", n), vnet.EasyNAT))
|
|
|
|
}
|
|
|
|
|
2024-08-10 20:46:47 +00:00
|
|
|
// easy + host firewall
|
|
|
|
func easyFW(c *vnet.Config) *vnet.Node {
|
|
|
|
n := c.NumNodes() + 1
|
|
|
|
return c.AddNode(vnet.HostFirewall, c.AddNetwork(
|
|
|
|
fmt.Sprintf("2.%d.%d.%d", n, n, n), // public IP
|
|
|
|
fmt.Sprintf("192.168.%d.1/24", n), vnet.EasyNAT))
|
|
|
|
}
|
|
|
|
|
2024-08-09 03:58:24 +00:00
|
|
|
func easyAF(c *vnet.Config) *vnet.Node {
|
|
|
|
n := c.NumNodes() + 1
|
|
|
|
return c.AddNode(c.AddNetwork(
|
|
|
|
fmt.Sprintf("2.%d.%d.%d", n, n, n), // public IP
|
|
|
|
fmt.Sprintf("192.168.%d.1/24", n), vnet.EasyAFNAT))
|
|
|
|
}
|
|
|
|
|
2024-08-08 21:11:07 +00:00
|
|
|
func sameLAN(c *vnet.Config) *vnet.Node {
|
|
|
|
nw := c.FirstNetwork()
|
|
|
|
if nw == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if !nw.CanTakeMoreNodes() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return c.AddNode(nw)
|
|
|
|
}
|
|
|
|
|
2024-08-08 19:47:54 +00:00
|
|
|
func one2one(c *vnet.Config) *vnet.Node {
|
|
|
|
n := c.NumNodes() + 1
|
|
|
|
return c.AddNode(c.AddNetwork(
|
|
|
|
fmt.Sprintf("2.%d.%d.%d", n, n, n), // public IP
|
|
|
|
fmt.Sprintf("172.16.%d.1/24", n), vnet.One2OneNAT))
|
|
|
|
}
|
|
|
|
|
2024-08-08 19:10:00 +00:00
|
|
|
func easyPMP(c *vnet.Config) *vnet.Node {
|
|
|
|
n := c.NumNodes() + 1
|
|
|
|
return c.AddNode(c.AddNetwork(
|
|
|
|
fmt.Sprintf("2.%d.%d.%d", n, n, n), // public IP
|
|
|
|
fmt.Sprintf("192.168.%d.1/24", n), vnet.EasyNAT, vnet.NATPMP))
|
|
|
|
}
|
|
|
|
|
2024-08-10 20:46:47 +00:00
|
|
|
// easy + port mapping + host firewall
|
|
|
|
func easyPMPFW(c *vnet.Config) *vnet.Node {
|
|
|
|
n := c.NumNodes() + 1
|
2024-08-13 19:23:44 +00:00
|
|
|
return c.AddNode(
|
|
|
|
vnet.HostFirewall,
|
|
|
|
vnet.TailscaledEnv{
|
|
|
|
Key: "TS_DEBUG_RAW_DISCO",
|
|
|
|
Value: "1",
|
|
|
|
},
|
|
|
|
vnet.TailscaledEnv{
|
|
|
|
Key: "TS_DEBUG_DISCO",
|
|
|
|
Value: "1",
|
|
|
|
},
|
|
|
|
vnet.TailscaledEnv{
|
|
|
|
Key: "TS_LOG_VERBOSITY",
|
|
|
|
Value: "2",
|
|
|
|
},
|
2024-08-10 20:46:47 +00:00
|
|
|
c.AddNetwork(
|
|
|
|
fmt.Sprintf("2.%d.%d.%d", n, n, n), // public IP
|
|
|
|
fmt.Sprintf("192.168.%d.1/24", n), vnet.EasyNAT, vnet.NATPMP))
|
|
|
|
}
|
|
|
|
|
|
|
|
// easy + port mapping + host firewall - BPF
|
|
|
|
func easyPMPFWNoBPF(c *vnet.Config) *vnet.Node {
|
|
|
|
n := c.NumNodes() + 1
|
|
|
|
return c.AddNode(
|
|
|
|
vnet.HostFirewall,
|
|
|
|
vnet.TailscaledEnv{
|
|
|
|
Key: "TS_DEBUG_DISABLE_RAW_DISCO",
|
|
|
|
Value: "1",
|
|
|
|
},
|
|
|
|
c.AddNetwork(
|
|
|
|
fmt.Sprintf("2.%d.%d.%d", n, n, n), // public IP
|
|
|
|
fmt.Sprintf("192.168.%d.1/24", n), vnet.EasyNAT, vnet.NATPMP))
|
|
|
|
}
|
|
|
|
|
2024-08-07 00:33:45 +00:00
|
|
|
func hard(c *vnet.Config) *vnet.Node {
|
|
|
|
n := c.NumNodes() + 1
|
|
|
|
return c.AddNode(c.AddNetwork(
|
|
|
|
fmt.Sprintf("2.%d.%d.%d", n, n, n), // public IP
|
|
|
|
fmt.Sprintf("10.0.%d.1/24", n), vnet.HardNAT))
|
|
|
|
}
|
|
|
|
|
2024-08-08 04:43:25 +00:00
|
|
|
func hardPMP(c *vnet.Config) *vnet.Node {
|
|
|
|
n := c.NumNodes() + 1
|
|
|
|
return c.AddNode(c.AddNetwork(
|
|
|
|
fmt.Sprintf("2.%d.%d.%d", n, n, n), // public IP
|
|
|
|
fmt.Sprintf("10.7.%d.1/24", n), vnet.HardNAT, vnet.NATPMP))
|
|
|
|
}
|
|
|
|
|
2024-08-08 20:46:55 +00:00
|
|
|
func (nt *natTest) runTest(node1, node2 addNodeFunc) pingRoute {
|
2024-08-07 00:33:45 +00:00
|
|
|
t := nt.tb
|
|
|
|
|
|
|
|
var c vnet.Config
|
2024-08-09 03:11:50 +00:00
|
|
|
c.SetPCAPFile(*pcapFile)
|
2024-08-07 00:33:45 +00:00
|
|
|
nodes := []*vnet.Node{
|
|
|
|
node1(&c),
|
|
|
|
node2(&c),
|
|
|
|
}
|
2024-08-08 21:11:07 +00:00
|
|
|
if nodes[0] == nil || nodes[1] == nil {
|
|
|
|
t.Skip("skipping test; not applicable combination")
|
|
|
|
}
|
2024-08-13 04:39:28 +00:00
|
|
|
if *logTailscaled {
|
|
|
|
nodes[0].SetVerboseSyslog(true)
|
|
|
|
nodes[1].SetVerboseSyslog(true)
|
|
|
|
}
|
2024-08-07 00:33:45 +00:00
|
|
|
|
|
|
|
var err error
|
|
|
|
nt.vnet, err = vnet.New(&c)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("newServer: %v", err)
|
|
|
|
}
|
|
|
|
nt.tb.Cleanup(func() {
|
|
|
|
nt.vnet.Close()
|
|
|
|
})
|
|
|
|
|
|
|
|
var wg sync.WaitGroup // waiting for srv.Accept goroutine
|
|
|
|
defer wg.Wait()
|
|
|
|
|
|
|
|
sockAddr := filepath.Join(nt.tempDir, "qemu.sock")
|
|
|
|
srv, err := net.Listen("unix", sockAddr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Listen: %v", err)
|
|
|
|
}
|
|
|
|
defer srv.Close()
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
for {
|
|
|
|
c, err := srv.Accept()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
go nt.vnet.ServeUnixConn(c.(*net.UnixConn), vnet.ProtocolQEMU)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
for i, node := range nodes {
|
|
|
|
disk := fmt.Sprintf("%s/node-%d.qcow2", nt.tempDir, i)
|
|
|
|
out, err := exec.Command("qemu-img", "create",
|
|
|
|
"-f", "qcow2",
|
|
|
|
"-F", "qcow2",
|
|
|
|
"-b", nt.base,
|
|
|
|
disk).CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("qemu-img create: %v, %s", err, out)
|
|
|
|
}
|
|
|
|
|
2024-08-10 20:46:47 +00:00
|
|
|
var envBuf bytes.Buffer
|
|
|
|
for _, e := range node.Env() {
|
|
|
|
fmt.Fprintf(&envBuf, " tailscaled.env=%s=%s", e.Key, e.Value)
|
|
|
|
}
|
|
|
|
envStr := envBuf.String()
|
|
|
|
|
2024-08-07 00:33:45 +00:00
|
|
|
cmd := exec.Command("qemu-system-x86_64",
|
|
|
|
"-M", "microvm,isa-serial=off",
|
2024-08-08 19:10:00 +00:00
|
|
|
"-m", "384M",
|
2024-08-07 00:33:45 +00:00
|
|
|
"-nodefaults", "-no-user-config", "-nographic",
|
2024-08-08 15:34:08 +00:00
|
|
|
"-kernel", nt.kernel,
|
2024-08-13 04:39:28 +00:00
|
|
|
"-append", "console=hvc0 root=PARTUUID=60c24cc1-f3f9-427a-8199-dd02023b0001/PARTNROFF=1 ro init=/gokrazy/init panic=10 oops=panic pci=off nousb tsc=unstable clocksource=hpet gokrazy.remote_syslog.target=52.52.0.9:995 tailscale-tta=1"+envStr,
|
2024-08-07 00:33:45 +00:00
|
|
|
"-drive", "id=blk0,file="+disk+",format=qcow2",
|
|
|
|
"-device", "virtio-blk-device,drive=blk0",
|
|
|
|
"-netdev", "stream,id=net0,addr.type=unix,addr.path="+sockAddr,
|
|
|
|
"-device", "virtio-serial-device",
|
|
|
|
"-device", "virtio-net-device,netdev=net0,mac="+node.MAC().String(),
|
|
|
|
"-chardev", "stdio,id=virtiocon0,mux=on",
|
|
|
|
"-device", "virtconsole,chardev=virtiocon0",
|
|
|
|
"-mon", "chardev=virtiocon0,mode=readline",
|
|
|
|
"-audio", "none",
|
|
|
|
)
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
if err := cmd.Start(); err != nil {
|
|
|
|
t.Fatalf("qemu: %v", err)
|
|
|
|
}
|
|
|
|
nt.tb.Cleanup(func() {
|
|
|
|
cmd.Process.Kill()
|
|
|
|
cmd.Wait()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
2024-08-08 04:31:50 +00:00
|
|
|
lc1 := nt.vnet.NodeAgentClient(nodes[0])
|
|
|
|
lc2 := nt.vnet.NodeAgentClient(nodes[1])
|
|
|
|
clients := []*vnet.NodeAgentClient{lc1, lc2}
|
2024-08-07 00:33:45 +00:00
|
|
|
|
|
|
|
var eg errgroup.Group
|
|
|
|
var sts [2]*ipnstate.Status
|
2024-08-08 04:31:50 +00:00
|
|
|
for i, c := range clients {
|
2024-08-07 00:33:45 +00:00
|
|
|
i, c := i, c
|
|
|
|
eg.Go(func() error {
|
2024-08-13 15:20:43 +00:00
|
|
|
node := nodes[i]
|
2024-08-08 04:31:50 +00:00
|
|
|
st, err := c.Status(ctx)
|
2024-08-07 00:33:45 +00:00
|
|
|
if err != nil {
|
2024-08-13 15:20:43 +00:00
|
|
|
return fmt.Errorf("%v status: %w", node, err)
|
2024-08-07 00:33:45 +00:00
|
|
|
}
|
2024-08-13 15:20:43 +00:00
|
|
|
t.Logf("%v status: %v", node, st)
|
2024-08-10 20:46:47 +00:00
|
|
|
|
|
|
|
if node.HostFirewall() {
|
|
|
|
if err := c.EnableHostFirewall(ctx); err != nil {
|
2024-08-13 15:20:43 +00:00
|
|
|
return fmt.Errorf("%v firewall: %w", node, err)
|
2024-08-10 20:46:47 +00:00
|
|
|
}
|
2024-08-13 15:20:43 +00:00
|
|
|
t.Logf("%v firewalled", node)
|
2024-08-10 20:46:47 +00:00
|
|
|
}
|
|
|
|
|
2024-08-07 00:33:45 +00:00
|
|
|
if err := up(ctx, c); err != nil {
|
2024-08-13 15:20:43 +00:00
|
|
|
return fmt.Errorf("%v up: %w", node, err)
|
2024-08-07 00:33:45 +00:00
|
|
|
}
|
2024-08-13 15:20:43 +00:00
|
|
|
t.Logf("%v up!", node)
|
2024-08-10 20:46:47 +00:00
|
|
|
|
2024-08-08 04:31:50 +00:00
|
|
|
st, err = c.Status(ctx)
|
2024-08-07 00:33:45 +00:00
|
|
|
if err != nil {
|
2024-08-13 15:20:43 +00:00
|
|
|
return fmt.Errorf("%v status: %w", node, err)
|
2024-08-07 00:33:45 +00:00
|
|
|
}
|
|
|
|
sts[i] = st
|
|
|
|
|
|
|
|
if st.BackendState != "Running" {
|
2024-08-13 15:20:43 +00:00
|
|
|
return fmt.Errorf("%v state = %q", node, st.BackendState)
|
2024-08-07 00:33:45 +00:00
|
|
|
}
|
2024-08-13 15:20:43 +00:00
|
|
|
t.Logf("%v up with %v", node, sts[i].Self.TailscaleIPs)
|
2024-08-07 00:33:45 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if err := eg.Wait(); err != nil {
|
|
|
|
t.Fatalf("initial setup: %v", err)
|
|
|
|
}
|
|
|
|
|
2024-08-08 19:47:54 +00:00
|
|
|
defer nt.vnet.Close()
|
|
|
|
|
|
|
|
pingRes, err := ping(ctx, lc1, sts[1].Self.TailscaleIPs[0])
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("ping failure: %v", err)
|
|
|
|
}
|
2024-08-13 03:36:31 +00:00
|
|
|
nt.gotRoute = classifyPing(pingRes)
|
|
|
|
t.Logf("ping route: %v", nt.gotRoute)
|
|
|
|
|
|
|
|
return nt.gotRoute
|
2024-08-08 19:47:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func classifyPing(pr *ipnstate.PingResult) pingRoute {
|
|
|
|
if pr == nil {
|
|
|
|
return routeNil
|
|
|
|
}
|
|
|
|
if pr.Endpoint != "" {
|
|
|
|
ap, err := netip.ParseAddrPort(pr.Endpoint)
|
|
|
|
if err == nil {
|
|
|
|
if ap.Addr().IsPrivate() {
|
|
|
|
return routeLocal
|
|
|
|
}
|
|
|
|
return routeDirect
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return routeDERP // presumably
|
2024-08-07 00:33:45 +00:00
|
|
|
}
|
|
|
|
|
2024-08-08 19:47:54 +00:00
|
|
|
type pingRoute string
|
|
|
|
|
|
|
|
const (
|
|
|
|
routeDERP pingRoute = "derp"
|
|
|
|
routeLocal pingRoute = "local"
|
|
|
|
routeDirect pingRoute = "direct"
|
|
|
|
routeNil pingRoute = "nil" // *ipnstate.PingResult is nil
|
|
|
|
)
|
|
|
|
|
2024-08-08 04:31:50 +00:00
|
|
|
func ping(ctx context.Context, c *vnet.NodeAgentClient, target netip.Addr) (*ipnstate.PingResult, error) {
|
|
|
|
n := 0
|
|
|
|
var res *ipnstate.PingResult
|
|
|
|
anyPong := false
|
2024-08-08 04:43:25 +00:00
|
|
|
for n < 10 {
|
2024-08-08 04:31:50 +00:00
|
|
|
n++
|
|
|
|
pr, err := c.PingWithOpts(ctx, target, tailcfg.PingDisco, tailscale.PingOpts{})
|
|
|
|
if err != nil {
|
|
|
|
if anyPong {
|
|
|
|
return res, nil
|
2024-08-07 00:33:45 +00:00
|
|
|
}
|
2024-08-08 04:31:50 +00:00
|
|
|
return nil, err
|
2024-08-07 00:33:45 +00:00
|
|
|
}
|
2024-08-08 04:31:50 +00:00
|
|
|
if pr.Err != "" {
|
|
|
|
return nil, errors.New(pr.Err)
|
|
|
|
}
|
|
|
|
if pr.DERPRegionID == 0 {
|
|
|
|
return pr, nil
|
|
|
|
}
|
2024-08-08 17:55:50 +00:00
|
|
|
res = pr
|
2024-08-08 04:43:25 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
}
|
2024-08-07 00:33:45 +00:00
|
|
|
}
|
2024-08-08 04:43:25 +00:00
|
|
|
if res == nil {
|
|
|
|
return nil, errors.New("no ping response")
|
|
|
|
}
|
|
|
|
return res, nil
|
2024-08-07 00:33:45 +00:00
|
|
|
}
|
|
|
|
|
2024-08-08 04:31:50 +00:00
|
|
|
func up(ctx context.Context, c *vnet.NodeAgentClient) error {
|
2024-08-07 00:33:45 +00:00
|
|
|
req, err := http.NewRequestWithContext(ctx, "GET", "http://unused/up", nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-08-08 04:31:50 +00:00
|
|
|
res, err := c.HTTPClient.Do(req)
|
2024-08-07 00:33:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
all, _ := io.ReadAll(res.Body)
|
|
|
|
if res.StatusCode != 200 {
|
|
|
|
return fmt.Errorf("unexpected status code %v: %s", res.Status, all)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-08-09 14:53:08 +00:00
|
|
|
type nodeType struct {
|
|
|
|
name string
|
|
|
|
fn addNodeFunc
|
2024-08-07 00:33:45 +00:00
|
|
|
}
|
2024-08-08 04:43:25 +00:00
|
|
|
|
2024-08-09 14:53:08 +00:00
|
|
|
var types = []nodeType{
|
|
|
|
{"easy", easy},
|
|
|
|
{"easyAF", easyAF},
|
|
|
|
{"hard", hard},
|
|
|
|
{"easyPMP", easyPMP},
|
|
|
|
{"hardPMP", hardPMP},
|
|
|
|
{"one2one", one2one},
|
|
|
|
{"sameLAN", sameLAN},
|
2024-08-08 04:43:25 +00:00
|
|
|
}
|
2024-08-08 19:10:00 +00:00
|
|
|
|
2024-08-13 03:36:31 +00:00
|
|
|
// want sets the expected ping route for the test.
|
|
|
|
func (nt *natTest) want(r pingRoute) {
|
|
|
|
if nt.gotRoute != r {
|
|
|
|
nt.tb.Errorf("ping route = %v; want %v", nt.gotRoute, r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-09 14:53:08 +00:00
|
|
|
func TestEasyEasy(t *testing.T) {
|
2024-08-08 19:10:00 +00:00
|
|
|
nt := newNatTest(t)
|
2024-08-09 14:53:08 +00:00
|
|
|
nt.runTest(easy, easy)
|
2024-08-13 03:36:31 +00:00
|
|
|
nt.want(routeDirect)
|
2024-08-08 19:10:00 +00:00
|
|
|
}
|
2024-08-08 19:47:54 +00:00
|
|
|
|
2024-08-13 03:36:31 +00:00
|
|
|
func TestSameLAN(t *testing.T) {
|
|
|
|
nt := newNatTest(t)
|
|
|
|
nt.runTest(easy, sameLAN)
|
|
|
|
nt.want(routeLocal)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestBPFDisco tests https://github.com/tailscale/tailscale/issues/3824 ...
|
2024-08-10 20:46:47 +00:00
|
|
|
// * server behind a Hard NAT
|
|
|
|
// * client behind a NAT with UPnP support
|
|
|
|
// * client machine has a stateful host firewall (e.g. ufw)
|
|
|
|
func TestBPFDisco(t *testing.T) {
|
|
|
|
nt := newNatTest(t)
|
|
|
|
nt.runTest(easyPMPFW, hard)
|
2024-08-13 03:36:31 +00:00
|
|
|
nt.want(routeDirect)
|
2024-08-10 20:46:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestHostFWNoBPF(t *testing.T) {
|
|
|
|
nt := newNatTest(t)
|
|
|
|
nt.runTest(easyPMPFWNoBPF, hard)
|
2024-08-13 03:36:31 +00:00
|
|
|
nt.want(routeDERP)
|
2024-08-10 20:46:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestHostFWPair(t *testing.T) {
|
|
|
|
nt := newNatTest(t)
|
|
|
|
nt.runTest(easyFW, easyFW)
|
2024-08-13 03:36:31 +00:00
|
|
|
nt.want(routeDirect)
|
2024-08-10 20:46:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestOneHostFW(t *testing.T) {
|
|
|
|
nt := newNatTest(t)
|
|
|
|
nt.runTest(easy, easyFW)
|
2024-08-13 03:36:31 +00:00
|
|
|
nt.want(routeDirect)
|
2024-08-10 20:46:47 +00:00
|
|
|
}
|
|
|
|
|
2024-08-09 14:53:08 +00:00
|
|
|
var pair = flag.String("pair", "", "comma-separated pair of types to test (easy, easyAF, hard, easyPMP, hardPMP, one2one, sameLAN)")
|
2024-08-08 19:47:54 +00:00
|
|
|
|
2024-08-09 14:53:08 +00:00
|
|
|
func TestPair(t *testing.T) {
|
|
|
|
t1, t2, ok := strings.Cut(*pair, ",")
|
|
|
|
if !ok {
|
|
|
|
t.Skipf("skipping test without --pair=type1,type2 set")
|
|
|
|
}
|
|
|
|
find := func(name string) addNodeFunc {
|
|
|
|
for _, nt := range types {
|
|
|
|
if nt.name == name {
|
|
|
|
return nt.fn
|
|
|
|
}
|
|
|
|
}
|
|
|
|
t.Fatalf("unknown type %q", name)
|
|
|
|
return nil
|
|
|
|
}
|
2024-08-08 19:47:54 +00:00
|
|
|
|
|
|
|
nt := newNatTest(t)
|
2024-08-09 14:53:08 +00:00
|
|
|
nt.runTest(find(t1), find(t2))
|
2024-08-08 19:47:54 +00:00
|
|
|
}
|
|
|
|
|
2024-08-09 14:53:08 +00:00
|
|
|
var runGrid = flag.Bool("run-grid", false, "run grid test")
|
2024-08-08 20:46:55 +00:00
|
|
|
|
|
|
|
func TestGrid(t *testing.T) {
|
2024-08-09 14:53:08 +00:00
|
|
|
if !*runGrid {
|
|
|
|
t.Skip("skipping grid test; set --run-grid to run")
|
2024-08-08 20:46:55 +00:00
|
|
|
}
|
2024-08-09 14:53:08 +00:00
|
|
|
t.Parallel()
|
2024-08-08 20:46:55 +00:00
|
|
|
|
|
|
|
sem := syncs.NewSemaphore(2)
|
|
|
|
var (
|
|
|
|
mu sync.Mutex
|
|
|
|
res = make(map[string]pingRoute)
|
|
|
|
)
|
2024-08-08 21:11:07 +00:00
|
|
|
for _, a := range types {
|
|
|
|
for _, b := range types {
|
2024-08-08 20:46:55 +00:00
|
|
|
key := a.name + "-" + b.name
|
2024-08-08 21:11:07 +00:00
|
|
|
keyBack := b.name + "-" + a.name
|
2024-08-08 20:46:55 +00:00
|
|
|
t.Run(key, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
sem.Acquire()
|
|
|
|
defer sem.Release()
|
|
|
|
|
|
|
|
filename := key + ".cache"
|
|
|
|
contents, _ := os.ReadFile(filename)
|
2024-08-08 21:11:07 +00:00
|
|
|
if len(contents) == 0 {
|
|
|
|
filename2 := keyBack + ".cache"
|
|
|
|
contents, _ = os.ReadFile(filename2)
|
|
|
|
}
|
2024-08-08 20:46:55 +00:00
|
|
|
route := pingRoute(strings.TrimSpace(string(contents)))
|
|
|
|
|
|
|
|
if route == "" {
|
|
|
|
nt := newNatTest(t)
|
|
|
|
route = nt.runTest(a.fn, b.fn)
|
|
|
|
if err := os.WriteFile(filename, []byte(string(route)), 0666); err != nil {
|
|
|
|
t.Fatalf("writeFile: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
res[key] = route
|
|
|
|
t.Logf("results: %v", res)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Cleanup(func() {
|
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
var hb bytes.Buffer
|
|
|
|
pf := func(format string, args ...any) {
|
|
|
|
fmt.Fprintf(&hb, format, args...)
|
|
|
|
}
|
2024-08-09 03:58:24 +00:00
|
|
|
rewrite := func(s string) string {
|
|
|
|
return strings.ReplaceAll(s, "PMP", "+pm")
|
|
|
|
}
|
2024-08-08 20:46:55 +00:00
|
|
|
pf("<html><table border=1 cellpadding=5>")
|
|
|
|
pf("<tr><td></td>")
|
|
|
|
for _, a := range types {
|
2024-08-09 03:58:24 +00:00
|
|
|
pf("<td><b>%s</b></td>", rewrite(a.name))
|
2024-08-08 20:46:55 +00:00
|
|
|
}
|
|
|
|
pf("</tr>\n")
|
|
|
|
|
|
|
|
for _, a := range types {
|
2024-08-08 21:11:07 +00:00
|
|
|
if a.name == "sameLAN" {
|
|
|
|
continue
|
|
|
|
}
|
2024-08-09 03:58:24 +00:00
|
|
|
pf("<tr><td><b>%s</b></td>", rewrite(a.name))
|
2024-08-08 20:46:55 +00:00
|
|
|
for _, b := range types {
|
|
|
|
key := a.name + "-" + b.name
|
|
|
|
key2 := b.name + "-" + a.name
|
2024-08-08 21:11:07 +00:00
|
|
|
v := cmp.Or(res[key], res[key2], "-")
|
2024-08-08 20:46:55 +00:00
|
|
|
if v == "derp" {
|
|
|
|
pf("<td><div style='color: red; font-weight: bold'>%s</div></td>", v)
|
2024-08-08 21:11:07 +00:00
|
|
|
} else if v == "local" {
|
|
|
|
pf("<td><div style='color: green; font-weight: bold'>%s</div></td>", v)
|
2024-08-08 20:46:55 +00:00
|
|
|
} else {
|
|
|
|
pf("<td>%s</td>", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pf("</tr>\n")
|
|
|
|
}
|
2024-08-09 03:58:24 +00:00
|
|
|
pf("</table>")
|
|
|
|
pf("<b>easy</b>: Endpoint-Independent Mapping, Address and Port-Dependent Filtering (e.g. Linux, Google Wifi, Unifi, eero)<br>")
|
|
|
|
pf("<b>easyAF</b>: Endpoint-Independent Mapping, Address-Dependent Filtering (James says telephony things or Zyxel type things)<br>")
|
|
|
|
pf("<b>hard</b>: Address and Port-Dependent Mapping, Address and Port-Dependent Filtering (FreeBSD, OPNSense, pfSense)<br>")
|
|
|
|
pf("<b>one2one</b>: One-to-One NAT (e.g. an EC2 instance with a public IPv4)<br>")
|
|
|
|
pf("<b>x+pm</b>: x, with port mapping (NAT-PMP, PCP, UPnP, etc)<br>")
|
|
|
|
pf("<b>sameLAN</b>: a second node in the same LAN as the first<br>")
|
|
|
|
pf("</html>")
|
2024-08-08 20:46:55 +00:00
|
|
|
|
|
|
|
if err := os.WriteFile("grid.html", hb.Bytes(), 0666); err != nil {
|
|
|
|
t.Fatalf("writeFile: %v", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|