mirror of
https://github.com/netbirdio/netbird.git
synced 2026-08-24 16:41:30 +02:00
Compare commits
1 Commits
notificati
...
test/batte
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
35b3a24b09 |
@@ -841,6 +841,8 @@ func (conn *Conn) enableWgWatcherIfNeeded(enabledTime time.Time) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
conn.endpointUpdater.EnableKeepAlive()
|
||||||
|
|
||||||
watcher := NewWGWatcher(conn.Log, conn.config.WgConfig.WgInterface, conn.config.Key, conn.dumpState)
|
watcher := NewWGWatcher(conn.Log, conn.config.WgConfig.WgInterface, conn.config.Key, conn.dumpState)
|
||||||
watcher.PrepareInitialHandshake()
|
watcher.PrepareInitialHandshake()
|
||||||
|
|
||||||
@@ -888,6 +890,7 @@ func (conn *Conn) resetEndpoint() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
conn.Log.Infof("reset wg endpoint")
|
conn.Log.Infof("reset wg endpoint")
|
||||||
|
conn.endpointUpdater.EnableKeepAlive()
|
||||||
if conn.wgWatcher != nil {
|
if conn.wgWatcher != nil {
|
||||||
conn.wgWatcher.Reset()
|
conn.wgWatcher.Reset()
|
||||||
}
|
}
|
||||||
@@ -945,7 +948,12 @@ func (conn *Conn) onWGHandshakeSuccess(when time.Time) {
|
|||||||
func (conn *Conn) onWGCheckSuccess() {
|
func (conn *Conn) onWGCheckSuccess() {
|
||||||
conn.mu.Lock()
|
conn.mu.Lock()
|
||||||
conn.wgTimeouts = 0
|
conn.wgTimeouts = 0
|
||||||
|
presharedKey := conn.presharedKey(conn.rosenpassRemoteKey)
|
||||||
conn.mu.Unlock()
|
conn.mu.Unlock()
|
||||||
|
|
||||||
|
if err := conn.endpointUpdater.DisableKeepAlive(presharedKey); err != nil {
|
||||||
|
conn.Log.Warnf("failed to disable WireGuard keepalive: %v", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// recordConnectionMetrics records connection stage timestamps as metrics
|
// recordConnectionMetrics records connection stage timestamps as metrics
|
||||||
|
|||||||
@@ -316,11 +316,16 @@ func newWGTimeoutTestConn(rosenpassEnabled bool, disconnected *[]string) *Conn {
|
|||||||
cfg.RosenpassConfig = RosenpassConfig{PubKey: []byte("dummykey")}
|
cfg.RosenpassConfig = RosenpassConfig{PubKey: []byte("dummykey")}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
connLog := log.WithField("peer", cfg.Key)
|
||||||
|
endpointUpdater := NewEndpointUpdater(connLog, cfg.WgConfig, false)
|
||||||
|
endpointUpdater.keepAlive = 0
|
||||||
|
|
||||||
conn := &Conn{
|
conn := &Conn{
|
||||||
ctx: context.Background(),
|
ctx: context.Background(),
|
||||||
config: cfg,
|
config: cfg,
|
||||||
Log: log.WithField("peer", cfg.Key),
|
Log: connLog,
|
||||||
metricsStages: &MetricsStages{},
|
metricsStages: &MetricsStages{},
|
||||||
|
endpointUpdater: endpointUpdater,
|
||||||
}
|
}
|
||||||
conn.SetOnDisconnected(func(remotePeer string) {
|
conn.SetOnDisconnected(func(remotePeer string) {
|
||||||
*disconnected = append(*disconnected, remotePeer)
|
*disconnected = append(*disconnected, remotePeer)
|
||||||
|
|||||||
@@ -20,10 +20,11 @@ type EndpointUpdater struct {
|
|||||||
wgConfig WgConfig
|
wgConfig WgConfig
|
||||||
initiator bool
|
initiator bool
|
||||||
|
|
||||||
// mu protects cancelFunc
|
// mu protects cancelFunc and keepAlive
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
cancelFunc func()
|
cancelFunc func()
|
||||||
updateWg sync.WaitGroup
|
updateWg sync.WaitGroup
|
||||||
|
keepAlive time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewEndpointUpdater(log *logrus.Entry, wgConfig WgConfig, initiator bool) *EndpointUpdater {
|
func NewEndpointUpdater(log *logrus.Entry, wgConfig WgConfig, initiator bool) *EndpointUpdater {
|
||||||
@@ -31,6 +32,7 @@ func NewEndpointUpdater(log *logrus.Entry, wgConfig WgConfig, initiator bool) *E
|
|||||||
log: log,
|
log: log,
|
||||||
wgConfig: wgConfig,
|
wgConfig: wgConfig,
|
||||||
initiator: initiator,
|
initiator: initiator,
|
||||||
|
keepAlive: defaultWgKeepAlive,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -73,6 +75,31 @@ func (e *EndpointUpdater) RemoveEndpointAddress() error {
|
|||||||
return e.wgConfig.WgInterface.RemoveEndpointAddress(e.wgConfig.RemoteKey)
|
return e.wgConfig.WgInterface.RemoveEndpointAddress(e.wgConfig.RemoteKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (e *EndpointUpdater) DisableKeepAlive(presharedKey *wgtypes.Key) error {
|
||||||
|
if !isWgKeepAliveDisabled() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
e.mu.Lock()
|
||||||
|
defer e.mu.Unlock()
|
||||||
|
|
||||||
|
if e.keepAlive == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
e.waitForCloseTheDelayedUpdate()
|
||||||
|
e.keepAlive = 0
|
||||||
|
e.log.Debugf("disable WireGuard persistent keepalive")
|
||||||
|
return e.updateWireGuardPeer(nil, presharedKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *EndpointUpdater) EnableKeepAlive() {
|
||||||
|
e.mu.Lock()
|
||||||
|
defer e.mu.Unlock()
|
||||||
|
|
||||||
|
e.keepAlive = defaultWgKeepAlive
|
||||||
|
}
|
||||||
|
|
||||||
func (e *EndpointUpdater) configureAsInitiator(addr *net.UDPAddr, presharedKey *wgtypes.Key) error {
|
func (e *EndpointUpdater) configureAsInitiator(addr *net.UDPAddr, presharedKey *wgtypes.Key) error {
|
||||||
if err := e.updateWireGuardPeer(addr, presharedKey); err != nil {
|
if err := e.updateWireGuardPeer(addr, presharedKey); err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -127,7 +154,7 @@ func (e *EndpointUpdater) updateWireGuardPeer(endpoint *net.UDPAddr, presharedKe
|
|||||||
return e.wgConfig.WgInterface.UpdatePeer(
|
return e.wgConfig.WgInterface.UpdatePeer(
|
||||||
e.wgConfig.RemoteKey,
|
e.wgConfig.RemoteKey,
|
||||||
e.wgConfig.AllowedIps,
|
e.wgConfig.AllowedIps,
|
||||||
defaultWgKeepAlive,
|
e.keepAlive,
|
||||||
endpoint,
|
endpoint,
|
||||||
presharedKey,
|
presharedKey,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -7,8 +7,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
EnvKeyNBForceRelay = "NB_FORCE_RELAY"
|
EnvKeyNBForceRelay = "NB_FORCE_RELAY"
|
||||||
EnvKeyNBHomeRelayServers = "NB_HOME_RELAY_SERVERS"
|
EnvKeyNBHomeRelayServers = "NB_HOME_RELAY_SERVERS"
|
||||||
|
EnvKeyNBDisableWgKeepAlive = "NB_DISABLE_WG_KEEP_ALIVE"
|
||||||
)
|
)
|
||||||
|
|
||||||
func IsForceRelayed() bool {
|
func IsForceRelayed() bool {
|
||||||
@@ -18,6 +19,10 @@ func IsForceRelayed() bool {
|
|||||||
return strings.EqualFold(os.Getenv(EnvKeyNBForceRelay), "true")
|
return strings.EqualFold(os.Getenv(EnvKeyNBForceRelay), "true")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isWgKeepAliveDisabled() bool {
|
||||||
|
return strings.EqualFold(os.Getenv(EnvKeyNBDisableWgKeepAlive), "true")
|
||||||
|
}
|
||||||
|
|
||||||
// OverrideRelayURLs returns the relay server URL list set in
|
// OverrideRelayURLs returns the relay server URL list set in
|
||||||
// NB_HOME_RELAY_SERVERS (comma-separated) and a boolean indicating whether
|
// NB_HOME_RELAY_SERVERS (comma-separated) and a boolean indicating whether
|
||||||
// the override is active. When the env var is unset, the boolean is false
|
// the override is active. When the env var is unset, the boolean is false
|
||||||
|
|||||||
@@ -109,10 +109,15 @@ func (w *WGWatcher) periodicHandshakeCheck(ctx context.Context, onDisconnectedFn
|
|||||||
}
|
}
|
||||||
|
|
||||||
lastHandshake = *handshake
|
lastHandshake = *handshake
|
||||||
|
w.stateDump.WGcheckSuccess()
|
||||||
|
|
||||||
|
if isWgKeepAliveDisabled() {
|
||||||
|
w.log.Debugf("WireGuard watcher waiting for peer reset")
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
resetTime := time.Until(handshake.Add(checkPeriod))
|
resetTime := time.Until(handshake.Add(checkPeriod))
|
||||||
timer.Reset(resetTime)
|
timer.Reset(resetTime)
|
||||||
w.stateDump.WGcheckSuccess()
|
|
||||||
|
|
||||||
w.log.Debugf("WireGuard watcher reset timer: %v", resetTime)
|
w.log.Debugf("WireGuard watcher reset timer: %v", resetTime)
|
||||||
case <-w.resetCh:
|
case <-w.resetCh:
|
||||||
|
|||||||
Reference in New Issue
Block a user