Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions client/internal/dns/local/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1263,9 +1263,9 @@ func TestLocalResolver_AuthoritativeFlag(t *testing.T) {
})
}

// TestLocalResolver_Stop tests cleanup on Stop
// TestLocalResolver_Stop tests cleanup on GracefullyStop
func TestLocalResolver_Stop(t *testing.T) {
t.Run("Stop clears all state", func(t *testing.T) {
t.Run("GracefullyStop clears all state", func(t *testing.T) {
resolver := NewResolver()
resolver.Update([]nbdns.CustomZone{{
Domain: "example.com.",
Expand All @@ -1285,7 +1285,7 @@ func TestLocalResolver_Stop(t *testing.T) {
assert.False(t, resolver.isInManagedZone("host.example.com."))
})

t.Run("Stop is safe to call multiple times", func(t *testing.T) {
t.Run("GracefullyStop is safe to call multiple times", func(t *testing.T) {
resolver := NewResolver()
resolver.Update([]nbdns.CustomZone{{
Domain: "example.com.",
Expand All @@ -1299,7 +1299,7 @@ func TestLocalResolver_Stop(t *testing.T) {
resolver.Stop()
})

t.Run("Stop cancels in-flight external resolution", func(t *testing.T) {
t.Run("GracefullyStop cancels in-flight external resolution", func(t *testing.T) {
resolver := NewResolver()

lookupStarted := make(chan struct{})
Expand Down
14 changes: 11 additions & 3 deletions client/internal/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ func NewEngine(
networkSerial: 0,
statusRecorder: statusRecorder,
stateManager: stateManager,
portForwardManager: portforward.NewManager(stateManager),
portForwardManager: portforward.NewManager(),
checks: checks,
connSemaphore: semaphoregroup.NewSemaphoreGroup(connInitLimit),
probeStunTurn: relay.NewStunTurnProbe(relay.DefaultCacheTTL),
Expand Down Expand Up @@ -513,7 +513,11 @@ func (e *Engine) Start(netbirdConfig *mgmProto.NetbirdConfig, mgmtURL *url.URL)
e.setupWGProxyNoTrack()

// Start after interface is up since port may have been resolved from 0 or changed if occupied
e.portForwardManager.Start(e.ctx, uint16(e.config.WgPort))
e.shutdownWg.Add(1)
go func() {
defer e.shutdownWg.Done()
e.portForwardManager.Start(e.ctx, uint16(e.config.WgPort))
}()

// Set the WireGuard interface for rosenpass after interface is up
if e.rpManager != nil {
Expand Down Expand Up @@ -1692,7 +1696,11 @@ func (e *Engine) close() {
_ = e.rpManager.Close()
}

e.portForwardManager.Stop()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := e.portForwardManager.GracefullyStop(ctx); err != nil {
log.Warnf("failed to gracefully stop port forwarding manager: %s", err)
}
}

func (e *Engine) readInitialSettings() ([]*route.Route, *nbdns.Config, bool, error) {
Expand Down
2 changes: 1 addition & 1 deletion client/internal/peer/worker_ice.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ func (w *WorkerICE) injectPortForwardedCandidate(srflxCandidate ice.Candidate) {
w.muxAgent.Unlock()

pfManager := w.conn.portForwardManager
if pfManager == nil || !pfManager.IsAvailable() {
if pfManager == nil {
return
}

Expand Down
26 changes: 26 additions & 0 deletions client/internal/portforward/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package portforward

import (
"os"
"strconv"

log "github.com/sirupsen/logrus"
)

const (
envDisableNATMapper = "NB_DISABLE_NAT_MAPPER"
)

func isDisabledByEnv() bool {
val := os.Getenv(envDisableNATMapper)
if val == "" {
return false
}

disabled, err := strconv.ParseBool(val)
if err != nil {
log.Warnf("failed to parse %s: %v", envDisableNATMapper, err)
return false
}
return disabled
}
Loading
Loading