Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions tailcat.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
51 changes: 35 additions & 16 deletions tailcat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()

Expand All @@ -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
Expand Down