-
Notifications
You must be signed in to change notification settings - Fork 214
Expand file tree
/
Copy pathwire.go
More file actions
112 lines (101 loc) · 4.1 KB
/
Copy pathwire.go
File metadata and controls
112 lines (101 loc) · 4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause
package tailcat
import (
"tailscale.com/tailcfg"
)
// This file defines the CBOR wire types behind [ConnBlob]. They mirror the
// subset of [ConnInfo] and the tailcfg DERP types that tailcat actually
// uses, with single-character CBOR field names and omitempty so that blobs
// with embedded DERP regions stay short. Having our own types also keeps
// the wire format independent of upstream tailcfg changes.
//
// The short CBOR field names are the wire format: do not change or reuse
// them. Each is globally unique across all the wire types here (one short
// name per Go field name and vice versa), which TestWireFieldNames locks
// in.
// The JSON tags are only for display (see [ParseConnBlobRaw]); they
// mirror the Go field names but omit empty fields, so the JSON shows
// just what the CBOR actually carries.
// wireConnInfo is the wire form of [ConnInfo].
type wireConnInfo struct {
ServerPublic NodePublic `cbor:"p" json:"ServerPublic"`
ServerDiscoPublic *DiscoPublic `cbor:"k,omitempty" json:"ServerDiscoPublic,omitempty"`
Region []*wireRegion `cbor:"r,omitempty" json:"Region,omitempty"`
RegionID int64 `cbor:"i,omitempty" json:"RegionID,omitempty"`
}
// wireRegion is the wire form of [tailcfg.DERPRegion].
type wireRegion struct {
RegionID int64 `cbor:"i,omitempty" json:"RegionID,omitempty"`
RegionCode string `cbor:"c,omitempty" json:"RegionCode,omitempty"`
RegionName string `cbor:"m,omitempty" json:"RegionName,omitempty"`
Nodes []*wireNode `cbor:"N,omitempty" json:"Nodes,omitempty"`
}
// wireNode is the wire form of [tailcfg.DERPNode].
type wireNode struct {
Name string `cbor:"n,omitempty" json:"Name,omitempty"`
RegionID int64 `cbor:"i,omitempty" json:"RegionID,omitempty"`
HostName string `cbor:"h,omitempty" json:"HostName,omitempty"`
// CertName is the expected TLS cert name when it differs from
// HostName (which is used for the SNI). Empty means the cert is
// expected to match HostName, as with [tailcfg.DERPNode.CertName];
// the production DERP map sets it on no nodes today, so this is
// usually absent.
CertName string `cbor:"t,omitempty" json:"CertName,omitempty"`
IPv4 string `cbor:"4,omitempty" json:"IPv4,omitempty"`
IPv6 string `cbor:"6,omitempty" json:"IPv6,omitempty"`
STUNPort int `cbor:"s,omitempty" json:"STUNPort,omitempty"`
DERPPort int `cbor:"d,omitempty" json:"DERPPort,omitempty"`
InsecureForTests bool `cbor:"x,omitempty" json:"InsecureForTests,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.
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(),
HostName: n.HostName,
CertName: n.CertName,
IPv4: n.IPv4,
IPv6: n.IPv6,
STUNPort: n.STUNPort,
DERPPort: n.DERPPort,
InsecureForTests: n.InsecureForTests,
})
}
return w
}
// derpRegion converts w back to a [tailcfg.DERPRegion].
func (w *wireRegion) derpRegion() *tailcfg.DERPRegion {
r := &tailcfg.DERPRegion{
RegionID: tailcfg.DERPRegionID(w.RegionID),
RegionCode: w.RegionCode,
RegionName: w.RegionName,
}
for _, n := range w.Nodes {
r.Nodes = append(r.Nodes, &tailcfg.DERPNode{
Name: n.Name,
RegionID: tailcfg.DERPRegionID(n.RegionID),
HostName: n.HostName,
CertName: n.CertName,
IPv4: n.IPv4,
IPv6: n.IPv6,
STUNPort: n.STUNPort,
DERPPort: n.DERPPort,
InsecureForTests: n.InsecureForTests,
})
}
return r
}