mirror of https://github.com/tailscale/tailscale/
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
278 lines
7.0 KiB
Go
278 lines
7.0 KiB
Go
5 years ago
|
// 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.
|
||
|
|
||
4 years ago
|
// Package netmap contains the netmap.NetworkMap type.
|
||
|
package netmap
|
||
5 years ago
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"fmt"
|
||
5 years ago
|
"reflect"
|
||
5 years ago
|
"strings"
|
||
|
"time"
|
||
|
|
||
4 years ago
|
"inet.af/netaddr"
|
||
5 years ago
|
"tailscale.com/tailcfg"
|
||
4 years ago
|
"tailscale.com/types/wgkey"
|
||
5 years ago
|
"tailscale.com/wgengine/filter"
|
||
|
)
|
||
|
|
||
|
type NetworkMap struct {
|
||
|
// Core networking
|
||
|
|
||
4 years ago
|
SelfNode *tailcfg.Node
|
||
4 years ago
|
NodeKey tailcfg.NodeKey
|
||
4 years ago
|
PrivateKey wgkey.Private
|
||
4 years ago
|
Expiry time.Time
|
||
|
// Name is the DNS name assigned to this node.
|
||
4 years ago
|
Name string
|
||
4 years ago
|
Addresses []netaddr.IPPrefix
|
||
5 years ago
|
LocalPort uint16 // used for debugging
|
||
|
MachineStatus tailcfg.MachineStatus
|
||
4 years ago
|
MachineKey tailcfg.MachineKey
|
||
4 years ago
|
Peers []*tailcfg.Node // sorted by Node.ID
|
||
4 years ago
|
DNS tailcfg.DNSConfig
|
||
5 years ago
|
Hostinfo tailcfg.Hostinfo
|
||
4 years ago
|
PacketFilter []filter.Match
|
||
5 years ago
|
|
||
4 years ago
|
// CollectServices reports whether this node's Tailnet has
|
||
|
// requested that info about services be included in HostInfo.
|
||
|
// If set, Hostinfo.ShieldsUp blocks services collection; that
|
||
|
// takes precedence over this field.
|
||
|
CollectServices bool
|
||
|
|
||
5 years ago
|
// DERPMap is the last DERP server map received. It's reused
|
||
|
// between updates and should not be modified.
|
||
|
DERPMap *tailcfg.DERPMap
|
||
|
|
||
5 years ago
|
// Debug knobs from control server for debug or feature gating.
|
||
|
Debug *tailcfg.Debug
|
||
|
|
||
5 years ago
|
// ACLs
|
||
|
|
||
4 years ago
|
User tailcfg.UserID
|
||
|
Domain string
|
||
|
// TODO(crawshaw): reduce UserProfiles to []tailcfg.UserProfile?
|
||
|
// There are lots of ways to slice this data, leave it up to users.
|
||
|
UserProfiles map[tailcfg.UserID]tailcfg.UserProfile
|
||
5 years ago
|
// TODO(crawshaw): Groups []tailcfg.Group
|
||
|
// TODO(crawshaw): Capabilities []tailcfg.Capability
|
||
|
}
|
||
|
|
||
4 years ago
|
// MagicDNSSuffix returns the domain's MagicDNS suffix (even if
|
||
|
// MagicDNS isn't necessarily in use).
|
||
|
//
|
||
|
// It will neither start nor end with a period.
|
||
4 years ago
|
func (nm *NetworkMap) MagicDNSSuffix() string {
|
||
4 years ago
|
name := strings.Trim(nm.Name, ".")
|
||
|
if i := strings.Index(name, "."); i != -1 {
|
||
|
name = name[i+1:]
|
||
4 years ago
|
}
|
||
4 years ago
|
return name
|
||
4 years ago
|
}
|
||
|
|
||
|
func (nm *NetworkMap) String() string {
|
||
5 years ago
|
return nm.Concise()
|
||
|
}
|
||
|
|
||
|
func (nm *NetworkMap) Concise() string {
|
||
|
buf := new(strings.Builder)
|
||
4 years ago
|
|
||
|
nm.printConciseHeader(buf)
|
||
|
for _, p := range nm.Peers {
|
||
|
printPeerConcise(buf, p)
|
||
|
}
|
||
|
return buf.String()
|
||
|
}
|
||
|
|
||
|
// printConciseHeader prints a concise header line representing nm to buf.
|
||
|
//
|
||
|
// If this function is changed to access different fields of nm, keep
|
||
|
// in equalConciseHeader in sync.
|
||
|
func (nm *NetworkMap) printConciseHeader(buf *strings.Builder) {
|
||
5 years ago
|
fmt.Fprintf(buf, "netmap: self: %v auth=%v",
|
||
|
nm.NodeKey.ShortString(), nm.MachineStatus)
|
||
4 years ago
|
login := nm.UserProfiles[nm.User].LoginName
|
||
|
if login == "" {
|
||
|
if nm.User.IsZero() {
|
||
|
login = "?"
|
||
|
} else {
|
||
|
login = fmt.Sprint(nm.User)
|
||
|
}
|
||
|
}
|
||
|
fmt.Fprintf(buf, " u=%s", login)
|
||
5 years ago
|
if nm.LocalPort != 0 {
|
||
|
fmt.Fprintf(buf, " port=%v", nm.LocalPort)
|
||
|
}
|
||
|
if nm.Debug != nil {
|
||
|
j, _ := json.Marshal(nm.Debug)
|
||
|
fmt.Fprintf(buf, " debug=%s", j)
|
||
|
}
|
||
|
fmt.Fprintf(buf, " %v", nm.Addresses)
|
||
|
buf.WriteByte('\n')
|
||
4 years ago
|
}
|
||
5 years ago
|
|
||
4 years ago
|
// equalConciseHeader reports whether a and b are equal for the fields
|
||
|
// used by printConciseHeader.
|
||
|
func (a *NetworkMap) equalConciseHeader(b *NetworkMap) bool {
|
||
|
if a.NodeKey != b.NodeKey ||
|
||
|
a.MachineStatus != b.MachineStatus ||
|
||
|
a.LocalPort != b.LocalPort ||
|
||
4 years ago
|
a.User != b.User ||
|
||
4 years ago
|
len(a.Addresses) != len(b.Addresses) {
|
||
|
return false
|
||
|
}
|
||
|
for i, a := range a.Addresses {
|
||
|
if b.Addresses[i] != a {
|
||
|
return false
|
||
5 years ago
|
}
|
||
4 years ago
|
}
|
||
|
return (a.Debug == nil && b.Debug == nil) || reflect.DeepEqual(a.Debug, b.Debug)
|
||
|
}
|
||
|
|
||
|
// printPeerConcise appends to buf a line repsenting the peer p.
|
||
|
//
|
||
|
// If this function is changed to access different fields of p, keep
|
||
|
// in nodeConciseEqual in sync.
|
||
|
func printPeerConcise(buf *strings.Builder, p *tailcfg.Node) {
|
||
|
aip := make([]string, len(p.AllowedIPs))
|
||
|
for i, a := range p.AllowedIPs {
|
||
|
s := strings.TrimSuffix(fmt.Sprint(a), "/32")
|
||
|
aip[i] = s
|
||
|
}
|
||
5 years ago
|
|
||
4 years ago
|
ep := make([]string, len(p.Endpoints))
|
||
|
for i, e := range p.Endpoints {
|
||
|
// Align vertically on the ':' between IP and port
|
||
|
colon := strings.IndexByte(e, ':')
|
||
|
spaces := 0
|
||
|
for colon > 0 && len(e)+spaces-colon < 6 {
|
||
|
spaces++
|
||
|
colon--
|
||
5 years ago
|
}
|
||
4 years ago
|
ep[i] = fmt.Sprintf("%21v", e+strings.Repeat(" ", spaces))
|
||
|
}
|
||
5 years ago
|
|
||
4 years ago
|
derp := p.DERP
|
||
|
const derpPrefix = "127.3.3.40:"
|
||
|
if strings.HasPrefix(derp, derpPrefix) {
|
||
|
derp = "D" + derp[len(derpPrefix):]
|
||
5 years ago
|
}
|
||
4 years ago
|
var discoShort string
|
||
|
if !p.DiscoKey.IsZero() {
|
||
|
discoShort = p.DiscoKey.ShortString() + " "
|
||
|
}
|
||
4 years ago
|
|
||
|
// Most of the time, aip is just one element, so format the
|
||
|
// table to look good in that case. This will also make multi-
|
||
|
// subnet nodes stand out visually.
|
||
4 years ago
|
fmt.Fprintf(buf, " %v %s%-2v %-15v : %v\n",
|
||
|
p.Key.ShortString(),
|
||
|
discoShort,
|
||
|
derp,
|
||
4 years ago
|
strings.Join(aip, " "),
|
||
|
strings.Join(ep, " "))
|
||
5 years ago
|
}
|
||
|
|
||
4 years ago
|
// nodeConciseEqual reports whether a and b are equal for the fields accessed by printPeerConcise.
|
||
|
func nodeConciseEqual(a, b *tailcfg.Node) bool {
|
||
|
return a.Key == b.Key &&
|
||
|
a.DERP == b.DERP &&
|
||
4 years ago
|
a.DiscoKey == b.DiscoKey &&
|
||
4 years ago
|
eqCIDRsIgnoreNil(a.AllowedIPs, b.AllowedIPs) &&
|
||
|
eqStringsIgnoreNil(a.Endpoints, b.Endpoints)
|
||
|
}
|
||
5 years ago
|
|
||
4 years ago
|
func (b *NetworkMap) ConciseDiffFrom(a *NetworkMap) string {
|
||
|
var diff strings.Builder
|
||
5 years ago
|
|
||
4 years ago
|
// See if header (non-peers, "bare") part of the network map changed.
|
||
|
// If so, print its diff lines first.
|
||
|
if !a.equalConciseHeader(b) {
|
||
|
diff.WriteByte('-')
|
||
|
a.printConciseHeader(&diff)
|
||
|
diff.WriteByte('+')
|
||
|
b.printConciseHeader(&diff)
|
||
5 years ago
|
}
|
||
|
|
||
4 years ago
|
aps, bps := a.Peers, b.Peers
|
||
|
for len(aps) > 0 && len(bps) > 0 {
|
||
|
pa, pb := aps[0], bps[0]
|
||
|
switch {
|
||
|
case pa.ID == pb.ID:
|
||
|
if !nodeConciseEqual(pa, pb) {
|
||
|
diff.WriteByte('-')
|
||
|
printPeerConcise(&diff, pa)
|
||
|
diff.WriteByte('+')
|
||
|
printPeerConcise(&diff, pb)
|
||
|
}
|
||
|
aps, bps = aps[1:], bps[1:]
|
||
|
case pa.ID > pb.ID:
|
||
|
// New peer in b.
|
||
|
diff.WriteByte('+')
|
||
|
printPeerConcise(&diff, pb)
|
||
|
bps = bps[1:]
|
||
|
case pb.ID > pa.ID:
|
||
|
// Deleted peer in b.
|
||
|
diff.WriteByte('-')
|
||
|
printPeerConcise(&diff, pa)
|
||
|
aps = aps[1:]
|
||
5 years ago
|
}
|
||
|
}
|
||
4 years ago
|
for _, pa := range aps {
|
||
|
diff.WriteByte('-')
|
||
|
printPeerConcise(&diff, pa)
|
||
5 years ago
|
}
|
||
4 years ago
|
for _, pb := range bps {
|
||
|
diff.WriteByte('+')
|
||
|
printPeerConcise(&diff, pb)
|
||
|
}
|
||
|
return diff.String()
|
||
5 years ago
|
}
|
||
|
|
||
5 years ago
|
func (nm *NetworkMap) JSON() string {
|
||
|
b, err := json.MarshalIndent(*nm, "", " ")
|
||
|
if err != nil {
|
||
|
return fmt.Sprintf("[json error: %v]", err)
|
||
|
}
|
||
|
return string(b)
|
||
|
}
|
||
|
|
||
4 years ago
|
// WGConfigFlags is a bitmask of flags to control the behavior of the
|
||
|
// wireguard configuration generation done by NetMap.WGCfg.
|
||
|
type WGConfigFlags int
|
||
5 years ago
|
|
||
4 years ago
|
const (
|
||
|
AllowSingleHosts WGConfigFlags = 1 << iota
|
||
|
AllowSubnetRoutes
|
||
5 years ago
|
)
|
||
|
|
||
4 years ago
|
// eqStringsIgnoreNil reports whether a and b have the same length and
|
||
|
// contents, but ignore whether a or b are nil.
|
||
|
func eqStringsIgnoreNil(a, b []string) bool {
|
||
|
if len(a) != len(b) {
|
||
|
return false
|
||
|
}
|
||
|
for i, v := range a {
|
||
|
if v != b[i] {
|
||
|
return false
|
||
|
}
|
||
|
}
|
||
|
return true
|
||
|
}
|
||
|
|
||
|
// eqCIDRsIgnoreNil reports whether a and b have the same length and
|
||
|
// contents, but ignore whether a or b are nil.
|
||
4 years ago
|
func eqCIDRsIgnoreNil(a, b []netaddr.IPPrefix) bool {
|
||
4 years ago
|
if len(a) != len(b) {
|
||
|
return false
|
||
|
}
|
||
|
for i, v := range a {
|
||
|
if v != b[i] {
|
||
|
return false
|
||
|
}
|
||
|
}
|
||
|
return true
|
||
|
}
|