Skip to content

Commit

Permalink
wireguard: properly remove peer
Browse files Browse the repository at this point in the history
  • Loading branch information
USA-RedDragon committed Feb 12, 2024
1 parent a2f6f5b commit d26186a
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions internal/wireguard/wireguard.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,6 @@ func (m *Manager) addPeer(peer models.Tunnel) {
// Create a new wireguard interface listening on the port from the peer tunnel
// If the peer is a client, then the password is the public key of the client
// If the peer is a server, then the password is the private key of the server
log.Println("adding peer", peer)

iface := GenerateWireguardInterfaceName(peer)

// Check if device exists
Expand Down Expand Up @@ -306,15 +304,33 @@ func (m *Manager) addPeer(peer models.Tunnel) {
}

func (m *Manager) removePeer(peer models.Tunnel) {
log.Println("removing peer", peer)
iface := GenerateWireguardInterfaceName(peer)

_, ok := m.activePeers.LoadAndDelete(GenerateWireguardInterfaceName(peer))
_, ok := m.activePeers.LoadAndDelete(iface)
if !ok {
m.peerRemoveConfirmChan <- peer
return
}

// TODO: remove peer
// Check if device exists
wgdev, err := netlink.LinkByName(iface)
if err != nil {
log.Println("wireguard interface does not exist", iface)
m.peerRemoveConfirmChan <- peer
return
}

err = netlink.LinkSetDown(wgdev)
if err != nil {
log.Println("failed to bring down wireguard device", err)
return
}

err = netlink.LinkDel(wgdev)
if err != nil {
log.Println("failed to delete wireguard device", err)
return
}

m.peerRemoveConfirmChan <- peer
}
Expand Down

0 comments on commit d26186a

Please sign in to comment.