Skip to content
Merged
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
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,21 @@ 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` | 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 `{}`.

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
Expand Down
6 changes: 5 additions & 1 deletion cmd/qcat/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
65 changes: 65 additions & 0 deletions cmd/qcat/status.go
Original file line number Diff line number Diff line change
@@ -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
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.LastWrite.IsZero() && 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
}
104 changes: 104 additions & 0 deletions cmd/qcat/status_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
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, "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"
}}
}`
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",
"Path": `"derp"`, "CurAddr": `""`,
"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,"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,"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 {
t.Fatal(err)
}
for peer, want := range map[string]string{
"lab": "direct", "mini": "derp", "udp-relay": "peer-relay",
"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)
}
}
}

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)
}
}
Loading