Skip to content

Commit 976fa50

Browse files
committed
fix: ignore duplicate disallowed networks or ones which contain another
- 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)
1 parent 6985a27 commit 976fa50

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

WireguardAllowedIPs/Core/Calculator.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,14 @@ public static IPv4Network[] CalculateAllowedIPv4s(IPv4Network[] allowed, IPv4Net
3838
if(disallowed.Length == 0)
3939
return allowed;
4040
List<IPv4Network> result = new();
41+
42+
// Remove duplicates
43+
disallowed = disallowed.Distinct().ToArray();
4144

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

4450
IPv4Network last = new(0, 32);
4551

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

76-
IPv6Network[] sortedDisallowed = disallowed.OrderBy(x => x.GetLowAddressValue()).ToArray();
82+
disallowed = disallowed.Distinct().ToArray();
83+
84+
IPv6Network[] sortedDisallowed = disallowed.Where(x => !disallowed.Any(y => !x.Equals(y) && y.Contains(x)))
85+
.OrderBy(x => x.GetLowAddressValue()).ToArray();
7786

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

0 commit comments

Comments
 (0)