tailcfg, all: use []netip.AddrPort instead of []string for Endpoints

It's JSON wire compatible.

Updates #cleanup

Change-Id: Ifa5c17768fec35b305b06d75eb5f0611c8a135a6
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
Brad Fitzpatrick
2023-09-30 21:05:02 -07:00
committed by Brad Fitzpatrick
parent 5f5c9142cc
commit 425cf9aa9d
14 changed files with 98 additions and 103 deletions

View File

@@ -267,7 +267,7 @@ func printPeerConcise(buf *strings.Builder, p tailcfg.NodeView) {
ep := make([]string, p.Endpoints().Len())
for i := range ep {
e := p.Endpoints().At(i)
e := p.Endpoints().At(i).String()
// Align vertically on the ':' between IP and port
colon := strings.IndexByte(e, ':')
spaces := 0

View File

@@ -42,6 +42,14 @@ func nodeViews(v []*tailcfg.Node) []tailcfg.NodeView {
return nv
}
func eps(s ...string) []netip.AddrPort {
var eps []netip.AddrPort
for _, ep := range s {
eps = append(eps, netip.MustParseAddrPort(ep))
}
return eps
}
func TestNetworkMapConcise(t *testing.T) {
for _, tt := range []struct {
name string
@@ -56,12 +64,12 @@ func TestNetworkMapConcise(t *testing.T) {
{
Key: testNodeKey(2),
DERP: "127.3.3.40:2",
Endpoints: []string{"192.168.0.100:12", "192.168.0.100:12354"},
Endpoints: eps("192.168.0.100:12", "192.168.0.100:12354"),
},
{
Key: testNodeKey(3),
DERP: "127.3.3.40:4",
Endpoints: []string{"10.2.0.100:12", "10.1.0.100:12345"},
Endpoints: eps("10.2.0.100:12", "10.1.0.100:12345"),
},
}),
},
@@ -95,7 +103,7 @@ func TestConciseDiffFrom(t *testing.T) {
{
Key: testNodeKey(2),
DERP: "127.3.3.40:2",
Endpoints: []string{"192.168.0.100:12", "192.168.0.100:12354"},
Endpoints: eps("192.168.0.100:12", "192.168.0.100:12354"),
},
}),
},
@@ -105,7 +113,7 @@ func TestConciseDiffFrom(t *testing.T) {
{
Key: testNodeKey(2),
DERP: "127.3.3.40:2",
Endpoints: []string{"192.168.0.100:12", "192.168.0.100:12354"},
Endpoints: eps("192.168.0.100:12", "192.168.0.100:12354"),
},
}),
},
@@ -119,7 +127,7 @@ func TestConciseDiffFrom(t *testing.T) {
{
Key: testNodeKey(2),
DERP: "127.3.3.40:2",
Endpoints: []string{"192.168.0.100:12", "192.168.0.100:12354"},
Endpoints: eps("192.168.0.100:12", "192.168.0.100:12354"),
},
}),
},
@@ -129,7 +137,7 @@ func TestConciseDiffFrom(t *testing.T) {
{
Key: testNodeKey(2),
DERP: "127.3.3.40:2",
Endpoints: []string{"192.168.0.100:12", "192.168.0.100:12354"},
Endpoints: eps("192.168.0.100:12", "192.168.0.100:12354"),
},
}),
},
@@ -144,7 +152,7 @@ func TestConciseDiffFrom(t *testing.T) {
ID: 2,
Key: testNodeKey(2),
DERP: "127.3.3.40:2",
Endpoints: []string{"192.168.0.100:12", "192.168.0.100:12354"},
Endpoints: eps("192.168.0.100:12", "192.168.0.100:12354"),
},
}),
},
@@ -155,19 +163,19 @@ func TestConciseDiffFrom(t *testing.T) {
ID: 1,
Key: testNodeKey(1),
DERP: "127.3.3.40:1",
Endpoints: []string{"192.168.0.100:12", "192.168.0.100:12354"},
Endpoints: eps("192.168.0.100:12", "192.168.0.100:12354"),
},
{
ID: 2,
Key: testNodeKey(2),
DERP: "127.3.3.40:2",
Endpoints: []string{"192.168.0.100:12", "192.168.0.100:12354"},
Endpoints: eps("192.168.0.100:12", "192.168.0.100:12354"),
},
{
ID: 3,
Key: testNodeKey(3),
DERP: "127.3.3.40:3",
Endpoints: []string{"192.168.0.100:12", "192.168.0.100:12354"},
Endpoints: eps("192.168.0.100:12", "192.168.0.100:12354"),
},
}),
},
@@ -182,19 +190,19 @@ func TestConciseDiffFrom(t *testing.T) {
ID: 1,
Key: testNodeKey(1),
DERP: "127.3.3.40:1",
Endpoints: []string{"192.168.0.100:12", "192.168.0.100:12354"},
Endpoints: eps("192.168.0.100:12", "192.168.0.100:12354"),
},
{
ID: 2,
Key: testNodeKey(2),
DERP: "127.3.3.40:2",
Endpoints: []string{"192.168.0.100:12", "192.168.0.100:12354"},
Endpoints: eps("192.168.0.100:12", "192.168.0.100:12354"),
},
{
ID: 3,
Key: testNodeKey(3),
DERP: "127.3.3.40:3",
Endpoints: []string{"192.168.0.100:12", "192.168.0.100:12354"},
Endpoints: eps("192.168.0.100:12", "192.168.0.100:12354"),
},
}),
},
@@ -205,7 +213,7 @@ func TestConciseDiffFrom(t *testing.T) {
ID: 2,
Key: testNodeKey(2),
DERP: "127.3.3.40:2",
Endpoints: []string{"192.168.0.100:12", "192.168.0.100:12354"},
Endpoints: eps("192.168.0.100:12", "192.168.0.100:12354"),
},
}),
},
@@ -220,7 +228,7 @@ func TestConciseDiffFrom(t *testing.T) {
ID: 2,
Key: testNodeKey(2),
DERP: "127.3.3.40:2",
Endpoints: []string{"192.168.0.100:12", "1.1.1.1:1"},
Endpoints: eps("192.168.0.100:12", "1.1.1.1:1"),
},
}),
},
@@ -231,7 +239,7 @@ func TestConciseDiffFrom(t *testing.T) {
ID: 2,
Key: testNodeKey(2),
DERP: "127.3.3.40:2",
Endpoints: []string{"192.168.0.100:12", "1.1.1.1:2"},
Endpoints: eps("192.168.0.100:12", "1.1.1.1:2"),
},
}),
},
@@ -246,7 +254,7 @@ func TestConciseDiffFrom(t *testing.T) {
ID: 2,
Key: testNodeKey(2),
DERP: "127.3.3.40:2",
Endpoints: []string{"192.168.0.100:41641", "1.1.1.1:41641"},
Endpoints: eps("192.168.0.100:41641", "1.1.1.1:41641"),
DiscoKey: testDiscoKey("f00f00f00f"),
AllowedIPs: []netip.Prefix{netip.PrefixFrom(netaddr.IPv4(100, 102, 103, 104), 32)},
},
@@ -259,7 +267,7 @@ func TestConciseDiffFrom(t *testing.T) {
ID: 2,
Key: testNodeKey(2),
DERP: "127.3.3.40:2",
Endpoints: []string{"192.168.0.100:41641", "1.1.1.1:41641"},
Endpoints: eps("192.168.0.100:41641", "1.1.1.1:41641"),
DiscoKey: testDiscoKey("ba4ba4ba4b"),
AllowedIPs: []netip.Prefix{netip.PrefixFrom(netaddr.IPv4(100, 102, 103, 104), 32)},
},

View File

@@ -45,11 +45,7 @@ type NodeMutationEndpoints struct {
}
func (m NodeMutationEndpoints) Apply(n *tailcfg.Node) {
eps := make([]string, len(m.Endpoints))
for i, ep := range m.Endpoints {
eps[i] = ep.String()
}
n.Endpoints = eps
n.Endpoints = slices.Clone(m.Endpoints)
}
// NodeMutationOnline is a NodeMutation that says a node is now online or
@@ -105,15 +101,7 @@ func NodeMutationsFromPatch(p *tailcfg.PeerChange) (_ []NodeMutation, ok bool) {
case "DERPRegion":
ret = append(ret, NodeMutationDERPHome{mutatingNodeID(p.NodeID), p.DERPRegion})
case "Endpoints":
eps := make([]netip.AddrPort, len(p.Endpoints))
for i, epStr := range p.Endpoints {
var err error
eps[i], err = netip.ParseAddrPort(epStr)
if err != nil {
return nil, false
}
}
ret = append(ret, NodeMutationEndpoints{mutatingNodeID(p.NodeID), eps})
ret = append(ret, NodeMutationEndpoints{mutatingNodeID(p.NodeID), slices.Clone(p.Endpoints)})
case "Online":
ret = append(ret, NodeMutationOnline{mutatingNodeID(p.NodeID), *p.Online})
case "LastSeen":

View File

@@ -91,10 +91,10 @@ func TestMutationsFromMapResponse(t *testing.T) {
name: "patch-ep",
mr: fromChanges(&tailcfg.PeerChange{
NodeID: 1,
Endpoints: []string{"1.2.3.4:567"},
Endpoints: eps("1.2.3.4:567"),
}, &tailcfg.PeerChange{
NodeID: 2,
Endpoints: []string{"8.9.10.11:1234"},
Endpoints: eps("8.9.10.11:1234"),
}),
want: muts(
NodeMutationEndpoints{1, []netip.AddrPort{netip.MustParseAddrPort("1.2.3.4:567")}},