add punchy.respond_delay config option (#721)

This commit is contained in:
Wade Simmons 2023-03-29 15:32:35 -04:00 committed by GitHub
parent 8a82e0fb16
commit 3e5c7e6860
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 6 deletions

View File

@ -142,9 +142,12 @@ punchy:
# Default is false # Default is false
#respond: true #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 #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 # 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! # IMPORTANT: this value must be identical on ALL NODES/LIGHTHOUSES. We do not/will not support use of different ciphers simultaneously!
#cipher: aes #cipher: aes

View File

@ -965,7 +965,7 @@ func (lhh *LightHouseHandler) handleHostPunchNotification(n *NebulaMeta, vpnIp i
if lhh.lh.punchy.GetRespond() { if lhh.lh.punchy.GetRespond() {
queryVpnIp := iputil.VpnIp(n.Details.VpnIp) queryVpnIp := iputil.VpnIp(n.Details.VpnIp)
go func() { go func() {
time.Sleep(time.Second * 5) time.Sleep(lhh.lh.punchy.GetRespondDelay())
if lhh.l.Level >= logrus.DebugLevel { if lhh.l.Level >= logrus.DebugLevel {
lhh.l.Debugf("Sending a nebula test packet to vpn ip %s", queryVpnIp) lhh.l.Debugf("Sending a nebula test packet to vpn ip %s", queryVpnIp)
} }

View File

@ -9,10 +9,11 @@ import (
) )
type Punchy struct { type Punchy struct {
punch atomic.Bool punch atomic.Bool
respond atomic.Bool respond atomic.Bool
delay atomic.Int64 delay atomic.Int64
l *logrus.Logger respondDelay atomic.Int64
l *logrus.Logger
} }
func NewPunchyFromConfig(l *logrus.Logger, c *config.C) *Punchy { 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()) 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 { func (p *Punchy) GetPunch() bool {
@ -78,3 +85,7 @@ func (p *Punchy) GetRespond() bool {
func (p *Punchy) GetDelay() time.Duration { func (p *Punchy) GetDelay() time.Duration {
return (time.Duration)(p.delay.Load()) return (time.Duration)(p.delay.Load())
} }
func (p *Punchy) GetRespondDelay() time.Duration {
return (time.Duration)(p.respondDelay.Load())
}

View File

@ -18,6 +18,7 @@ func TestNewPunchyFromConfig(t *testing.T) {
assert.Equal(t, false, p.GetPunch()) assert.Equal(t, false, p.GetPunch())
assert.Equal(t, false, p.GetRespond()) assert.Equal(t, false, p.GetRespond())
assert.Equal(t, time.Second, p.GetDelay()) assert.Equal(t, time.Second, p.GetDelay())
assert.Equal(t, 5*time.Second, p.GetRespondDelay())
// punchy deprecation // punchy deprecation
c.Settings["punchy"] = true c.Settings["punchy"] = true
@ -44,6 +45,11 @@ func TestNewPunchyFromConfig(t *testing.T) {
c.Settings["punchy"] = map[interface{}]interface{}{"delay": "1m"} c.Settings["punchy"] = map[interface{}]interface{}{"delay": "1m"}
p = NewPunchyFromConfig(l, c) p = NewPunchyFromConfig(l, c)
assert.Equal(t, time.Minute, p.GetDelay()) 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) { func TestPunchy_reload(t *testing.T) {