Skip to content

Commit 19fa39f

Browse files
committed
Simplify WireGuard endpoint configuration by removing unused parameters and redundant logic
1 parent d89733c commit 19fa39f

File tree

2 files changed

+17
-25
lines changed

2 files changed

+17
-25
lines changed

client/internal/peer/conn.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -392,10 +392,8 @@ func (conn *Conn) onICEConnectionIsReady(priority conntype.ConnPriority, iceConn
392392
conn.Log.Infof("configure WireGuard endpoint to: %s", ep.String())
393393
conn.enableWgWatcherIfNeeded()
394394

395-
hasRelayBackup := conn.wgProxyRelay != nil
396-
397395
presharedKey := conn.presharedKey(iceConnInfo.RosenpassPubKey)
398-
if err = conn.endpointUpdater.ConfigureWGEndpoint(ep, presharedKey, hasRelayBackup); err != nil {
396+
if err = conn.endpointUpdater.ConfigureWGEndpoint(ep, presharedKey); err != nil {
399397
conn.handleConfigurationFailure(err, wgProxy)
400398
return
401399
}
@@ -436,7 +434,7 @@ func (conn *Conn) onICEStateDisconnected() {
436434
conn.wgProxyRelay.Work()
437435

438436
presharedKey := conn.presharedKey(conn.rosenpassRemoteKey)
439-
if err := conn.endpointUpdater.ConfigureWGEndpoint(conn.wgProxyRelay.EndpointAddr(), presharedKey, true); err != nil {
437+
if err := conn.endpointUpdater.SwitchWGEndpoint(conn.wgProxyRelay.EndpointAddr(), presharedKey); err != nil {
440438
conn.Log.Errorf("failed to switch to relay conn: %v", err)
441439
}
442440

@@ -501,20 +499,20 @@ func (conn *Conn) onRelayConnectionIsReady(rci RelayConnInfo) {
501499
return
502500
}
503501

504-
setEndpointNow := isController(conn.config)
502+
controller := isController(conn.config)
505503

506-
if setEndpointNow {
504+
if controller {
507505
wgProxy.Work()
508506
}
509507
conn.enableWgWatcherIfNeeded()
510-
if err := conn.endpointUpdater.ConfigureWGEndpoint(wgProxy.EndpointAddr(), conn.presharedKey(rci.rosenpassPubKey), setEndpointNow); err != nil {
508+
if err := conn.endpointUpdater.ConfigureWGEndpoint(wgProxy.EndpointAddr(), conn.presharedKey(rci.rosenpassPubKey)); err != nil {
511509
if err := wgProxy.CloseConn(); err != nil {
512510
conn.Log.Warnf("Failed to close relay connection: %v", err)
513511
}
514512
conn.Log.Errorf("Failed to update WireGuard peer configuration: %v", err)
515513
return
516514
}
517-
if !setEndpointNow {
515+
if !controller {
518516
wgProxy.Work()
519517
}
520518
conn.rosenpassRemoteKey = rci.rosenpassPubKey

client/internal/peer/endpoint.go

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func NewEndpointUpdater(log *logrus.Entry, wgConfig WgConfig, initiator bool) *E
3434
}
3535
}
3636

37-
func (e *EndpointUpdater) ConfigureWGEndpoint(addr *net.UDPAddr, presharedKey *wgtypes.Key, setEndpointNow bool) error {
37+
func (e *EndpointUpdater) ConfigureWGEndpoint(addr *net.UDPAddr, presharedKey *wgtypes.Key) error {
3838
e.mu.Lock()
3939
defer e.mu.Unlock()
4040

@@ -44,7 +44,7 @@ func (e *EndpointUpdater) ConfigureWGEndpoint(addr *net.UDPAddr, presharedKey *w
4444
}
4545

4646
e.log.Debugf("configure up WireGuard as responder")
47-
return e.configureAsResponder(addr, presharedKey, setEndpointNow)
47+
return e.configureAsResponder(addr, presharedKey)
4848
}
4949

5050
func (e *EndpointUpdater) SwitchWGEndpoint(addr *net.UDPAddr, presharedKey *wgtypes.Key) error {
@@ -80,25 +80,19 @@ func (e *EndpointUpdater) configureAsInitiator(addr *net.UDPAddr, presharedKey *
8080
return nil
8181
}
8282

83-
func (e *EndpointUpdater) configureAsResponder(addr *net.UDPAddr, presharedKey *wgtypes.Key, setEndpointNow bool) error {
83+
func (e *EndpointUpdater) configureAsResponder(addr *net.UDPAddr, presharedKey *wgtypes.Key) error {
8484
// prevent to run new update while cancel the previous update
8585
e.waitForCloseTheDelayedUpdate()
8686

8787
e.log.Debugf("configure up WireGuard and wait for handshake")
88-
if setEndpointNow {
89-
if err := e.updateWireGuardPeer(addr, presharedKey); err != nil {
90-
return err
91-
}
92-
} else {
93-
var ctx context.Context
94-
ctx, e.cancelFunc = context.WithCancel(context.Background())
95-
e.updateWg.Add(1)
96-
go e.scheduleDelayedUpdate(ctx, addr, presharedKey)
97-
98-
if err := e.updateWireGuardPeer(nil, presharedKey); err != nil {
99-
e.waitForCloseTheDelayedUpdate()
100-
return err
101-
}
88+
var ctx context.Context
89+
ctx, e.cancelFunc = context.WithCancel(context.Background())
90+
e.updateWg.Add(1)
91+
go e.scheduleDelayedUpdate(ctx, addr, presharedKey)
92+
93+
if err := e.updateWireGuardPeer(nil, presharedKey); err != nil {
94+
e.waitForCloseTheDelayedUpdate()
95+
return err
10296
}
10397
return nil
10498
}

0 commit comments

Comments
 (0)