|
|
@ -30,7 +30,7 @@ type NetworkMap struct {
|
|
|
|
Addresses []wgcfg.CIDR
|
|
|
|
Addresses []wgcfg.CIDR
|
|
|
|
LocalPort uint16 // used for debugging
|
|
|
|
LocalPort uint16 // used for debugging
|
|
|
|
MachineStatus tailcfg.MachineStatus
|
|
|
|
MachineStatus tailcfg.MachineStatus
|
|
|
|
Peers []*tailcfg.Node
|
|
|
|
Peers []*tailcfg.Node // sorted by Node.ID
|
|
|
|
DNS []wgcfg.IP
|
|
|
|
DNS []wgcfg.IP
|
|
|
|
DNSDomains []string
|
|
|
|
DNSDomains []string
|
|
|
|
Hostinfo tailcfg.Hostinfo
|
|
|
|
Hostinfo tailcfg.Hostinfo
|
|
|
@ -56,6 +56,13 @@ type NetworkMap struct {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (n *NetworkMap) Equal(n2 *NetworkMap) bool {
|
|
|
|
func (n *NetworkMap) Equal(n2 *NetworkMap) bool {
|
|
|
|
|
|
|
|
if n == nil && n2 == nil {
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if n == nil || n2 == nil {
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// TODO(crawshaw): this is crude, but is an easy way to avoid bugs.
|
|
|
|
// TODO(crawshaw): this is crude, but is an easy way to avoid bugs.
|
|
|
|
b, err := json.Marshal(n)
|
|
|
|
b, err := json.Marshal(n)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
@ -74,6 +81,19 @@ func (nm NetworkMap) String() string {
|
|
|
|
|
|
|
|
|
|
|
|
func (nm *NetworkMap) Concise() string {
|
|
|
|
func (nm *NetworkMap) Concise() string {
|
|
|
|
buf := new(strings.Builder)
|
|
|
|
buf := new(strings.Builder)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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) {
|
|
|
|
fmt.Fprintf(buf, "netmap: self: %v auth=%v",
|
|
|
|
fmt.Fprintf(buf, "netmap: self: %v auth=%v",
|
|
|
|
nm.NodeKey.ShortString(), nm.MachineStatus)
|
|
|
|
nm.NodeKey.ShortString(), nm.MachineStatus)
|
|
|
|
if nm.LocalPort != 0 {
|
|
|
|
if nm.LocalPort != 0 {
|
|
|
@ -85,7 +105,30 @@ func (nm *NetworkMap) Concise() string {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fmt.Fprintf(buf, " %v", nm.Addresses)
|
|
|
|
fmt.Fprintf(buf, " %v", nm.Addresses)
|
|
|
|
buf.WriteByte('\n')
|
|
|
|
buf.WriteByte('\n')
|
|
|
|
for _, p := range nm.Peers {
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 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 ||
|
|
|
|
|
|
|
|
len(a.Addresses) != len(b.Addresses) {
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, a := range a.Addresses {
|
|
|
|
|
|
|
|
if b.Addresses[i] != a {
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
|
|
|
aip := make([]string, len(p.AllowedIPs))
|
|
|
|
for i, a := range p.AllowedIPs {
|
|
|
|
for i, a := range p.AllowedIPs {
|
|
|
|
s := strings.TrimSuffix(fmt.Sprint(a), "/32")
|
|
|
|
s := strings.TrimSuffix(fmt.Sprint(a), "/32")
|
|
|
@ -117,40 +160,61 @@ func (nm *NetworkMap) Concise() string {
|
|
|
|
p.Key.ShortString(), derp,
|
|
|
|
p.Key.ShortString(), derp,
|
|
|
|
strings.Join(aip, " "),
|
|
|
|
strings.Join(aip, " "),
|
|
|
|
strings.Join(ep, " "))
|
|
|
|
strings.Join(ep, " "))
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.String()
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (b *NetworkMap) ConciseDiffFrom(a *NetworkMap) string {
|
|
|
|
// nodeConciseEqual reports whether a and b are equal for the fields accessed by printPeerConcise.
|
|
|
|
if reflect.DeepEqual(a, b) {
|
|
|
|
func nodeConciseEqual(a, b *tailcfg.Node) bool {
|
|
|
|
// Fast path that only does one allocation.
|
|
|
|
return a.Key == b.Key &&
|
|
|
|
return ""
|
|
|
|
a.DERP == b.DERP &&
|
|
|
|
}
|
|
|
|
eqCIDRsIgnoreNil(a.AllowedIPs, b.AllowedIPs) &&
|
|
|
|
out := []string{}
|
|
|
|
eqStringsIgnoreNil(a.Endpoints, b.Endpoints)
|
|
|
|
ra := strings.Split(a.Concise(), "\n")
|
|
|
|
}
|
|
|
|
rb := strings.Split(b.Concise(), "\n")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ma := map[string]struct{}{}
|
|
|
|
func (b *NetworkMap) ConciseDiffFrom(a *NetworkMap) string {
|
|
|
|
for _, s := range ra {
|
|
|
|
var diff strings.Builder
|
|
|
|
ma[s] = struct{}{}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mb := map[string]struct{}{}
|
|
|
|
// See if header (non-peers, "bare") part of the network map changed.
|
|
|
|
for _, s := range rb {
|
|
|
|
// If so, print its diff lines first.
|
|
|
|
mb[s] = struct{}{}
|
|
|
|
if !a.equalConciseHeader(b) {
|
|
|
|
|
|
|
|
diff.WriteByte('-')
|
|
|
|
|
|
|
|
a.printConciseHeader(&diff)
|
|
|
|
|
|
|
|
diff.WriteByte('+')
|
|
|
|
|
|
|
|
b.printConciseHeader(&diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for _, s := range ra {
|
|
|
|
aps, bps := a.Peers, b.Peers
|
|
|
|
if _, ok := mb[s]; !ok {
|
|
|
|
for len(aps) > 0 && len(bps) > 0 {
|
|
|
|
out = append(out, "-"+s)
|
|
|
|
pa, pb := aps[0], bps[0]
|
|
|
|
}
|
|
|
|
switch {
|
|
|
|
}
|
|
|
|
case pa.ID == pb.ID:
|
|
|
|
for _, s := range rb {
|
|
|
|
if !nodeConciseEqual(pa, pb) {
|
|
|
|
if _, ok := ma[s]; !ok {
|
|
|
|
diff.WriteByte('-')
|
|
|
|
out = append(out, "+"+s)
|
|
|
|
printPeerConcise(&diff, pa)
|
|
|
|
}
|
|
|
|
diff.WriteByte('+')
|
|
|
|
}
|
|
|
|
printPeerConcise(&diff, pb)
|
|
|
|
return strings.Join(out, "\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
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:]
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, pa := range aps {
|
|
|
|
|
|
|
|
diff.WriteByte('-')
|
|
|
|
|
|
|
|
printPeerConcise(&diff, pa)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, pb := range bps {
|
|
|
|
|
|
|
|
diff.WriteByte('+')
|
|
|
|
|
|
|
|
printPeerConcise(&diff, pb)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return diff.String()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (nm *NetworkMap) JSON() string {
|
|
|
|
func (nm *NetworkMap) JSON() string {
|
|
|
@ -271,3 +335,31 @@ func appendEndpoint(peer *wgcfg.Peer, epStr string) error {
|
|
|
|
peer.Endpoints = append(peer.Endpoints, wgcfg.Endpoint{Host: host, Port: uint16(port16)})
|
|
|
|
peer.Endpoints = append(peer.Endpoints, wgcfg.Endpoint{Host: host, Port: uint16(port16)})
|
|
|
|
return nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
func eqCIDRsIgnoreNil(a, b []wgcfg.CIDR) bool {
|
|
|
|
|
|
|
|
if len(a) != len(b) {
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, v := range a {
|
|
|
|
|
|
|
|
if v != b[i] {
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
|
|
|
}
|
|
|
|