2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2021-01-05 19:02:52 +00:00
|
|
|
|
2021-02-04 21:12:42 +00:00
|
|
|
package ipnlocal
|
2021-01-05 19:02:52 +00:00
|
|
|
|
|
|
|
import (
|
2023-01-17 20:59:03 +00:00
|
|
|
"context"
|
2021-04-21 19:57:48 +00:00
|
|
|
"fmt"
|
2021-10-14 20:40:06 +00:00
|
|
|
"net"
|
2021-04-21 19:57:48 +00:00
|
|
|
"net/http"
|
2022-07-26 03:55:44 +00:00
|
|
|
"net/netip"
|
2021-03-04 20:04:31 +00:00
|
|
|
"reflect"
|
2021-02-04 21:12:42 +00:00
|
|
|
"testing"
|
2021-04-28 15:52:04 +00:00
|
|
|
"time"
|
2021-02-04 21:12:42 +00:00
|
|
|
|
2022-07-25 03:08:42 +00:00
|
|
|
"go4.org/netipx"
|
2022-12-19 18:23:47 +00:00
|
|
|
"tailscale.com/control/controlclient"
|
2021-04-21 19:57:48 +00:00
|
|
|
"tailscale.com/ipn"
|
2022-02-28 21:08:45 +00:00
|
|
|
"tailscale.com/ipn/store/mem"
|
2021-03-18 00:04:32 +00:00
|
|
|
"tailscale.com/net/interfaces"
|
2021-02-23 04:43:35 +00:00
|
|
|
"tailscale.com/net/tsaddr"
|
2021-01-05 19:02:52 +00:00
|
|
|
"tailscale.com/tailcfg"
|
2023-05-03 20:57:17 +00:00
|
|
|
"tailscale.com/tsd"
|
2022-12-19 18:23:47 +00:00
|
|
|
"tailscale.com/tstest"
|
2023-02-19 16:17:06 +00:00
|
|
|
"tailscale.com/types/key"
|
2021-04-28 15:52:04 +00:00
|
|
|
"tailscale.com/types/logger"
|
2023-03-23 17:49:56 +00:00
|
|
|
"tailscale.com/types/logid"
|
2021-02-05 23:44:46 +00:00
|
|
|
"tailscale.com/types/netmap"
|
2023-09-02 02:28:00 +00:00
|
|
|
"tailscale.com/types/ptr"
|
2023-09-05 17:51:52 +00:00
|
|
|
"tailscale.com/util/set"
|
2021-04-21 19:57:48 +00:00
|
|
|
"tailscale.com/wgengine"
|
2022-11-02 20:13:26 +00:00
|
|
|
"tailscale.com/wgengine/filter"
|
2021-03-04 20:04:31 +00:00
|
|
|
"tailscale.com/wgengine/wgcfg"
|
2021-01-05 19:02:52 +00:00
|
|
|
)
|
|
|
|
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
func inRemove(ip netip.Addr) bool {
|
2021-03-18 00:04:32 +00:00
|
|
|
for _, pfx := range removeFromDefaultRoute {
|
|
|
|
if pfx.Contains(ip) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-02-23 04:43:35 +00:00
|
|
|
func TestShrinkDefaultRoute(t *testing.T) {
|
|
|
|
tests := []struct {
|
2021-03-18 00:04:32 +00:00
|
|
|
route string
|
|
|
|
in []string
|
|
|
|
out []string
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
localIPFn func(netip.Addr) bool // true if this machine's local IP address should be "in" after shrinking.
|
2021-02-23 04:43:35 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
route: "0.0.0.0/0",
|
|
|
|
in: []string{"1.2.3.4", "25.0.0.1"},
|
|
|
|
out: []string{
|
|
|
|
"10.0.0.1",
|
|
|
|
"10.255.255.255",
|
|
|
|
"192.168.0.1",
|
|
|
|
"192.168.255.255",
|
|
|
|
"172.16.0.1",
|
|
|
|
"172.31.255.255",
|
|
|
|
"100.101.102.103",
|
2021-03-18 00:04:32 +00:00
|
|
|
"224.0.0.1",
|
|
|
|
"169.254.169.254",
|
2021-02-23 04:43:35 +00:00
|
|
|
// Some random IPv6 stuff that shouldn't be in a v4
|
|
|
|
// default route.
|
|
|
|
"fe80::",
|
|
|
|
"2601::1",
|
|
|
|
},
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
localIPFn: func(ip netip.Addr) bool { return !inRemove(ip) && ip.Is4() },
|
2021-02-23 04:43:35 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
route: "::/0",
|
|
|
|
in: []string{"::1", "2601::1"},
|
|
|
|
out: []string{
|
|
|
|
"fe80::1",
|
2021-03-18 00:04:32 +00:00
|
|
|
"ff00::1",
|
2022-07-25 03:08:42 +00:00
|
|
|
tsaddr.TailscaleULARange().Addr().String(),
|
2021-02-23 04:43:35 +00:00
|
|
|
},
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
localIPFn: func(ip netip.Addr) bool { return !inRemove(ip) && ip.Is6() },
|
2021-02-23 04:43:35 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-12-16 19:15:45 +00:00
|
|
|
// Construct a fake local network environment to make this test hermetic.
|
|
|
|
// localInterfaceRoutes and hostIPs would normally come from calling interfaceRoutes,
|
|
|
|
// and localAddresses would normally come from calling interfaces.LocalAddresses.
|
2022-07-25 03:08:42 +00:00
|
|
|
var b netipx.IPSetBuilder
|
2021-12-16 19:15:45 +00:00
|
|
|
for _, c := range []string{"127.0.0.0/8", "192.168.9.0/24", "fe80::/32"} {
|
2022-07-26 03:55:44 +00:00
|
|
|
p := netip.MustParsePrefix(c)
|
2021-12-16 19:15:45 +00:00
|
|
|
b.AddPrefix(p)
|
|
|
|
}
|
|
|
|
localInterfaceRoutes, err := b.IPSet()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
hostIPs := []netip.Addr{
|
2022-07-26 03:55:44 +00:00
|
|
|
netip.MustParseAddr("127.0.0.1"),
|
|
|
|
netip.MustParseAddr("192.168.9.39"),
|
|
|
|
netip.MustParseAddr("fe80::1"),
|
|
|
|
netip.MustParseAddr("fe80::437d:feff:feca:49a7"),
|
2021-12-16 19:15:45 +00:00
|
|
|
}
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
localAddresses := []netip.Addr{
|
2022-07-26 03:55:44 +00:00
|
|
|
netip.MustParseAddr("192.168.9.39"),
|
2021-12-16 19:15:45 +00:00
|
|
|
}
|
|
|
|
|
2021-02-23 04:43:35 +00:00
|
|
|
for _, test := range tests {
|
2022-07-26 03:55:44 +00:00
|
|
|
def := netip.MustParsePrefix(test.route)
|
2021-12-16 19:15:45 +00:00
|
|
|
got, err := shrinkDefaultRoute(def, localInterfaceRoutes, hostIPs)
|
2021-02-23 04:43:35 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("shrinkDefaultRoute(%q): %v", test.route, err)
|
|
|
|
}
|
|
|
|
for _, ip := range test.in {
|
2022-07-26 03:55:44 +00:00
|
|
|
if !got.Contains(netip.MustParseAddr(ip)) {
|
2021-02-23 04:43:35 +00:00
|
|
|
t.Errorf("shrink(%q).Contains(%v) = false, want true", test.route, ip)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, ip := range test.out {
|
2022-07-26 03:55:44 +00:00
|
|
|
if got.Contains(netip.MustParseAddr(ip)) {
|
2021-02-23 04:43:35 +00:00
|
|
|
t.Errorf("shrink(%q).Contains(%v) = true, want false", test.route, ip)
|
|
|
|
}
|
|
|
|
}
|
2021-12-16 19:15:45 +00:00
|
|
|
for _, ip := range localAddresses {
|
2021-03-18 00:04:32 +00:00
|
|
|
want := test.localIPFn(ip)
|
|
|
|
if gotContains := got.Contains(ip); gotContains != want {
|
|
|
|
t.Errorf("shrink(%q).Contains(%v) = %v, want %v", test.route, ip, gotContains, want)
|
|
|
|
}
|
|
|
|
}
|
2021-02-23 04:43:35 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-04 20:04:31 +00:00
|
|
|
|
|
|
|
func TestPeerRoutes(t *testing.T) {
|
2022-07-26 03:55:44 +00:00
|
|
|
pp := netip.MustParsePrefix
|
2021-03-04 20:04:31 +00:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
peers []wgcfg.Peer
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
want []netip.Prefix
|
2021-03-04 20:04:31 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "small_v4",
|
|
|
|
peers: []wgcfg.Peer{
|
|
|
|
{
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
AllowedIPs: []netip.Prefix{
|
2021-03-04 20:04:31 +00:00
|
|
|
pp("100.101.102.103/32"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
want: []netip.Prefix{
|
2021-03-04 20:04:31 +00:00
|
|
|
pp("100.101.102.103/32"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "big_v4",
|
|
|
|
peers: []wgcfg.Peer{
|
|
|
|
{
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
AllowedIPs: []netip.Prefix{
|
2021-03-04 20:04:31 +00:00
|
|
|
pp("100.101.102.103/32"),
|
|
|
|
pp("100.101.102.104/32"),
|
|
|
|
pp("100.101.102.105/32"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
want: []netip.Prefix{
|
2021-03-04 20:04:31 +00:00
|
|
|
pp("100.64.0.0/10"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "has_1_v6",
|
|
|
|
peers: []wgcfg.Peer{
|
|
|
|
{
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
AllowedIPs: []netip.Prefix{
|
2021-03-04 20:04:31 +00:00
|
|
|
pp("fd7a:115c:a1e0:ab12:4843:cd96:6258:b240/128"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
want: []netip.Prefix{
|
2021-03-04 20:04:31 +00:00
|
|
|
pp("fd7a:115c:a1e0::/48"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "has_2_v6",
|
|
|
|
peers: []wgcfg.Peer{
|
|
|
|
{
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
AllowedIPs: []netip.Prefix{
|
2021-03-04 20:04:31 +00:00
|
|
|
pp("fd7a:115c:a1e0:ab12:4843:cd96:6258:b240/128"),
|
|
|
|
pp("fd7a:115c:a1e0:ab12:4843:cd96:6258:b241/128"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
want: []netip.Prefix{
|
2021-03-04 20:04:31 +00:00
|
|
|
pp("fd7a:115c:a1e0::/48"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "big_v4_big_v6",
|
|
|
|
peers: []wgcfg.Peer{
|
|
|
|
{
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
AllowedIPs: []netip.Prefix{
|
2021-03-04 20:04:31 +00:00
|
|
|
pp("100.101.102.103/32"),
|
|
|
|
pp("100.101.102.104/32"),
|
|
|
|
pp("100.101.102.105/32"),
|
|
|
|
pp("fd7a:115c:a1e0:ab12:4843:cd96:6258:b240/128"),
|
|
|
|
pp("fd7a:115c:a1e0:ab12:4843:cd96:6258:b241/128"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
want: []netip.Prefix{
|
2021-03-04 20:04:31 +00:00
|
|
|
pp("100.64.0.0/10"),
|
2022-04-13 22:41:04 +00:00
|
|
|
pp("fd7a:115c:a1e0::/48"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "output-should-be-sorted",
|
|
|
|
peers: []wgcfg.Peer{
|
|
|
|
{
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
AllowedIPs: []netip.Prefix{
|
2022-04-13 22:41:04 +00:00
|
|
|
pp("100.64.0.2/32"),
|
|
|
|
pp("10.0.0.0/16"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
AllowedIPs: []netip.Prefix{
|
2022-04-13 22:41:04 +00:00
|
|
|
pp("100.64.0.1/32"),
|
|
|
|
pp("10.0.0.0/8"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
want: []netip.Prefix{
|
2022-04-13 22:41:04 +00:00
|
|
|
pp("10.0.0.0/8"),
|
|
|
|
pp("10.0.0.0/16"),
|
|
|
|
pp("100.64.0.1/32"),
|
|
|
|
pp("100.64.0.2/32"),
|
2021-03-04 20:04:31 +00:00
|
|
|
},
|
|
|
|
},
|
2023-02-19 16:17:06 +00:00
|
|
|
{
|
|
|
|
name: "skip-unmasked-prefixes",
|
|
|
|
peers: []wgcfg.Peer{
|
|
|
|
{
|
|
|
|
PublicKey: key.NewNode().Public(),
|
|
|
|
AllowedIPs: []netip.Prefix{
|
|
|
|
pp("100.64.0.2/32"),
|
|
|
|
pp("10.0.0.100/16"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: []netip.Prefix{
|
|
|
|
pp("100.64.0.2/32"),
|
|
|
|
},
|
|
|
|
},
|
2021-03-04 20:04:31 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2023-02-19 16:17:06 +00:00
|
|
|
got := peerRoutes(t.Logf, tt.peers, 2)
|
2021-03-04 20:04:31 +00:00
|
|
|
if !reflect.DeepEqual(got, tt.want) {
|
|
|
|
t.Errorf("got = %v; want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2021-04-05 04:35:52 +00:00
|
|
|
|
|
|
|
func TestPeerAPIBase(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
nm *netmap.NetworkMap
|
|
|
|
peer *tailcfg.Node
|
|
|
|
want string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "nil_netmap",
|
|
|
|
peer: new(tailcfg.Node),
|
|
|
|
want: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "nil_peer",
|
|
|
|
nm: new(netmap.NetworkMap),
|
|
|
|
want: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "self_only_4_them_both",
|
|
|
|
nm: &netmap.NetworkMap{
|
2023-09-18 06:31:34 +00:00
|
|
|
SelfNode: (&tailcfg.Node{
|
|
|
|
Addresses: []netip.Prefix{
|
|
|
|
netip.MustParsePrefix("100.64.1.1/32"),
|
|
|
|
},
|
|
|
|
}).View(),
|
2021-04-05 04:35:52 +00:00
|
|
|
},
|
|
|
|
peer: &tailcfg.Node{
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
Addresses: []netip.Prefix{
|
2022-07-26 03:55:44 +00:00
|
|
|
netip.MustParsePrefix("100.64.1.2/32"),
|
|
|
|
netip.MustParsePrefix("fe70::2/128"),
|
2021-04-05 04:35:52 +00:00
|
|
|
},
|
2022-02-15 16:19:44 +00:00
|
|
|
Hostinfo: (&tailcfg.Hostinfo{
|
2021-04-05 04:35:52 +00:00
|
|
|
Services: []tailcfg.Service{
|
|
|
|
{Proto: "peerapi4", Port: 444},
|
|
|
|
{Proto: "peerapi6", Port: 666},
|
|
|
|
},
|
2022-02-15 16:19:44 +00:00
|
|
|
}).View(),
|
2021-04-05 04:35:52 +00:00
|
|
|
},
|
|
|
|
want: "http://100.64.1.2:444",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "self_only_6_them_both",
|
|
|
|
nm: &netmap.NetworkMap{
|
2023-09-18 06:31:34 +00:00
|
|
|
SelfNode: (&tailcfg.Node{
|
|
|
|
Addresses: []netip.Prefix{
|
|
|
|
netip.MustParsePrefix("fe70::1/128"),
|
|
|
|
},
|
|
|
|
}).View(),
|
2021-04-05 04:35:52 +00:00
|
|
|
},
|
|
|
|
peer: &tailcfg.Node{
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
Addresses: []netip.Prefix{
|
2022-07-26 03:55:44 +00:00
|
|
|
netip.MustParsePrefix("100.64.1.2/32"),
|
|
|
|
netip.MustParsePrefix("fe70::2/128"),
|
2021-04-05 04:35:52 +00:00
|
|
|
},
|
2022-02-15 16:19:44 +00:00
|
|
|
Hostinfo: (&tailcfg.Hostinfo{
|
2021-04-05 04:35:52 +00:00
|
|
|
Services: []tailcfg.Service{
|
|
|
|
{Proto: "peerapi4", Port: 444},
|
|
|
|
{Proto: "peerapi6", Port: 666},
|
|
|
|
},
|
2022-02-15 16:19:44 +00:00
|
|
|
}).View(),
|
2021-04-05 04:35:52 +00:00
|
|
|
},
|
|
|
|
want: "http://[fe70::2]:666",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "self_both_them_only_4",
|
|
|
|
nm: &netmap.NetworkMap{
|
2023-09-18 06:31:34 +00:00
|
|
|
SelfNode: (&tailcfg.Node{
|
|
|
|
Addresses: []netip.Prefix{
|
|
|
|
netip.MustParsePrefix("100.64.1.1/32"),
|
|
|
|
netip.MustParsePrefix("fe70::1/128"),
|
|
|
|
},
|
|
|
|
}).View(),
|
2021-04-05 04:35:52 +00:00
|
|
|
},
|
|
|
|
peer: &tailcfg.Node{
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
Addresses: []netip.Prefix{
|
2022-07-26 03:55:44 +00:00
|
|
|
netip.MustParsePrefix("100.64.1.2/32"),
|
|
|
|
netip.MustParsePrefix("fe70::2/128"),
|
2021-04-05 04:35:52 +00:00
|
|
|
},
|
2022-02-15 16:19:44 +00:00
|
|
|
Hostinfo: (&tailcfg.Hostinfo{
|
2021-04-05 04:35:52 +00:00
|
|
|
Services: []tailcfg.Service{
|
|
|
|
{Proto: "peerapi4", Port: 444},
|
|
|
|
},
|
2022-02-15 16:19:44 +00:00
|
|
|
}).View(),
|
2021-04-05 04:35:52 +00:00
|
|
|
},
|
|
|
|
want: "http://100.64.1.2:444",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "self_both_them_only_6",
|
|
|
|
nm: &netmap.NetworkMap{
|
2023-09-18 06:31:34 +00:00
|
|
|
SelfNode: (&tailcfg.Node{
|
|
|
|
Addresses: []netip.Prefix{
|
|
|
|
netip.MustParsePrefix("100.64.1.1/32"),
|
|
|
|
netip.MustParsePrefix("fe70::1/128"),
|
|
|
|
},
|
|
|
|
}).View(),
|
2021-04-05 04:35:52 +00:00
|
|
|
},
|
|
|
|
peer: &tailcfg.Node{
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
Addresses: []netip.Prefix{
|
2022-07-26 03:55:44 +00:00
|
|
|
netip.MustParsePrefix("100.64.1.2/32"),
|
|
|
|
netip.MustParsePrefix("fe70::2/128"),
|
2021-04-05 04:35:52 +00:00
|
|
|
},
|
2022-02-15 16:19:44 +00:00
|
|
|
Hostinfo: (&tailcfg.Hostinfo{
|
2021-04-05 04:35:52 +00:00
|
|
|
Services: []tailcfg.Service{
|
|
|
|
{Proto: "peerapi6", Port: 666},
|
|
|
|
},
|
2022-02-15 16:19:44 +00:00
|
|
|
}).View(),
|
2021-04-05 04:35:52 +00:00
|
|
|
},
|
|
|
|
want: "http://[fe70::2]:666",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "self_both_them_no_peerapi_service",
|
|
|
|
nm: &netmap.NetworkMap{
|
2023-09-18 06:31:34 +00:00
|
|
|
SelfNode: (&tailcfg.Node{
|
|
|
|
Addresses: []netip.Prefix{
|
|
|
|
netip.MustParsePrefix("100.64.1.1/32"),
|
|
|
|
netip.MustParsePrefix("fe70::1/128"),
|
|
|
|
},
|
|
|
|
}).View(),
|
2021-04-05 04:35:52 +00:00
|
|
|
},
|
|
|
|
peer: &tailcfg.Node{
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
Addresses: []netip.Prefix{
|
2022-07-26 03:55:44 +00:00
|
|
|
netip.MustParsePrefix("100.64.1.2/32"),
|
|
|
|
netip.MustParsePrefix("fe70::2/128"),
|
2021-04-05 04:35:52 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
want: "",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2023-08-18 14:57:44 +00:00
|
|
|
got := peerAPIBase(tt.nm, tt.peer.View())
|
2021-04-05 04:35:52 +00:00
|
|
|
if got != tt.want {
|
|
|
|
t.Errorf("got %q; want %q", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-04-21 19:57:48 +00:00
|
|
|
|
|
|
|
type panicOnUseTransport struct{}
|
|
|
|
|
|
|
|
func (panicOnUseTransport) RoundTrip(*http.Request) (*http.Response, error) {
|
|
|
|
panic("unexpected HTTP request")
|
|
|
|
}
|
|
|
|
|
2023-09-20 14:34:12 +00:00
|
|
|
func newTestLocalBackend(t testing.TB) *LocalBackend {
|
2021-04-28 15:52:04 +00:00
|
|
|
var logf logger.Logf = logger.Discard
|
2023-05-03 20:57:17 +00:00
|
|
|
sys := new(tsd.System)
|
2022-02-28 21:08:45 +00:00
|
|
|
store := new(mem.Store)
|
2023-05-03 20:57:17 +00:00
|
|
|
sys.Set(store)
|
|
|
|
eng, err := wgengine.NewFakeUserspaceEngine(logf, sys.Set)
|
2021-04-28 15:52:04 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("NewFakeUserspaceEngine: %v", err)
|
|
|
|
}
|
2021-09-15 18:48:49 +00:00
|
|
|
t.Cleanup(eng.Close)
|
2023-05-03 20:57:17 +00:00
|
|
|
sys.Set(eng)
|
|
|
|
lb, err := NewLocalBackend(logf, logid.PublicID{}, sys, 0)
|
2021-04-28 15:52:04 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("NewLocalBackend: %v", err)
|
|
|
|
}
|
2023-09-20 14:34:12 +00:00
|
|
|
return lb
|
|
|
|
}
|
|
|
|
|
|
|
|
// Issue 1573: don't generate a machine key if we don't want to be running.
|
|
|
|
func TestLazyMachineKeyGeneration(t *testing.T) {
|
|
|
|
tstest.Replace(t, &panicOnMachineKeyGeneration, func() bool { return true })
|
2021-04-28 15:52:04 +00:00
|
|
|
|
2023-09-20 14:34:12 +00:00
|
|
|
lb := newTestLocalBackend(t)
|
2021-04-28 15:52:04 +00:00
|
|
|
lb.SetHTTPTestClient(&http.Client{
|
|
|
|
Transport: panicOnUseTransport{}, // validate we don't send HTTP requests
|
|
|
|
})
|
|
|
|
|
2022-11-09 05:58:10 +00:00
|
|
|
if err := lb.Start(ipn.Options{}); err != nil {
|
2021-04-28 15:52:04 +00:00
|
|
|
t.Fatalf("Start: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Give the controlclient package goroutines (if they're
|
|
|
|
// accidentally started) extra time to schedule and run (and thus
|
|
|
|
// hit panicOnUseTransport).
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
|
|
|
}
|
2021-04-29 17:26:53 +00:00
|
|
|
|
|
|
|
func TestFileTargets(t *testing.T) {
|
|
|
|
b := new(LocalBackend)
|
|
|
|
_, err := b.FileTargets()
|
2022-06-08 16:11:50 +00:00
|
|
|
if got, want := fmt.Sprint(err), "not connected to the tailnet"; got != want {
|
2021-04-29 17:26:53 +00:00
|
|
|
t.Errorf("before connect: got %q; want %q", got, want)
|
|
|
|
}
|
|
|
|
|
|
|
|
b.netMap = new(netmap.NetworkMap)
|
|
|
|
_, err = b.FileTargets()
|
2022-06-08 16:11:50 +00:00
|
|
|
if got, want := fmt.Sprint(err), "not connected to the tailnet"; got != want {
|
2021-04-29 17:26:53 +00:00
|
|
|
t.Errorf("non-running netmap: got %q; want %q", got, want)
|
|
|
|
}
|
|
|
|
|
|
|
|
b.state = ipn.Running
|
|
|
|
_, err = b.FileTargets()
|
|
|
|
if got, want := fmt.Sprint(err), "file sharing not enabled by Tailscale admin"; got != want {
|
|
|
|
t.Errorf("without cap: got %q; want %q", got, want)
|
|
|
|
}
|
|
|
|
|
|
|
|
b.capFileSharing = true
|
|
|
|
got, err := b.FileTargets()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(got) != 0 {
|
|
|
|
t.Fatalf("unexpected %d peers", len(got))
|
|
|
|
}
|
|
|
|
// (other cases handled by TestPeerAPIBase above)
|
|
|
|
}
|
2021-10-14 20:40:06 +00:00
|
|
|
|
|
|
|
func TestInternalAndExternalInterfaces(t *testing.T) {
|
|
|
|
type interfacePrefix struct {
|
|
|
|
i interfaces.Interface
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
pfx netip.Prefix
|
2021-10-14 20:40:06 +00:00
|
|
|
}
|
|
|
|
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
masked := func(ips ...interfacePrefix) (pfxs []netip.Prefix) {
|
2021-10-14 20:40:06 +00:00
|
|
|
for _, ip := range ips {
|
|
|
|
pfxs = append(pfxs, ip.pfx.Masked())
|
|
|
|
}
|
|
|
|
return pfxs
|
|
|
|
}
|
|
|
|
iList := func(ips ...interfacePrefix) (il interfaces.List) {
|
|
|
|
for _, ip := range ips {
|
|
|
|
il = append(il, ip.i)
|
|
|
|
}
|
|
|
|
return il
|
|
|
|
}
|
|
|
|
newInterface := func(name, pfx string, wsl2, loopback bool) interfacePrefix {
|
2022-07-26 03:55:44 +00:00
|
|
|
ippfx := netip.MustParsePrefix(pfx)
|
2021-10-14 20:40:06 +00:00
|
|
|
ip := interfaces.Interface{
|
|
|
|
Interface: &net.Interface{},
|
|
|
|
AltAddrs: []net.Addr{
|
2022-07-25 03:08:42 +00:00
|
|
|
netipx.PrefixIPNet(ippfx),
|
2021-10-14 20:40:06 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
if loopback {
|
|
|
|
ip.Flags = net.FlagLoopback
|
|
|
|
}
|
|
|
|
if wsl2 {
|
|
|
|
ip.HardwareAddr = []byte{0x00, 0x15, 0x5d, 0x00, 0x00, 0x00}
|
|
|
|
}
|
|
|
|
return interfacePrefix{i: ip, pfx: ippfx}
|
|
|
|
}
|
|
|
|
var (
|
|
|
|
en0 = newInterface("en0", "10.20.2.5/16", false, false)
|
|
|
|
en1 = newInterface("en1", "192.168.1.237/24", false, false)
|
|
|
|
wsl = newInterface("wsl", "192.168.5.34/24", true, false)
|
|
|
|
loopback = newInterface("lo0", "127.0.0.1/8", false, true)
|
|
|
|
)
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
goos string
|
|
|
|
il interfaces.List
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
wantInt []netip.Prefix
|
|
|
|
wantExt []netip.Prefix
|
2021-10-14 20:40:06 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "single-interface",
|
|
|
|
goos: "linux",
|
|
|
|
il: iList(
|
|
|
|
en0,
|
|
|
|
loopback,
|
|
|
|
),
|
|
|
|
wantInt: masked(loopback),
|
|
|
|
wantExt: masked(en0),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "multiple-interfaces",
|
|
|
|
goos: "linux",
|
|
|
|
il: iList(
|
|
|
|
en0,
|
|
|
|
en1,
|
|
|
|
wsl,
|
|
|
|
loopback,
|
|
|
|
),
|
|
|
|
wantInt: masked(loopback),
|
|
|
|
wantExt: masked(en0, en1, wsl),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "wsl2",
|
|
|
|
goos: "windows",
|
|
|
|
il: iList(
|
|
|
|
en0,
|
|
|
|
en1,
|
|
|
|
wsl,
|
|
|
|
loopback,
|
|
|
|
),
|
|
|
|
wantInt: masked(loopback, wsl),
|
|
|
|
wantExt: masked(en0, en1),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
gotInt, gotExt, err := internalAndExternalInterfacesFrom(tc.il, tc.goos)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(gotInt, tc.wantInt) {
|
|
|
|
t.Errorf("unexpected internal prefixes\ngot %v\nwant %v", gotInt, tc.wantInt)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(gotExt, tc.wantExt) {
|
|
|
|
t.Errorf("unexpected external prefixes\ngot %v\nwant %v", gotExt, tc.wantExt)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-11-02 20:13:26 +00:00
|
|
|
|
|
|
|
func TestPacketFilterPermitsUnlockedNodes(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
peers []*tailcfg.Node
|
|
|
|
filter []filter.Match
|
|
|
|
want bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "empty",
|
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no-unsigned",
|
|
|
|
peers: []*tailcfg.Node{
|
|
|
|
{ID: 1},
|
|
|
|
},
|
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "unsigned-good",
|
|
|
|
peers: []*tailcfg.Node{
|
|
|
|
{ID: 1, UnsignedPeerAPIOnly: true},
|
|
|
|
},
|
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "unsigned-bad",
|
|
|
|
peers: []*tailcfg.Node{
|
|
|
|
{
|
|
|
|
ID: 1,
|
|
|
|
UnsignedPeerAPIOnly: true,
|
|
|
|
AllowedIPs: []netip.Prefix{
|
|
|
|
netip.MustParsePrefix("100.64.0.0/32"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
filter: []filter.Match{
|
|
|
|
{
|
|
|
|
Srcs: []netip.Prefix{netip.MustParsePrefix("100.64.0.0/32")},
|
|
|
|
Dsts: []filter.NetPortRange{
|
|
|
|
{
|
|
|
|
Net: netip.MustParsePrefix("100.99.0.0/32"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "unsigned-bad-src-is-superset",
|
|
|
|
peers: []*tailcfg.Node{
|
|
|
|
{
|
|
|
|
ID: 1,
|
|
|
|
UnsignedPeerAPIOnly: true,
|
|
|
|
AllowedIPs: []netip.Prefix{
|
|
|
|
netip.MustParsePrefix("100.64.0.0/32"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
filter: []filter.Match{
|
|
|
|
{
|
|
|
|
Srcs: []netip.Prefix{netip.MustParsePrefix("100.64.0.0/24")},
|
|
|
|
Dsts: []filter.NetPortRange{
|
|
|
|
{
|
|
|
|
Net: netip.MustParsePrefix("100.99.0.0/32"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "unsigned-okay-because-no-dsts",
|
|
|
|
peers: []*tailcfg.Node{
|
|
|
|
{
|
|
|
|
ID: 1,
|
|
|
|
UnsignedPeerAPIOnly: true,
|
|
|
|
AllowedIPs: []netip.Prefix{
|
|
|
|
netip.MustParsePrefix("100.64.0.0/32"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
filter: []filter.Match{
|
|
|
|
{
|
|
|
|
Srcs: []netip.Prefix{netip.MustParsePrefix("100.64.0.0/32")},
|
|
|
|
Caps: []filter.CapMatch{
|
|
|
|
{
|
|
|
|
Dst: netip.MustParsePrefix("100.99.0.0/32"),
|
|
|
|
Cap: "foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2023-09-17 07:13:52 +00:00
|
|
|
if got := packetFilterPermitsUnlockedNodes(peersMap(nodeViews(tt.peers)), tt.filter); got != tt.want {
|
2022-11-02 20:13:26 +00:00
|
|
|
t.Errorf("got %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2022-11-29 02:47:05 +00:00
|
|
|
|
2022-12-19 18:23:47 +00:00
|
|
|
func TestStatusWithoutPeers(t *testing.T) {
|
2023-09-20 14:34:12 +00:00
|
|
|
b := newTestLocalBackend(t)
|
2022-12-19 18:23:47 +00:00
|
|
|
|
|
|
|
var cc *mockControl
|
|
|
|
b.SetControlClientGetterForTesting(func(opts controlclient.Options) (controlclient.Client, error) {
|
|
|
|
cc = newClient(t, opts)
|
|
|
|
|
|
|
|
t.Logf("ccGen: new mockControl.")
|
|
|
|
cc.called("New")
|
|
|
|
return cc, nil
|
|
|
|
})
|
|
|
|
b.Start(ipn.Options{})
|
|
|
|
b.Login(nil)
|
|
|
|
cc.send(nil, "", false, &netmap.NetworkMap{
|
2023-08-21 17:53:57 +00:00
|
|
|
SelfNode: (&tailcfg.Node{
|
2023-09-18 06:31:34 +00:00
|
|
|
MachineAuthorized: true,
|
|
|
|
Addresses: ipps("100.101.101.101"),
|
2023-08-21 17:53:57 +00:00
|
|
|
}).View(),
|
2022-12-19 18:23:47 +00:00
|
|
|
})
|
|
|
|
got := b.StatusWithoutPeers()
|
|
|
|
if got.TailscaleIPs == nil {
|
|
|
|
t.Errorf("got nil, expected TailscaleIPs value to not be nil")
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(got.TailscaleIPs, got.Self.TailscaleIPs) {
|
|
|
|
t.Errorf("got %v, expected %v", got.TailscaleIPs, got.Self.TailscaleIPs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-29 02:47:05 +00:00
|
|
|
// legacyBackend was the interface between Tailscale frontends
|
|
|
|
// (e.g. cmd/tailscale, iOS/MacOS/Windows GUIs) and the tailscale
|
|
|
|
// backend (e.g. cmd/tailscaled) running on the same machine.
|
|
|
|
// (It has nothing to do with the interface between the backends
|
|
|
|
// and the cloud control plane.)
|
|
|
|
type legacyBackend interface {
|
|
|
|
// SetNotifyCallback sets the callback to be called on updates
|
|
|
|
// from the backend to the client.
|
|
|
|
SetNotifyCallback(func(ipn.Notify))
|
|
|
|
// Start starts or restarts the backend, typically when a
|
|
|
|
// frontend client connects.
|
|
|
|
Start(ipn.Options) error
|
|
|
|
// StartLoginInteractive requests to start a new interactive login
|
|
|
|
// flow. This should trigger a new BrowseToURL notification
|
|
|
|
// eventually.
|
|
|
|
StartLoginInteractive()
|
|
|
|
// Login logs in with an OAuth2 token.
|
|
|
|
Login(token *tailcfg.Oauth2Token)
|
|
|
|
// SetPrefs installs a new set of user preferences, including
|
|
|
|
// WantRunning. This may cause the wireguard engine to
|
|
|
|
// reconfigure or stop.
|
|
|
|
SetPrefs(*ipn.Prefs)
|
|
|
|
// RequestEngineStatus polls for an update from the wireguard
|
|
|
|
// engine. Only needed if you want to display byte
|
|
|
|
// counts. Connection events are emitted automatically without
|
|
|
|
// polling.
|
|
|
|
RequestEngineStatus()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that LocalBackend still implements the legacyBackend interface
|
|
|
|
// for now, at least until the macOS and iOS clients move off of it.
|
|
|
|
var _ legacyBackend = (*LocalBackend)(nil)
|
2023-01-17 20:59:03 +00:00
|
|
|
|
|
|
|
func TestWatchNotificationsCallbacks(t *testing.T) {
|
|
|
|
b := new(LocalBackend)
|
2023-09-05 17:51:52 +00:00
|
|
|
// activeWatchSessions is typically set in NewLocalBackend
|
|
|
|
// so WatchNotifications expects it to be non-empty.
|
|
|
|
b.activeWatchSessions = make(set.Set[string])
|
2023-01-17 20:59:03 +00:00
|
|
|
n := new(ipn.Notify)
|
|
|
|
b.WatchNotifications(context.Background(), 0, func() {
|
|
|
|
b.mu.Lock()
|
|
|
|
defer b.mu.Unlock()
|
|
|
|
|
|
|
|
// Ensure a watcher has been installed.
|
|
|
|
if len(b.notifyWatchers) != 1 {
|
|
|
|
t.Fatalf("unexpected number of watchers in new LocalBackend, want: 1 got: %v", len(b.notifyWatchers))
|
|
|
|
}
|
|
|
|
// Send a notification. Range over notifyWatchers to get the channel
|
|
|
|
// because WatchNotifications doesn't expose the handle for it.
|
2023-09-18 14:30:58 +00:00
|
|
|
for _, sess := range b.notifyWatchers {
|
2023-01-17 20:59:03 +00:00
|
|
|
select {
|
2023-09-18 14:30:58 +00:00
|
|
|
case sess.ch <- n:
|
2023-01-17 20:59:03 +00:00
|
|
|
default:
|
|
|
|
t.Fatalf("could not send notification")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, func(roNotify *ipn.Notify) bool {
|
|
|
|
if roNotify != n {
|
|
|
|
t.Fatalf("unexpected notification received. want: %v got: %v", n, roNotify)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
|
|
|
// Ensure watchers have been cleaned up.
|
|
|
|
b.mu.Lock()
|
|
|
|
defer b.mu.Unlock()
|
|
|
|
if len(b.notifyWatchers) != 0 {
|
|
|
|
t.Fatalf("unexpected number of watchers in new LocalBackend, want: 0 got: %v", len(b.notifyWatchers))
|
|
|
|
}
|
|
|
|
}
|
2023-09-02 02:28:00 +00:00
|
|
|
|
|
|
|
// tests LocalBackend.updateNetmapDeltaLocked
|
|
|
|
func TestUpdateNetmapDelta(t *testing.T) {
|
|
|
|
var b LocalBackend
|
|
|
|
if b.updateNetmapDeltaLocked(nil) {
|
|
|
|
t.Errorf("updateNetmapDeltaLocked() = true, want false with nil netmap")
|
|
|
|
}
|
|
|
|
|
|
|
|
b.netMap = &netmap.NetworkMap{}
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
b.netMap.Peers = append(b.netMap.Peers, (&tailcfg.Node{ID: (tailcfg.NodeID(i) + 1)}).View())
|
|
|
|
}
|
2023-09-17 07:13:52 +00:00
|
|
|
b.updatePeersFromNetmapLocked(b.netMap)
|
2023-09-02 02:28:00 +00:00
|
|
|
|
|
|
|
someTime := time.Unix(123, 0)
|
|
|
|
muts, ok := netmap.MutationsFromMapResponse(&tailcfg.MapResponse{
|
|
|
|
PeersChangedPatch: []*tailcfg.PeerChange{
|
|
|
|
{
|
|
|
|
NodeID: 1,
|
|
|
|
DERPRegion: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
NodeID: 2,
|
|
|
|
Online: ptr.To(true),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
NodeID: 3,
|
|
|
|
Online: ptr.To(false),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
NodeID: 4,
|
|
|
|
LastSeen: ptr.To(someTime),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, someTime)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("netmap.MutationsFromMapResponse failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !b.updateNetmapDeltaLocked(muts) {
|
|
|
|
t.Fatalf("updateNetmapDeltaLocked() = false, want true with new netmap")
|
|
|
|
}
|
|
|
|
|
|
|
|
wants := []*tailcfg.Node{
|
|
|
|
{
|
|
|
|
ID: 1,
|
2023-09-17 07:13:52 +00:00
|
|
|
DERP: "127.3.3.40:1",
|
2023-09-02 02:28:00 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: 2,
|
|
|
|
Online: ptr.To(true),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: 3,
|
|
|
|
Online: ptr.To(false),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: 4,
|
|
|
|
LastSeen: ptr.To(someTime),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, want := range wants {
|
2023-09-17 07:13:52 +00:00
|
|
|
gotv, ok := b.peers[want.ID]
|
|
|
|
if !ok {
|
|
|
|
t.Errorf("netmap.Peer %v missing from b.peers", want.ID)
|
2023-09-02 02:28:00 +00:00
|
|
|
continue
|
|
|
|
}
|
2023-09-17 07:13:52 +00:00
|
|
|
got := gotv.AsStruct()
|
2023-09-02 02:28:00 +00:00
|
|
|
if !reflect.DeepEqual(got, want) {
|
|
|
|
t.Errorf("netmap.Peer %v wrong.\n got: %v\nwant: %v", want.ID, logger.AsJSON(got), logger.AsJSON(want))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-09-20 14:34:12 +00:00
|
|
|
|
|
|
|
// tests WhoIs and indirectly that setNetMapLocked updates b.nodeByAddr correctly.
|
|
|
|
func TestWhoIs(t *testing.T) {
|
|
|
|
b := newTestLocalBackend(t)
|
|
|
|
b.setNetMapLocked(&netmap.NetworkMap{
|
|
|
|
SelfNode: (&tailcfg.Node{
|
|
|
|
ID: 1,
|
|
|
|
User: 10,
|
|
|
|
Addresses: []netip.Prefix{netip.MustParsePrefix("100.101.102.103/32")},
|
|
|
|
}).View(),
|
|
|
|
Peers: []tailcfg.NodeView{
|
|
|
|
(&tailcfg.Node{
|
|
|
|
ID: 2,
|
|
|
|
User: 20,
|
|
|
|
Addresses: []netip.Prefix{netip.MustParsePrefix("100.200.200.200/32")},
|
|
|
|
}).View(),
|
|
|
|
},
|
|
|
|
UserProfiles: map[tailcfg.UserID]tailcfg.UserProfile{
|
|
|
|
10: {
|
|
|
|
DisplayName: "Myself",
|
|
|
|
},
|
|
|
|
20: {
|
|
|
|
DisplayName: "Peer",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
tests := []struct {
|
|
|
|
q string
|
|
|
|
want tailcfg.NodeID // 0 means want ok=false
|
|
|
|
wantName string
|
|
|
|
}{
|
|
|
|
{"100.101.102.103:0", 1, "Myself"},
|
|
|
|
{"100.101.102.103:123", 1, "Myself"},
|
|
|
|
{"100.200.200.200:0", 2, "Peer"},
|
|
|
|
{"100.200.200.200:123", 2, "Peer"},
|
|
|
|
{"100.4.0.4:404", 0, ""},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.q, func(t *testing.T) {
|
|
|
|
nv, up, ok := b.WhoIs(netip.MustParseAddrPort(tt.q))
|
|
|
|
var got tailcfg.NodeID
|
|
|
|
if ok {
|
|
|
|
got = nv.ID()
|
|
|
|
}
|
|
|
|
if got != tt.want {
|
|
|
|
t.Errorf("got nodeID %v; want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
if up.DisplayName != tt.wantName {
|
|
|
|
t.Errorf("got name %q; want %q", up.DisplayName, tt.wantName)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|