From fe68841dc7649162c43849beb2fcf9a2ad80ee7c Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Mon, 27 Jul 2020 10:19:05 -0700 Subject: [PATCH] wgengine/magicsock: log better with less spam on transition to stopped state Required a minor test update too, which now needs a private key to get far enough to test the thing being tested. --- wgengine/magicsock/magicsock.go | 23 ++++++++++++++++++----- wgengine/magicsock/magicsock_test.go | 3 ++- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/wgengine/magicsock/magicsock.go b/wgengine/magicsock/magicsock.go index 248c3f5e1..929ac0069 100644 --- a/wgengine/magicsock/magicsock.go +++ b/wgengine/magicsock/magicsock.go @@ -1788,13 +1788,16 @@ func (c *Conn) SetPrivateKey(privateKey wgcfg.PrivateKey) error { if oldKey.IsZero() { c.logf("magicsock: SetPrivateKey called (init)") go c.ReSTUN("set-private-key") + } else if newKey.IsZero() { + c.logf("magicsock: SetPrivateKey called (zeroed)") + c.closeAllDerpLocked("zero-private-key") } else { - c.logf("magicsock: SetPrivateKey called (changed") + c.logf("magicsock: SetPrivateKey called (changed)") + c.closeAllDerpLocked("new-private-key") } - c.closeAllDerpLocked("new-private-key") // Key changed. Close existing DERP connections and reconnect to home. - if c.myDerp != 0 { + if c.myDerp != 0 && !newKey.IsZero() { c.logf("magicsock: private key changed, reconnecting to home derp-%d", c.myDerp) c.goDerpConnect(c.myDerp) } @@ -1844,7 +1847,9 @@ func (c *Conn) SetDERPMap(dm *tailcfg.DERPMap) { return } - go c.ReSTUN("derp-map-update") + if c.started { + go c.ReSTUN("derp-map-update") + } } func nodesEqual(x, y []*tailcfg.Node) bool { @@ -2152,6 +2157,10 @@ func (c *Conn) ReSTUN(why string) { // raced with a shutdown. return } + if c.privateKey.IsZero() { + c.logf("magicsock: ReSTUN(%q) ignored; no private key", why) + return + } if c.endpointsUpdateActive { if c.wantEndpointsUpdate != why { @@ -2242,8 +2251,12 @@ func (c *Conn) Rebind() { c.mu.Lock() c.closeAllDerpLocked("rebind") + haveKey := !c.privateKey.IsZero() c.mu.Unlock() - c.goDerpConnect(c.myDerp) + + if haveKey { + c.goDerpConnect(c.myDerp) + } c.resetAddrSetStates() } diff --git a/wgengine/magicsock/magicsock_test.go b/wgengine/magicsock/magicsock_test.go index 306ce755a..82945d978 100644 --- a/wgengine/magicsock/magicsock_test.go +++ b/wgengine/magicsock/magicsock_test.go @@ -215,8 +215,9 @@ func TestNewConn(t *testing.T) { t.Fatal(err) } defer conn.Close() - conn.Start() conn.SetDERPMap(stuntest.DERPMapOf(stunAddr.String())) + conn.SetPrivateKey(wgcfg.PrivateKey(key.NewPrivate())) + conn.Start() go func() { var pkt [64 << 10]byte