ipn: drop unchanged network map updates
This should never happen. But it is, so defend against it while I fix the bug elsewhere. Signed-off-by: David Crawshaw <crawshaw@tailscale.com>pull/158/head
parent
bb93d7aaba
commit
c9d2fb6d11
67
ipn/local.go
67
ipn/local.go
|
@ -5,6 +5,8 @@
|
||||||
package ipn
|
package ipn
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/sha256"
|
||||||
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
@ -199,7 +201,24 @@ func (b *LocalBackend) Start(opts Options) error {
|
||||||
cli.UpdateEndpoints(0, endpoints)
|
cli.UpdateEndpoints(0, endpoints)
|
||||||
}
|
}
|
||||||
|
|
||||||
cli.SetStatusFunc(func(newSt controlclient.Status) {
|
cli.SetStatusFunc(b.controlStatusUpdate)
|
||||||
|
b.e.SetStatusCallback(b.engineStatusUpdate)
|
||||||
|
|
||||||
|
b.mu.Lock()
|
||||||
|
prefs := b.prefs.Clone()
|
||||||
|
b.mu.Unlock()
|
||||||
|
|
||||||
|
blid := b.backendLogID
|
||||||
|
b.logf("Backend: logs: be:%v fe:%v\n", blid, opts.FrontendLogID)
|
||||||
|
b.send(Notify{BackendLogID: &blid})
|
||||||
|
b.send(Notify{Prefs: prefs})
|
||||||
|
|
||||||
|
cli.Login(nil, controlclient.LoginDefault)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// controlStatusUpdate is passed as a callback to controlclient SetStatusFunc.
|
||||||
|
func (b *LocalBackend) controlStatusUpdate(newSt controlclient.Status) {
|
||||||
if newSt.LoginFinished != nil {
|
if newSt.LoginFinished != nil {
|
||||||
// Auth completed, unblock the engine
|
// Auth completed, unblock the engine
|
||||||
b.blockEngineUpdates(false)
|
b.blockEngineUpdates(false)
|
||||||
|
@ -223,8 +242,19 @@ func (b *LocalBackend) Start(opts Options) error {
|
||||||
b.send(Notify{Prefs: prefs})
|
b.send(Notify{Prefs: prefs})
|
||||||
}
|
}
|
||||||
if newSt.NetMap != nil {
|
if newSt.NetMap != nil {
|
||||||
|
changed := true
|
||||||
|
|
||||||
b.mu.Lock()
|
b.mu.Lock()
|
||||||
if b.netMapCache != nil && b.cmpDiff != nil {
|
if b.netMapCache != nil {
|
||||||
|
hash1, err1 := jsonHash(newSt.NetMap)
|
||||||
|
hash2, err2 := jsonHash(b.netMapCache)
|
||||||
|
if err1 == nil && err2 == nil {
|
||||||
|
changed = hash1 != hash2
|
||||||
|
} else {
|
||||||
|
b.logf("[unexpected] netmap hash encode failed: %v, %v", err1, err2)
|
||||||
|
}
|
||||||
|
|
||||||
|
if changed && b.cmpDiff != nil {
|
||||||
s1 := strings.Split(b.netMapCache.Concise(), "\n")
|
s1 := strings.Split(b.netMapCache.Concise(), "\n")
|
||||||
s2 := strings.Split(newSt.NetMap.Concise(), "\n")
|
s2 := strings.Split(newSt.NetMap.Concise(), "\n")
|
||||||
diff := b.cmpDiff(s1, s2)
|
diff := b.cmpDiff(s1, s2)
|
||||||
|
@ -232,11 +262,21 @@ func (b *LocalBackend) Start(opts Options) error {
|
||||||
b.logf("netmap diff:\n%v\n", diff)
|
b.logf("netmap diff:\n%v\n", diff)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
b.netMapCache = newSt.NetMap
|
b.netMapCache = newSt.NetMap
|
||||||
b.mu.Unlock()
|
b.mu.Unlock()
|
||||||
|
|
||||||
|
if changed {
|
||||||
b.send(Notify{NetMap: newSt.NetMap})
|
b.send(Notify{NetMap: newSt.NetMap})
|
||||||
b.updateFilter(newSt.NetMap)
|
b.updateFilter(newSt.NetMap)
|
||||||
|
} else {
|
||||||
|
// This should never happen. A properly
|
||||||
|
// functioning CONTROL should skip sending
|
||||||
|
// equal netmaps. We detect and log here both
|
||||||
|
// as defense-in-depth and to give us warning
|
||||||
|
// (in the logs) that something is wrong.
|
||||||
|
b.logf("[unexpected] unchanged netmap, skipping")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if newSt.URL != "" {
|
if newSt.URL != "" {
|
||||||
b.logf("Received auth URL: %.20v...\n", newSt.URL)
|
b.logf("Received auth URL: %.20v...\n", newSt.URL)
|
||||||
|
@ -266,9 +306,10 @@ func (b *LocalBackend) Start(opts Options) error {
|
||||||
b.SetPrefs(prefs)
|
b.SetPrefs(prefs)
|
||||||
}
|
}
|
||||||
b.stateMachine()
|
b.stateMachine()
|
||||||
})
|
}
|
||||||
|
|
||||||
b.e.SetStatusCallback(func(s *wgengine.Status, err error) {
|
// engineStatusUpdate is passed as a callback to wgengine SetStatusCallback.
|
||||||
|
func (b *LocalBackend) engineStatusUpdate(s *wgengine.Status, err error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
b.logf("wgengine status error: %#v", err)
|
b.logf("wgengine status error: %#v", err)
|
||||||
return
|
return
|
||||||
|
@ -295,19 +336,13 @@ func (b *LocalBackend) Start(opts Options) error {
|
||||||
b.statusLock.Unlock()
|
b.statusLock.Unlock()
|
||||||
|
|
||||||
b.send(Notify{Engine: &es})
|
b.send(Notify{Engine: &es})
|
||||||
})
|
}
|
||||||
|
|
||||||
b.mu.Lock()
|
func jsonHash(v interface{}) (hash [sha256.Size]byte, err error) {
|
||||||
prefs := b.prefs.Clone()
|
h := sha256.New()
|
||||||
b.mu.Unlock()
|
err = json.NewEncoder(h).Encode(v)
|
||||||
|
h.Sum(hash[:0])
|
||||||
blid := b.backendLogID
|
return hash, err
|
||||||
b.logf("Backend: logs: be:%v fe:%v\n", blid, opts.FrontendLogID)
|
|
||||||
b.send(Notify{BackendLogID: &blid})
|
|
||||||
b.send(Notify{Prefs: prefs})
|
|
||||||
|
|
||||||
cli.Login(nil, controlclient.LoginDefault)
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *LocalBackend) updateFilter(netMap *controlclient.NetworkMap) {
|
func (b *LocalBackend) updateFilter(netMap *controlclient.NetworkMap) {
|
||||||
|
|
Loading…
Reference in New Issue