Skip to content

Commit

Permalink
Merge pull request #2073 from asimovo-platform/Feature-multiNodeBehin…
Browse files Browse the repository at this point in the history
…dSingleIP

Bugfix/feature: Allow multiple nodes behind a single public IP address, e.g. remote nodes behind NAT.
  • Loading branch information
thomasferrandiz authored Oct 22, 2024
2 parents 67b8790 + 233e281 commit 4ec57ab
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 4ec57ab

Please sign in to comment.