Skip to content

Commit

Permalink
add punchy.respond_delay config option (#721)
Browse files Browse the repository at this point in the history
  • Loading branch information
wadey authored Mar 29, 2023
1 parent 8a82e0f commit 3e5c7e6
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
5 changes: 4 additions & 1 deletion examples/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lighthouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
19 changes: 15 additions & 4 deletions punchy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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())
}
6 changes: 6 additions & 0 deletions punchy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down

0 comments on commit 3e5c7e6

Please sign in to comment.