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

@ -29,7 +29,6 @@ import (
"tailscale.com/tstime/mono"
"tailscale.com/types/key"
"tailscale.com/types/logger"
"tailscale.com/types/views"
"tailscale.com/util/mak"
"tailscale.com/util/ringbuffer"
)
@ -812,19 +811,7 @@ func (de *endpoint) updateFromNode(n tailcfg.NodeView, heartbeatDisabled bool) {
de.derpAddr = newDerp
}
de.setEndpointsLocked(addrPortsFromStringsView{n.Endpoints()})
}
// addrPortsFromStringsView converts a view of AddrPort strings
// to a view-like thing of netip.AddrPort.
// TODO(bradfitz): change the type of tailcfg.Node.Endpoint.
type addrPortsFromStringsView struct {
views.Slice[string]
}
func (a addrPortsFromStringsView) At(i int) netip.AddrPort {
ap, _ := netip.ParseAddrPort(a.Slice.At(i))
return ap // or the zero value on error
de.setEndpointsLocked(n.Endpoints())
}
func (de *endpoint) setEndpointsLocked(eps interface {

View File

@ -290,7 +290,7 @@ func meshStacks(logf logger.Logf, mutateNetmap func(idx int, nm *netmap.NetworkM
DiscoKey: peer.conn.DiscoPublicKey(),
Addresses: addrs,
AllowedIPs: addrs,
Endpoints: epStrings(eps[i]),
Endpoints: epFromTyped(eps[i]),
DERP: "127.3.3.40:1",
}
nm.Peers = append(nm.Peers, peer.View())
@ -1265,7 +1265,7 @@ func addTestEndpoint(tb testing.TB, conn *Conn, sendConn net.PacketConn) (key.No
ID: 1,
Key: nodeKey,
DiscoKey: discoKey,
Endpoints: []string{sendConn.LocalAddr().String()},
Endpoints: eps(sendConn.LocalAddr().String()),
},
}),
})
@ -1470,7 +1470,7 @@ func TestSetNetworkMapChangingNodeKey(t *testing.T) {
ID: 1,
Key: nodeKey1,
DiscoKey: discoKey,
Endpoints: []string{"192.168.1.2:345"},
Endpoints: eps("192.168.1.2:345"),
},
}),
})
@ -1486,7 +1486,7 @@ func TestSetNetworkMapChangingNodeKey(t *testing.T) {
ID: 2,
Key: nodeKey2,
DiscoKey: discoKey,
Endpoints: []string{"192.168.1.2:345"},
Endpoints: eps("192.168.1.2:345"),
},
}),
})
@ -1752,13 +1752,21 @@ func TestBetterAddr(t *testing.T) {
}
func epStrings(eps []tailcfg.Endpoint) (ret []string) {
func epFromTyped(eps []tailcfg.Endpoint) (ret []netip.AddrPort) {
for _, ep := range eps {
ret = append(ret, ep.Addr.String())
ret = append(ret, ep.Addr)
}
return
}
func eps(s ...string) []netip.AddrPort {
var eps []netip.AddrPort
for _, ep := range s {
eps = append(eps, netip.MustParseAddrPort(ep))
}
return eps
}
func TestStressSetNetworkMap(t *testing.T) {
t.Parallel()
@ -1778,7 +1786,7 @@ func TestStressSetNetworkMap(t *testing.T) {
ID: tailcfg.NodeID(i) + 1,
DiscoKey: randDiscoKey(),
Key: randNodeKey(),
Endpoints: []string{fmt.Sprintf("192.168.1.2:%d", i)},
Endpoints: eps(fmt.Sprintf("192.168.1.2:%d", i)),
}
}
@ -2276,7 +2284,7 @@ func TestIsWireGuardOnlyPeer(t *testing.T) {
{
ID: 1,
Key: wgkey.Public(),
Endpoints: []string{wgEp.String()},
Endpoints: []netip.AddrPort{wgEp},
IsWireGuardOnly: true,
Addresses: []netip.Prefix{wgaip},
AllowedIPs: []netip.Prefix{wgaip},
@ -2337,7 +2345,7 @@ func TestIsWireGuardOnlyPeerWithMasquerade(t *testing.T) {
{
ID: 1,
Key: wgkey.Public(),
Endpoints: []string{wgEp.String()},
Endpoints: []netip.AddrPort{wgEp},
IsWireGuardOnly: true,
Addresses: []netip.Prefix{wgaip},
AllowedIPs: []netip.Prefix{wgaip},
@ -2465,7 +2473,7 @@ func TestIsWireGuardOnlyPickEndpointByPing(t *testing.T) {
Peers: nodeViews([]*tailcfg.Node{
{
Key: wgkey.Public(),
Endpoints: []string{wgEp.String(), wgEp2.String(), wgEpV6.String()},
Endpoints: []netip.AddrPort{wgEp, wgEp2, wgEpV6},
IsWireGuardOnly: true,
Addresses: []netip.Prefix{wgaip},
AllowedIPs: []netip.Prefix{wgaip},