Skip to content

Commit

Permalink
Try not to cleanup multiple peers behind same PublicIP
Browse files Browse the repository at this point in the history
  • Loading branch information
ludost committed Oct 18, 2024
1 parent 4a65da5 commit 233e281
Showing 1 changed file with 0 additions and 46 deletions.
46 changes: 0 additions & 46 deletions pkg/backend/wireguard/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,52 +306,6 @@ func (dev *wgDevice) addPeer(publicEndpoint string, peerPublicKeyRaw string, pee
return fmt.Errorf("failed to configure device %w", err)
}

// Remove peers from this endpoint with different public keys
err = dev.cleanupEndpointPeers(udpEndpoint, peerPublicKeyRaw)
if err != nil {
return fmt.Errorf("failed to clean up endpoint peers %w", err)
}

return nil
}

func (dev *wgDevice) cleanupEndpointPeers(udpEndpoint *net.UDPAddr, latestPublicKeyRaw string) error {
client, err := wgctrl.New()
if err != nil {
return fmt.Errorf("failed to open wgctrl: %w", err)
}
defer client.Close()

currentDev, err := client.Device(dev.attrs.name)
if err != nil {
return fmt.Errorf("failed to open device: %w", err)
}

peers := []wgtypes.PeerConfig{}
for _, peer := range currentDev.Peers {
if peer.Endpoint.IP.Equal(udpEndpoint.IP) {
if peer.PublicKey.String() != latestPublicKeyRaw {
removePeer := wgtypes.PeerConfig{
PublicKey: peer.PublicKey,
Remove: true,
}
peers = append(peers, removePeer)
}
}
}

wgcfg := wgtypes.Config{
PrivateKey: dev.attrs.privateKey,
ListenPort: &dev.attrs.listenPort,
ReplacePeers: false,
Peers: peers,
}

err = client.ConfigureDevice(dev.attrs.name, wgcfg)
if err != nil {
return fmt.Errorf("failed to cleanup peers %w", err)
}

return nil
}

Expand Down

0 comments on commit 233e281

Please sign in to comment.