Skip to content

Commit

Permalink
fix: ignore duplicate disallowed networks or ones which contain another
Browse files Browse the repository at this point in the history
- Call Distinct() before processing
- And remove any disallowed network which is already contained fully within another one, and
  thus has no effect. (This caused bugs in the calculation logic)
  • Loading branch information
fabyr committed Sep 27, 2023
1 parent 6985a27 commit 976fa50
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions WireguardAllowedIPs/Core/Calculator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,14 @@ public static IPv4Network[] CalculateAllowedIPv4s(IPv4Network[] allowed, IPv4Net
if(disallowed.Length == 0)
return allowed;
List<IPv4Network> result = new();

// Remove duplicates
disallowed = disallowed.Distinct().ToArray();

IPv4Network[] sortedDisallowed = disallowed.OrderBy(x => x.GetLowAddressValue()).ToArray();
// Remove disallowed ranges which are already contained within another disallowed range
// Then sort by ascending address value
IPv4Network[] sortedDisallowed = disallowed.Where(x => !disallowed.Any(y => !x.Equals(y) && y.Contains(x)))
.OrderBy(x => x.GetLowAddressValue()).ToArray();

IPv4Network last = new(0, 32);

Expand Down Expand Up @@ -73,7 +79,10 @@ public static IPv6Network[] CalculateAllowedIPv6s(IPv6Network[] allowed, IPv6Net
return allowed;
List<IPv6Network> result = new();

IPv6Network[] sortedDisallowed = disallowed.OrderBy(x => x.GetLowAddressValue()).ToArray();
disallowed = disallowed.Distinct().ToArray();

IPv6Network[] sortedDisallowed = disallowed.Where(x => !disallowed.Any(y => !x.Equals(y) && y.Contains(x)))
.OrderBy(x => x.GetLowAddressValue()).ToArray();

IPv6Network last = new(UInt128.Zero, 128);

Expand Down

0 comments on commit 976fa50

Please sign in to comment.