2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2021-04-29 21:44:08 +00:00
|
|
|
|
|
|
|
package integration
|
|
|
|
|
2021-07-07 19:01:57 +00:00
|
|
|
//go:generate go run gen_deps.go
|
|
|
|
|
2021-04-29 21:44:08 +00:00
|
|
|
import (
|
|
|
|
"bytes"
|
2021-06-10 22:12:25 +00:00
|
|
|
"context"
|
2021-04-29 21:44:08 +00:00
|
|
|
"encoding/json"
|
2021-05-12 21:43:43 +00:00
|
|
|
"errors"
|
2021-05-12 04:57:25 +00:00
|
|
|
"flag"
|
2021-04-29 21:44:08 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
2024-05-06 18:08:25 +00:00
|
|
|
"net"
|
2021-04-29 21:44:08 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2022-07-26 03:55:44 +00:00
|
|
|
"net/netip"
|
2021-04-29 21:44:08 +00:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
2021-05-12 04:57:25 +00:00
|
|
|
"regexp"
|
2021-04-29 21:44:08 +00:00
|
|
|
"runtime"
|
2024-08-29 01:50:13 +00:00
|
|
|
"strconv"
|
2021-07-09 15:51:30 +00:00
|
|
|
"strings"
|
2021-04-29 21:44:08 +00:00
|
|
|
"sync"
|
2021-05-03 17:49:45 +00:00
|
|
|
"sync/atomic"
|
2021-04-29 21:44:08 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2024-08-23 23:40:18 +00:00
|
|
|
"github.com/miekg/dns"
|
2021-04-29 21:44:08 +00:00
|
|
|
"go4.org/mem"
|
2024-05-06 18:08:25 +00:00
|
|
|
"tailscale.com/client/tailscale"
|
2024-01-08 17:32:18 +00:00
|
|
|
"tailscale.com/clientupdate"
|
2023-01-19 14:42:58 +00:00
|
|
|
"tailscale.com/cmd/testwrapper/flakytest"
|
2021-07-13 21:03:05 +00:00
|
|
|
"tailscale.com/ipn"
|
2022-11-09 05:58:10 +00:00
|
|
|
"tailscale.com/ipn/ipnlocal"
|
2021-05-03 21:22:18 +00:00
|
|
|
"tailscale.com/ipn/ipnstate"
|
2022-02-28 21:08:45 +00:00
|
|
|
"tailscale.com/ipn/store"
|
2024-08-23 23:40:18 +00:00
|
|
|
"tailscale.com/net/tsaddr"
|
2024-08-29 18:17:27 +00:00
|
|
|
"tailscale.com/net/tstun"
|
2021-05-03 17:49:45 +00:00
|
|
|
"tailscale.com/safesocket"
|
2022-08-04 17:43:49 +00:00
|
|
|
"tailscale.com/syncs"
|
2021-05-03 17:49:45 +00:00
|
|
|
"tailscale.com/tailcfg"
|
2021-04-29 21:44:08 +00:00
|
|
|
"tailscale.com/tstest"
|
2021-04-30 04:52:31 +00:00
|
|
|
"tailscale.com/tstest/integration/testcontrol"
|
2023-03-03 00:00:19 +00:00
|
|
|
"tailscale.com/types/key"
|
2021-05-03 17:49:45 +00:00
|
|
|
"tailscale.com/types/logger"
|
2024-01-08 17:32:18 +00:00
|
|
|
"tailscale.com/types/opt"
|
2023-10-16 19:15:03 +00:00
|
|
|
"tailscale.com/types/ptr"
|
2024-08-23 23:40:18 +00:00
|
|
|
"tailscale.com/util/dnsname"
|
2023-10-16 19:15:03 +00:00
|
|
|
"tailscale.com/util/must"
|
2023-09-12 20:37:51 +00:00
|
|
|
"tailscale.com/util/rands"
|
2023-10-14 02:22:36 +00:00
|
|
|
"tailscale.com/version"
|
2021-04-29 21:44:08 +00:00
|
|
|
)
|
|
|
|
|
2021-06-10 18:23:53 +00:00
|
|
|
var (
|
2021-06-10 22:12:25 +00:00
|
|
|
verboseTailscaled = flag.Bool("verbose-tailscaled", false, "verbose tailscaled logging")
|
2021-07-19 18:07:42 +00:00
|
|
|
verboseTailscale = flag.Bool("verbose-tailscale", false, "verbose tailscale CLI logging")
|
2021-06-10 18:23:53 +00:00
|
|
|
)
|
2021-05-12 04:57:25 +00:00
|
|
|
|
2022-08-04 17:43:49 +00:00
|
|
|
var mainError syncs.AtomicValue[error]
|
2021-05-03 17:49:45 +00:00
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
2021-06-22 22:29:01 +00:00
|
|
|
// Have to disable UPnP which hits the network, otherwise it fails due to HTTP proxy.
|
|
|
|
os.Setenv("TS_DISABLE_UPNP", "true")
|
2021-06-10 22:12:25 +00:00
|
|
|
flag.Parse()
|
2021-05-03 17:49:45 +00:00
|
|
|
v := m.Run()
|
2021-07-20 20:55:09 +00:00
|
|
|
CleanupBinaries()
|
2021-05-03 17:49:45 +00:00
|
|
|
if v != 0 {
|
|
|
|
os.Exit(v)
|
|
|
|
}
|
2022-08-04 17:43:49 +00:00
|
|
|
if err := mainError.Load(); err != nil {
|
2021-05-03 17:49:45 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "FAIL: %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2023-10-13 19:29:28 +00:00
|
|
|
// Tests that tailscaled starts up in TUN mode, and also without data races:
|
|
|
|
// https://github.com/tailscale/tailscale/issues/7894
|
|
|
|
func TestTUNMode(t *testing.T) {
|
2023-10-17 01:47:03 +00:00
|
|
|
tstest.Shard(t)
|
2023-10-13 19:29:28 +00:00
|
|
|
if os.Getuid() != 0 {
|
|
|
|
t.Skip("skipping when not root")
|
|
|
|
}
|
2023-10-17 03:55:28 +00:00
|
|
|
tstest.Parallel(t)
|
2023-10-13 19:29:28 +00:00
|
|
|
env := newTestEnv(t)
|
|
|
|
env.tunMode = true
|
|
|
|
n1 := newTestNode(t, env)
|
|
|
|
d1 := n1.StartDaemon()
|
|
|
|
|
|
|
|
n1.AwaitResponding()
|
|
|
|
n1.MustUp()
|
|
|
|
|
|
|
|
t.Logf("Got IP: %v", n1.AwaitIP4())
|
|
|
|
n1.AwaitRunning()
|
|
|
|
|
|
|
|
d1.MustCleanShutdown(t)
|
|
|
|
}
|
|
|
|
|
2021-12-03 22:28:36 +00:00
|
|
|
func TestOneNodeUpNoAuth(t *testing.T) {
|
2023-10-17 01:47:03 +00:00
|
|
|
tstest.Shard(t)
|
2023-10-17 03:55:28 +00:00
|
|
|
tstest.Parallel(t)
|
2021-07-20 20:55:09 +00:00
|
|
|
env := newTestEnv(t)
|
2021-05-01 03:27:05 +00:00
|
|
|
n1 := newTestNode(t, env)
|
|
|
|
|
2021-12-16 01:05:21 +00:00
|
|
|
d1 := n1.StartDaemon()
|
|
|
|
n1.AwaitResponding()
|
2021-05-12 21:43:43 +00:00
|
|
|
n1.MustUp()
|
2021-04-30 04:52:31 +00:00
|
|
|
|
2023-10-03 18:55:06 +00:00
|
|
|
t.Logf("Got IP: %v", n1.AwaitIP4())
|
2021-12-16 01:05:21 +00:00
|
|
|
n1.AwaitRunning()
|
2021-05-12 04:57:25 +00:00
|
|
|
|
|
|
|
d1.MustCleanShutdown(t)
|
|
|
|
|
|
|
|
t.Logf("number of HTTP logcatcher requests: %v", env.LogCatcher.numRequests())
|
|
|
|
}
|
|
|
|
|
2021-10-29 00:44:18 +00:00
|
|
|
func TestOneNodeExpiredKey(t *testing.T) {
|
2023-10-17 01:47:03 +00:00
|
|
|
tstest.Shard(t)
|
2023-10-17 03:55:28 +00:00
|
|
|
tstest.Parallel(t)
|
2021-07-20 20:55:09 +00:00
|
|
|
env := newTestEnv(t)
|
2021-10-29 00:44:18 +00:00
|
|
|
n1 := newTestNode(t, env)
|
|
|
|
|
2021-12-16 01:05:21 +00:00
|
|
|
d1 := n1.StartDaemon()
|
|
|
|
n1.AwaitResponding()
|
2021-10-29 00:44:18 +00:00
|
|
|
n1.MustUp()
|
2021-12-16 01:05:21 +00:00
|
|
|
n1.AwaitRunning()
|
2021-10-29 00:44:18 +00:00
|
|
|
|
|
|
|
nodes := env.Control.AllNodes()
|
|
|
|
if len(nodes) != 1 {
|
|
|
|
t.Fatalf("expected 1 node, got %d nodes", len(nodes))
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeKey := nodes[0].Key
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
|
if err := env.Control.AwaitNodeInMapRequest(ctx, nodeKey); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
cancel()
|
|
|
|
|
|
|
|
env.Control.SetExpireAllNodes(true)
|
2021-12-16 01:05:21 +00:00
|
|
|
n1.AwaitNeedsLogin()
|
2021-10-29 00:44:18 +00:00
|
|
|
ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
|
if err := env.Control.AwaitNodeInMapRequest(ctx, nodeKey); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
cancel()
|
|
|
|
|
|
|
|
env.Control.SetExpireAllNodes(false)
|
2021-12-16 01:05:21 +00:00
|
|
|
n1.AwaitRunning()
|
2021-10-29 00:44:18 +00:00
|
|
|
|
|
|
|
d1.MustCleanShutdown(t)
|
|
|
|
}
|
|
|
|
|
2023-09-12 04:44:38 +00:00
|
|
|
func TestControlKnobs(t *testing.T) {
|
2023-10-17 01:47:03 +00:00
|
|
|
tstest.Shard(t)
|
2023-10-17 03:55:28 +00:00
|
|
|
tstest.Parallel(t)
|
2023-09-12 04:44:38 +00:00
|
|
|
env := newTestEnv(t)
|
|
|
|
n1 := newTestNode(t, env)
|
|
|
|
|
|
|
|
d1 := n1.StartDaemon()
|
|
|
|
defer d1.MustCleanShutdown(t)
|
|
|
|
n1.AwaitResponding()
|
|
|
|
n1.MustUp()
|
|
|
|
|
2023-10-03 18:55:06 +00:00
|
|
|
t.Logf("Got IP: %v", n1.AwaitIP4())
|
2023-09-12 04:44:38 +00:00
|
|
|
n1.AwaitRunning()
|
|
|
|
|
|
|
|
cmd := n1.Tailscale("debug", "control-knobs")
|
|
|
|
cmd.Stdout = nil // in case --verbose-tailscale was set
|
|
|
|
cmd.Stderr = nil // in case --verbose-tailscale was set
|
|
|
|
out, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
t.Logf("control-knobs output:\n%s", out)
|
|
|
|
var m map[string]any
|
|
|
|
if err := json.Unmarshal(out, &m); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if got, want := m["DisableUPnP"], true; got != want {
|
|
|
|
t.Errorf("control-knobs DisableUPnP = %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-28 22:17:31 +00:00
|
|
|
func TestCollectPanic(t *testing.T) {
|
2023-10-17 01:47:03 +00:00
|
|
|
tstest.Shard(t)
|
2023-10-17 03:55:28 +00:00
|
|
|
tstest.Parallel(t)
|
2021-07-20 20:55:09 +00:00
|
|
|
env := newTestEnv(t)
|
2021-07-28 22:17:31 +00:00
|
|
|
n := newTestNode(t, env)
|
|
|
|
|
2021-07-20 20:55:09 +00:00
|
|
|
cmd := exec.Command(env.daemon, "--cleanup")
|
2021-07-28 22:17:31 +00:00
|
|
|
cmd.Env = append(os.Environ(),
|
|
|
|
"TS_PLEASE_PANIC=1",
|
|
|
|
"TS_LOG_TARGET="+n.env.LogCatcherServer.URL,
|
|
|
|
)
|
|
|
|
got, _ := cmd.CombinedOutput() // we expect it to fail, ignore err
|
|
|
|
t.Logf("initial run: %s", got)
|
|
|
|
|
|
|
|
// Now we run it again, and on start, it will upload the logs to logcatcher.
|
2021-07-20 20:55:09 +00:00
|
|
|
cmd = exec.Command(env.daemon, "--cleanup")
|
2021-07-28 22:17:31 +00:00
|
|
|
cmd.Env = append(os.Environ(), "TS_LOG_TARGET="+n.env.LogCatcherServer.URL)
|
|
|
|
if out, err := cmd.CombinedOutput(); err != nil {
|
|
|
|
t.Fatalf("cleanup failed: %v: %q", err, out)
|
|
|
|
}
|
|
|
|
if err := tstest.WaitFor(20*time.Second, func() error {
|
|
|
|
const sub = `panic`
|
|
|
|
if !n.env.LogCatcher.logsContains(mem.S(sub)) {
|
|
|
|
return fmt.Errorf("log catcher didn't see %#q; got %s", sub, n.env.LogCatcher.logsString())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-17 02:36:04 +00:00
|
|
|
func TestControlTimeLogLine(t *testing.T) {
|
2023-10-17 01:47:03 +00:00
|
|
|
tstest.Shard(t)
|
2023-10-17 03:55:28 +00:00
|
|
|
tstest.Parallel(t)
|
2022-02-17 02:36:04 +00:00
|
|
|
env := newTestEnv(t)
|
2022-02-18 21:00:08 +00:00
|
|
|
env.LogCatcher.StoreRawJSON()
|
2022-02-17 02:36:04 +00:00
|
|
|
n := newTestNode(t, env)
|
|
|
|
|
|
|
|
n.StartDaemon()
|
|
|
|
n.AwaitResponding()
|
|
|
|
n.MustUp()
|
|
|
|
n.AwaitRunning()
|
|
|
|
|
|
|
|
if err := tstest.WaitFor(20*time.Second, func() error {
|
2022-02-18 21:00:08 +00:00
|
|
|
const sub = `"controltime":"2020-08-03T00:00:00.000000001Z"`
|
2022-02-17 02:36:04 +00:00
|
|
|
if !n.env.LogCatcher.logsContains(mem.S(sub)) {
|
|
|
|
return fmt.Errorf("log catcher didn't see %#q; got %s", sub, n.env.LogCatcher.logsString())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-13 21:03:05 +00:00
|
|
|
// test Issue 2321: Start with UpdatePrefs should save prefs to disk
|
|
|
|
func TestStateSavedOnStart(t *testing.T) {
|
2023-10-17 01:47:03 +00:00
|
|
|
tstest.Shard(t)
|
2023-10-17 03:55:28 +00:00
|
|
|
tstest.Parallel(t)
|
2021-07-20 20:55:09 +00:00
|
|
|
env := newTestEnv(t)
|
2021-07-13 21:03:05 +00:00
|
|
|
n1 := newTestNode(t, env)
|
|
|
|
|
2021-12-16 01:05:21 +00:00
|
|
|
d1 := n1.StartDaemon()
|
|
|
|
n1.AwaitResponding()
|
2021-07-13 21:03:05 +00:00
|
|
|
n1.MustUp()
|
|
|
|
|
2023-10-03 18:55:06 +00:00
|
|
|
t.Logf("Got IP: %v", n1.AwaitIP4())
|
2021-12-16 01:05:21 +00:00
|
|
|
n1.AwaitRunning()
|
2021-07-13 21:03:05 +00:00
|
|
|
|
2021-12-16 01:05:21 +00:00
|
|
|
p1 := n1.diskPrefs()
|
2021-07-13 21:03:05 +00:00
|
|
|
t.Logf("Prefs1: %v", p1.Pretty())
|
|
|
|
|
|
|
|
// Bring it down, to prevent an EditPrefs call in the
|
|
|
|
// subsequent "up", as we want to test the bug when
|
|
|
|
// cmd/tailscale implements "up" via LocalBackend.Start.
|
|
|
|
n1.MustDown()
|
|
|
|
|
|
|
|
// And change the hostname to something:
|
2024-05-07 04:33:37 +00:00
|
|
|
if err := n1.Tailscale("up", "--login-server="+n1.env.controlURL(), "--hostname=foo").Run(); err != nil {
|
2021-07-13 21:03:05 +00:00
|
|
|
t.Fatalf("up: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-12-16 01:05:21 +00:00
|
|
|
p2 := n1.diskPrefs()
|
2021-07-13 21:03:05 +00:00
|
|
|
if pretty := p1.Pretty(); pretty == p2.Pretty() {
|
|
|
|
t.Errorf("Prefs didn't change on disk after 'up', still: %s", pretty)
|
|
|
|
}
|
|
|
|
if p2.Hostname != "foo" {
|
|
|
|
t.Errorf("Prefs.Hostname = %q; want foo", p2.Hostname)
|
|
|
|
}
|
|
|
|
|
|
|
|
d1.MustCleanShutdown(t)
|
|
|
|
}
|
|
|
|
|
2021-12-03 22:28:36 +00:00
|
|
|
func TestOneNodeUpAuth(t *testing.T) {
|
2023-10-17 01:47:03 +00:00
|
|
|
tstest.Shard(t)
|
2023-10-17 03:55:28 +00:00
|
|
|
tstest.Parallel(t)
|
2021-07-20 20:55:09 +00:00
|
|
|
env := newTestEnv(t, configureControl(func(control *testcontrol.Server) {
|
2021-06-22 17:24:05 +00:00
|
|
|
control.RequireAuth = true
|
|
|
|
}))
|
2021-05-12 04:57:25 +00:00
|
|
|
|
|
|
|
n1 := newTestNode(t, env)
|
2021-12-16 01:05:21 +00:00
|
|
|
d1 := n1.StartDaemon()
|
2021-05-12 04:57:25 +00:00
|
|
|
|
2021-12-16 01:05:21 +00:00
|
|
|
n1.AwaitListening()
|
2021-05-12 04:57:25 +00:00
|
|
|
|
2021-12-16 01:05:21 +00:00
|
|
|
st := n1.MustStatus()
|
2021-05-12 04:57:25 +00:00
|
|
|
t.Logf("Status: %s", st.BackendState)
|
|
|
|
|
2024-05-07 04:33:37 +00:00
|
|
|
t.Logf("Running up --login-server=%s ...", env.controlURL())
|
2021-05-12 04:57:25 +00:00
|
|
|
|
2024-05-07 04:33:37 +00:00
|
|
|
cmd := n1.Tailscale("up", "--login-server="+env.controlURL())
|
2021-05-12 04:57:25 +00:00
|
|
|
var authCountAtomic int32
|
|
|
|
cmd.Stdout = &authURLParserWriter{fn: func(urlStr string) error {
|
|
|
|
if env.Control.CompleteAuth(urlStr) {
|
|
|
|
atomic.AddInt32(&authCountAtomic, 1)
|
|
|
|
t.Logf("completed auth path %s", urlStr)
|
|
|
|
return nil
|
2021-04-30 04:52:31 +00:00
|
|
|
}
|
2021-05-12 04:57:25 +00:00
|
|
|
err := fmt.Errorf("Failed to complete auth path to %q", urlStr)
|
|
|
|
t.Log(err)
|
|
|
|
return err
|
|
|
|
}}
|
|
|
|
cmd.Stderr = cmd.Stdout
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
t.Fatalf("up: %v", err)
|
2021-04-30 04:52:31 +00:00
|
|
|
}
|
2023-10-03 18:55:06 +00:00
|
|
|
t.Logf("Got IP: %v", n1.AwaitIP4())
|
2021-04-30 04:52:31 +00:00
|
|
|
|
2021-12-16 01:05:21 +00:00
|
|
|
n1.AwaitRunning()
|
2021-04-29 21:44:08 +00:00
|
|
|
|
2021-05-12 04:57:25 +00:00
|
|
|
if n := atomic.LoadInt32(&authCountAtomic); n != 1 {
|
|
|
|
t.Errorf("Auth URLs completed = %d; want 1", n)
|
2021-04-29 21:44:08 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 04:57:25 +00:00
|
|
|
d1.MustCleanShutdown(t)
|
2021-05-01 03:27:05 +00:00
|
|
|
}
|
|
|
|
|
2023-10-16 19:15:03 +00:00
|
|
|
func TestConfigFileAuthKey(t *testing.T) {
|
|
|
|
tstest.SkipOnUnshardedCI(t)
|
|
|
|
tstest.Shard(t)
|
|
|
|
t.Parallel()
|
|
|
|
const authKey = "opensesame"
|
|
|
|
env := newTestEnv(t, configureControl(func(control *testcontrol.Server) {
|
|
|
|
control.RequireAuthKey = authKey
|
|
|
|
}))
|
|
|
|
|
|
|
|
n1 := newTestNode(t, env)
|
|
|
|
n1.configFile = filepath.Join(n1.dir, "config.json")
|
|
|
|
authKeyFile := filepath.Join(n1.dir, "my-auth-key")
|
|
|
|
must.Do(os.WriteFile(authKeyFile, fmt.Appendf(nil, "%s\n", authKey), 0666))
|
|
|
|
must.Do(os.WriteFile(n1.configFile, must.Get(json.Marshal(ipn.ConfigVAlpha{
|
|
|
|
Version: "alpha0",
|
|
|
|
AuthKey: ptr.To("file:" + authKeyFile),
|
|
|
|
ServerURL: ptr.To(n1.env.ControlServer.URL),
|
|
|
|
})), 0644))
|
|
|
|
d1 := n1.StartDaemon()
|
|
|
|
|
|
|
|
n1.AwaitListening()
|
|
|
|
t.Logf("Got IP: %v", n1.AwaitIP4())
|
|
|
|
n1.AwaitRunning()
|
|
|
|
|
|
|
|
d1.MustCleanShutdown(t)
|
|
|
|
}
|
|
|
|
|
2021-05-12 21:43:43 +00:00
|
|
|
func TestTwoNodes(t *testing.T) {
|
2023-10-17 01:47:03 +00:00
|
|
|
tstest.Shard(t)
|
2023-10-17 03:55:28 +00:00
|
|
|
tstest.Parallel(t)
|
2021-07-20 20:55:09 +00:00
|
|
|
env := newTestEnv(t)
|
2021-05-12 21:43:43 +00:00
|
|
|
|
|
|
|
// Create two nodes:
|
|
|
|
n1 := newTestNode(t, env)
|
2021-06-28 16:33:42 +00:00
|
|
|
n1SocksAddrCh := n1.socks5AddrChan()
|
2021-12-16 01:05:21 +00:00
|
|
|
d1 := n1.StartDaemon()
|
2021-05-12 21:43:43 +00:00
|
|
|
|
|
|
|
n2 := newTestNode(t, env)
|
2021-06-28 16:33:42 +00:00
|
|
|
n2SocksAddrCh := n2.socks5AddrChan()
|
2021-12-16 01:05:21 +00:00
|
|
|
d2 := n2.StartDaemon()
|
2021-05-12 21:43:43 +00:00
|
|
|
|
2024-05-06 21:08:28 +00:00
|
|
|
// Drop some logs to disk on test failure.
|
|
|
|
//
|
|
|
|
// TODO(bradfitz): make all nodes for all tests do this? give each node a
|
|
|
|
// unique integer within the test? But for now only do this test because
|
|
|
|
// this is what we often saw flaking.
|
|
|
|
t.Cleanup(func() {
|
|
|
|
if !t.Failed() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
n1.mu.Lock()
|
|
|
|
n2.mu.Lock()
|
|
|
|
defer n1.mu.Unlock()
|
|
|
|
defer n2.mu.Unlock()
|
|
|
|
|
|
|
|
rxNoDates := regexp.MustCompile(`(?m)^\d{4}.\d{2}.\d{2}.\d{2}:\d{2}:\d{2}`)
|
|
|
|
cleanLog := func(n *testNode) []byte {
|
|
|
|
b := n.tailscaledParser.allBuf.Bytes()
|
|
|
|
b = rxNoDates.ReplaceAll(b, nil)
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("writing tailscaled logs to n1.log and n2.log")
|
|
|
|
os.WriteFile("n1.log", cleanLog(n1), 0666)
|
|
|
|
os.WriteFile("n2.log", cleanLog(n2), 0666)
|
|
|
|
})
|
|
|
|
|
2021-12-16 01:05:21 +00:00
|
|
|
n1Socks := n1.AwaitSocksAddr(n1SocksAddrCh)
|
|
|
|
n2Socks := n1.AwaitSocksAddr(n2SocksAddrCh)
|
2021-06-28 16:33:42 +00:00
|
|
|
t.Logf("node1 SOCKS5 addr: %v", n1Socks)
|
|
|
|
t.Logf("node2 SOCKS5 addr: %v", n2Socks)
|
|
|
|
|
2021-12-16 01:05:21 +00:00
|
|
|
n1.AwaitListening()
|
2024-05-06 00:41:13 +00:00
|
|
|
t.Logf("n1 is listening")
|
2021-12-16 01:05:21 +00:00
|
|
|
n2.AwaitListening()
|
2024-05-06 00:41:13 +00:00
|
|
|
t.Logf("n2 is listening")
|
2021-05-12 21:43:43 +00:00
|
|
|
n1.MustUp()
|
2024-05-06 00:41:13 +00:00
|
|
|
t.Logf("n1 is up")
|
2021-05-12 21:43:43 +00:00
|
|
|
n2.MustUp()
|
2024-05-06 00:41:13 +00:00
|
|
|
t.Logf("n2 is up")
|
2021-12-16 01:05:21 +00:00
|
|
|
n1.AwaitRunning()
|
2024-05-06 00:41:13 +00:00
|
|
|
t.Logf("n1 is running")
|
2021-12-16 01:05:21 +00:00
|
|
|
n2.AwaitRunning()
|
2024-05-06 00:41:13 +00:00
|
|
|
t.Logf("n2 is running")
|
2021-05-12 21:43:43 +00:00
|
|
|
|
|
|
|
if err := tstest.WaitFor(2*time.Second, func() error {
|
2021-12-16 01:05:21 +00:00
|
|
|
st := n1.MustStatus()
|
2021-05-12 21:43:43 +00:00
|
|
|
if len(st.Peer) == 0 {
|
|
|
|
return errors.New("no peers")
|
|
|
|
}
|
|
|
|
if len(st.Peer) > 1 {
|
|
|
|
return fmt.Errorf("got %d peers; want 1", len(st.Peer))
|
|
|
|
}
|
|
|
|
peer := st.Peer[st.Peers()[0]]
|
|
|
|
if peer.ID == st.Self.ID {
|
|
|
|
return errors.New("peer is self")
|
|
|
|
}
|
2024-04-13 23:22:15 +00:00
|
|
|
|
|
|
|
if len(st.TailscaleIPs) == 0 {
|
|
|
|
return errors.New("no Tailscale IPs")
|
|
|
|
}
|
|
|
|
|
2021-05-12 21:43:43 +00:00
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d1.MustCleanShutdown(t)
|
|
|
|
d2.MustCleanShutdown(t)
|
|
|
|
}
|
|
|
|
|
2023-09-12 18:43:34 +00:00
|
|
|
// tests two nodes where the first gets a incremental MapResponse (with only
|
|
|
|
// PeersRemoved set) saying that the second node disappeared.
|
|
|
|
func TestIncrementalMapUpdatePeersRemoved(t *testing.T) {
|
2023-10-17 01:47:03 +00:00
|
|
|
tstest.Shard(t)
|
2023-10-17 03:55:28 +00:00
|
|
|
tstest.Parallel(t)
|
2023-09-12 18:43:34 +00:00
|
|
|
env := newTestEnv(t)
|
|
|
|
|
|
|
|
// Create one node:
|
|
|
|
n1 := newTestNode(t, env)
|
|
|
|
d1 := n1.StartDaemon()
|
|
|
|
n1.AwaitListening()
|
|
|
|
n1.MustUp()
|
|
|
|
n1.AwaitRunning()
|
|
|
|
|
|
|
|
all := env.Control.AllNodes()
|
|
|
|
if len(all) != 1 {
|
|
|
|
t.Fatalf("expected 1 node, got %d nodes", len(all))
|
|
|
|
}
|
|
|
|
tnode1 := all[0]
|
|
|
|
|
|
|
|
n2 := newTestNode(t, env)
|
|
|
|
d2 := n2.StartDaemon()
|
|
|
|
n2.AwaitListening()
|
|
|
|
n2.MustUp()
|
|
|
|
n2.AwaitRunning()
|
|
|
|
|
|
|
|
all = env.Control.AllNodes()
|
|
|
|
if len(all) != 2 {
|
|
|
|
t.Fatalf("expected 2 node, got %d nodes", len(all))
|
|
|
|
}
|
|
|
|
var tnode2 *tailcfg.Node
|
|
|
|
for _, n := range all {
|
|
|
|
if n.ID != tnode1.ID {
|
|
|
|
tnode2 = n
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if tnode2 == nil {
|
|
|
|
t.Fatalf("failed to find second node ID (two dups?)")
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("node1=%v, node2=%v", tnode1.ID, tnode2.ID)
|
|
|
|
|
|
|
|
if err := tstest.WaitFor(2*time.Second, func() error {
|
|
|
|
st := n1.MustStatus()
|
|
|
|
if len(st.Peer) == 0 {
|
|
|
|
return errors.New("no peers")
|
|
|
|
}
|
|
|
|
if len(st.Peer) > 1 {
|
|
|
|
return fmt.Errorf("got %d peers; want 1", len(st.Peer))
|
|
|
|
}
|
|
|
|
peer := st.Peer[st.Peers()[0]]
|
|
|
|
if peer.ID == st.Self.ID {
|
|
|
|
return errors.New("peer is self")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("node1 saw node2")
|
|
|
|
|
|
|
|
// Now tell node1 that node2 is removed.
|
|
|
|
if !env.Control.AddRawMapResponse(tnode1.Key, &tailcfg.MapResponse{
|
|
|
|
PeersRemoved: []tailcfg.NodeID{tnode2.ID},
|
|
|
|
}) {
|
|
|
|
t.Fatalf("failed to add map response")
|
|
|
|
}
|
|
|
|
|
|
|
|
// And see that node1 saw that.
|
|
|
|
if err := tstest.WaitFor(2*time.Second, func() error {
|
|
|
|
st := n1.MustStatus()
|
|
|
|
if len(st.Peer) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("got %d peers; want 0", len(st.Peer))
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("node1 saw node2 disappear")
|
|
|
|
|
|
|
|
d1.MustCleanShutdown(t)
|
|
|
|
d2.MustCleanShutdown(t)
|
|
|
|
}
|
|
|
|
|
2021-05-18 20:20:29 +00:00
|
|
|
func TestNodeAddressIPFields(t *testing.T) {
|
2023-10-17 01:47:03 +00:00
|
|
|
tstest.Shard(t)
|
2023-01-19 14:42:58 +00:00
|
|
|
flakytest.Mark(t, "https://github.com/tailscale/tailscale/issues/7008")
|
2023-10-17 03:55:28 +00:00
|
|
|
tstest.Parallel(t)
|
2021-07-20 20:55:09 +00:00
|
|
|
env := newTestEnv(t)
|
2021-05-18 20:20:29 +00:00
|
|
|
n1 := newTestNode(t, env)
|
2021-12-16 01:05:21 +00:00
|
|
|
d1 := n1.StartDaemon()
|
2021-05-18 20:20:29 +00:00
|
|
|
|
2021-12-16 01:05:21 +00:00
|
|
|
n1.AwaitListening()
|
2021-05-18 20:20:29 +00:00
|
|
|
n1.MustUp()
|
2021-12-16 01:05:21 +00:00
|
|
|
n1.AwaitRunning()
|
2021-05-18 20:20:29 +00:00
|
|
|
|
|
|
|
testNodes := env.Control.AllNodes()
|
|
|
|
|
|
|
|
if len(testNodes) != 1 {
|
|
|
|
t.Errorf("Expected %d nodes, got %d", 1, len(testNodes))
|
|
|
|
}
|
|
|
|
node := testNodes[0]
|
|
|
|
if len(node.Addresses) == 0 {
|
|
|
|
t.Errorf("Empty Addresses field in node")
|
|
|
|
}
|
|
|
|
if len(node.AllowedIPs) == 0 {
|
|
|
|
t.Errorf("Empty AllowedIPs field in node")
|
|
|
|
}
|
|
|
|
|
|
|
|
d1.MustCleanShutdown(t)
|
|
|
|
}
|
|
|
|
|
2021-06-02 15:23:24 +00:00
|
|
|
func TestAddPingRequest(t *testing.T) {
|
2023-10-17 01:47:03 +00:00
|
|
|
tstest.Shard(t)
|
2023-10-17 03:55:28 +00:00
|
|
|
tstest.Parallel(t)
|
2021-07-20 20:55:09 +00:00
|
|
|
env := newTestEnv(t)
|
2021-06-02 15:23:24 +00:00
|
|
|
n1 := newTestNode(t, env)
|
2021-12-16 01:05:21 +00:00
|
|
|
n1.StartDaemon()
|
2021-06-02 15:23:24 +00:00
|
|
|
|
2021-12-16 01:05:21 +00:00
|
|
|
n1.AwaitListening()
|
2021-06-02 15:23:24 +00:00
|
|
|
n1.MustUp()
|
2021-12-16 01:05:21 +00:00
|
|
|
n1.AwaitRunning()
|
2021-06-02 15:23:24 +00:00
|
|
|
|
|
|
|
gotPing := make(chan bool, 1)
|
|
|
|
waitPing := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
gotPing <- true
|
|
|
|
}))
|
|
|
|
defer waitPing.Close()
|
|
|
|
|
|
|
|
nodes := env.Control.AllNodes()
|
|
|
|
if len(nodes) != 1 {
|
|
|
|
t.Fatalf("expected 1 node, got %d nodes", len(nodes))
|
|
|
|
}
|
|
|
|
|
2021-11-02 03:55:52 +00:00
|
|
|
nodeKey := nodes[0].Key
|
2021-06-15 19:41:06 +00:00
|
|
|
|
|
|
|
// Check that we get at least one ping reply after 10 tries.
|
|
|
|
for try := 1; try <= 10; try++ {
|
|
|
|
t.Logf("ping %v ...", try)
|
2021-06-10 22:12:25 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
|
if err := env.Control.AwaitNodeInMapRequest(ctx, nodeKey); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
cancel()
|
2021-06-15 19:41:06 +00:00
|
|
|
|
|
|
|
pr := &tailcfg.PingRequest{URL: fmt.Sprintf("%s/ping-%d", waitPing.URL, try), Log: true}
|
|
|
|
if !env.Control.AddPingRequest(nodeKey, pr) {
|
|
|
|
t.Logf("failed to AddPingRequest")
|
|
|
|
continue
|
2021-06-10 22:12:25 +00:00
|
|
|
}
|
2021-06-02 15:23:24 +00:00
|
|
|
|
2021-06-10 22:12:25 +00:00
|
|
|
// Wait for PingRequest to come back
|
|
|
|
pingTimeout := time.NewTimer(2 * time.Second)
|
2021-06-15 19:41:06 +00:00
|
|
|
defer pingTimeout.Stop()
|
2021-06-10 22:12:25 +00:00
|
|
|
select {
|
|
|
|
case <-gotPing:
|
2021-06-15 19:41:06 +00:00
|
|
|
t.Logf("got ping; success")
|
|
|
|
return
|
2021-06-10 22:12:25 +00:00
|
|
|
case <-pingTimeout.C:
|
2021-06-15 19:41:06 +00:00
|
|
|
// Try again.
|
2021-06-10 22:12:25 +00:00
|
|
|
}
|
2021-06-02 15:23:24 +00:00
|
|
|
}
|
2021-06-15 19:41:06 +00:00
|
|
|
t.Error("all ping attempts failed")
|
2021-06-02 15:23:24 +00:00
|
|
|
}
|
|
|
|
|
2022-08-27 19:55:41 +00:00
|
|
|
func TestC2NPingRequest(t *testing.T) {
|
2023-10-17 01:47:03 +00:00
|
|
|
tstest.Shard(t)
|
2023-10-17 03:55:28 +00:00
|
|
|
tstest.Parallel(t)
|
2022-08-27 19:55:41 +00:00
|
|
|
|
2024-04-02 04:23:27 +00:00
|
|
|
env := newTestEnv(t)
|
2022-08-27 19:55:41 +00:00
|
|
|
|
|
|
|
gotPing := make(chan bool, 1)
|
2024-04-02 04:23:27 +00:00
|
|
|
env.Control.HandleC2N = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2022-08-27 19:55:41 +00:00
|
|
|
if r.Method != "POST" {
|
|
|
|
t.Errorf("unexpected ping method %q", r.Method)
|
|
|
|
}
|
|
|
|
got, err := io.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("ping body read error: %v", err)
|
|
|
|
}
|
|
|
|
const want = "HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Type: text/plain; charset=utf-8\r\n\r\nabc"
|
|
|
|
if string(got) != want {
|
|
|
|
t.Errorf("body error\n got: %q\nwant: %q", got, want)
|
|
|
|
}
|
|
|
|
gotPing <- true
|
2024-04-02 04:23:27 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
n1 := newTestNode(t, env)
|
|
|
|
n1.StartDaemon()
|
|
|
|
|
|
|
|
n1.AwaitListening()
|
|
|
|
n1.MustUp()
|
|
|
|
n1.AwaitRunning()
|
2022-08-27 19:55:41 +00:00
|
|
|
|
|
|
|
nodes := env.Control.AllNodes()
|
|
|
|
if len(nodes) != 1 {
|
|
|
|
t.Fatalf("expected 1 node, got %d nodes", len(nodes))
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeKey := nodes[0].Key
|
|
|
|
|
|
|
|
// Check that we get at least one ping reply after 10 tries.
|
|
|
|
for try := 1; try <= 10; try++ {
|
|
|
|
t.Logf("ping %v ...", try)
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
|
if err := env.Control.AwaitNodeInMapRequest(ctx, nodeKey); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
cancel()
|
|
|
|
|
|
|
|
pr := &tailcfg.PingRequest{
|
2024-04-02 04:23:27 +00:00
|
|
|
URL: fmt.Sprintf("https://unused/some-c2n-path/ping-%d", try),
|
2022-08-27 19:55:41 +00:00
|
|
|
Log: true,
|
|
|
|
Types: "c2n",
|
|
|
|
Payload: []byte("POST /echo HTTP/1.0\r\nContent-Length: 3\r\n\r\nabc"),
|
|
|
|
}
|
|
|
|
if !env.Control.AddPingRequest(nodeKey, pr) {
|
|
|
|
t.Logf("failed to AddPingRequest")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for PingRequest to come back
|
|
|
|
pingTimeout := time.NewTimer(2 * time.Second)
|
|
|
|
defer pingTimeout.Stop()
|
|
|
|
select {
|
|
|
|
case <-gotPing:
|
|
|
|
t.Logf("got ping; success")
|
|
|
|
return
|
|
|
|
case <-pingTimeout.C:
|
|
|
|
// Try again.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
t.Error("all ping attempts failed")
|
|
|
|
}
|
|
|
|
|
2021-07-16 22:21:00 +00:00
|
|
|
// Issue 2434: when "down" (WantRunning false), tailscaled shouldn't
|
|
|
|
// be connected to control.
|
2021-07-20 19:13:20 +00:00
|
|
|
func TestNoControlConnWhenDown(t *testing.T) {
|
2023-10-17 01:47:03 +00:00
|
|
|
tstest.Shard(t)
|
2023-10-17 03:55:28 +00:00
|
|
|
tstest.Parallel(t)
|
2021-07-20 20:55:09 +00:00
|
|
|
env := newTestEnv(t)
|
2021-07-16 22:21:00 +00:00
|
|
|
n1 := newTestNode(t, env)
|
|
|
|
|
2021-12-16 01:05:21 +00:00
|
|
|
d1 := n1.StartDaemon()
|
|
|
|
n1.AwaitResponding()
|
2021-07-16 22:21:00 +00:00
|
|
|
|
|
|
|
// Come up the first time.
|
|
|
|
n1.MustUp()
|
2023-10-03 18:55:06 +00:00
|
|
|
ip1 := n1.AwaitIP4()
|
2021-12-16 01:05:21 +00:00
|
|
|
n1.AwaitRunning()
|
2021-07-16 22:21:00 +00:00
|
|
|
|
|
|
|
// Then bring it down and stop the daemon.
|
|
|
|
n1.MustDown()
|
|
|
|
d1.MustCleanShutdown(t)
|
|
|
|
|
|
|
|
env.LogCatcher.Reset()
|
2021-12-16 01:05:21 +00:00
|
|
|
d2 := n1.StartDaemon()
|
|
|
|
n1.AwaitResponding()
|
2021-07-16 22:21:00 +00:00
|
|
|
|
2024-04-16 16:10:50 +00:00
|
|
|
n1.AwaitBackendState("Stopped")
|
2021-07-16 22:21:00 +00:00
|
|
|
|
2023-10-03 18:55:06 +00:00
|
|
|
ip2 := n1.AwaitIP4()
|
2021-07-16 22:21:00 +00:00
|
|
|
if ip1 != ip2 {
|
|
|
|
t.Errorf("IPs different: %q vs %q", ip1, ip2)
|
|
|
|
}
|
|
|
|
|
2024-04-16 16:10:50 +00:00
|
|
|
// The real test: verify our daemon doesn't have an HTTP request open.
|
2021-07-16 22:21:00 +00:00
|
|
|
if n := env.Control.InServeMap(); n != 0 {
|
|
|
|
t.Errorf("in serve map = %d; want 0", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
d2.MustCleanShutdown(t)
|
|
|
|
}
|
|
|
|
|
2021-07-19 18:07:42 +00:00
|
|
|
// Issue 2137: make sure Windows tailscaled works with the CLI alone,
|
|
|
|
// without the GUI to kick off a Start.
|
2021-07-20 19:13:20 +00:00
|
|
|
func TestOneNodeUpWindowsStyle(t *testing.T) {
|
2023-10-17 01:47:03 +00:00
|
|
|
tstest.Shard(t)
|
2023-10-17 03:55:28 +00:00
|
|
|
tstest.Parallel(t)
|
2021-07-20 20:55:09 +00:00
|
|
|
env := newTestEnv(t)
|
2021-07-19 18:07:42 +00:00
|
|
|
n1 := newTestNode(t, env)
|
|
|
|
n1.upFlagGOOS = "windows"
|
|
|
|
|
2021-12-16 01:05:21 +00:00
|
|
|
d1 := n1.StartDaemonAsIPNGOOS("windows")
|
|
|
|
n1.AwaitResponding()
|
2021-07-19 18:07:42 +00:00
|
|
|
n1.MustUp("--unattended")
|
|
|
|
|
2023-10-03 18:55:06 +00:00
|
|
|
t.Logf("Got IP: %v", n1.AwaitIP4())
|
2021-12-16 01:05:21 +00:00
|
|
|
n1.AwaitRunning()
|
2021-07-19 18:07:42 +00:00
|
|
|
|
|
|
|
d1.MustCleanShutdown(t)
|
|
|
|
}
|
|
|
|
|
2024-05-06 18:08:25 +00:00
|
|
|
// TestClientSideJailing tests that when one node is jailed for another, the
|
|
|
|
// jailed node cannot initiate connections to the other node however the other
|
|
|
|
// node can initiate connections to the jailed node.
|
|
|
|
func TestClientSideJailing(t *testing.T) {
|
|
|
|
tstest.Shard(t)
|
|
|
|
tstest.Parallel(t)
|
|
|
|
env := newTestEnv(t)
|
|
|
|
registerNode := func() (*testNode, key.NodePublic) {
|
|
|
|
n := newTestNode(t, env)
|
|
|
|
n.StartDaemon()
|
|
|
|
n.AwaitListening()
|
|
|
|
n.MustUp()
|
|
|
|
n.AwaitRunning()
|
|
|
|
k := n.MustStatus().Self.PublicKey
|
|
|
|
return n, k
|
|
|
|
}
|
|
|
|
n1, k1 := registerNode()
|
|
|
|
n2, k2 := registerNode()
|
|
|
|
|
|
|
|
ln, err := net.Listen("tcp", "localhost:0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer ln.Close()
|
|
|
|
port := uint16(ln.Addr().(*net.TCPAddr).Port)
|
|
|
|
|
|
|
|
lc1 := &tailscale.LocalClient{
|
|
|
|
Socket: n1.sockFile,
|
|
|
|
UseSocketOnly: true,
|
|
|
|
}
|
|
|
|
lc2 := &tailscale.LocalClient{
|
|
|
|
Socket: n2.sockFile,
|
|
|
|
UseSocketOnly: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
ip1 := n1.AwaitIP4()
|
|
|
|
ip2 := n2.AwaitIP4()
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
n1JailedForN2 bool
|
|
|
|
n2JailedForN1 bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "not_jailed",
|
|
|
|
n1JailedForN2: false,
|
|
|
|
n2JailedForN1: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "uni_jailed",
|
|
|
|
n1JailedForN2: true,
|
|
|
|
n2JailedForN1: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "bi_jailed", // useless config?
|
|
|
|
n1JailedForN2: true,
|
|
|
|
n2JailedForN1: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
testDial := func(t *testing.T, lc *tailscale.LocalClient, ip netip.Addr, port uint16, shouldFail bool) {
|
|
|
|
t.Helper()
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
|
|
|
defer cancel()
|
|
|
|
c, err := lc.DialTCP(ctx, ip.String(), port)
|
|
|
|
failed := err != nil
|
|
|
|
if failed != shouldFail {
|
|
|
|
t.Errorf("failed = %v; want %v", failed, shouldFail)
|
|
|
|
}
|
|
|
|
if c != nil {
|
|
|
|
c.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
b1, err := lc1.WatchIPNBus(context.Background(), 0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
b2, err := lc2.WatchIPNBus(context.Background(), 0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
waitPeerIsJailed := func(t *testing.T, b *tailscale.IPNBusWatcher, jailed bool) {
|
|
|
|
t.Helper()
|
|
|
|
for {
|
|
|
|
n, err := b.Next()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if n.NetMap == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if len(n.NetMap.Peers) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if j := n.NetMap.Peers[0].IsJailed(); j == jailed {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
env.Control.SetJailed(k1, k2, tc.n2JailedForN1)
|
|
|
|
env.Control.SetJailed(k2, k1, tc.n1JailedForN2)
|
|
|
|
|
|
|
|
// Wait for the jailed status to propagate.
|
|
|
|
waitPeerIsJailed(t, b1, tc.n2JailedForN1)
|
|
|
|
waitPeerIsJailed(t, b2, tc.n1JailedForN2)
|
|
|
|
|
|
|
|
testDial(t, lc1, ip2, port, tc.n1JailedForN2)
|
|
|
|
testDial(t, lc2, ip1, port, tc.n2JailedForN1)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-03 00:00:19 +00:00
|
|
|
// TestNATPing creates two nodes, n1 and n2, sets up masquerades for both and
|
|
|
|
// tries to do bi-directional pings between them.
|
|
|
|
func TestNATPing(t *testing.T) {
|
2024-08-03 21:54:25 +00:00
|
|
|
flakytest.Mark(t, "https://github.com/tailscale/tailscale/issues/12169")
|
2023-10-17 01:47:03 +00:00
|
|
|
tstest.Shard(t)
|
2023-10-17 03:55:28 +00:00
|
|
|
tstest.Parallel(t)
|
2023-10-03 18:55:06 +00:00
|
|
|
for _, v6 := range []bool{false, true} {
|
|
|
|
env := newTestEnv(t)
|
|
|
|
registerNode := func() (*testNode, key.NodePublic) {
|
|
|
|
n := newTestNode(t, env)
|
|
|
|
n.StartDaemon()
|
|
|
|
n.AwaitListening()
|
|
|
|
n.MustUp()
|
|
|
|
n.AwaitRunning()
|
|
|
|
k := n.MustStatus().Self.PublicKey
|
|
|
|
return n, k
|
|
|
|
}
|
|
|
|
n1, k1 := registerNode()
|
|
|
|
n2, k2 := registerNode()
|
|
|
|
|
|
|
|
var n1IP, n2IP netip.Addr
|
|
|
|
if v6 {
|
|
|
|
n1IP = n1.AwaitIP6()
|
|
|
|
n2IP = n2.AwaitIP6()
|
|
|
|
} else {
|
|
|
|
n1IP = n1.AwaitIP4()
|
|
|
|
n2IP = n2.AwaitIP4()
|
|
|
|
}
|
|
|
|
|
|
|
|
n1ExternalIP := netip.MustParseAddr("100.64.1.1")
|
|
|
|
n2ExternalIP := netip.MustParseAddr("100.64.2.1")
|
|
|
|
if v6 {
|
|
|
|
n1ExternalIP = netip.MustParseAddr("fd7a:115c:a1e0::1a")
|
|
|
|
n2ExternalIP = netip.MustParseAddr("fd7a:115c:a1e0::1b")
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
pairs []testcontrol.MasqueradePair
|
|
|
|
n1SeesN2IP netip.Addr
|
|
|
|
n2SeesN1IP netip.Addr
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "no_nat",
|
|
|
|
n1SeesN2IP: n2IP,
|
|
|
|
n2SeesN1IP: n1IP,
|
2023-03-03 00:00:19 +00:00
|
|
|
},
|
2023-10-03 18:55:06 +00:00
|
|
|
{
|
|
|
|
name: "n1_has_external_ip",
|
|
|
|
pairs: []testcontrol.MasqueradePair{
|
|
|
|
{
|
|
|
|
Node: k1,
|
|
|
|
Peer: k2,
|
|
|
|
NodeMasqueradesAs: n1ExternalIP,
|
|
|
|
},
|
2023-03-03 00:00:19 +00:00
|
|
|
},
|
2023-10-03 18:55:06 +00:00
|
|
|
n1SeesN2IP: n2IP,
|
|
|
|
n2SeesN1IP: n1ExternalIP,
|
2023-03-03 00:00:19 +00:00
|
|
|
},
|
2023-10-03 18:55:06 +00:00
|
|
|
{
|
|
|
|
name: "n2_has_external_ip",
|
|
|
|
pairs: []testcontrol.MasqueradePair{
|
|
|
|
{
|
|
|
|
Node: k2,
|
|
|
|
Peer: k1,
|
|
|
|
NodeMasqueradesAs: n2ExternalIP,
|
|
|
|
},
|
2023-03-03 00:00:19 +00:00
|
|
|
},
|
2023-10-03 18:55:06 +00:00
|
|
|
n1SeesN2IP: n2ExternalIP,
|
|
|
|
n2SeesN1IP: n1IP,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "both_have_external_ips",
|
|
|
|
pairs: []testcontrol.MasqueradePair{
|
|
|
|
{
|
|
|
|
Node: k1,
|
|
|
|
Peer: k2,
|
|
|
|
NodeMasqueradesAs: n1ExternalIP,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Node: k2,
|
|
|
|
Peer: k1,
|
|
|
|
NodeMasqueradesAs: n2ExternalIP,
|
|
|
|
},
|
2023-03-03 00:00:19 +00:00
|
|
|
},
|
2023-10-03 18:55:06 +00:00
|
|
|
n1SeesN2IP: n2ExternalIP,
|
|
|
|
n2SeesN1IP: n1ExternalIP,
|
2023-03-03 00:00:19 +00:00
|
|
|
},
|
2023-10-03 18:55:06 +00:00
|
|
|
}
|
2023-04-19 22:23:26 +00:00
|
|
|
|
2023-10-03 18:55:06 +00:00
|
|
|
for _, tc := range tests {
|
|
|
|
t.Run(fmt.Sprintf("v6=%t/%v", v6, tc.name), func(t *testing.T) {
|
|
|
|
env.Control.SetMasqueradeAddresses(tc.pairs)
|
|
|
|
|
|
|
|
ipIdx := 0
|
|
|
|
if v6 {
|
|
|
|
ipIdx = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
s1 := n1.MustStatus()
|
|
|
|
n2AsN1Peer := s1.Peer[k2]
|
|
|
|
if got := n2AsN1Peer.TailscaleIPs[ipIdx]; got != tc.n1SeesN2IP {
|
|
|
|
t.Fatalf("n1 sees n2 as %v; want %v", got, tc.n1SeesN2IP)
|
|
|
|
}
|
|
|
|
|
|
|
|
s2 := n2.MustStatus()
|
|
|
|
n1AsN2Peer := s2.Peer[k1]
|
|
|
|
if got := n1AsN2Peer.TailscaleIPs[ipIdx]; got != tc.n2SeesN1IP {
|
|
|
|
t.Fatalf("n2 sees n1 as %v; want %v", got, tc.n2SeesN1IP)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := n1.Tailscale("ping", tc.n1SeesN2IP.String()).Run(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := n1.Tailscale("ping", "-peerapi", tc.n1SeesN2IP.String()).Run(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := n2.Tailscale("ping", tc.n2SeesN1IP.String()).Run(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := n2.Tailscale("ping", "-peerapi", tc.n2SeesN1IP.String()).Run(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2023-03-03 00:00:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-15 23:55:02 +00:00
|
|
|
func TestLogoutRemovesAllPeers(t *testing.T) {
|
2023-10-17 01:47:03 +00:00
|
|
|
tstest.Shard(t)
|
2023-10-17 03:55:28 +00:00
|
|
|
tstest.Parallel(t)
|
2021-12-15 23:55:02 +00:00
|
|
|
env := newTestEnv(t)
|
|
|
|
// Spin up some nodes.
|
|
|
|
nodes := make([]*testNode, 2)
|
|
|
|
for i := range nodes {
|
|
|
|
nodes[i] = newTestNode(t, env)
|
2021-12-16 01:05:21 +00:00
|
|
|
nodes[i].StartDaemon()
|
|
|
|
nodes[i].AwaitResponding()
|
2021-12-15 23:55:02 +00:00
|
|
|
nodes[i].MustUp()
|
2023-10-03 18:55:06 +00:00
|
|
|
nodes[i].AwaitIP4()
|
2021-12-16 01:05:21 +00:00
|
|
|
nodes[i].AwaitRunning()
|
2021-12-15 23:55:02 +00:00
|
|
|
}
|
2022-11-12 12:39:29 +00:00
|
|
|
expectedPeers := len(nodes) - 1
|
2021-12-15 23:55:02 +00:00
|
|
|
|
|
|
|
// Make every node ping every other node.
|
|
|
|
// This makes sure magicsock is fully populated.
|
|
|
|
for i := range nodes {
|
|
|
|
for j := range nodes {
|
|
|
|
if i <= j {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := tstest.WaitFor(20*time.Second, func() error {
|
|
|
|
return nodes[i].Ping(nodes[j])
|
|
|
|
}); err != nil {
|
2023-10-03 18:55:06 +00:00
|
|
|
t.Fatalf("ping %v -> %v: %v", nodes[i].AwaitIP4(), nodes[j].AwaitIP4(), err)
|
2021-12-15 23:55:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// wantNode0PeerCount waits until node[0] status includes exactly want peers.
|
|
|
|
wantNode0PeerCount := func(want int) {
|
|
|
|
if err := tstest.WaitFor(20*time.Second, func() error {
|
2021-12-16 01:05:21 +00:00
|
|
|
s := nodes[0].MustStatus()
|
2021-12-15 23:55:02 +00:00
|
|
|
if peers := s.Peers(); len(peers) != want {
|
|
|
|
return fmt.Errorf("want %d peer(s) in status, got %v", want, peers)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-12 12:39:29 +00:00
|
|
|
wantNode0PeerCount(expectedPeers) // all other nodes are peers
|
2021-12-15 23:55:02 +00:00
|
|
|
nodes[0].MustLogOut()
|
|
|
|
wantNode0PeerCount(0) // node[0] is logged out, so it should not have any peers
|
2022-11-12 12:39:29 +00:00
|
|
|
|
|
|
|
nodes[0].MustUp() // This will create a new node
|
|
|
|
expectedPeers++
|
|
|
|
|
2023-10-03 18:55:06 +00:00
|
|
|
nodes[0].AwaitIP4()
|
2022-11-12 12:39:29 +00:00
|
|
|
wantNode0PeerCount(expectedPeers) // all existing peers and the new node
|
2021-12-15 23:55:02 +00:00
|
|
|
}
|
|
|
|
|
2024-01-08 17:32:18 +00:00
|
|
|
func TestAutoUpdateDefaults(t *testing.T) {
|
|
|
|
if !clientupdate.CanAutoUpdate() {
|
|
|
|
t.Skip("auto-updates not supported on this platform")
|
|
|
|
}
|
|
|
|
tstest.Shard(t)
|
|
|
|
tstest.Parallel(t)
|
|
|
|
env := newTestEnv(t)
|
|
|
|
|
|
|
|
checkDefault := func(n *testNode, want bool) error {
|
|
|
|
enabled, ok := n.diskPrefs().AutoUpdate.Apply.Get()
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("auto-update for node is unset, should be set as %v", want)
|
|
|
|
}
|
|
|
|
if enabled != want {
|
|
|
|
return fmt.Errorf("auto-update for node is %v, should be set as %v", enabled, want)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
sendAndCheckDefault := func(t *testing.T, n *testNode, send, want bool) {
|
|
|
|
t.Helper()
|
|
|
|
if !env.Control.AddRawMapResponse(n.MustStatus().Self.PublicKey, &tailcfg.MapResponse{
|
|
|
|
DefaultAutoUpdate: opt.NewBool(send),
|
|
|
|
}) {
|
|
|
|
t.Fatal("failed to send MapResponse to node")
|
|
|
|
}
|
|
|
|
if err := tstest.WaitFor(2*time.Second, func() error {
|
|
|
|
return checkDefault(n, want)
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
desc string
|
|
|
|
run func(t *testing.T, n *testNode)
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "tailnet-default-false",
|
|
|
|
run: func(t *testing.T, n *testNode) {
|
|
|
|
// First received default "false".
|
|
|
|
sendAndCheckDefault(t, n, false, false)
|
|
|
|
// Should not be changed even if sent "true" later.
|
|
|
|
sendAndCheckDefault(t, n, true, false)
|
|
|
|
// But can be changed explicitly by the user.
|
2024-05-07 04:33:37 +00:00
|
|
|
if out, err := n.TailscaleForOutput("set", "--auto-update").CombinedOutput(); err != nil {
|
2024-01-08 17:32:18 +00:00
|
|
|
t.Fatalf("failed to enable auto-update on node: %v\noutput: %s", err, out)
|
|
|
|
}
|
|
|
|
sendAndCheckDefault(t, n, false, true)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "tailnet-default-true",
|
|
|
|
run: func(t *testing.T, n *testNode) {
|
|
|
|
// First received default "true".
|
|
|
|
sendAndCheckDefault(t, n, true, true)
|
|
|
|
// Should not be changed even if sent "false" later.
|
|
|
|
sendAndCheckDefault(t, n, false, true)
|
|
|
|
// But can be changed explicitly by the user.
|
2024-05-07 04:33:37 +00:00
|
|
|
if out, err := n.TailscaleForOutput("set", "--auto-update=false").CombinedOutput(); err != nil {
|
2024-01-08 17:32:18 +00:00
|
|
|
t.Fatalf("failed to disable auto-update on node: %v\noutput: %s", err, out)
|
|
|
|
}
|
|
|
|
sendAndCheckDefault(t, n, true, false)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "user-sets-first",
|
|
|
|
run: func(t *testing.T, n *testNode) {
|
|
|
|
// User sets auto-update first, before receiving defaults.
|
2024-05-07 04:33:37 +00:00
|
|
|
if out, err := n.TailscaleForOutput("set", "--auto-update=false").CombinedOutput(); err != nil {
|
2024-01-08 17:32:18 +00:00
|
|
|
t.Fatalf("failed to disable auto-update on node: %v\noutput: %s", err, out)
|
|
|
|
}
|
|
|
|
// Defaults sent from control should be ignored.
|
|
|
|
sendAndCheckDefault(t, n, true, false)
|
|
|
|
sendAndCheckDefault(t, n, false, false)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.desc, func(t *testing.T) {
|
|
|
|
n := newTestNode(t, env)
|
|
|
|
d := n.StartDaemon()
|
|
|
|
defer d.MustCleanShutdown(t)
|
|
|
|
|
|
|
|
n.AwaitResponding()
|
|
|
|
n.MustUp()
|
|
|
|
n.AwaitRunning()
|
|
|
|
|
|
|
|
tt.run(t, n)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-23 23:40:18 +00:00
|
|
|
// TestDNSOverTCPIntervalResolver tests that the quad-100 resolver successfully
|
|
|
|
// serves TCP queries. It exercises the host's TCP stack, a TUN device, and
|
|
|
|
// gVisor/netstack.
|
|
|
|
// https://github.com/tailscale/corp/issues/22511
|
|
|
|
func TestDNSOverTCPIntervalResolver(t *testing.T) {
|
|
|
|
tstest.Shard(t)
|
|
|
|
if os.Getuid() != 0 {
|
|
|
|
t.Skip("skipping when not root")
|
|
|
|
}
|
|
|
|
env := newTestEnv(t)
|
|
|
|
env.tunMode = true
|
|
|
|
n1 := newTestNode(t, env)
|
|
|
|
d1 := n1.StartDaemon()
|
|
|
|
|
|
|
|
n1.AwaitResponding()
|
|
|
|
n1.MustUp()
|
|
|
|
|
|
|
|
wantIP4 := n1.AwaitIP4()
|
|
|
|
n1.AwaitRunning()
|
|
|
|
|
|
|
|
status, err := n1.Status()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to get node status: %v", err)
|
|
|
|
}
|
|
|
|
selfDNSName, err := dnsname.ToFQDN(status.Self.DNSName)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error converting self dns name to fqdn: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
network string
|
|
|
|
serviceAddr netip.Addr
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"tcp4",
|
|
|
|
tsaddr.TailscaleServiceIP(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"tcp6",
|
|
|
|
tsaddr.TailscaleServiceIPv6(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, c := range cases {
|
|
|
|
err = tstest.WaitFor(time.Second*5, func() error {
|
|
|
|
m := new(dns.Msg)
|
|
|
|
m.SetQuestion(selfDNSName.WithTrailingDot(), dns.TypeA)
|
|
|
|
conn, err := net.DialTimeout(c.network, net.JoinHostPort(c.serviceAddr.String(), "53"), time.Second*1)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
dnsConn := &dns.Conn{
|
|
|
|
Conn: conn,
|
|
|
|
}
|
|
|
|
dnsClient := &dns.Client{}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
|
|
|
defer cancel()
|
|
|
|
resp, _, err := dnsClient.ExchangeWithConnContext(ctx, m, dnsConn)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(resp.Answer) != 1 {
|
|
|
|
return fmt.Errorf("unexpected DNS resp: %s", resp)
|
|
|
|
}
|
|
|
|
var gotAddr net.IP
|
|
|
|
answer, ok := resp.Answer[0].(*dns.A)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("unexpected answer type: %s", resp.Answer[0])
|
|
|
|
}
|
|
|
|
gotAddr = answer.A
|
|
|
|
if !bytes.Equal(gotAddr, wantIP4.AsSlice()) {
|
|
|
|
return fmt.Errorf("got (%s) != want (%s)", gotAddr, wantIP4)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2024-08-23 23:58:31 +00:00
|
|
|
t.Fatal(err)
|
2024-08-23 23:40:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
d1.MustCleanShutdown(t)
|
|
|
|
}
|
|
|
|
|
2024-08-29 01:50:13 +00:00
|
|
|
// TestNetstackTCPLoopback tests netstack loopback of a TCP stream, in both
|
|
|
|
// directions.
|
|
|
|
func TestNetstackTCPLoopback(t *testing.T) {
|
|
|
|
tstest.Shard(t)
|
|
|
|
if os.Getuid() != 0 {
|
|
|
|
t.Skip("skipping when not root")
|
|
|
|
}
|
|
|
|
|
|
|
|
env := newTestEnv(t)
|
|
|
|
env.tunMode = true
|
2024-08-29 18:17:27 +00:00
|
|
|
loopbackPort := 5201
|
2024-08-29 01:50:13 +00:00
|
|
|
env.loopbackPort = &loopbackPort
|
2024-08-29 18:17:27 +00:00
|
|
|
loopbackPortStr := strconv.Itoa(loopbackPort)
|
2024-08-29 01:50:13 +00:00
|
|
|
n1 := newTestNode(t, env)
|
|
|
|
d1 := n1.StartDaemon()
|
|
|
|
|
|
|
|
n1.AwaitResponding()
|
|
|
|
n1.MustUp()
|
|
|
|
|
|
|
|
n1.AwaitIP4()
|
|
|
|
n1.AwaitRunning()
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
lisAddr string
|
|
|
|
network string
|
|
|
|
dialAddr string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
lisAddr: net.JoinHostPort("127.0.0.1", loopbackPortStr),
|
|
|
|
network: "tcp4",
|
|
|
|
dialAddr: net.JoinHostPort(tsaddr.TailscaleServiceIPString, loopbackPortStr),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
lisAddr: net.JoinHostPort("::1", loopbackPortStr),
|
|
|
|
network: "tcp6",
|
|
|
|
dialAddr: net.JoinHostPort(tsaddr.TailscaleServiceIPv6String, loopbackPortStr),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
writeBufSize := 128 << 10 // 128KiB, exercise GSO if enabled
|
|
|
|
writeBufIterations := 100 // allow TCP send window to open up
|
|
|
|
wantTotal := writeBufSize * writeBufIterations
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
lis, err := net.Listen(c.network, c.lisAddr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer lis.Close()
|
|
|
|
|
|
|
|
writeFn := func(conn net.Conn) error {
|
|
|
|
for i := 0; i < writeBufIterations; i++ {
|
|
|
|
toWrite := make([]byte, writeBufSize)
|
|
|
|
var wrote int
|
|
|
|
for {
|
|
|
|
n, err := conn.Write(toWrite)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
wrote += n
|
|
|
|
if wrote == len(toWrite) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
readFn := func(conn net.Conn) error {
|
|
|
|
var read int
|
|
|
|
for {
|
|
|
|
b := make([]byte, writeBufSize)
|
|
|
|
n, err := conn.Read(b)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
read += n
|
|
|
|
if read == wantTotal {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lisStepCh := make(chan error)
|
|
|
|
go func() {
|
|
|
|
conn, err := lis.Accept()
|
|
|
|
if err != nil {
|
|
|
|
lisStepCh <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
lisStepCh <- readFn(conn)
|
|
|
|
lisStepCh <- writeFn(conn)
|
|
|
|
}()
|
|
|
|
|
|
|
|
var conn net.Conn
|
|
|
|
err = tstest.WaitFor(time.Second*5, func() error {
|
|
|
|
conn, err = net.DialTimeout(c.network, c.dialAddr, time.Second*1)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
dialerStepCh := make(chan error)
|
|
|
|
go func() {
|
|
|
|
dialerStepCh <- writeFn(conn)
|
|
|
|
dialerStepCh <- readFn(conn)
|
|
|
|
}()
|
|
|
|
|
|
|
|
var (
|
|
|
|
dialerSteps int
|
|
|
|
lisSteps int
|
|
|
|
)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case lisErr := <-lisStepCh:
|
|
|
|
if lisErr != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
lisSteps++
|
|
|
|
if dialerSteps == 2 && lisSteps == 2 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case dialerErr := <-dialerStepCh:
|
|
|
|
if dialerErr != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
dialerSteps++
|
|
|
|
if dialerSteps == 2 && lisSteps == 2 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
d1.MustCleanShutdown(t)
|
|
|
|
}
|
|
|
|
|
2024-08-29 18:17:27 +00:00
|
|
|
// TestNetstackUDPLoopback tests netstack loopback of UDP packets, in both
|
|
|
|
// directions.
|
|
|
|
func TestNetstackUDPLoopback(t *testing.T) {
|
|
|
|
tstest.Shard(t)
|
|
|
|
if os.Getuid() != 0 {
|
|
|
|
t.Skip("skipping when not root")
|
|
|
|
}
|
|
|
|
|
|
|
|
env := newTestEnv(t)
|
|
|
|
env.tunMode = true
|
|
|
|
loopbackPort := 5201
|
|
|
|
env.loopbackPort = &loopbackPort
|
|
|
|
n1 := newTestNode(t, env)
|
|
|
|
d1 := n1.StartDaemon()
|
|
|
|
|
|
|
|
n1.AwaitResponding()
|
|
|
|
n1.MustUp()
|
|
|
|
|
|
|
|
ip4 := n1.AwaitIP4()
|
|
|
|
ip6 := n1.AwaitIP6()
|
|
|
|
n1.AwaitRunning()
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
pingerLAddr *net.UDPAddr
|
|
|
|
pongerLAddr *net.UDPAddr
|
|
|
|
network string
|
|
|
|
dialAddr *net.UDPAddr
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
pingerLAddr: &net.UDPAddr{IP: ip4.AsSlice(), Port: loopbackPort + 1},
|
|
|
|
pongerLAddr: &net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: loopbackPort},
|
|
|
|
network: "udp4",
|
|
|
|
dialAddr: &net.UDPAddr{IP: tsaddr.TailscaleServiceIP().AsSlice(), Port: loopbackPort},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
pingerLAddr: &net.UDPAddr{IP: ip6.AsSlice(), Port: loopbackPort + 1},
|
|
|
|
pongerLAddr: &net.UDPAddr{IP: net.ParseIP("::1"), Port: loopbackPort},
|
|
|
|
network: "udp6",
|
|
|
|
dialAddr: &net.UDPAddr{IP: tsaddr.TailscaleServiceIPv6().AsSlice(), Port: loopbackPort},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
writeBufSize := int(tstun.DefaultTUNMTU()) - 40 - 8 // mtu - ipv6 header - udp header
|
|
|
|
wantPongs := 100
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
pongerConn, err := net.ListenUDP(c.network, c.pongerLAddr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer pongerConn.Close()
|
|
|
|
|
|
|
|
var pingerConn *net.UDPConn
|
|
|
|
err = tstest.WaitFor(time.Second*5, func() error {
|
|
|
|
pingerConn, err = net.DialUDP(c.network, c.pingerLAddr, c.dialAddr)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer pingerConn.Close()
|
|
|
|
|
|
|
|
pingerFn := func(conn *net.UDPConn) error {
|
|
|
|
b := make([]byte, writeBufSize)
|
|
|
|
n, err := conn.Write(b)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if n != len(b) {
|
|
|
|
return fmt.Errorf("bad write size: %d", n)
|
|
|
|
}
|
|
|
|
err = conn.SetReadDeadline(time.Now().Add(time.Millisecond * 500))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
n, err = conn.Read(b)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if n != len(b) {
|
|
|
|
return fmt.Errorf("bad read size: %d", n)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
pongerFn := func(conn *net.UDPConn) error {
|
|
|
|
for {
|
|
|
|
b := make([]byte, writeBufSize)
|
|
|
|
n, from, err := conn.ReadFromUDP(b)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if n != len(b) {
|
|
|
|
return fmt.Errorf("bad read size: %d", n)
|
|
|
|
}
|
|
|
|
n, err = conn.WriteToUDP(b, from)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if n != len(b) {
|
|
|
|
return fmt.Errorf("bad write size: %d", n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pongerErrCh := make(chan error, 1)
|
|
|
|
go func() {
|
|
|
|
pongerErrCh <- pongerFn(pongerConn)
|
|
|
|
}()
|
|
|
|
|
|
|
|
err = tstest.WaitFor(time.Second*5, func() error {
|
|
|
|
err = pingerFn(pingerConn)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var pongsRX int
|
|
|
|
for {
|
|
|
|
pingerErrCh := make(chan error)
|
|
|
|
go func() {
|
|
|
|
pingerErrCh <- pingerFn(pingerConn)
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case err := <-pongerErrCh:
|
|
|
|
t.Fatal(err)
|
|
|
|
case err := <-pingerErrCh:
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pongsRX++
|
|
|
|
if pongsRX == wantPongs {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
d1.MustCleanShutdown(t)
|
|
|
|
}
|
|
|
|
|
2021-05-01 03:27:05 +00:00
|
|
|
// testEnv contains the test environment (set of servers) used by one
|
|
|
|
// or more nodes.
|
|
|
|
type testEnv struct {
|
2024-08-29 01:50:13 +00:00
|
|
|
t testing.TB
|
|
|
|
tunMode bool
|
|
|
|
cli string
|
|
|
|
daemon string
|
2024-08-29 18:17:27 +00:00
|
|
|
loopbackPort *int
|
2021-05-01 03:27:05 +00:00
|
|
|
|
2021-07-08 18:39:45 +00:00
|
|
|
LogCatcher *LogCatcher
|
2021-05-01 03:27:05 +00:00
|
|
|
LogCatcherServer *httptest.Server
|
|
|
|
|
|
|
|
Control *testcontrol.Server
|
|
|
|
ControlServer *httptest.Server
|
|
|
|
|
2021-05-03 21:22:18 +00:00
|
|
|
TrafficTrap *trafficTrap
|
|
|
|
TrafficTrapServer *httptest.Server
|
2021-05-01 03:27:05 +00:00
|
|
|
}
|
|
|
|
|
2024-05-07 04:33:37 +00:00
|
|
|
// controlURL returns e.ControlServer.URL, panicking if it's the empty string,
|
|
|
|
// which it should never be in tests.
|
|
|
|
func (e *testEnv) controlURL() string {
|
|
|
|
s := e.ControlServer.URL
|
|
|
|
if s == "" {
|
|
|
|
panic("control server not set")
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2021-06-22 17:24:05 +00:00
|
|
|
type testEnvOpt interface {
|
|
|
|
modifyTestEnv(*testEnv)
|
|
|
|
}
|
|
|
|
|
|
|
|
type configureControl func(*testcontrol.Server)
|
|
|
|
|
|
|
|
func (f configureControl) modifyTestEnv(te *testEnv) {
|
|
|
|
f(te.Control)
|
|
|
|
}
|
|
|
|
|
2021-12-01 18:24:25 +00:00
|
|
|
// newTestEnv starts a bunch of services and returns a new test environment.
|
|
|
|
// newTestEnv arranges for the environment's resources to be cleaned up on exit.
|
2021-07-20 20:55:09 +00:00
|
|
|
func newTestEnv(t testing.TB, opts ...testEnvOpt) *testEnv {
|
2021-05-12 04:57:25 +00:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.Skip("not tested/working on Windows yet")
|
|
|
|
}
|
2021-06-25 19:59:45 +00:00
|
|
|
derpMap := RunDERPAndSTUN(t, logger.Discard, "127.0.0.1")
|
2021-07-08 18:39:45 +00:00
|
|
|
logc := new(LogCatcher)
|
2021-05-03 17:49:45 +00:00
|
|
|
control := &testcontrol.Server{
|
|
|
|
DERPMap: derpMap,
|
2021-05-01 03:27:05 +00:00
|
|
|
}
|
2021-06-18 17:09:16 +00:00
|
|
|
control.HTTPTestServer = httptest.NewUnstartedServer(control)
|
2021-05-03 21:22:18 +00:00
|
|
|
trafficTrap := new(trafficTrap)
|
2021-05-03 17:49:45 +00:00
|
|
|
e := &testEnv{
|
2021-05-12 04:57:25 +00:00
|
|
|
t: t,
|
2021-07-20 20:55:09 +00:00
|
|
|
cli: TailscaleBinary(t),
|
|
|
|
daemon: TailscaledBinary(t),
|
2021-05-03 21:22:18 +00:00
|
|
|
LogCatcher: logc,
|
|
|
|
LogCatcherServer: httptest.NewServer(logc),
|
|
|
|
Control: control,
|
2021-06-18 17:09:16 +00:00
|
|
|
ControlServer: control.HTTPTestServer,
|
2021-05-03 21:22:18 +00:00
|
|
|
TrafficTrap: trafficTrap,
|
|
|
|
TrafficTrapServer: httptest.NewServer(trafficTrap),
|
2021-05-03 17:49:45 +00:00
|
|
|
}
|
2021-06-22 17:24:05 +00:00
|
|
|
for _, o := range opts {
|
|
|
|
o.modifyTestEnv(e)
|
|
|
|
}
|
|
|
|
control.HTTPTestServer.Start()
|
2021-12-01 18:24:25 +00:00
|
|
|
t.Cleanup(func() {
|
|
|
|
// Shut down e.
|
|
|
|
if err := e.TrafficTrap.Err(); err != nil {
|
|
|
|
e.t.Errorf("traffic trap: %v", err)
|
|
|
|
e.t.Logf("logs: %s", e.LogCatcher.logsString())
|
|
|
|
}
|
|
|
|
e.LogCatcherServer.Close()
|
|
|
|
e.TrafficTrapServer.Close()
|
|
|
|
e.ControlServer.Close()
|
|
|
|
})
|
2024-05-07 04:33:37 +00:00
|
|
|
t.Logf("control URL: %v", e.controlURL())
|
2021-05-03 17:49:45 +00:00
|
|
|
return e
|
2021-05-01 03:27:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// testNode is a machine with a tailscale & tailscaled.
|
|
|
|
// Currently, the test is simplistic and user==node==machine.
|
|
|
|
// That may grow complexity later to test more.
|
|
|
|
type testNode struct {
|
2024-05-06 21:08:28 +00:00
|
|
|
env *testEnv
|
|
|
|
tailscaledParser *nodeOutputParser
|
2021-05-01 03:27:05 +00:00
|
|
|
|
2021-07-19 18:07:42 +00:00
|
|
|
dir string // temp dir for sock & state
|
2023-10-16 19:15:03 +00:00
|
|
|
configFile string // or empty for none
|
2021-07-19 18:07:42 +00:00
|
|
|
sockFile string
|
|
|
|
stateFile string
|
|
|
|
upFlagGOOS string // if non-empty, sets TS_DEBUG_UP_FLAG_GOOS for cmd/tailscale CLI
|
2021-06-28 16:33:42 +00:00
|
|
|
|
|
|
|
mu sync.Mutex
|
|
|
|
onLogLine []func([]byte)
|
2021-05-01 03:27:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// newTestNode allocates a temp directory for a new test node.
|
|
|
|
// The node is not started automatically.
|
|
|
|
func newTestNode(t *testing.T, env *testEnv) *testNode {
|
|
|
|
dir := t.TempDir()
|
2021-07-20 19:13:20 +00:00
|
|
|
sockFile := filepath.Join(dir, "tailscale.sock")
|
|
|
|
if len(sockFile) >= 104 {
|
2023-09-12 20:37:51 +00:00
|
|
|
// Maximum length for a unix socket on darwin. Try something else.
|
|
|
|
sockFile = filepath.Join(os.TempDir(), rands.HexString(8)+".sock")
|
|
|
|
t.Cleanup(func() { os.Remove(sockFile) })
|
2021-07-20 19:13:20 +00:00
|
|
|
}
|
2023-10-13 19:29:28 +00:00
|
|
|
n := &testNode{
|
2021-05-01 03:27:05 +00:00
|
|
|
env: env,
|
|
|
|
dir: dir,
|
2021-07-20 19:13:20 +00:00
|
|
|
sockFile: sockFile,
|
2021-05-01 03:27:05 +00:00
|
|
|
stateFile: filepath.Join(dir, "tailscale.state"),
|
|
|
|
}
|
2023-10-13 19:29:28 +00:00
|
|
|
|
|
|
|
// Look for a data race. Once we see the start marker, start logging the rest.
|
|
|
|
var sawRace bool
|
2024-05-06 00:41:13 +00:00
|
|
|
var sawPanic bool
|
2023-10-13 19:29:28 +00:00
|
|
|
n.addLogLineHook(func(line []byte) {
|
2024-05-06 00:41:13 +00:00
|
|
|
lineB := mem.B(line)
|
|
|
|
if mem.Contains(lineB, mem.S("WARNING: DATA RACE")) {
|
2023-10-13 19:29:28 +00:00
|
|
|
sawRace = true
|
|
|
|
}
|
2024-05-06 00:41:13 +00:00
|
|
|
if mem.HasPrefix(lineB, mem.S("panic: ")) {
|
|
|
|
sawPanic = true
|
|
|
|
}
|
|
|
|
if sawRace || sawPanic {
|
2023-10-13 19:29:28 +00:00
|
|
|
t.Logf("%s", line)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return n
|
2021-05-01 03:27:05 +00:00
|
|
|
}
|
|
|
|
|
2021-12-16 01:05:21 +00:00
|
|
|
func (n *testNode) diskPrefs() *ipn.Prefs {
|
|
|
|
t := n.env.t
|
2021-07-13 21:03:05 +00:00
|
|
|
t.Helper()
|
2022-09-15 12:06:59 +00:00
|
|
|
if _, err := os.ReadFile(n.stateFile); err != nil {
|
2021-07-13 21:03:05 +00:00
|
|
|
t.Fatalf("reading prefs: %v", err)
|
|
|
|
}
|
2022-02-28 21:08:45 +00:00
|
|
|
fs, err := store.NewFileStore(nil, n.stateFile)
|
2021-07-13 21:03:05 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("reading prefs, NewFileStore: %v", err)
|
|
|
|
}
|
2022-11-09 05:58:10 +00:00
|
|
|
p, err := ipnlocal.ReadStartupPrefsForTest(t.Logf, fs)
|
2021-07-13 21:03:05 +00:00
|
|
|
if err != nil {
|
2022-11-09 05:58:10 +00:00
|
|
|
t.Fatalf("reading prefs, ReadDiskPrefsForTest: %v", err)
|
2021-07-13 21:03:05 +00:00
|
|
|
}
|
2022-11-09 05:58:10 +00:00
|
|
|
return p.AsStruct()
|
2021-07-13 21:03:05 +00:00
|
|
|
}
|
|
|
|
|
2021-07-16 22:21:00 +00:00
|
|
|
// AwaitResponding waits for n's tailscaled to be up enough to be
|
|
|
|
// responding, but doesn't wait for any particular state.
|
2021-12-16 01:05:21 +00:00
|
|
|
func (n *testNode) AwaitResponding() {
|
|
|
|
t := n.env.t
|
2021-07-16 22:21:00 +00:00
|
|
|
t.Helper()
|
2021-12-16 01:05:21 +00:00
|
|
|
n.AwaitListening()
|
2021-07-16 22:21:00 +00:00
|
|
|
|
2021-12-16 01:05:21 +00:00
|
|
|
st := n.MustStatus()
|
2021-07-16 22:21:00 +00:00
|
|
|
t.Logf("Status: %s", st.BackendState)
|
|
|
|
|
|
|
|
if err := tstest.WaitFor(20*time.Second, func() error {
|
|
|
|
const sub = `Program starting: `
|
|
|
|
if !n.env.LogCatcher.logsContains(mem.S(sub)) {
|
|
|
|
return fmt.Errorf("log catcher didn't see %#q; got %s", sub, n.env.LogCatcher.logsString())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-28 16:33:42 +00:00
|
|
|
// addLogLineHook registers a hook f to be called on each tailscaled
|
|
|
|
// log line output.
|
|
|
|
func (n *testNode) addLogLineHook(f func([]byte)) {
|
|
|
|
n.mu.Lock()
|
|
|
|
defer n.mu.Unlock()
|
|
|
|
n.onLogLine = append(n.onLogLine, f)
|
|
|
|
}
|
|
|
|
|
|
|
|
// socks5AddrChan returns a channel that receives the address (e.g. "localhost:23874")
|
|
|
|
// of the node's SOCKS5 listener, once started.
|
|
|
|
func (n *testNode) socks5AddrChan() <-chan string {
|
|
|
|
ch := make(chan string, 1)
|
|
|
|
n.addLogLineHook(func(line []byte) {
|
|
|
|
const sub = "SOCKS5 listening on "
|
|
|
|
i := mem.Index(mem.B(line), mem.S(sub))
|
|
|
|
if i == -1 {
|
|
|
|
return
|
|
|
|
}
|
2023-10-13 19:29:28 +00:00
|
|
|
addr := strings.TrimSpace(string(line)[i+len(sub):])
|
2021-06-28 16:33:42 +00:00
|
|
|
select {
|
|
|
|
case ch <- addr:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return ch
|
|
|
|
}
|
|
|
|
|
2021-12-16 01:05:21 +00:00
|
|
|
func (n *testNode) AwaitSocksAddr(ch <-chan string) string {
|
|
|
|
t := n.env.t
|
2021-06-28 16:33:42 +00:00
|
|
|
t.Helper()
|
|
|
|
timer := time.NewTimer(10 * time.Second)
|
|
|
|
defer timer.Stop()
|
|
|
|
select {
|
|
|
|
case v := <-ch:
|
|
|
|
return v
|
|
|
|
case <-timer.C:
|
|
|
|
t.Fatal("timeout waiting for node to log its SOCK5 listening address")
|
|
|
|
panic("unreachable")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// nodeOutputParser parses stderr of tailscaled processes, calling the
|
|
|
|
// per-line callbacks previously registered via
|
|
|
|
// testNode.addLogLineHook.
|
|
|
|
type nodeOutputParser struct {
|
2024-05-06 21:08:28 +00:00
|
|
|
allBuf bytes.Buffer
|
|
|
|
pendLineBuf bytes.Buffer
|
|
|
|
n *testNode
|
2021-06-28 16:33:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (op *nodeOutputParser) Write(p []byte) (n int, err error) {
|
2024-05-06 21:08:28 +00:00
|
|
|
tn := op.n
|
|
|
|
tn.mu.Lock()
|
|
|
|
defer tn.mu.Unlock()
|
|
|
|
|
|
|
|
op.allBuf.Write(p)
|
|
|
|
n, err = op.pendLineBuf.Write(p)
|
|
|
|
op.parseLinesLocked()
|
2021-06-28 16:33:42 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-05-06 21:08:28 +00:00
|
|
|
func (op *nodeOutputParser) parseLinesLocked() {
|
2021-06-28 16:33:42 +00:00
|
|
|
n := op.n
|
2024-05-06 21:08:28 +00:00
|
|
|
buf := op.pendLineBuf.Bytes()
|
2021-06-28 16:33:42 +00:00
|
|
|
for len(buf) > 0 {
|
|
|
|
nl := bytes.IndexByte(buf, '\n')
|
|
|
|
if nl == -1 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
line := buf[:nl+1]
|
|
|
|
buf = buf[nl+1:]
|
|
|
|
|
|
|
|
for _, f := range n.onLogLine {
|
2023-10-13 19:29:28 +00:00
|
|
|
f(line)
|
2021-06-28 16:33:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(buf) == 0 {
|
2024-05-06 21:08:28 +00:00
|
|
|
op.pendLineBuf.Reset()
|
2021-06-28 16:33:42 +00:00
|
|
|
} else {
|
2024-05-06 21:08:28 +00:00
|
|
|
io.CopyN(io.Discard, &op.pendLineBuf, int64(op.pendLineBuf.Len()-len(buf)))
|
2021-06-28 16:33:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-12 04:57:25 +00:00
|
|
|
type Daemon struct {
|
|
|
|
Process *os.Process
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Daemon) MustCleanShutdown(t testing.TB) {
|
|
|
|
d.Process.Signal(os.Interrupt)
|
|
|
|
ps, err := d.Process.Wait()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("tailscaled Wait: %v", err)
|
|
|
|
}
|
|
|
|
if ps.ExitCode() != 0 {
|
|
|
|
t.Errorf("tailscaled ExitCode = %d; want 0", ps.ExitCode())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-01 18:24:25 +00:00
|
|
|
// StartDaemon starts the node's tailscaled, failing if it fails to start.
|
|
|
|
// StartDaemon ensures that the process will exit when the test completes.
|
2021-12-16 01:05:21 +00:00
|
|
|
func (n *testNode) StartDaemon() *Daemon {
|
|
|
|
return n.StartDaemonAsIPNGOOS(runtime.GOOS)
|
2021-07-19 18:07:42 +00:00
|
|
|
}
|
|
|
|
|
2021-12-16 01:05:21 +00:00
|
|
|
func (n *testNode) StartDaemonAsIPNGOOS(ipnGOOS string) *Daemon {
|
|
|
|
t := n.env.t
|
2023-10-13 19:29:28 +00:00
|
|
|
cmd := exec.Command(n.env.daemon)
|
|
|
|
cmd.Args = append(cmd.Args,
|
2021-05-01 03:27:05 +00:00
|
|
|
"--state="+n.stateFile,
|
|
|
|
"--socket="+n.sockFile,
|
2021-06-28 16:33:42 +00:00
|
|
|
"--socks5-server=localhost:0",
|
2021-05-01 03:27:05 +00:00
|
|
|
)
|
2021-12-15 23:30:13 +00:00
|
|
|
if *verboseTailscaled {
|
|
|
|
cmd.Args = append(cmd.Args, "-verbose=2")
|
|
|
|
}
|
2023-10-13 19:29:28 +00:00
|
|
|
if !n.env.tunMode {
|
|
|
|
cmd.Args = append(cmd.Args,
|
|
|
|
"--tun=userspace-networking",
|
|
|
|
)
|
|
|
|
}
|
2023-10-16 19:15:03 +00:00
|
|
|
if n.configFile != "" {
|
|
|
|
cmd.Args = append(cmd.Args, "--config="+n.configFile)
|
|
|
|
}
|
2021-05-01 03:27:05 +00:00
|
|
|
cmd.Env = append(os.Environ(),
|
2022-08-27 19:55:41 +00:00
|
|
|
"TS_DEBUG_PERMIT_HTTP_C2N=1",
|
2021-05-01 03:27:05 +00:00
|
|
|
"TS_LOG_TARGET="+n.env.LogCatcherServer.URL,
|
2021-05-03 21:22:18 +00:00
|
|
|
"HTTP_PROXY="+n.env.TrafficTrapServer.URL,
|
|
|
|
"HTTPS_PROXY="+n.env.TrafficTrapServer.URL,
|
2022-11-23 23:40:47 +00:00
|
|
|
"TS_DEBUG_FAKE_GOOS="+ipnGOOS,
|
2021-07-20 21:10:11 +00:00
|
|
|
"TS_LOGS_DIR="+t.TempDir(),
|
2022-09-20 19:31:49 +00:00
|
|
|
"TS_NETCHECK_GENERATE_204_URL="+n.env.ControlServer.URL+"/generate_204",
|
2024-05-05 18:57:47 +00:00
|
|
|
"TS_ASSUME_NETWORK_UP_FOR_TEST=1", // don't pause control client in airplane mode (no wifi, etc)
|
2024-05-06 00:41:13 +00:00
|
|
|
"TS_PANIC_IF_HIT_MAIN_CONTROL=1",
|
2024-05-06 18:05:00 +00:00
|
|
|
"TS_DISABLE_PORTMAPPER=1", // shouldn't be needed; test is all localhost
|
2024-05-06 21:08:28 +00:00
|
|
|
"TS_DEBUG_LOG_RATE=all",
|
2021-05-01 03:27:05 +00:00
|
|
|
)
|
2024-08-29 01:50:13 +00:00
|
|
|
if n.env.loopbackPort != nil {
|
2024-08-29 18:17:27 +00:00
|
|
|
cmd.Env = append(cmd.Env, "TS_DEBUG_NETSTACK_LOOPBACK_PORT="+strconv.Itoa(*n.env.loopbackPort))
|
2024-08-29 01:50:13 +00:00
|
|
|
}
|
2023-10-14 02:22:36 +00:00
|
|
|
if version.IsRace() {
|
2023-10-13 19:29:28 +00:00
|
|
|
cmd.Env = append(cmd.Env, "GORACE=halt_on_error=1")
|
2023-10-14 02:22:36 +00:00
|
|
|
}
|
2024-05-06 21:08:28 +00:00
|
|
|
n.tailscaledParser = &nodeOutputParser{n: n}
|
|
|
|
cmd.Stderr = n.tailscaledParser
|
2021-06-10 22:12:25 +00:00
|
|
|
if *verboseTailscaled {
|
|
|
|
cmd.Stdout = os.Stdout
|
2021-06-28 16:33:42 +00:00
|
|
|
cmd.Stderr = io.MultiWriter(cmd.Stderr, os.Stderr)
|
2021-06-10 22:12:25 +00:00
|
|
|
}
|
2023-09-01 19:19:54 +00:00
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
pr, pw, err := os.Pipe()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
t.Cleanup(func() { pw.Close() })
|
|
|
|
cmd.ExtraFiles = append(cmd.ExtraFiles, pr)
|
|
|
|
cmd.Env = append(cmd.Env, "TS_PARENT_DEATH_FD=3")
|
|
|
|
}
|
2021-05-01 03:27:05 +00:00
|
|
|
if err := cmd.Start(); err != nil {
|
|
|
|
t.Fatalf("starting tailscaled: %v", err)
|
|
|
|
}
|
2021-12-01 18:24:25 +00:00
|
|
|
t.Cleanup(func() { cmd.Process.Kill() })
|
2021-05-12 04:57:25 +00:00
|
|
|
return &Daemon{
|
|
|
|
Process: cmd.Process,
|
|
|
|
}
|
2021-05-01 03:27:05 +00:00
|
|
|
}
|
|
|
|
|
2021-07-19 18:07:42 +00:00
|
|
|
func (n *testNode) MustUp(extraArgs ...string) {
|
2021-05-12 21:43:43 +00:00
|
|
|
t := n.env.t
|
2023-09-01 00:44:13 +00:00
|
|
|
t.Helper()
|
2021-07-19 18:07:42 +00:00
|
|
|
args := []string{
|
|
|
|
"up",
|
2024-05-07 04:33:37 +00:00
|
|
|
"--login-server=" + n.env.controlURL(),
|
2023-09-01 00:44:13 +00:00
|
|
|
"--reset",
|
2021-07-19 18:07:42 +00:00
|
|
|
}
|
|
|
|
args = append(args, extraArgs...)
|
2021-12-03 22:29:54 +00:00
|
|
|
cmd := n.Tailscale(args...)
|
|
|
|
t.Logf("Running %v ...", cmd)
|
|
|
|
cmd.Stdout = nil // in case --verbose-tailscale was set
|
|
|
|
cmd.Stderr = nil // in case --verbose-tailscale was set
|
|
|
|
if b, err := cmd.CombinedOutput(); err != nil {
|
2021-10-26 17:19:35 +00:00
|
|
|
t.Fatalf("up: %v, %v", string(b), err)
|
2021-05-12 21:43:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-13 21:03:05 +00:00
|
|
|
func (n *testNode) MustDown() {
|
|
|
|
t := n.env.t
|
|
|
|
t.Logf("Running down ...")
|
2022-11-18 19:14:16 +00:00
|
|
|
if err := n.Tailscale("down", "--accept-risk=all").Run(); err != nil {
|
2021-07-13 21:03:05 +00:00
|
|
|
t.Fatalf("down: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-15 23:55:02 +00:00
|
|
|
func (n *testNode) MustLogOut() {
|
|
|
|
t := n.env.t
|
|
|
|
t.Logf("Running logout ...")
|
|
|
|
if err := n.Tailscale("logout").Run(); err != nil {
|
|
|
|
t.Fatalf("logout: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *testNode) Ping(otherNode *testNode) error {
|
|
|
|
t := n.env.t
|
2023-10-03 18:55:06 +00:00
|
|
|
ip := otherNode.AwaitIP4().String()
|
|
|
|
t.Logf("Running ping %v (from %v)...", ip, n.AwaitIP4())
|
2021-12-15 23:55:02 +00:00
|
|
|
return n.Tailscale("ping", ip).Run()
|
|
|
|
}
|
|
|
|
|
2021-05-03 17:49:45 +00:00
|
|
|
// AwaitListening waits for the tailscaled to be serving local clients
|
|
|
|
// over its localhost IPC mechanism. (Unix socket, etc)
|
2021-12-16 01:05:21 +00:00
|
|
|
func (n *testNode) AwaitListening() {
|
|
|
|
t := n.env.t
|
2021-05-03 17:49:45 +00:00
|
|
|
if err := tstest.WaitFor(20*time.Second, func() (err error) {
|
2024-06-11 02:38:10 +00:00
|
|
|
c, err := safesocket.ConnectContext(context.Background(), n.sockFile)
|
2023-10-13 19:29:28 +00:00
|
|
|
if err == nil {
|
|
|
|
c.Close()
|
2021-05-03 17:49:45 +00:00
|
|
|
}
|
2023-10-13 19:29:28 +00:00
|
|
|
return err
|
2021-05-03 17:49:45 +00:00
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
func (n *testNode) AwaitIPs() []netip.Addr {
|
2021-12-16 01:05:21 +00:00
|
|
|
t := n.env.t
|
2021-05-12 04:57:25 +00:00
|
|
|
t.Helper()
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
var addrs []netip.Addr
|
2021-05-12 04:57:25 +00:00
|
|
|
if err := tstest.WaitFor(20*time.Second, func() error {
|
2021-07-19 18:07:42 +00:00
|
|
|
cmd := n.Tailscale("ip")
|
|
|
|
cmd.Stdout = nil // in case --verbose-tailscale was set
|
|
|
|
cmd.Stderr = nil // in case --verbose-tailscale was set
|
|
|
|
out, err := cmd.Output()
|
2021-05-12 04:57:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-07-09 15:51:30 +00:00
|
|
|
ips := string(out)
|
|
|
|
ipslice := strings.Fields(ips)
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
addrs = make([]netip.Addr, len(ipslice))
|
2021-07-09 15:51:30 +00:00
|
|
|
|
|
|
|
for i, ip := range ipslice {
|
2022-07-26 03:55:44 +00:00
|
|
|
netIP, err := netip.ParseAddr(ip)
|
2021-07-09 15:51:30 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
addrs[i] = netIP
|
|
|
|
}
|
2021-05-12 04:57:25 +00:00
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatalf("awaiting an IP address: %v", err)
|
|
|
|
}
|
2021-07-09 15:51:30 +00:00
|
|
|
if len(addrs) == 0 {
|
2021-05-12 04:57:25 +00:00
|
|
|
t.Fatalf("returned IP address was blank")
|
|
|
|
}
|
2021-07-09 15:51:30 +00:00
|
|
|
return addrs
|
|
|
|
}
|
|
|
|
|
2023-10-03 18:55:06 +00:00
|
|
|
// AwaitIP4 returns the IPv4 address of n.
|
|
|
|
func (n *testNode) AwaitIP4() netip.Addr {
|
2021-12-16 01:05:21 +00:00
|
|
|
t := n.env.t
|
2021-07-09 15:51:30 +00:00
|
|
|
t.Helper()
|
2021-12-16 01:05:21 +00:00
|
|
|
ips := n.AwaitIPs()
|
2021-07-09 15:51:30 +00:00
|
|
|
return ips[0]
|
2021-05-12 04:57:25 +00:00
|
|
|
}
|
|
|
|
|
2023-10-03 18:55:06 +00:00
|
|
|
// AwaitIP6 returns the IPv6 address of n.
|
|
|
|
func (n *testNode) AwaitIP6() netip.Addr {
|
|
|
|
t := n.env.t
|
|
|
|
t.Helper()
|
|
|
|
ips := n.AwaitIPs()
|
|
|
|
return ips[1]
|
|
|
|
}
|
|
|
|
|
2021-07-16 22:21:00 +00:00
|
|
|
// AwaitRunning waits for n to reach the IPN state "Running".
|
2021-12-16 01:05:21 +00:00
|
|
|
func (n *testNode) AwaitRunning() {
|
2024-04-16 16:10:50 +00:00
|
|
|
n.AwaitBackendState("Running")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *testNode) AwaitBackendState(state string) {
|
2021-12-16 01:05:21 +00:00
|
|
|
t := n.env.t
|
2021-05-12 04:57:25 +00:00
|
|
|
t.Helper()
|
|
|
|
if err := tstest.WaitFor(20*time.Second, func() error {
|
|
|
|
st, err := n.Status()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-04-16 16:10:50 +00:00
|
|
|
if st.BackendState != state {
|
|
|
|
return fmt.Errorf("in state %q; want %q", st.BackendState, state)
|
2021-05-12 04:57:25 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatalf("failure/timeout waiting for transition to Running status: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-29 00:44:18 +00:00
|
|
|
// AwaitNeedsLogin waits for n to reach the IPN state "NeedsLogin".
|
2021-12-16 01:05:21 +00:00
|
|
|
func (n *testNode) AwaitNeedsLogin() {
|
|
|
|
t := n.env.t
|
2021-10-29 00:44:18 +00:00
|
|
|
t.Helper()
|
|
|
|
if err := tstest.WaitFor(20*time.Second, func() error {
|
|
|
|
st, err := n.Status()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if st.BackendState != "NeedsLogin" {
|
|
|
|
return fmt.Errorf("in state %q", st.BackendState)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatalf("failure/timeout waiting for transition to NeedsLogin status: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-07 04:33:37 +00:00
|
|
|
func (n *testNode) TailscaleForOutput(arg ...string) *exec.Cmd {
|
|
|
|
cmd := n.Tailscale(arg...)
|
|
|
|
cmd.Stdout = nil
|
|
|
|
cmd.Stderr = nil
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2021-05-01 03:27:05 +00:00
|
|
|
// Tailscale returns a command that runs the tailscale CLI with the provided arguments.
|
|
|
|
// It does not start the process.
|
|
|
|
func (n *testNode) Tailscale(arg ...string) *exec.Cmd {
|
2023-10-13 19:29:28 +00:00
|
|
|
cmd := exec.Command(n.env.cli)
|
|
|
|
cmd.Args = append(cmd.Args, "--socket="+n.sockFile)
|
2021-05-01 03:27:05 +00:00
|
|
|
cmd.Args = append(cmd.Args, arg...)
|
|
|
|
cmd.Dir = n.dir
|
2021-07-19 18:07:42 +00:00
|
|
|
cmd.Env = append(os.Environ(),
|
|
|
|
"TS_DEBUG_UP_FLAG_GOOS="+n.upFlagGOOS,
|
2021-07-20 21:10:11 +00:00
|
|
|
"TS_LOGS_DIR="+n.env.t.TempDir(),
|
2021-07-19 18:07:42 +00:00
|
|
|
)
|
|
|
|
if *verboseTailscale {
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
}
|
2021-05-01 03:27:05 +00:00
|
|
|
return cmd
|
2021-04-29 21:44:08 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 04:57:25 +00:00
|
|
|
func (n *testNode) Status() (*ipnstate.Status, error) {
|
2021-07-19 18:07:42 +00:00
|
|
|
cmd := n.Tailscale("status", "--json")
|
|
|
|
cmd.Stdout = nil // in case --verbose-tailscale was set
|
|
|
|
cmd.Stderr = nil // in case --verbose-tailscale was set
|
|
|
|
out, err := cmd.CombinedOutput()
|
2021-05-03 21:22:18 +00:00
|
|
|
if err != nil {
|
2021-05-12 04:57:25 +00:00
|
|
|
return nil, fmt.Errorf("running tailscale status: %v, %s", err, out)
|
2021-05-03 21:22:18 +00:00
|
|
|
}
|
|
|
|
st := new(ipnstate.Status)
|
|
|
|
if err := json.Unmarshal(out, st); err != nil {
|
2021-05-12 04:57:25 +00:00
|
|
|
return nil, fmt.Errorf("decoding tailscale status JSON: %w", err)
|
|
|
|
}
|
|
|
|
return st, nil
|
|
|
|
}
|
|
|
|
|
2021-12-16 01:05:21 +00:00
|
|
|
func (n *testNode) MustStatus() *ipnstate.Status {
|
|
|
|
tb := n.env.t
|
2021-05-12 04:57:25 +00:00
|
|
|
tb.Helper()
|
|
|
|
st, err := n.Status()
|
|
|
|
if err != nil {
|
|
|
|
tb.Fatal(err)
|
2021-05-03 21:22:18 +00:00
|
|
|
}
|
|
|
|
return st
|
|
|
|
}
|
|
|
|
|
|
|
|
// trafficTrap is an HTTP proxy handler to note whether any
|
2021-05-03 17:49:45 +00:00
|
|
|
// HTTP traffic tries to leave localhost from tailscaled. We don't
|
|
|
|
// expect any, so any request triggers a failure.
|
2021-05-03 21:22:18 +00:00
|
|
|
type trafficTrap struct {
|
2022-08-04 17:43:49 +00:00
|
|
|
atomicErr syncs.AtomicValue[error]
|
2021-05-03 21:22:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (tt *trafficTrap) Err() error {
|
2022-08-04 17:43:49 +00:00
|
|
|
return tt.atomicErr.Load()
|
2021-05-03 21:22:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (tt *trafficTrap) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2021-05-01 03:27:05 +00:00
|
|
|
var got bytes.Buffer
|
|
|
|
r.Write(&got)
|
2024-05-06 00:41:13 +00:00
|
|
|
err := fmt.Errorf("unexpected HTTP request via proxy: %s", got.Bytes())
|
2021-05-03 17:49:45 +00:00
|
|
|
mainError.Store(err)
|
2021-05-03 21:22:18 +00:00
|
|
|
if tt.Err() == nil {
|
|
|
|
// Best effort at remembering the first request.
|
|
|
|
tt.atomicErr.Store(err)
|
|
|
|
}
|
2021-05-03 17:49:45 +00:00
|
|
|
log.Printf("Error: %v", err)
|
2021-05-03 21:22:18 +00:00
|
|
|
w.WriteHeader(403)
|
2021-05-03 17:49:45 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 04:57:25 +00:00
|
|
|
type authURLParserWriter struct {
|
|
|
|
buf bytes.Buffer
|
|
|
|
fn func(urlStr string) error
|
|
|
|
}
|
|
|
|
|
|
|
|
var authURLRx = regexp.MustCompile(`(https?://\S+/auth/\S+)`)
|
|
|
|
|
|
|
|
func (w *authURLParserWriter) Write(p []byte) (n int, err error) {
|
|
|
|
n, err = w.buf.Write(p)
|
|
|
|
m := authURLRx.FindSubmatch(w.buf.Bytes())
|
|
|
|
if m != nil {
|
|
|
|
urlStr := string(m[1])
|
|
|
|
w.buf.Reset() // so it's not matched again
|
|
|
|
if err := w.fn(urlStr); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return n, err
|
|
|
|
}
|