diff --git a/cmd/tta/tta.go b/cmd/tta/tta.go index d33526031..ed5892e76 100644 --- a/cmd/tta/tta.go +++ b/cmd/tta/tta.go @@ -11,6 +11,7 @@ package main import ( + "bufio" "bytes" "errors" "flag" @@ -23,10 +24,12 @@ "net/url" "os" "os/exec" + "regexp" "strings" "sync" "time" + "github.com/mitchellh/go-ps" "tailscale.com/client/tailscale" "tailscale.com/util/must" "tailscale.com/util/set" @@ -45,11 +48,15 @@ func absify(cmd string) string { } func serveCmd(w http.ResponseWriter, cmd string, args ...string) { + log.Printf("Got serveCmd for %q %v", cmd, args) out, err := exec.Command(absify(cmd), args...).CombinedOutput() w.Header().Set("Content-Type", "text/plain; charset=utf-8") if err != nil { w.Header().Set("Exec-Err", err.Error()) w.WriteHeader(500) + log.Printf("Err on serveCmd for %q %v, %d bytes of output: %v", cmd, args, len(out), err) + } else { + log.Printf("Did serveCmd for %q %v, %d bytes of output", cmd, args, len(out)) } w.Write(out) } @@ -74,6 +81,16 @@ func main() { } flag.Parse() + if distro.Get() == distro.Gokrazy { + nsRx := regexp.MustCompile(`(?m)^nameserver (.*)`) + for t := time.Now(); time.Since(t) < 10*time.Second; time.Sleep(10 * time.Millisecond) { + all, _ := os.ReadFile("/etc/resolv.conf") + if nsRx.Match(all) { + break + } + } + } + logc, err := net.Dial("tcp", "9.9.9.9:124") if err == nil { log.SetOutput(logc) @@ -81,6 +98,34 @@ func main() { log.Printf("Tailscale Test Agent running.") + if distro.Get() == distro.Gokrazy { + procs, err := ps.Processes() + if err != nil { + log.Fatalf("ps.Processes: %v", err) + } + killed := false + for _, p := range procs { + if p.Executable() == "tailscaled" { + if op, err := os.FindProcess(p.Pid()); err == nil { + op.Signal(os.Interrupt) + killed = true + } + } + } + log.Printf("killed = %v", killed) + if killed { + for { + _, err := exec.Command(absify("tailscale"), "status", "--json").CombinedOutput() + if err == nil { + log.Printf("tailscaled back up") + break + } + log.Printf("tailscale status error; sleeping before trying again...") + time.Sleep(50 * time.Millisecond) + } + } + } + var mux http.ServeMux var hs http.Server hs.Handler = &mux @@ -117,6 +162,22 @@ func main() { return }) mux.HandleFunc("/up", func(w http.ResponseWriter, r *http.Request) { + cmd := exec.Command(absify("tailscale"), "debug", "daemon-logs") + out, err := cmd.StdoutPipe() + if err != nil { + http.Error(w, err.Error(), 500) + return + } + defer out.Close() + cmd.Start() + defer cmd.Process.Kill() + go func() { + bs := bufio.NewScanner(out) + for bs.Scan() { + log.Printf("Daemon: %s", bs.Text()) + } + }() + serveCmd(w, "tailscale", "up", "--login-server=http://control.tailscale") }) mux.HandleFunc("/status", func(w http.ResponseWriter, r *http.Request) { diff --git a/tstest/integration/nat/nat_test.go b/tstest/integration/nat/nat_test.go index d5c43083d..3717012f4 100644 --- a/tstest/integration/nat/nat_test.go +++ b/tstest/integration/nat/nat_test.go @@ -17,6 +17,7 @@ "testing" "time" + "golang.org/x/sync/errgroup" "tailscale.com/ipn/ipnstate" "tailscale.com/tstest/natlab/vnet" ) @@ -141,41 +142,37 @@ func (nt *natTest) runTest(node1, node2 addNodeFunc) { c1 := &http.Client{Transport: nt.vnet.NodeAgentRoundTripper(nodes[0])} c2 := &http.Client{Transport: nt.vnet.NodeAgentRoundTripper(nodes[1])} - // var lc1 tailscale.LocalClient - // lc1.Dial = nt.vnet.NodeAgentDialer(nodes[0]) - //st, err := lc1.Status(ctx) - + var eg errgroup.Group + var sts [2]*ipnstate.Status for i, c := range []*http.Client{c1, c2} { - st, err := status(ctx, c) - if err != nil { - t.Fatalf("node%d status: %v", i, err) - } - t.Logf("XXX node%d status: %v", i, st) - if err := up(ctx, c); err != nil { - t.Fatalf("node%d up: %v", i, err) - } - t.Logf("XXX node%d up!", i) + i, c := i, c + eg.Go(func() error { + st, err := status(ctx, c) + if err != nil { + return fmt.Errorf("node%d status: %w", i, err) + } + t.Logf("node%d status: %v", i, st) + if err := up(ctx, c); err != nil { + return fmt.Errorf("node%d up: %w", i, err) + } + t.Logf("node%d up!", i) + st, err = status(ctx, c) + if err != nil { + return fmt.Errorf("node%d status: %w", i, err) + } + sts[i] = st + + if st.BackendState != "Running" { + return fmt.Errorf("node%d state = %q", i, st.BackendState) + } + t.Logf("node%d up with %v", i, sts[i].Self.TailscaleIPs) + return nil + }) } - - t.Logf("both up1") - - var sts []*ipnstate.Status - for i, c := range []*http.Client{c1, c2} { - st, err := status(ctx, c) - if err != nil { - t.Fatalf("node%d status second time: %v", i, err) - } - if st.BackendState != "Running" { - t.Fatalf("node%d state = %q", i, st.BackendState) - } - if len(st.Peer) != 1 { - t.Fatalf("node%d peer count = %d; want 1", i, len(st.Peer)) - } - sts = append(sts, st) + if err := eg.Wait(); err != nil { + t.Fatalf("initial setup: %v", err) } - t.Logf("both up2") - route, err := ping(ctx, c1, sts[1].Self.TailscaleIPs[0].String()) t.Logf("ping route: %v, %v", route, err) } diff --git a/tstest/natlab/vnet/vnet.go b/tstest/natlab/vnet/vnet.go index 1601addaf..e3332ae3f 100644 --- a/tstest/natlab/vnet/vnet.go +++ b/tstest/natlab/vnet/vnet.go @@ -1373,8 +1373,7 @@ func (s *Server) NodeAgentDialer(n *Node) DialFunc { func (s *Server) NodeAgentRoundTripper(n *Node) http.RoundTripper { return &http.Transport{ - DisableKeepAlives: true, // XXX - DialContext: s.NodeAgentDialer(n), + DialContext: s.NodeAgentDialer(n), } }