From 3e5c7e6860ce8e2a09e476268db2fb31058d79c4 Mon Sep 17 00:00:00 2001 From: Wade Simmons Date: Wed, 29 Mar 2023 15:32:35 -0400 Subject: [PATCH] add punchy.respond_delay config option (#721) --- examples/config.yml | 5 ++++- lighthouse.go | 2 +- punchy.go | 19 +++++++++++++++---- punchy_test.go | 6 ++++++ 4 files changed, 26 insertions(+), 6 deletions(-) diff --git a/examples/config.yml b/examples/config.yml index f7bb95d..444592f 100644 --- a/examples/config.yml +++ b/examples/config.yml @@ -142,9 +142,12 @@ punchy: # Default is false #respond: true - # delays a punch response for misbehaving NATs, default is 1 second, respond must be true to take effect + # delays a punch response for misbehaving NATs, default is 1 second. #delay: 1s + # set the delay before attempting punchy.respond. Default is 5 seconds. respond must be true to take effect. + #respond_delay: 5s + # Cipher allows you to choose between the available ciphers for your network. Options are chachapoly or aes # IMPORTANT: this value must be identical on ALL NODES/LIGHTHOUSES. We do not/will not support use of different ciphers simultaneously! #cipher: aes diff --git a/lighthouse.go b/lighthouse.go index a3341b4..402caff 100644 --- a/lighthouse.go +++ b/lighthouse.go @@ -965,7 +965,7 @@ func (lhh *LightHouseHandler) handleHostPunchNotification(n *NebulaMeta, vpnIp i if lhh.lh.punchy.GetRespond() { queryVpnIp := iputil.VpnIp(n.Details.VpnIp) go func() { - time.Sleep(time.Second * 5) + time.Sleep(lhh.lh.punchy.GetRespondDelay()) if lhh.l.Level >= logrus.DebugLevel { lhh.l.Debugf("Sending a nebula test packet to vpn ip %s", queryVpnIp) } diff --git a/punchy.go b/punchy.go index 1ecf7c5..a930ac5 100644 --- a/punchy.go +++ b/punchy.go @@ -9,10 +9,11 @@ import ( ) type Punchy struct { - punch atomic.Bool - respond atomic.Bool - delay atomic.Int64 - l *logrus.Logger + punch atomic.Bool + respond atomic.Bool + delay atomic.Int64 + respondDelay atomic.Int64 + l *logrus.Logger } func NewPunchyFromConfig(l *logrus.Logger, c *config.C) *Punchy { @@ -65,6 +66,12 @@ func (p *Punchy) reload(c *config.C, initial bool) { p.l.Infof("punchy.delay changed to %s", p.GetDelay()) } } + if initial || c.HasChanged("punchy.respond_delay") { + p.respondDelay.Store((int64)(c.GetDuration("punchy.respond_delay", 5*time.Second))) + if !initial { + p.l.Infof("punchy.respond_delay changed to %s", p.GetRespondDelay()) + } + } } func (p *Punchy) GetPunch() bool { @@ -78,3 +85,7 @@ func (p *Punchy) GetRespond() bool { func (p *Punchy) GetDelay() time.Duration { return (time.Duration)(p.delay.Load()) } + +func (p *Punchy) GetRespondDelay() time.Duration { + return (time.Duration)(p.respondDelay.Load()) +} diff --git a/punchy_test.go b/punchy_test.go index 0aa9b62..bedd2b2 100644 --- a/punchy_test.go +++ b/punchy_test.go @@ -18,6 +18,7 @@ func TestNewPunchyFromConfig(t *testing.T) { assert.Equal(t, false, p.GetPunch()) assert.Equal(t, false, p.GetRespond()) assert.Equal(t, time.Second, p.GetDelay()) + assert.Equal(t, 5*time.Second, p.GetRespondDelay()) // punchy deprecation c.Settings["punchy"] = true @@ -44,6 +45,11 @@ func TestNewPunchyFromConfig(t *testing.T) { c.Settings["punchy"] = map[interface{}]interface{}{"delay": "1m"} p = NewPunchyFromConfig(l, c) assert.Equal(t, time.Minute, p.GetDelay()) + + // punchy.respond_delay + c.Settings["punchy"] = map[interface{}]interface{}{"respond_delay": "1m"} + p = NewPunchyFromConfig(l, c) + assert.Equal(t, time.Minute, p.GetRespondDelay()) } func TestPunchy_reload(t *testing.T) {