Skip to content

Commit 54e898d

Browse files
committed
Refactor for dotnet 8
1 parent 0a85f11 commit 54e898d

File tree

11 files changed

+219
-216
lines changed

11 files changed

+219
-216
lines changed

WireguardAllowedIPs.Tests/AllowedIPsTest.cs

Lines changed: 27 additions & 25 deletions
Large diffs are not rendered by default.

WireguardAllowedIPs.Tests/IPv4Test.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ public void Address_SummaryRange_Test1()
3434
IPv4Network from = new("4.21.11.19", 32);
3535
IPv4Network to = new("10.0.11.0", 32);
3636

37-
string[] expected = new[]
38-
{
37+
string[] expected =
38+
[
3939
"4.21.11.19/32", "4.21.11.20/30", "4.21.11.24/29", "4.21.11.32/27", "4.21.11.64/26", "4.21.11.128/25", "4.21.12.0/22", "4.21.16.0/20", "4.21.32.0/19", "4.21.64.0/18", "4.21.128.0/17", "4.22.0.0/15", "4.24.0.0/13", "4.32.0.0/11", "4.64.0.0/10", "4.128.0.0/9", "5.0.0.0/8", "6.0.0.0/7", "8.0.0.0/7", "10.0.0.0/21", "10.0.8.0/23", "10.0.10.0/24", "10.0.11.0/32"
40-
};
40+
];
4141

4242
string[] output = from.SummarizeAddressRangeWith(to).Select(x => x.ToString()).ToArray();
4343

@@ -50,10 +50,10 @@ public void Address_SummaryRange_Test2()
5050
IPv4Network from = new("192.168.0.0", 32);
5151
IPv4Network to = new("255.254.224.1", 32);
5252

53-
string[] expected = new[]
54-
{
53+
string[] expected =
54+
[
5555
"192.168.0.0/13", "192.176.0.0/12", "192.192.0.0/10", "193.0.0.0/8", "194.0.0.0/7", "196.0.0.0/6", "200.0.0.0/5", "208.0.0.0/4", "224.0.0.0/4", "240.0.0.0/5", "248.0.0.0/6", "252.0.0.0/7", "254.0.0.0/8", "255.0.0.0/9", "255.128.0.0/10", "255.192.0.0/11", "255.224.0.0/12", "255.240.0.0/13", "255.248.0.0/14", "255.252.0.0/15", "255.254.0.0/17", "255.254.128.0/18", "255.254.192.0/19", "255.254.224.0/31"
56-
};
56+
];
5757

5858
string[] output = from.SummarizeAddressRangeWith(to).Select(x => x.ToString()).ToArray();
5959

WireguardAllowedIPs.Tests/IPv6Test.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#pragma warning disable CA1861 // Avoid constant arrays as arguments
12
using System.Globalization;
23

34
namespace WireguardAllowedIPs.Tests;
@@ -61,12 +62,12 @@ public void Address_SummaryRange_Test1()
6162
IPv6Network from = new("c4f1:dea:f260:b2d5::", 128);
6263
IPv6Network to = new("efa5:8fe1::", 128);
6364

64-
IPv6Network[] expected = new[]
65+
IPv6Network[] expected = new[]
6566
{
6667
"c4f1:dea:f260:b2d5::/64", "c4f1:dea:f260:b2d6::/63", "c4f1:dea:f260:b2d8::/61", "c4f1:dea:f260:b2e0::/59", "c4f1:dea:f260:b300::/56", "c4f1:dea:f260:b400::/54", "c4f1:dea:f260:b800::/53", "c4f1:dea:f260:c000::/50", "c4f1:dea:f261::/48", "c4f1:dea:f262::/47", "c4f1:dea:f264::/46", "c4f1:dea:f268::/45", "c4f1:dea:f270::/44", "c4f1:dea:f280::/41", "c4f1:dea:f300::/40", "c4f1:dea:f400::/38", "c4f1:dea:f800::/37", "c4f1:deb::/32", "c4f1:dec::/30", "c4f1:df0::/28", "c4f1:e00::/23", "c4f1:1000::/20", "c4f1:2000::/19", "c4f1:4000::/18", "c4f1:8000::/17", "c4f2::/15", "c4f4::/14", "c4f8::/13", "c500::/8", "c600::/7", "c800::/5", "d000::/4", "e000::/5", "e800::/6", "ec00::/7", "ee00::/8", "ef00::/9", "ef80::/11", "efa0::/14", "efa4::/16", "efa5::/17", "efa5:8000::/21", "efa5:8800::/22", "efa5:8c00::/23", "efa5:8e00::/24", "efa5:8f00::/25", "efa5:8f80::/26", "efa5:8fc0::/27", "efa5:8fe0::/32", "efa5:8fe1::/128"
6768
}.Select(x => (IPv6Network)IPNetwork.Parse(x)).ToArray();
6869

69-
IPv6Network[] output = Array.ConvertAll(from.SummarizeAddressRangeWith(to),x => (IPv6Network)x);
70+
IPv6Network[] output = Array.ConvertAll(from.SummarizeAddressRangeWith(to), x => (IPv6Network)x);
7071

7172
Assert.True(new HashSet<IPv6Network>(expected).SetEquals(output), $"Missing Values: {string.Join(",", expected.Where(x => !output.Contains(x)))}; {string.Join(",", output.Where(x => !expected.Contains(x)))}");
7273
}
@@ -77,13 +78,14 @@ public void Address_SummaryRange_Test2()
7778
IPv6Network from = new("10eb:a194:e96f:af7:b562:5d0b:f8de:8bfd", 128);
7879
IPv6Network to = new("fe80::ea:f260:b2d5", 128);
7980

80-
IPv6Network[] expected = new[]
81+
IPv6Network[] expected = new[]
8182
{
8283
"10eb:a194:e96f:af7:b562:5d0b:f8de:8bfd/128", "10eb:a194:e96f:af7:b562:5d0b:f8de:8bfe/127", "10eb:a194:e96f:af7:b562:5d0b:f8de:8c00/118", "10eb:a194:e96f:af7:b562:5d0b:f8de:9000/116", "10eb:a194:e96f:af7:b562:5d0b:f8de:a000/115", "10eb:a194:e96f:af7:b562:5d0b:f8de:c000/114", "10eb:a194:e96f:af7:b562:5d0b:f8df:0/112", "10eb:a194:e96f:af7:b562:5d0b:f8e0:0/107", "10eb:a194:e96f:af7:b562:5d0b:f900:0/104", "10eb:a194:e96f:af7:b562:5d0b:fa00:0/103", "10eb:a194:e96f:af7:b562:5d0b:fc00:0/102", "10eb:a194:e96f:af7:b562:5d0c::/94", "10eb:a194:e96f:af7:b562:5d10::/92", "10eb:a194:e96f:af7:b562:5d20::/91", "10eb:a194:e96f:af7:b562:5d40::/90", "10eb:a194:e96f:af7:b562:5d80::/89", "10eb:a194:e96f:af7:b562:5e00::/87", "10eb:a194:e96f:af7:b562:6000::/83", "10eb:a194:e96f:af7:b562:8000::/81", "10eb:a194:e96f:af7:b563::/80", "10eb:a194:e96f:af7:b564::/78", "10eb:a194:e96f:af7:b568::/77", "10eb:a194:e96f:af7:b570::/76", "10eb:a194:e96f:af7:b580::/73", "10eb:a194:e96f:af7:b600::/71", "10eb:a194:e96f:af7:b800::/69", "10eb:a194:e96f:af7:c000::/66", "10eb:a194:e96f:af8::/61", "10eb:a194:e96f:b00::/56", "10eb:a194:e96f:c00::/54", "10eb:a194:e96f:1000::/52", "10eb:a194:e96f:2000::/51", "10eb:a194:e96f:4000::/50", "10eb:a194:e96f:8000::/49", "10eb:a194:e970::/44", "10eb:a194:e980::/41", "10eb:a194:ea00::/39", "10eb:a194:ec00::/38", "10eb:a194:f000::/36", "10eb:a195::/32", "10eb:a196::/31", "10eb:a198::/29", "10eb:a1a0::/27", "10eb:a1c0::/26", "10eb:a200::/23", "10eb:a400::/22", "10eb:a800::/21", "10eb:b000::/20", "10eb:c000::/18", "10ec::/14", "10f0::/12", "1100::/8", "1200::/7", "1400::/6", "1800::/5", "2000::/3", "4000::/2", "8000::/2", "c000::/3", "e000::/4", "f000::/5", "f800::/6", "fc00::/7", "fe00::/9", "fe80::/89", "fe80::80:0:0/90", "fe80::c0:0:0/91", "fe80::e0:0:0/93", "fe80::e8:0:0/95", "fe80::ea:0:0/97", "fe80::ea:8000:0/98", "fe80::ea:c000:0/99", "fe80::ea:e000:0/100", "fe80::ea:f000:0/103", "fe80::ea:f200:0/106", "fe80::ea:f240:0/107", "fe80::ea:f260:0/113", "fe80::ea:f260:8000/115", "fe80::ea:f260:a000/116", "fe80::ea:f260:b000/119", "fe80::ea:f260:b200/121", "fe80::ea:f260:b280/122", "fe80::ea:f260:b2c0/124", "fe80::ea:f260:b2d0/126", "fe80::ea:f260:b2d4/127"
8384
}.Select(x => (IPv6Network)IPNetwork.Parse(x)).ToArray();
8485

85-
IPv6Network[] output = Array.ConvertAll(from.SummarizeAddressRangeWith(to),x => (IPv6Network)x);
86+
IPv6Network[] output = Array.ConvertAll(from.SummarizeAddressRangeWith(to), x => (IPv6Network)x);
8687

8788
Assert.True(new HashSet<IPv6Network>(expected).SetEquals(output), $"Missing Values: {string.Join(",", expected.Where(x => !output.Contains(x)))}; {string.Join(",", output.Where(x => !expected.Contains(x)))}");
8889
}
89-
}
90+
}
91+
#pragma warning restore CA1861 // Avoid constant arrays as arguments
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
global using Xunit;
2-
global using WireguardAllowedIPs.Core;
2+
global using WireguardAllowedIPs.Core;

WireguardAllowedIPs/Core/Calculator.cs

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,31 +28,30 @@ private static IPNetwork[] CleanAllowedIPs(IPNetwork[] values, IPNetwork[] allow
2828

2929
// Add the networks which did not conflict with any disallowed ips
3030
result.AddRange(allowed.Where(x => !disallowed.Any(y => y.Overlaps(x))));
31-
31+
3232
// Remove duplicates and return
3333
return result.Distinct().ToArray();
3434
}
3535

3636
public static IPv4Network[] CalculateAllowedIPv4s(IPv4Network[] allowed, IPv4Network[] disallowed)
3737
{
38-
if(disallowed.Length == 0)
38+
if (disallowed.Length == 0)
3939
return allowed;
40-
List<IPv4Network> result = new();
40+
List<IPv4Network> result = [];
4141

4242
// Remove duplicates
4343
disallowed = disallowed.Distinct().ToArray();
44-
44+
4545
// Remove disallowed ranges which are already contained within another disallowed range
4646
// 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();
47+
IPv4Network[] sortedDisallowed = [.. disallowed.Where(x => !disallowed.Any(y => !x.Equals(y) && y.Contains(x))).OrderBy(x => x.GetLowAddressValue())];
4948

5049
IPv4Network last = new(0, 32);
51-
50+
5251
// Treat the entire address space as a continous numeric range (which it is)
5352
// and find the sections which are not inside a disallowed range.
5453
// This can be done sequentially because we sorted the disallowed ranges by ascending address value
55-
foreach(IPv4Network dis in sortedDisallowed)
54+
foreach (IPv4Network dis in sortedDisallowed)
5655
{
5756
uint low = dis.GetLowAddressValue();
5857
uint high = dis.GetHighAddressValue();
@@ -62,9 +61,9 @@ public static IPv4Network[] CalculateAllowedIPv4s(IPv4Network[] allowed, IPv4Net
6261

6362
// Finally the last section extends to the maximum address value (all 1s)
6463
result.AddRange(Array.ConvertAll(last.SummarizeAddressRangeWith(new IPv4Network(uint.MaxValue, 32)), x => (IPv4Network)x));
65-
64+
6665
return Array.ConvertAll(CleanAllowedIPs(
67-
Array.ConvertAll(result.ToArray(), x => (IPNetwork)x),
66+
Array.ConvertAll(result.ToArray(), x => (IPNetwork)x),
6867
Array.ConvertAll(allowed, x => (IPNetwork)x),
6968
Array.ConvertAll(disallowed, x => (IPNetwork)x)
7069
), x => (IPv4Network)x);
@@ -75,18 +74,17 @@ public static IPv6Network[] CalculateAllowedIPv6s(IPv6Network[] allowed, IPv6Net
7574
// This is exactly the same as the IPv4 version
7675
// Maybe this could be condensed into a single method with some clever generics/methods in each class?
7776

78-
if(disallowed.Length == 0)
77+
if (disallowed.Length == 0)
7978
return allowed;
80-
List<IPv6Network> result = new();
81-
79+
List<IPv6Network> result = [];
80+
8281
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();
82+
83+
IPv6Network[] sortedDisallowed = [.. disallowed.Where(x => !disallowed.Any(y => !x.Equals(y) && y.Contains(x))).OrderBy(x => x.GetLowAddressValue())];
8684

8785
IPv6Network last = new(UInt128.Zero, 128);
88-
89-
foreach(IPv6Network dis in sortedDisallowed)
86+
87+
foreach (IPv6Network dis in sortedDisallowed)
9088
{
9189
UInt128 low = dis.GetLowAddressValue();
9290
UInt128 high = dis.GetHighAddressValue();
@@ -95,15 +93,15 @@ public static IPv6Network[] CalculateAllowedIPv6s(IPv6Network[] allowed, IPv6Net
9593
}
9694

9795
result.AddRange(Array.ConvertAll(last.SummarizeAddressRangeWith(new IPv6Network(UInt128.MaxValue, 128)), x => (IPv6Network)x));
98-
96+
9997
return Array.ConvertAll(CleanAllowedIPs(
100-
Array.ConvertAll(result.ToArray(), x => (IPNetwork)x),
98+
Array.ConvertAll(result.ToArray(), x => (IPNetwork)x),
10199
Array.ConvertAll(allowed, x => (IPNetwork)x),
102100
Array.ConvertAll(disallowed, x => (IPNetwork)x)
103101
), x => (IPv6Network)x);
104102
}
105103

106104
public static IPNetwork[] CalculateAllowedIPs(string[] allowed, string[] disallowed)
107-
=> CalculateAllowedIPs(allowed.Select(IPNetwork.Parse).ToArray(),
105+
=> CalculateAllowedIPs(allowed.Select(IPNetwork.Parse).ToArray(),
108106
disallowed.Select(IPNetwork.Parse).ToArray());
109107
}

WireguardAllowedIPs/Core/IPNetwork.cs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,12 @@ namespace WireguardAllowedIPs.Core;
33
/// <summary>
44
/// Baseclass for IPv4Network and IPv6Network
55
/// </summary>
6-
public abstract class IPNetwork
6+
public abstract class IPNetwork(int cidr)
77
{
8-
public int Cidr { get; set; }
8+
public int Cidr { get; set; } = cidr;
99
public abstract string AddressRepresentation { get; }
1010
public byte[]? AddressBytes { get; protected set; }
1111

12-
public IPNetwork(int cidr)
13-
{
14-
Cidr = cidr;
15-
}
16-
1712
/// <summary>
1813
/// Tests if the current instance fully contains another network. <br/>
1914
/// (If all addresses in the other network fall within this network.) <br/>
@@ -31,7 +26,7 @@ public IPNetwork(int cidr)
3126
/// <param name="other">The other network to compare with.</param>
3227
/// <returns>True, if they overlap. False otherwise.</returns>
3328
public abstract bool Overlaps(IPNetwork other);
34-
29+
3530
/// <summary>
3631
/// Finds all address ranges (networks) between the address of the current instance and <paramref name="b"/> <br/>
3732
/// Note: Both addresses must be pure addresses and not networks. <br/>
@@ -62,11 +57,11 @@ public override string ToString()
6257
public static IPNetwork Parse(string value)
6358
{
6459
string[] parts = value.Split('/');
65-
if(parts.Length != 2)
60+
if (parts.Length != 2)
6661
throw new FormatException("Invalid address.");
67-
if(!int.TryParse(parts[1], out int cidr))
62+
if (!int.TryParse(parts[1], out int cidr))
6863
throw new FormatException("Invalid CIDR.");
69-
if(parts[0].Contains(':')) // IPv6
64+
if (parts[0].Contains(':')) // IPv6
7065
return new IPv6Network(parts[0], cidr);
7166
return new IPv4Network(parts[0], cidr);
7267
}

WireguardAllowedIPs/Core/IPv4Network.cs

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@ public class IPv4Network : IPNetwork
1313

1414
public IPv4Network(byte[] bytes, int cidr) : base(cidr)
1515
{
16-
if(bytes.Length != 4)
16+
if (bytes.Length != 4)
1717
throw new ArgumentException("An IPv4 address always consists of 32 bits (4 bytes)");
18-
if(cidr < 0 || cidr > 32)
19-
throw new ArgumentOutOfRangeException("cidr", "CIDR must be between 0 and 32 (both inclusive)");
18+
if (cidr < 0 || cidr > 32)
19+
throw new ArgumentOutOfRangeException(nameof(cidr), "CIDR must be between 0 and 32 (both inclusive)");
2020
AddressBytes = bytes;
2121
}
2222

23-
public IPv4Network(uint value, int cidr) : this(new byte[] {
23+
public IPv4Network(uint value, int cidr) : this([
2424
(byte)(value >> 24),
2525
(byte)((value >> 16) & 0xFF),
2626
(byte)((value >> 8) & 0xFF),
2727
(byte)(value & 0xFF),
28-
}, cidr)
28+
], cidr)
2929
{ }
3030

3131
public IPv4Network(string addressString, int cidr) : this(ParseAddressString(addressString), cidr)
@@ -40,7 +40,7 @@ public IPv4Network(string addressString, int cidr) : this(ParseAddressString(add
4040

4141
public override bool Contains(IPNetwork other)
4242
{
43-
if(other is IPv4Network ip)
43+
if (other is IPv4Network ip)
4444
{
4545
return ip.GetLowAddressValue() >= GetLowAddressValue()
4646
&& ip.GetHighAddressValue() <= GetHighAddressValue();
@@ -50,9 +50,9 @@ public override bool Contains(IPNetwork other)
5050

5151
public override bool Overlaps(IPNetwork other)
5252
{
53-
if(other is IPv4Network ip)
53+
if (other is IPv4Network ip)
5454
{
55-
return uint.Max(ip.GetLowAddressValue(), GetLowAddressValue()) <=
55+
return uint.Max(ip.GetLowAddressValue(), GetLowAddressValue()) <=
5656
uint.Min(ip.GetHighAddressValue(), GetHighAddressValue());
5757
}
5858
throw DifferentTypeException();
@@ -62,55 +62,56 @@ public override bool Overlaps(IPNetwork other)
6262
// https://github.com/python/cpython/blob/8ac20e5404127d68624339c0b318abe2d14fe514/Lib/ipaddress.py#L200
6363
public override IPNetwork[] SummarizeAddressRangeWith(IPNetwork b)
6464
{
65-
if(b is IPv4Network ip)
65+
if (b is IPv4Network ip)
6666
{
67-
if(Cidr != 32 || ip.Cidr != 32)
67+
if (Cidr != 32 || ip.Cidr != 32)
6868
throw new ArgumentException("Can only construct an address range between two /32 addresses");
6969
uint first = AddressValue;
7070
uint last = ip.AddressValue;
71-
if(last < first)
71+
if (last < first)
7272
(first, last) = (last, first);
73-
List<IPv4Network> list = new();
74-
while(first <= last)
73+
List<IPv4Network> list = [];
74+
while (first <= last)
7575
{
7676
int nbits = Math.Min(Util.CountRighthandZeroBits32(first), Util.BitLength32(last - first + 1) - 1);
7777
list.Add(new IPv4Network(first, 32 - nbits));
7878
try
7979
{
80-
checked
80+
checked
8181
{
8282
first += 1u << nbits;
8383
}
84-
} catch(OverflowException)
84+
}
85+
catch (OverflowException)
8586
{
8687
break;
8788
}
8889
}
89-
return list.ToArray();
90+
return [.. list];
9091
}
9192
throw DifferentTypeException();
9293
}
93-
94+
9495
public static byte[] ParseAddressString(string addressString)
9596
{
9697
string[] parts = addressString.Split('.');
97-
if(parts.Length != 4)
98+
if (parts.Length != 4)
9899
throw new FormatException("IPv4 Address must consist of 4 octets.");
99-
if(parts.Any(x => !byte.TryParse(x, out _)))
100+
if (parts.Any(x => !byte.TryParse(x, out _)))
100101
throw new FormatException("Invalid IPv4 octet values.");
101-
102+
102103
return parts.Select(x => byte.Parse(x)).ToArray();
103104
}
104105

105106
public override bool Equals(object? obj)
106107
{
107-
if(obj is IPv4Network ip)
108+
if (obj is IPv4Network ip)
108109
return ip.AddressValue == AddressValue && ip.Cidr == Cidr;
109110
return false;
110111
}
111112

112113
public override int GetHashCode()
113-
{
114+
{
114115
return HashCode.Combine(AddressValue, Cidr);
115116
}
116117
}

0 commit comments

Comments
 (0)