From 80eee8faac724cda024f942a22ff67a25e8cb294 Mon Sep 17 00:00:00 2001 From: Erik Karulf Date: Thu, 3 Dec 2026 20:13:02 -0600 Subject: [PATCH 1/2] Report per-peer transport paths in qcat status --- README.md | 14 ++++++ cmd/qcat/main.go | 6 ++- cmd/qcat/status.go | 65 +++++++++++++++++++++++++ cmd/qcat/status_test.go | 102 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 186 insertions(+), 1 deletion(-) create mode 100644 cmd/qcat/status.go create mode 100644 cmd/qcat/status_test.go diff --git a/README.md b/README.md index d83abe4..7ad4ca1 100644 --- a/README.md +++ b/README.md @@ -309,6 +309,20 @@ qcat down outpost qcat down serve ``` +`qcat status` prints JSON keyed by running session. Each entry in a session's `Peer` map +has a `Path` field for the tunnel's current send path. + +| Path | Meaning | +| --- | --- | +| `direct` | qcat has selected the UDP endpoint in `CurAddr`. | +| `derp` | The peer is active and qcat selects the DERP fallback. It may also try UDP during path discovery. | +| `peer-relay` | The transport reports a selected UDP peer relay in `PeerRelay`. | +| `unknown` | The snapshot has too little information to identify a path, for example before the first send. | + +`Relay` identifies the peer's home DERP region even when its path is direct. +`LastHandshake` records the last successful WireGuard handshake. A session with no +running backend reports `null`. If no sessions are running, the result is `{}`. + Client connections periodically re-authenticate with a fresh signed ML-KEM exchange and refresh paths after sleep or network changes. Renewal requires the bootstrap DERP relay even when application traffic is direct. If the relay stays unavailable through the one-hour server lease, the server diff --git a/cmd/qcat/main.go b/cmd/qcat/main.go index 156d07d..54d8a79 100644 --- a/cmd/qcat/main.go +++ b/cmd/qcat/main.go @@ -127,7 +127,11 @@ func run(ctx context.Context, args []string) error { if err != nil { return err } - return emit(status) + sessions, err := summarizeStatus(status) + if err != nil { + return err + } + return emit(sessions) case "down": if len(args) > 2 { return errors.New("down takes an optional peer or runtime name") diff --git a/cmd/qcat/status.go b/cmd/qcat/status.go new file mode 100644 index 0000000..6255a09 --- /dev/null +++ b/cmd/qcat/status.go @@ -0,0 +1,65 @@ +package main + +import ( + "encoding/json" + "fmt" + "time" +) + +// The status command reads raw Tailscale status from the local control sockets, +// including sockets owned by daemons started before this CLI was installed. +type sessionStatus struct { + Self *struct { + Addrs []string + Relay string `json:",omitempty"` + } + Peer map[string]*peerStatus +} + +type peerStatus struct { + PublicKey string + Path string + CurAddr string `json:",omitempty"` + Relay string `json:",omitempty"` + PeerRelay string `json:",omitempty"` + Active bool + RxBytes int64 + TxBytes int64 + LastWrite time.Time `json:",omitzero"` + LastHandshake time.Time `json:",omitzero"` + InMagicSock bool + InEngine bool +} + +func (p *peerStatus) transportPath() string { + switch { + case p.CurAddr != "": + return "direct" + case p.PeerRelay != "": + return "peer-relay" + case p.InMagicSock && p.Active && p.Relay != "": + // Magicsock selects DERP here and may also try a direct UDP candidate. + return "derp" + default: + return "unknown" + } +} + +func summarizeStatus(responses map[string]json.RawMessage) (map[string]*sessionStatus, error) { + sessions := make(map[string]*sessionStatus, len(responses)) + for name, raw := range responses { + var session *sessionStatus + if err := json.Unmarshal(raw, &session); err != nil { + return nil, fmt.Errorf("%s: invalid session status: %w", name, err) + } + if session != nil { + for _, peer := range session.Peer { + if peer != nil { + peer.Path = peer.transportPath() + } + } + } + sessions[name] = session + } + return sessions, nil +} diff --git a/cmd/qcat/status_test.go b/cmd/qcat/status_test.go new file mode 100644 index 0000000..102cd71 --- /dev/null +++ b/cmd/qcat/status_test.go @@ -0,0 +1,102 @@ +package main + +import ( + "encoding/json" + "strings" + "testing" +) + +func TestSummarizeStatusLegacyResponse(t *testing.T) { + const peerKey = "nodekey:0000000000000000000000000000000000000000000000000000000000000001" + const input = `{ + "BackendState": "", "TUN": false, + "Self": {"Online": false, "Addrs": ["192.0.2.1:1000"], "Relay": "1"}, + "Peer": {"` + peerKey + `": { + "PublicKey": "` + peerKey + `", "Online": false, "InNetworkMap": false, "InMagicSock": true, + "Active": false, "RxBytes": 0, "TxBytes": 9007199254740993, + "LastWrite": "2026-12-03T19:00:00Z", "LastHandshake": "2026-12-03T19:00:01Z", + "LastSeen": "0001-01-01T00:00:00Z" + }} + }` + summary, err := summarizeStatus(map[string]json.RawMessage{"mosh-lab": json.RawMessage(input)}) + if err != nil { + t.Fatal(err) + } + data, err := json.Marshal(summary) + if err != nil { + t.Fatal(err) + } + for _, unwanted := range []string{"BackendState", "TUN", "Online", "InNetworkMap", "LastSeen", "0001-01-01"} { + if strings.Contains(string(data), unwanted) { + t.Errorf("unexpected %q in %s", unwanted, data) + } + } + var decoded map[string]struct { + Self map[string]json.RawMessage + Peer map[string]map[string]json.RawMessage + } + if err := json.Unmarshal(data, &decoded); err != nil { + t.Fatal(err) + } + session := decoded["mosh-lab"] + if got := string(session.Self["Addrs"]); got != `["192.0.2.1:1000"]` { + t.Errorf("Self.Addrs = %s", got) + } + peer := session.Peer[peerKey] + for field, want := range map[string]string{ + "PublicKey": `"` + peerKey + `"`, "InMagicSock": "true", "InEngine": "false", + "Active": "false", "RxBytes": "0", "TxBytes": "9007199254740993", + "LastWrite": `"2026-12-03T19:00:00Z"`, "LastHandshake": `"2026-12-03T19:00:01Z"`, + } { + if got := string(peer[field]); got != want { + t.Errorf("peer %s = %q, want %q", field, got, want) + } + } +} + +func TestSummarizeStatusPaths(t *testing.T) { + const raw = `{"Peer":{ + "lab":{"Active":true,"InMagicSock":true,"CurAddr":"192.0.2.2:2000","Relay":"1"}, + "mini":{"Active":true,"InMagicSock":true,"Relay":"1"}, + "udp-relay":{"Active":true,"InMagicSock":true,"PeerRelay":"192.0.2.3:3000","Relay":"1"}, + "idle":{"Active":false,"InMagicSock":true,"Relay":"1"}, + "no-relay":{"Active":true,"InMagicSock":true}, + "engine-only":{"InEngine":true,"Relay":"1"} + }}` + summary, err := summarizeStatus(map[string]json.RawMessage{"serve": json.RawMessage(raw)}) + if err != nil { + t.Fatal(err) + } + for peer, want := range map[string]string{ + "lab": "direct", "mini": "derp", "udp-relay": "peer-relay", + "idle": "unknown", "no-relay": "unknown", "engine-only": "unknown", + } { + if got := summary["serve"].Peer[peer].Path; got != want { + t.Errorf("%s path = %q, want %q", peer, got, want) + } + } +} + +func TestSummarizeStatusEmptyAndInvalid(t *testing.T) { + for _, tt := range []struct { + input map[string]json.RawMessage + want string + }{ + {map[string]json.RawMessage{}, "{}"}, + {map[string]json.RawMessage{"serve": json.RawMessage(`{"Peer":{},"Self":null}`)}, `{"serve":{"Self":null,"Peer":{}}}`}, + {map[string]json.RawMessage{"closed": json.RawMessage("null")}, `{"closed":null}`}, + } { + summary, err := summarizeStatus(tt.input) + if err != nil { + t.Fatal(err) + } + got, err := json.Marshal(summary) + if err != nil || string(got) != tt.want { + t.Errorf("marshaled status = %s, %v; want %s", got, err, tt.want) + } + } + _, err := summarizeStatus(map[string]json.RawMessage{"bad-runtime": json.RawMessage(`{"Peer":42}`)}) + if err == nil || !strings.Contains(err.Error(), "bad-runtime") { + t.Fatalf("expected a status error naming bad-runtime, got %v", err) + } +} From f1a96b7d8bb796b3979d55a1752103f2ec112602 Mon Sep 17 00:00:00 2001 From: Erik Karulf Date: Thu, 3 Dec 2026 20:15:08 -0600 Subject: [PATCH 2/2] Report idle DERP fallback and preserve the CurAddr field --- README.md | 3 ++- cmd/qcat/status.go | 4 ++-- cmd/qcat/status_test.go | 14 ++++++++------ 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 7ad4ca1..f7280d5 100644 --- a/README.md +++ b/README.md @@ -315,10 +315,11 @@ has a `Path` field for the tunnel's current send path. | Path | Meaning | | --- | --- | | `direct` | qcat has selected the UDP endpoint in `CurAddr`. | -| `derp` | The peer is active and qcat selects the DERP fallback. It may also try UDP during path discovery. | +| `derp` | qcat selects the DERP fallback for the next send, even if the peer is idle. It may also try UDP during path discovery. | | `peer-relay` | The transport reports a selected UDP peer relay in `PeerRelay`. | | `unknown` | The snapshot has too little information to identify a path, for example before the first send. | +`CurAddr` is an empty string when there is no selected direct endpoint. `Relay` identifies the peer's home DERP region even when its path is direct. `LastHandshake` records the last successful WireGuard handshake. A session with no running backend reports `null`. If no sessions are running, the result is `{}`. diff --git a/cmd/qcat/status.go b/cmd/qcat/status.go index 6255a09..6c815b9 100644 --- a/cmd/qcat/status.go +++ b/cmd/qcat/status.go @@ -19,7 +19,7 @@ type sessionStatus struct { type peerStatus struct { PublicKey string Path string - CurAddr string `json:",omitempty"` + CurAddr string Relay string `json:",omitempty"` PeerRelay string `json:",omitempty"` Active bool @@ -37,7 +37,7 @@ func (p *peerStatus) transportPath() string { return "direct" case p.PeerRelay != "": return "peer-relay" - case p.InMagicSock && p.Active && p.Relay != "": + case p.InMagicSock && !p.LastWrite.IsZero() && p.Relay != "": // Magicsock selects DERP here and may also try a direct UDP candidate. return "derp" default: diff --git a/cmd/qcat/status_test.go b/cmd/qcat/status_test.go index 102cd71..4568341 100644 --- a/cmd/qcat/status_test.go +++ b/cmd/qcat/status_test.go @@ -13,7 +13,7 @@ func TestSummarizeStatusLegacyResponse(t *testing.T) { "Self": {"Online": false, "Addrs": ["192.0.2.1:1000"], "Relay": "1"}, "Peer": {"` + peerKey + `": { "PublicKey": "` + peerKey + `", "Online": false, "InNetworkMap": false, "InMagicSock": true, - "Active": false, "RxBytes": 0, "TxBytes": 9007199254740993, + "Active": false, "Relay": "1", "CurAddr": "", "RxBytes": 0, "TxBytes": 9007199254740993, "LastWrite": "2026-12-03T19:00:00Z", "LastHandshake": "2026-12-03T19:00:01Z", "LastSeen": "0001-01-01T00:00:00Z" }} @@ -45,6 +45,7 @@ func TestSummarizeStatusLegacyResponse(t *testing.T) { peer := session.Peer[peerKey] for field, want := range map[string]string{ "PublicKey": `"` + peerKey + `"`, "InMagicSock": "true", "InEngine": "false", + "Path": `"derp"`, "CurAddr": `""`, "Active": "false", "RxBytes": "0", "TxBytes": "9007199254740993", "LastWrite": `"2026-12-03T19:00:00Z"`, "LastHandshake": `"2026-12-03T19:00:01Z"`, } { @@ -57,11 +58,12 @@ func TestSummarizeStatusLegacyResponse(t *testing.T) { func TestSummarizeStatusPaths(t *testing.T) { const raw = `{"Peer":{ "lab":{"Active":true,"InMagicSock":true,"CurAddr":"192.0.2.2:2000","Relay":"1"}, - "mini":{"Active":true,"InMagicSock":true,"Relay":"1"}, + "mini":{"Active":true,"LastWrite":"2026-12-03T19:00:00Z","InMagicSock":true,"Relay":"1"}, "udp-relay":{"Active":true,"InMagicSock":true,"PeerRelay":"192.0.2.3:3000","Relay":"1"}, - "idle":{"Active":false,"InMagicSock":true,"Relay":"1"}, - "no-relay":{"Active":true,"InMagicSock":true}, - "engine-only":{"InEngine":true,"Relay":"1"} + "idle":{"Active":false,"LastWrite":"2026-12-03T19:00:00Z","InMagicSock":true,"Relay":"1"}, + "never-sent":{"LastWrite":"0001-01-01T00:00:00Z","InMagicSock":true,"Relay":"1"}, + "no-relay":{"LastWrite":"2026-12-03T19:00:00Z","InMagicSock":true}, + "engine-only":{"InEngine":true,"LastWrite":"2026-12-03T19:00:00Z","Relay":"1"} }}` summary, err := summarizeStatus(map[string]json.RawMessage{"serve": json.RawMessage(raw)}) if err != nil { @@ -69,7 +71,7 @@ func TestSummarizeStatusPaths(t *testing.T) { } for peer, want := range map[string]string{ "lab": "direct", "mini": "derp", "udp-relay": "peer-relay", - "idle": "unknown", "no-relay": "unknown", "engine-only": "unknown", + "idle": "derp", "never-sent": "unknown", "no-relay": "unknown", "engine-only": "unknown", } { if got := summary["serve"].Peer[peer].Path; got != want { t.Errorf("%s path = %q, want %q", peer, got, want)