Change-Id: I228007b4f361a2b63766689f09b1932f86955d0b
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
Brad Fitzpatrick 2024-08-07 14:44:35 -07:00
parent 602adde5dc
commit 583f1b73c5
3 changed files with 90 additions and 33 deletions

View File

@ -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) {

View File

@ -17,6 +17,7 @@
"testing"
"time"
"golang.org/x/sync/errgroup"
"tailscale.com/ipn/ipnstate"
"tailscale.com/tstest/natlab/vnet"
)
@ -141,40 +142,36 @@ 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} {
i, c := i, c
eg.Go(func() error {
st, err := status(ctx, c)
if err != nil {
t.Fatalf("node%d status: %v", i, err)
return fmt.Errorf("node%d status: %w", i, err)
}
t.Logf("XXX node%d status: %v", i, st)
t.Logf("node%d status: %v", i, st)
if err := up(ctx, c); err != nil {
t.Fatalf("node%d up: %v", i, err)
return fmt.Errorf("node%d up: %w", i, err)
}
t.Logf("XXX node%d up!", i)
}
t.Logf("both up1")
var sts []*ipnstate.Status
for i, c := range []*http.Client{c1, c2} {
st, err := status(ctx, c)
t.Logf("node%d up!", i)
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)
return fmt.Errorf("node%d status: %w", i, err)
}
sts[i] = st
t.Logf("both up2")
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
})
}
if err := eg.Wait(); err != nil {
t.Fatalf("initial setup: %v", err)
}
route, err := ping(ctx, c1, sts[1].Self.TailscaleIPs[0].String())
t.Logf("ping route: %v, %v", route, err)

View File

@ -1373,7 +1373,6 @@ 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),
}
}