From 17181ddf65d161db34929f925230e04ee454690b Mon Sep 17 00:00:00 2001 From: Erik Karulf <67336+ekarulf@users.noreply.github.com> Date: Thu, 3 Dec 2026 07:14:08 -0600 Subject: [PATCH] tailcat: expose server peer status through Client.Status Reuse the existing status collector to report the direct or relayed path to the server without sending a probe. Check peer visibility at both ends and client status during shutdown. Signed-off-by: Erik Karulf <67336+ekarulf@users.noreply.github.com> --- tailcat.go | 17 +++++++++++++++++ tailcat_test.go | 51 +++++++++++++++++++++++++++++++++---------------- 2 files changed, 52 insertions(+), 16 deletions(-) diff --git a/tailcat.go b/tailcat.go index 7ec393c20..a89a1fb8c 100644 --- a/tailcat.go +++ b/tailcat.go @@ -2277,6 +2277,23 @@ func (c *Client) DERPRegion() *tailcfg.DERPRegion { return c.ci.Region[0] } +// Status returns the current WireGuard and DERP connection status. The server's +// Peer entry reports its direct or relayed path, as in [Server.Status]. +// It returns nil before the client has started or after it has closed. +func (c *Client) Status() *ipnstate.Status { + c.startMu.Lock() + defer c.startMu.Unlock() + if !c.started { + return nil + } + select { + case <-c.lb.sys.Engine.Get().Done(): + return nil + default: + return c.lb.Status() + } +} + // Close shuts down the client, closing the WireGuard engine and DERP connections. func (c *Client) Close() error { // Don't hold startMu while closing the backend, so a slow diff --git a/tailcat_test.go b/tailcat_test.go index b42779e10..b53264637 100644 --- a/tailcat_test.go +++ b/tailcat_test.go @@ -34,6 +34,7 @@ import ( "gvisor.dev/gvisor/pkg/tcpip/stack" "gvisor.dev/gvisor/pkg/tcpip/transport/tcp" "tailscale.com/envknob" + "tailscale.com/ipn/ipnstate" "tailscale.com/net/netcheck" "tailscale.com/net/netmon" "tailscale.com/syncs" @@ -360,7 +361,7 @@ func TestTailcat(t *testing.T) { } -// TestStatusReportsPeers checks that Server.Status reports connected clients. +// TestStatusReportsPeers checks that each end reports its connected peer. func TestStatusReportsPeers(t *testing.T) { t.Parallel() @@ -379,28 +380,46 @@ func TestStatusReportsPeers(t *testing.T) { c := &Client{Server: s.TailcatAddr(), Logf: mkLogger(t, "client")} t.Cleanup(func() { c.Close() }) + if got := c.Status(); got != nil { + t.Fatalf("Status before start = %v, want nil", got) + } allow.Add(c.PublicKey()) // A successful ping means the server has fully added us as a peer. PingForTest(t, s, c) - st := s.Status() - ps, ok := st.Peer[c.PublicKey()] - if !ok { - t.Fatalf("Status().Peer has no entry for client %v; got %d peer(s)", c.PublicKey(), len(st.Peer)) - } - // The path may still be settling, so wait for CurAddr or Relay to show up. - deadline := time.Now().Add(10 * time.Second) - for ps.CurAddr == "" && ps.Relay == "" { - if time.Now().After(deadline) { - t.Fatalf("peer status has neither CurAddr nor Relay: %v", logger.AsJSON(ps)) - } - time.Sleep(10 * time.Millisecond) - if ps, ok = s.Status().Peer[c.PublicKey()]; !ok { - t.Fatalf("client %v disappeared from Status().Peer", c.PublicKey()) + checkPeer := func(status func() *ipnstate.Status, peer key.NodePublic) { + t.Helper() + deadline := time.Now().Add(10 * time.Second) + for { + st := status() + if st == nil || st.Peer[peer] == nil { + t.Fatalf("Status missing peer %v: %v", peer, st) + } + ps := st.Peer[peer] + if ps.CurAddr != "" || ps.Relay != "" { + return + } + // The path may still be settling. + if time.Now().After(deadline) { + t.Fatalf("peer status has neither CurAddr nor Relay: %v", logger.AsJSON(ps)) + } + time.Sleep(10 * time.Millisecond) } } - t.Logf("peer %v: CurAddr=%q Relay=%q", c.PublicKey(), ps.CurAddr, ps.Relay) + checkPeer(s.Status, c.PublicKey()) + checkPeer(c.Status, s.Key.Public()) + + // A status poll may overlap shutdown. + closed := make(chan error, 1) + go func() { closed <- c.Close() }() + c.Status() + if err := <-closed; err != nil { + t.Fatal(err) + } + if got := c.Status(); got != nil { + t.Fatalf("Status after close = %v, want nil", got) + } } // TestNetmonFallback checks that a Server and Client still work when the