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
4 changes: 2 additions & 2 deletions resolve_stun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
)

// TestResolveEmbedsRelayNodes checks that resolving a short address embeds
// usable DERP relay nodes. STUN-only nodes can't relay DERP traffic and are
// dropped when the address is encoded (see wireRegionOf), so truncating the
// usable DERP relay nodes. STUN-only nodes can't relay DERP traffic and
// Resolve leaves a DERP map's STUN-only nodes out, so truncating the
// region's raw node list before filtering them can leave a resolved address
// with fewer than two relay nodes, or none at all, breaking the promise that
// a resolved address is self-contained and works offline.
Expand Down
8 changes: 4 additions & 4 deletions tailcat.go
Original file line number Diff line number Diff line change
Expand Up @@ -1177,10 +1177,10 @@ func (a Addr) Resolve(ctx context.Context, opts ...any) (Addr, error) {
if err := ci.Expand(ctx, opts...); err != nil {
return "", err
}
// Keep the address short: two relay nodes suffice. STUN-only nodes
// are dropped by wireRegionOf when the address is encoded, so take
// the first two usable nodes here rather than a raw prefix of the
// region's node list, which STUN-only nodes may fill.
// Keep the address short: two relay nodes suffice. A DERP map's own
// relay nodes answer STUN, so leave out its STUN-only nodes and take
// the first two relay nodes rather than a raw prefix of the region's
// node list, which STUN-only nodes may fill.
for _, r := range ci.Region {
var nodes []*tailcfg.DERPNode
for _, n := range r.Nodes {
Expand Down
10 changes: 4 additions & 6 deletions wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,23 +71,19 @@ type wireNode struct {
STUNPort int `cbor:"s,omitempty" json:"STUNPort,omitempty"`
DERPPort int `cbor:"d,omitempty" json:"DERPPort,omitempty"`
InsecureForTests bool `cbor:"x,omitempty" json:"InsecureForTests,omitempty"`
STUNOnly bool `cbor:"o,omitempty" json:"STUNOnly,omitempty"`
}

// wireRegionOf converts a [tailcfg.DERPRegion] (such as one from the
// control plane's DERP map) to its wire form. Fields tailcat doesn't
// use (Latitude, Longitude, CanPort80, ...) are dropped, as are
// STUN-only nodes: they can't relay DERP traffic, which is all an
// embedded region is for.
// use (Latitude, Longitude, CanPort80, ...) are dropped.
func wireRegionOf(r *tailcfg.DERPRegion) *wireRegion {
w := &wireRegion{
RegionID: r.RegionID.Int64(),
RegionCode: r.RegionCode,
RegionName: r.RegionName,
}
for _, n := range r.Nodes {
if n.STUNOnly {
continue
}
w.Nodes = append(w.Nodes, &wireNode{
Name: n.Name,
RegionID: n.RegionID.Int64(),
Expand All @@ -98,6 +94,7 @@ func wireRegionOf(r *tailcfg.DERPRegion) *wireRegion {
STUNPort: n.STUNPort,
DERPPort: n.DERPPort,
InsecureForTests: n.InsecureForTests,
STUNOnly: n.STUNOnly,
})
}
return w
Expand All @@ -121,6 +118,7 @@ func (w *wireRegion) derpRegion() *tailcfg.DERPRegion {
STUNPort: n.STUNPort,
DERPPort: n.DERPPort,
InsecureForTests: n.InsecureForTests,
STUNOnly: n.STUNOnly,
})
}
return r
Expand Down
81 changes: 79 additions & 2 deletions wire_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import (
"strings"
"testing"

"github.com/fxamacker/cbor/v2"
"github.com/google/go-cmp/cmp"
"tailscale.com/tailcfg"
"tailscale.com/types/logger"
)

// wireFieldNames maps the short CBOR field names used by the wire
Expand Down Expand Up @@ -37,6 +39,7 @@ var wireFieldNames = map[string]string{
"s": "STUNPort",
"d": "DERPPort",
"x": "InsecureForTests",
"o": "STUNOnly",
}

// TestWireFieldNames verifies that every field of every wire type
Expand Down Expand Up @@ -83,7 +86,7 @@ func TestWireFieldNames(t *testing.T) {
// TestWireRegionRoundTrip tests the mapping between the upstream
// tailcfg DERP types (as fetched from the control plane's DERP map)
// and tailcat's wire types: fields tailcat uses survive the round
// trip, unused fields are dropped, and STUN-only nodes disappear.
// trip, unused fields are dropped, and STUN-only nodes keep their flag.
func TestWireRegionRoundTrip(t *testing.T) {
in := &tailcfg.DERPRegion{
RegionID: 10,
Expand All @@ -108,7 +111,7 @@ func TestWireRegionRoundTrip(t *testing.T) {
Name: "10s",
RegionID: 10,
HostName: "stun-only.tailscale.com",
STUNOnly: true, // whole node dropped
STUNOnly: true, // must survive
},
{
Name: "custom",
Expand All @@ -135,6 +138,12 @@ func TestWireRegionRoundTrip(t *testing.T) {
STUNPort: 3478,
DERPPort: 8443,
},
{
Name: "10s",
RegionID: 10,
HostName: "stun-only.tailscale.com",
STUNOnly: true,
},
{
Name: "custom",
HostName: "my-derp.example.com",
Expand All @@ -150,3 +159,71 @@ func TestWireRegionRoundTrip(t *testing.T) {
t.Errorf("round trip diff (-want +got):\n%s", diff)
}
}

// oldRelayOnlyAddr was made by tailcat v0.8.0, which dropped STUN-only
// nodes, from a region with one relay node (relay.example.com, STUN
// off) and one STUN-only node (stun.example.com:3478).
const oldRelayOnlyAddr = Addr("tcomFwWCABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAGFygaFhToGiYWhxcmVsYXkuZXhhbXBsZS5jb21hcyA")

// TestWireSTUNOnlyCompat checks that the STUNOnly field leaves older
// addresses alone: they still parse, and a region with no STUN-only
// nodes encodes to the same bytes as before.
func TestWireSTUNOnlyCompat(t *testing.T) {
ci, err := ParseAddr(oldRelayOnlyAddr)
if err != nil {
t.Fatalf("ParseAddr(old address): %v", err)
}
if len(ci.Region) != 1 || len(ci.Region[0].Nodes) != 1 {
t.Fatalf("old address regions = %v; want one region with one node", logger.AsJSON(ci.Region))
}
if n := ci.Region[0].Nodes[0]; n.HostName != "relay.example.com" || n.STUNOnly {
t.Fatalf("old address node = %v; want relay.example.com, not STUN-only", logger.AsJSON(n))
}

relayOnly := ConnInfo{
ServerPublic: ci.ServerPublic,
Region: []*tailcfg.DERPRegion{{
Nodes: []*tailcfg.DERPNode{{HostName: "relay.example.com", STUNPort: -1}},
}},
}
if got := relayOnly.Addr(); got != oldRelayOnlyAddr {
t.Errorf("relay-only region encodes to %s; want the old bytes %s", got, oldRelayOnlyAddr)
}

withSTUN := relayOnly
withSTUN.Region = []*tailcfg.DERPRegion{{
Nodes: []*tailcfg.DERPNode{
{HostName: "relay.example.com", STUNPort: -1},
{HostName: "stun.example.com", STUNPort: 3478, STUNOnly: true},
},
}}
back, err := ParseAddr(withSTUN.Addr())
if err != nil {
t.Fatalf("ParseAddr(new address): %v", err)
}
nodes := back.Region[0].Nodes
if len(nodes) != 2 || nodes[0].STUNOnly || !nodes[1].STUNOnly || nodes[1].STUNPort != 3478 {
t.Errorf("new address nodes = %v; want the relay then the STUN-only node", logger.AsJSON(nodes))
}
}

// TestOldClientsIgnoreSTUNOnly verifies that tailcat clients from before
// the STUNOnly field, whose wire node type has no "o" key, still decode
// a node that carries it, and so see it as a plain node.
func TestOldClientsIgnoreSTUNOnly(t *testing.T) {
type oldWireNode struct {
HostName string `cbor:"h,omitempty"`
STUNPort int `cbor:"s,omitempty"`
}
x, err := cbor.Marshal(&wireNode{HostName: "stun.example.com", STUNPort: 3478, STUNOnly: true})
if err != nil {
t.Fatal(err)
}
var old oldWireNode
if err := cbor.Unmarshal(x, &old); err != nil {
t.Fatalf("old decoder rejected a STUN-only node: %v", err)
}
if old.HostName != "stun.example.com" || old.STUNPort != 3478 {
t.Errorf("old decoder got %+v; want the node's host and STUN port", old)
}
}