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
23 changes: 12 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ module github.com/ekarulf/quantumcat
go 1.27.4

require (
github.com/tailscale/tailcat v0.7.1-0.20261105151022-e00cdde6ff78
github.com/tailscale/tailcat v0.8.1-0.20261202233757-9245129f1aff
go4.org/mem v0.0.0-20240501181205-ae6ca9944745
tailscale.com v1.105.0-pre.0.20261013225543-fb79793d99f5
tailscale.com v1.107.0-pre.0.20261201224323-cd4f62139953
)

require (
filippo.io/edwards25519 v1.2.0 // indirect
github.com/akutz/memconn v0.1.0 // indirect
github.com/alexbrainman/sspi v0.0.0-20250919150558-7d374ff0d59e // indirect
github.com/axiomhq/hyperloglog v0.3.0 // indirect
github.com/coder/websocket v1.8.15 // indirect
github.com/creachadair/msync v0.10.1 // indirect
Expand All @@ -28,7 +29,7 @@ require (
github.com/huin/goupnp v1.3.0 // indirect
github.com/jsimonetti/rtnetlink v1.4.2 // indirect
github.com/kamstrup/intmap v0.5.2 // indirect
github.com/klauspost/compress v1.20.1 // indirect
github.com/klauspost/compress v1.20.2 // indirect
github.com/mdlayher/netlink v1.11.2 // indirect
github.com/mdlayher/socket v0.7.0 // indirect
github.com/mitchellh/go-ps v1.0.0 // indirect
Expand All @@ -39,20 +40,20 @@ require (
github.com/tailscale/hujson v0.0.0-20260727124030-b80ff77dac4f // indirect
github.com/tailscale/peercred v0.0.0-20250107143737-35a0c7bd7edc // indirect
github.com/tailscale/web-client-prebuilt v0.0.0-20260917222731-e0ed2d0d0fea // indirect
github.com/tailscale/wireguard-go v0.0.0-20260928213353-e61b6b78110b // indirect
github.com/tailscale/wireguard-go v0.0.0-20261125191149-d74d4ff62061 // indirect
github.com/x448/float16 v0.8.4 // indirect
go4.org/netipx v0.0.0-20260823151212-3075585bcbeb // indirect
golang.org/x/crypto v0.57.0 // indirect
golang.org/x/crypto v0.59.0 // indirect
golang.org/x/exp v0.0.0-20261007192929-f45ad48fbe92 // indirect
golang.org/x/net v0.60.0 // indirect
golang.org/x/sync v0.23.0 // indirect
golang.org/x/sys v0.48.0 // indirect
golang.org/x/term v0.46.0 // indirect
golang.org/x/text v0.42.0 // indirect
golang.org/x/net v0.62.0 // indirect
golang.org/x/sync v0.25.0 // indirect
golang.org/x/sys v0.50.0 // indirect
golang.org/x/term v0.48.0 // indirect
golang.org/x/text v0.44.0 // indirect
golang.org/x/time v0.16.0 // indirect
golang.zx2c4.com/wintun v0.0.0-20230126152724-0fa3db229ce2 // indirect
golang.zx2c4.com/wireguard/windows v1.1.1 // indirect
gvisor.dev/gvisor v0.0.0-20260915211658-a6f909f08a72 // indirect
gvisor.dev/gvisor v0.0.0-20261129202619-2c60c87d5cf0 // indirect
)

replace github.com/tailscale/tailcat => ./third_party/tailcat
118 changes: 58 additions & 60 deletions go.sum

Large diffs are not rendered by default.

13 changes: 7 additions & 6 deletions third_party/tailcat/QUANTUMCAT.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
# Upstream provenance

Networking library subset from github.com/tailscale/tailcat main,
commit e00cdde6ff787f801361ff85ed085775c775d4c5. BSD-3-Clause license retained.
commit 9245129f1affd5b4b838e36ea0a2276bf83fd4c4. BSD-3-Clause license retained.
Optional SSH/file-server and CLI code is omitted.

The update from 52fbad323e9d to e00cdde6ff78 changes upstream dependency and
build metadata only; no retained upstream Go source changed. Both local module
manifests use its coordinated Tailscale/WireGuard versions, including the
Tailscale netstack shutdown fix. The reduced mirror does not carry upstream's
Nix, release CLI, or optional-feature build-tag configuration.
Both local Go modules use the Tailscale, WireGuard, and gVisor versions from
this revision. The networking subset includes upstream's DERP map handling,
proxy and WebSocket support, netstack shutdown changes, and legacy client
restart handling. Quantumcat keeps its authenticated bootstrap and full-length
per-peer PSKs. Upstream's experimental hybrid WireGuard mode is outside this
mirror.

Quantumcat modifications add a bounded DERP bootstrap callback, disable Meow
authorization when that callback is configured, gate client WireGuard peer
Expand Down
10 changes: 5 additions & 5 deletions third_party/tailcat/admission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ func TestLegacyAdmissionCancellation(t *testing.T) {
t.Fatal(err)
}
result := make(chan bool, 1)
go func() { result <- s.lb.onMeow(client, disco) }()
go func() { result <- s.lb.onMeow(client, disco, meowInstanceID{}) }()
awaitBootstrapEvent(t, entered)
allowed.Store(false)
if action == "disconnect" {
Expand All @@ -427,7 +427,7 @@ func TestLegacyAdmissionCancellation(t *testing.T) {
}
}
// Cancellation must retain per-key serialization until the hook returns.
if s.lb.onMeow(client, disco) || calls.Load() != 1 {
if s.lb.onMeow(client, disco, meowInstanceID{}) || calls.Load() != 1 {
t.Fatal("retry started a second admission while the old hook was pending")
}
releaseOnce.Do(func() { close(release) })
Expand All @@ -441,14 +441,14 @@ func TestLegacyAdmissionCancellation(t *testing.T) {
t.Fatalf("retained peers=%d pending=%d", peers, pending)
}
if action == "disconnect" {
if s.lb.onMeow(client, disco) {
if s.lb.onMeow(client, disco, meowInstanceID{}) {
t.Fatal("fresh admission ignored updated rejection policy")
}
allowed.Store(true)
if !s.lb.onMeow(client, disco) {
if !s.lb.onMeow(client, disco, meowInstanceID{}) {
t.Fatal("fresh approved admission was not installed")
}
} else if s.lb.onMeow(client, disco) {
} else if s.lb.onMeow(client, disco, meowInstanceID{}) {
t.Fatal("closed server admitted a new peer")
}
})
Expand Down
12 changes: 12 additions & 0 deletions third_party/tailcat/derpwebsocket.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause

package tailcat

// Some sandboxes, such as ChatGPT Work's, reach the internet only through
// an HTTP proxy that intercepts TLS and refuses DERP's "Upgrade: DERP"
// request but allows WebSocket upgrades. This package makes DERP clients
// that get refused retry by speaking DERP inside WebSockets, as browsers
// do. See https://github.com/tailscale/tailscale/issues/22412.
// (On js/wasm, where DERP clients always use WebSockets, it does nothing.)
import _ "tailscale.com/feature/derpwebsocket"
96 changes: 96 additions & 0 deletions third_party/tailcat/derpwebsocket_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause

package tailcat

import (
"context"
"crypto/tls"
"io"
"net"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"

"tailscale.com/derp/derpserver"
"tailscale.com/net/stun/stuntest"
"tailscale.com/tailcfg"
"tailscale.com/types/key"
"tailscale.com/types/logger"
"tailscale.com/types/nettype"
)

// TestDERPWebsocketFallback checks that a Server and Client get through a
// DERP server that, like some HTTP proxies, refuses the usual "Upgrade: DERP"
// request and only allows WebSocket upgrades.
func TestDERPWebsocketFallback(t *testing.T) {
t.Parallel()

logf := mkLogger(t, "derp")
d := derpserver.New(key.NewNode(), logf)
t.Cleanup(func() { d.Close() })
noDERPUpgrade := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.EqualFold(r.Header.Get("Upgrade"), "derp") {
http.Error(w, "DERP upgrade blocked", http.StatusForbidden)
return
}
derpserver.Handler(d).ServeHTTP(w, r)
})
httpsrv := httptest.NewUnstartedServer(derpserver.AddWebSocketSupport(d, noDERPUpgrade))
httpsrv.Config.ErrorLog = logger.StdLogger(logf)
httpsrv.Config.TLSNextProto = make(map[string]func(*http.Server, *tls.Conn, http.Handler))
httpsrv.StartTLS()
t.Cleanup(httpsrv.Close)

stunAddr, stunCleanup := stuntest.ServeWithPacketListener(t, nettype.Std{})
t.Cleanup(stunCleanup)

reg := &tailcfg.DERPRegion{
RegionID: 1,
RegionCode: "test",
Nodes: []*tailcfg.DERPNode{{
Name: "t1",
RegionID: 1,
HostName: "127.0.0.1",
IPv4: "127.0.0.1",
IPv6: "none",
STUNPort: stunAddr.Port,
DERPPort: httpsrv.Listener.Addr().(*net.TCPAddr).Port,
InsecureForTests: true,
STUNTestIP: "127.0.0.1",
}},
}

s := &Server{Key: key.NewNode(), Logf: mkLogger(t, "server"), Region: reg}
t.Cleanup(func() { s.Close() })
s.OnTCP = func(port uint16) (handler func(net.Conn)) {
return func(c net.Conn) {
io.WriteString(c, "Hello over WebSockets\n")
c.Close()
}
}
if err := s.Start(); err != nil {
t.Fatalf("server Start: %v", err)
}

c := &Client{Server: s.TailcatAddr(), Logf: mkLogger(t, "client")}
t.Cleanup(func() { c.Close() })
PingForTest(t, s, c)

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
conn, err := c.DialTCPPort(ctx, 80)
if err != nil {
t.Fatalf("DialTCPPort: %v", err)
}
defer conn.Close()
got, err := io.ReadAll(conn)
if err != nil {
t.Fatalf("ReadAll: %v", err)
}
if want := "Hello over WebSockets\n"; string(got) != want {
t.Errorf("got %q; want %q", got, want)
}
}
30 changes: 30 additions & 0 deletions third_party/tailcat/disco.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,33 @@ func ParseMeowPing(pkt []byte) (nodeKey key.NodePublic, discoKey key.DiscoPublic
func IsMeowedPacket(pkt []byte) bool {
return len(pkt) >= 5 && [4]byte(pkt[:4]) == meowMagic && pkt[4] == meowTypePong
}

// meowInstanceLen is the length of the optional instance ID that follows
// the keys in a meow ping.
const meowInstanceLen = 8

// meowInstanceID identifies one [Client] value, and so one client process.
// Clients send it after the keys in every meow ping. The server compares it
// with the instance ID it saw last for the same node key, so it can tell a
// restarted client from a retried meow even though the disco key, which is
// derived from the node key, stays the same. The zero value means that the
// client didn't send one, as clients older than v0.9.0 don't.
type meowInstanceID [meowInstanceLen]byte

// appendMeowInstance appends id to a meow ping from [EncodeMeowPing].
// Servers older than v0.9.0 ignore it, because [ParseMeowPing] ignores
// trailing bytes.
func appendMeowInstance(pkt []byte, id meowInstanceID) []byte {
return append(pkt, id[:]...)
}

// parseMeowInstance returns the instance ID in a meow ping that
// [ParseMeowPing] accepted, or the zero value if it has none.
func parseMeowInstance(pkt []byte) meowInstanceID {
const off = 4 + 1 + key.NodePublicRawLen + key.DiscoPublicRawLen
var id meowInstanceID
if len(pkt) >= off+meowInstanceLen {
copy(id[:], pkt[off:])
}
return id
}
38 changes: 38 additions & 0 deletions third_party/tailcat/disco_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause

package tailcat

import (
"testing"

"tailscale.com/types/key"
)

// TestMeowInstance checks the optional instance ID after the keys in a
// meow ping, including that a ping without one, from a client older than
// v0.9.0, reads as the zero ID.
func TestMeowInstance(t *testing.T) {
nodeKey := key.NewNode().Public()
discoKey := key.NewDisco().Public()
old := EncodeMeowPing(nodeKey, discoKey)
if got := parseMeowInstance(old); got != (meowInstanceID{}) {
t.Errorf("instance ID of a ping without one = %x; want zero", got)
}

id := meowInstanceID{1, 2, 3, 4, 5, 6, 7, 8}
pkt := appendMeowInstance(EncodeMeowPing(nodeKey, discoKey), id)
if got := parseMeowInstance(pkt); got != id {
t.Errorf("instance ID = %x; want %x", got, id)
}
// ParseMeowPing, as in servers older than v0.9.0, must still accept
// the longer ping.
gotNode, gotDisco, ok := ParseMeowPing(pkt)
if !ok || gotNode != nodeKey || gotDisco != discoKey {
t.Errorf("ParseMeowPing of a ping with an instance ID = %v, %v, %v", gotNode, gotDisco, ok)
}
// A truncated instance ID reads as none.
if got := parseMeowInstance(pkt[:len(pkt)-1]); got != (meowInstanceID{}) {
t.Errorf("instance ID of a truncated ping = %x; want zero", got)
}
}
14 changes: 14 additions & 0 deletions third_party/tailcat/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@ func WaitForDERPForTest(t testing.TB, s *Server, c *Client) {
}
}

// MeowForTest sets up c's network stack without going on the network and
// hands its meow straight to s, as if it had arrived over DERP. A test can
// then check how s took the client before c advertises any endpoints.
func MeowForTest(t testing.TB, s *Server, c *Client) {
t.Helper()
c.startMu.Lock()
err := c.initLocked()
c.startMu.Unlock()
if err != nil {
t.Fatalf("initializing client: %v", err)
}
s.lb.onMeow(c.lb.pub, c.lb.discoPublic(), c.lb.meowInstance)
}

// PingForTest waits for the client's DERP connection and performs the
// meow/meowed handshake.
func PingForTest(t testing.TB, s *Server, c *Client) PingResult {
Expand Down
Loading
Loading