From d12ac44fe16fc0369217dc8e05e22d714f6aa4bc Mon Sep 17 00:00:00 2001 From: 0xCA Date: Wed, 27 Dec 2023 08:26:41 +0500 Subject: [PATCH] Fix duplicated IP suggestions --- handler/routes.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/handler/routes.go b/handler/routes.go index 1937985..0358750 100644 --- a/handler/routes.go +++ b/handler/routes.go @@ -1034,6 +1034,9 @@ func SuggestIPAllocation(db store.IStore) echo.HandlerFunc { searchCIDRList = append(searchCIDRList, server.Interface.Addresses...) } + // Save only unique IPs + ipSet := make(map[string]struct{}) + for _, cidr := range searchCIDRList { ip, err := util.GetAvailableIP(cidr, allocatedIPs, server.Interface.Addresses) if err != nil { @@ -1042,9 +1045,9 @@ func SuggestIPAllocation(db store.IStore) echo.HandlerFunc { } found = true if strings.Contains(ip, ":") { - suggestedIPs = append(suggestedIPs, fmt.Sprintf("%s/128", ip)) + ipSet[fmt.Sprintf("%s/128", ip)] = struct{}{} } else { - suggestedIPs = append(suggestedIPs, fmt.Sprintf("%s/32", ip)) + ipSet[fmt.Sprintf("%s/32", ip)] = struct{}{} } } @@ -1055,6 +1058,10 @@ func SuggestIPAllocation(db store.IStore) echo.HandlerFunc { }) } + for ip := range ipSet { + suggestedIPs = append(suggestedIPs, ip) + } + return c.JSON(http.StatusOK, suggestedIPs) } }