From 6c032e1d97d3555099167d3f1c5118365fa24028 Mon Sep 17 00:00:00 2001 From: HeshamTB Date: Mon, 18 Mar 2024 02:06:56 +0300 Subject: [PATCH] bugfix: Free net.IP from pool on DeletePeer --- link.go | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/link.go b/link.go index fa9b105..5608813 100644 --- a/link.go +++ b/link.go @@ -142,7 +142,28 @@ func (wg *WGLink) DeletePeer(publickey string) error { func (wg *WGLink) deletePeer(publickey wgtypes.Key) error { rmCfg := createARemovePeerCfg(publickey) - return wg.ApplyConfig(rmCfg) + ip, err := wg.getPeerIP(publickey) + if err != nil { + return err + } + + err = wg.ApplyConfig(rmCfg) + if err != nil { + return err + } + + err = wg.Free(ip) + if err != nil { + return err + } + + return nil +} + +func (wg *WGLink) getPeerIP(publickey wgtypes.Key) (net.IP, error) { + peer, err := wg.getPeer(publickey) + if err != nil { return nil, err } + return peer.AllowedIPs[0].IP, nil } func (wg *WGLink) Exists(publicKey string) (bool, error) {