2020-08-08 03:44:04 +00:00
|
|
|
// Copyright (c) 2020 Tailscale Inc & AUTHORS All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package controlclient
|
|
|
|
|
|
|
|
import (
|
2021-02-05 23:44:46 +00:00
|
|
|
"encoding/json"
|
2020-08-08 03:44:04 +00:00
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2021-04-15 23:03:59 +00:00
|
|
|
"time"
|
2020-08-08 03:44:04 +00:00
|
|
|
|
tailcfg: add Endpoint, EndpointType, MapRequest.EndpointType
Track endpoints internally with a new tailcfg.Endpoint type that
includes a typed netaddr.IPPort (instead of just a string) and
includes a type for how that endpoint was discovered (STUN, local,
etc).
Use []tailcfg.Endpoint instead of []string internally.
At the last second, send it to the control server as the existing
[]string for endpoints, but also include a new parallel
MapRequest.EndpointType []tailcfg.EndpointType, so the control server
can start filtering out less-important endpoint changes from
new-enough clients. Notably, STUN-discovered endpoints can be filtered
out from 1.6+ clients, as they can discover them amongst each other
via CallMeMaybe disco exchanges started over DERP. And STUN endpoints
change a lot, causing a lot of MapResposne updates. But portmapped
endpoints are worth keeping for now, as they they work right away
without requiring the firewall traversal extra RTT dance.
End result will be less control->client bandwidth. (despite negligible
increase in client->control bandwidth)
Updates tailscale/corp#1543
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2021-04-12 20:24:29 +00:00
|
|
|
"inet.af/netaddr"
|
2020-08-08 03:44:04 +00:00
|
|
|
"tailscale.com/tailcfg"
|
2021-01-08 13:49:29 +00:00
|
|
|
"tailscale.com/types/wgkey"
|
2020-08-08 03:44:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestUndeltaPeers(t *testing.T) {
|
2021-04-15 23:03:59 +00:00
|
|
|
defer func(old func() time.Time) { clockNow = old }(clockNow)
|
|
|
|
|
|
|
|
var curTime time.Time
|
|
|
|
clockNow = func() time.Time {
|
|
|
|
return curTime
|
|
|
|
}
|
|
|
|
online := func(v bool) func(*tailcfg.Node) {
|
|
|
|
return func(n *tailcfg.Node) {
|
|
|
|
n.Online = &v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
seenAt := func(t time.Time) func(*tailcfg.Node) {
|
|
|
|
return func(n *tailcfg.Node) {
|
|
|
|
n.LastSeen = &t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
n := func(id tailcfg.NodeID, name string, mod ...func(*tailcfg.Node)) *tailcfg.Node {
|
|
|
|
n := &tailcfg.Node{ID: id, Name: name}
|
|
|
|
for _, f := range mod {
|
|
|
|
f(n)
|
|
|
|
}
|
|
|
|
return n
|
2020-08-08 03:44:04 +00:00
|
|
|
}
|
|
|
|
peers := func(nv ...*tailcfg.Node) []*tailcfg.Node { return nv }
|
|
|
|
tests := []struct {
|
2021-04-15 23:03:59 +00:00
|
|
|
name string
|
|
|
|
mapRes *tailcfg.MapResponse
|
|
|
|
curTime time.Time
|
|
|
|
prev []*tailcfg.Node
|
|
|
|
want []*tailcfg.Node
|
2020-08-08 03:44:04 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "full_peers",
|
|
|
|
mapRes: &tailcfg.MapResponse{
|
|
|
|
Peers: peers(n(1, "foo"), n(2, "bar")),
|
|
|
|
},
|
|
|
|
want: peers(n(1, "foo"), n(2, "bar")),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "full_peers_ignores_deltas",
|
|
|
|
mapRes: &tailcfg.MapResponse{
|
|
|
|
Peers: peers(n(1, "foo"), n(2, "bar")),
|
|
|
|
PeersRemoved: []tailcfg.NodeID{2},
|
|
|
|
},
|
|
|
|
want: peers(n(1, "foo"), n(2, "bar")),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "add_and_update",
|
|
|
|
prev: peers(n(1, "foo"), n(2, "bar")),
|
|
|
|
mapRes: &tailcfg.MapResponse{
|
|
|
|
PeersChanged: peers(n(0, "zero"), n(2, "bar2"), n(3, "three")),
|
|
|
|
},
|
|
|
|
want: peers(n(0, "zero"), n(1, "foo"), n(2, "bar2"), n(3, "three")),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "remove",
|
|
|
|
prev: peers(n(1, "foo"), n(2, "bar")),
|
|
|
|
mapRes: &tailcfg.MapResponse{
|
|
|
|
PeersRemoved: []tailcfg.NodeID{1},
|
|
|
|
},
|
|
|
|
want: peers(n(2, "bar")),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "add_and_remove",
|
|
|
|
prev: peers(n(1, "foo"), n(2, "bar")),
|
|
|
|
mapRes: &tailcfg.MapResponse{
|
|
|
|
PeersChanged: peers(n(1, "foo2")),
|
|
|
|
PeersRemoved: []tailcfg.NodeID{2},
|
|
|
|
},
|
|
|
|
want: peers(n(1, "foo2")),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "unchanged",
|
|
|
|
prev: peers(n(1, "foo"), n(2, "bar")),
|
|
|
|
mapRes: &tailcfg.MapResponse{},
|
|
|
|
want: peers(n(1, "foo"), n(2, "bar")),
|
|
|
|
},
|
2021-04-15 23:03:59 +00:00
|
|
|
{
|
|
|
|
name: "online_change",
|
|
|
|
prev: peers(n(1, "foo"), n(2, "bar")),
|
|
|
|
mapRes: &tailcfg.MapResponse{
|
|
|
|
OnlineChange: map[tailcfg.NodeID]bool{
|
|
|
|
1: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: peers(
|
|
|
|
n(1, "foo", online(true)),
|
|
|
|
n(2, "bar"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "online_change_offline",
|
|
|
|
prev: peers(n(1, "foo"), n(2, "bar")),
|
|
|
|
mapRes: &tailcfg.MapResponse{
|
|
|
|
OnlineChange: map[tailcfg.NodeID]bool{
|
|
|
|
1: false,
|
|
|
|
2: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: peers(
|
|
|
|
n(1, "foo", online(false)),
|
|
|
|
n(2, "bar", online(true)),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "peer_seen_at",
|
|
|
|
prev: peers(n(1, "foo", seenAt(time.Unix(111, 0))), n(2, "bar")),
|
|
|
|
curTime: time.Unix(123, 0),
|
|
|
|
mapRes: &tailcfg.MapResponse{
|
|
|
|
PeerSeenChange: map[tailcfg.NodeID]bool{
|
|
|
|
1: false,
|
|
|
|
2: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: peers(
|
|
|
|
n(1, "foo"),
|
|
|
|
n(2, "bar", seenAt(time.Unix(123, 0))),
|
|
|
|
),
|
|
|
|
},
|
2020-08-08 03:44:04 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2021-04-15 23:03:59 +00:00
|
|
|
if !tt.curTime.IsZero() {
|
|
|
|
curTime = tt.curTime
|
|
|
|
}
|
2020-08-08 03:44:04 +00:00
|
|
|
undeltaPeers(tt.mapRes, tt.prev)
|
|
|
|
if !reflect.DeepEqual(tt.mapRes.Peers, tt.want) {
|
|
|
|
t.Errorf("wrong results\n got: %s\nwant: %s", formatNodes(tt.mapRes.Peers), formatNodes(tt.want))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func formatNodes(nodes []*tailcfg.Node) string {
|
|
|
|
var sb strings.Builder
|
|
|
|
for i, n := range nodes {
|
|
|
|
if i > 0 {
|
|
|
|
sb.WriteString(", ")
|
|
|
|
}
|
2021-04-15 23:03:59 +00:00
|
|
|
var extra string
|
|
|
|
if n.Online != nil {
|
|
|
|
extra += fmt.Sprintf(", online=%v", *n.Online)
|
|
|
|
}
|
|
|
|
if n.LastSeen != nil {
|
|
|
|
extra += fmt.Sprintf(", lastSeen=%v", n.LastSeen.Unix())
|
|
|
|
}
|
|
|
|
fmt.Fprintf(&sb, "(%d, %q%s)", n.ID, n.Name, extra)
|
2020-08-08 03:44:04 +00:00
|
|
|
}
|
|
|
|
return sb.String()
|
|
|
|
}
|
2021-01-08 13:49:29 +00:00
|
|
|
|
|
|
|
func TestNewDirect(t *testing.T) {
|
|
|
|
hi := NewHostinfo()
|
|
|
|
ni := tailcfg.NetInfo{LinkType: "wired"}
|
|
|
|
hi.NetInfo = &ni
|
|
|
|
|
|
|
|
key, err := wgkey.NewPrivate()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2021-03-31 15:51:22 +00:00
|
|
|
opts := Options{
|
|
|
|
ServerURL: "https://example.com",
|
|
|
|
Hostinfo: hi,
|
|
|
|
GetMachinePrivateKey: func() (wgkey.Private, error) {
|
|
|
|
return key, nil
|
|
|
|
},
|
|
|
|
}
|
2021-01-08 13:49:29 +00:00
|
|
|
c, err := NewDirect(opts)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.serverURL != opts.ServerURL {
|
|
|
|
t.Errorf("c.serverURL got %v want %v", c.serverURL, opts.ServerURL)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !hi.Equal(c.hostinfo) {
|
|
|
|
t.Errorf("c.hostinfo got %v want %v", c.hostinfo, hi)
|
|
|
|
}
|
|
|
|
|
|
|
|
changed := c.SetNetInfo(&ni)
|
|
|
|
if changed {
|
|
|
|
t.Errorf("c.SetNetInfo(ni) want false got %v", changed)
|
|
|
|
}
|
|
|
|
ni = tailcfg.NetInfo{LinkType: "wifi"}
|
|
|
|
changed = c.SetNetInfo(&ni)
|
|
|
|
if !changed {
|
|
|
|
t.Errorf("c.SetNetInfo(ni) want true got %v", changed)
|
|
|
|
}
|
|
|
|
|
|
|
|
changed = c.SetHostinfo(hi)
|
|
|
|
if changed {
|
|
|
|
t.Errorf("c.SetHostinfo(hi) want false got %v", changed)
|
|
|
|
}
|
|
|
|
hi = NewHostinfo()
|
|
|
|
hi.Hostname = "different host name"
|
|
|
|
changed = c.SetHostinfo(hi)
|
|
|
|
if !changed {
|
|
|
|
t.Errorf("c.SetHostinfo(hi) want true got %v", changed)
|
|
|
|
}
|
|
|
|
|
tailcfg: add Endpoint, EndpointType, MapRequest.EndpointType
Track endpoints internally with a new tailcfg.Endpoint type that
includes a typed netaddr.IPPort (instead of just a string) and
includes a type for how that endpoint was discovered (STUN, local,
etc).
Use []tailcfg.Endpoint instead of []string internally.
At the last second, send it to the control server as the existing
[]string for endpoints, but also include a new parallel
MapRequest.EndpointType []tailcfg.EndpointType, so the control server
can start filtering out less-important endpoint changes from
new-enough clients. Notably, STUN-discovered endpoints can be filtered
out from 1.6+ clients, as they can discover them amongst each other
via CallMeMaybe disco exchanges started over DERP. And STUN endpoints
change a lot, causing a lot of MapResposne updates. But portmapped
endpoints are worth keeping for now, as they they work right away
without requiring the firewall traversal extra RTT dance.
End result will be less control->client bandwidth. (despite negligible
increase in client->control bandwidth)
Updates tailscale/corp#1543
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2021-04-12 20:24:29 +00:00
|
|
|
endpoints := fakeEndpoints(1, 2, 3)
|
2021-01-08 13:49:29 +00:00
|
|
|
changed = c.newEndpoints(12, endpoints)
|
|
|
|
if !changed {
|
|
|
|
t.Errorf("c.newEndpoints(12) want true got %v", changed)
|
|
|
|
}
|
|
|
|
changed = c.newEndpoints(12, endpoints)
|
|
|
|
if changed {
|
|
|
|
t.Errorf("c.newEndpoints(12) want false got %v", changed)
|
|
|
|
}
|
|
|
|
changed = c.newEndpoints(13, endpoints)
|
|
|
|
if !changed {
|
|
|
|
t.Errorf("c.newEndpoints(13) want true got %v", changed)
|
|
|
|
}
|
tailcfg: add Endpoint, EndpointType, MapRequest.EndpointType
Track endpoints internally with a new tailcfg.Endpoint type that
includes a typed netaddr.IPPort (instead of just a string) and
includes a type for how that endpoint was discovered (STUN, local,
etc).
Use []tailcfg.Endpoint instead of []string internally.
At the last second, send it to the control server as the existing
[]string for endpoints, but also include a new parallel
MapRequest.EndpointType []tailcfg.EndpointType, so the control server
can start filtering out less-important endpoint changes from
new-enough clients. Notably, STUN-discovered endpoints can be filtered
out from 1.6+ clients, as they can discover them amongst each other
via CallMeMaybe disco exchanges started over DERP. And STUN endpoints
change a lot, causing a lot of MapResposne updates. But portmapped
endpoints are worth keeping for now, as they they work right away
without requiring the firewall traversal extra RTT dance.
End result will be less control->client bandwidth. (despite negligible
increase in client->control bandwidth)
Updates tailscale/corp#1543
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2021-04-12 20:24:29 +00:00
|
|
|
endpoints = fakeEndpoints(4, 5, 6)
|
2021-01-08 13:49:29 +00:00
|
|
|
changed = c.newEndpoints(13, endpoints)
|
|
|
|
if !changed {
|
|
|
|
t.Errorf("c.newEndpoints(13) want true got %v", changed)
|
|
|
|
}
|
|
|
|
}
|
2021-02-05 23:44:46 +00:00
|
|
|
|
tailcfg: add Endpoint, EndpointType, MapRequest.EndpointType
Track endpoints internally with a new tailcfg.Endpoint type that
includes a typed netaddr.IPPort (instead of just a string) and
includes a type for how that endpoint was discovered (STUN, local,
etc).
Use []tailcfg.Endpoint instead of []string internally.
At the last second, send it to the control server as the existing
[]string for endpoints, but also include a new parallel
MapRequest.EndpointType []tailcfg.EndpointType, so the control server
can start filtering out less-important endpoint changes from
new-enough clients. Notably, STUN-discovered endpoints can be filtered
out from 1.6+ clients, as they can discover them amongst each other
via CallMeMaybe disco exchanges started over DERP. And STUN endpoints
change a lot, causing a lot of MapResposne updates. But portmapped
endpoints are worth keeping for now, as they they work right away
without requiring the firewall traversal extra RTT dance.
End result will be less control->client bandwidth. (despite negligible
increase in client->control bandwidth)
Updates tailscale/corp#1543
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2021-04-12 20:24:29 +00:00
|
|
|
func fakeEndpoints(ports ...uint16) (ret []tailcfg.Endpoint) {
|
|
|
|
for _, port := range ports {
|
|
|
|
ret = append(ret, tailcfg.Endpoint{
|
|
|
|
Addr: netaddr.IPPort{Port: port},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-02-05 23:44:46 +00:00
|
|
|
func TestNewHostinfo(t *testing.T) {
|
|
|
|
hi := NewHostinfo()
|
|
|
|
if hi == nil {
|
|
|
|
t.Fatal("no Hostinfo")
|
|
|
|
}
|
|
|
|
j, err := json.MarshalIndent(hi, " ", "")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
t.Logf("Got: %s", j)
|
|
|
|
}
|