Skip to content

Commit d0bd6f0

Browse files
authored
Merge pull request #16 from hmlendea/sources
Added more connectivity and public IP check sources
2 parents 9719a2f + 19cf352 commit d0bd6f0

3 files changed

Lines changed: 216 additions & 5 deletions

File tree

NuciWeb.HTTP.UnitTests/NetworkUtilsTests.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,43 @@ public void GivenCreateHttpClient_WhenInvoked_ThenClientHasExpectedDefaults()
123123
});
124124
}
125125

126+
[Test]
127+
[TestCase("203.0.113.7", "203.0.113.7")]
128+
public void GivenValidIpResponse_WhenTryNormalizePublicIpAddress_ThenReturnsTrue(string response, string expectedIp)
129+
{
130+
MethodInfo method = typeof(NetworkUtils).GetMethod("TryNormalizePublicIpAddress", BindingFlags.NonPublic | BindingFlags.Static)!;
131+
object?[] parameters = [response, null];
132+
133+
bool isValid = (bool)method.Invoke(null, parameters)!;
134+
135+
Assert.Multiple(() =>
136+
{
137+
Assert.That(isValid, Is.True);
138+
Assert.That(parameters[1], Is.EqualTo(expectedIp));
139+
});
140+
}
141+
142+
[Test]
143+
[TestCase("")]
144+
[TestCase(" ")]
145+
[TestCase("not-an-ip")]
146+
[TestCase("2001:db8::1")]
147+
[TestCase("203.0.113.5 extra")]
148+
[TestCase("<html>203.0.113.5</html>")]
149+
public void GivenInvalidIpResponse_WhenTryNormalizePublicIpAddress_ThenReturnsFalse(string response)
150+
{
151+
MethodInfo method = typeof(NetworkUtils).GetMethod("TryNormalizePublicIpAddress", BindingFlags.NonPublic | BindingFlags.Static)!;
152+
object?[] parameters = [response, null];
153+
154+
bool isValid = (bool)method.Invoke(null, parameters)!;
155+
156+
Assert.Multiple(() =>
157+
{
158+
Assert.That(isValid, Is.False);
159+
Assert.That(parameters[1], Is.EqualTo(string.Empty));
160+
});
161+
}
162+
126163
private static async Task<bool> InvokePrivateNetworkCheckAsync(string methodName, CancellationToken cancellationToken)
127164
{
128165
MethodInfo method = typeof(NetworkUtils).GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Static)!;

NuciWeb.HTTP/NetworkUtils.cs

Lines changed: 178 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,42 +18,191 @@ public static class NetworkUtils
1818
private static readonly List<string> PingHosts =
1919
[
2020
"1.1.1.1",
21+
"1.0.0.1",
2122
"9.9.9.9",
23+
"149.112.112.112",
24+
"94.140.14.14",
25+
"94.140.15.15",
26+
"89.233.43.71",
27+
"91.239.100.100",
28+
"185.95.218.42",
29+
"185.95.218.43",
30+
"185.228.168.9",
31+
"185.228.169.9",
32+
"194.242.2.2",
33+
"194.242.2.3",
34+
"193.110.81.0",
35+
"185.253.5.0",
36+
"76.76.2.0",
37+
"76.76.10.0",
38+
"149.154.159.92",
2239
"cloudflare.com",
40+
"bitwarden.com",
41+
"discourse.privacyguides.net",
42+
"disroot.org",
43+
"debian.org",
44+
"duckduckgo.com",
2345
"ecloud.global",
46+
"epic.org",
2447
"eff.org",
48+
"fdroid.org",
49+
"fsf.org",
50+
"fsfe.org",
51+
"gnu.org",
52+
"infomaniak.com",
53+
"ivpn.net",
54+
"libreoffice.org",
55+
"mailbox.org",
56+
"matrix.org",
57+
"mozilla.org",
58+
"mullvad.net",
59+
"nixos.org",
60+
"noyb.eu",
61+
"privacyinternational.org",
62+
"posteo.de",
63+
"proton.me",
64+
"openstreetmap.org",
2565
"ping.archlinux.org",
66+
"privacyguides.org",
2667
"quad9.net",
68+
"riseup.net",
69+
"signal.org",
2770
"torproject.org",
71+
"tuta.com",
72+
"wikimedia.org",
2873
"wikipedia.org",
2974
];
3075

3176
private static readonly List<string> TcpHosts =
3277
[
3378
"1.1.1.1",
79+
"1.0.0.1",
3480
"9.9.9.9",
81+
"149.112.112.112",
82+
"94.140.14.14",
83+
"94.140.15.15",
84+
"89.233.43.71",
85+
"91.239.100.100",
86+
"185.95.218.42",
87+
"185.95.218.43",
88+
"185.228.168.9",
89+
"185.228.169.9",
90+
"194.242.2.2",
91+
"194.242.2.3",
92+
"193.110.81.0",
93+
"185.253.5.0",
94+
"76.76.2.0",
95+
"76.76.10.0",
96+
"149.154.159.92",
3597
"checkonline.home-assistant.io",
3698
"cloudflare.com",
99+
"bitwarden.com",
100+
"discourse.privacyguides.net",
101+
"disroot.org",
102+
"debian.org",
103+
"duckduckgo.com",
37104
"ecloud.global",
105+
"epic.org",
106+
"fsf.org",
107+
"fdroid.org",
108+
"fsfe.org",
109+
"gnu.org",
110+
"infomaniak.com",
111+
"ivpn.net",
112+
"libreoffice.org",
113+
"mailbox.org",
114+
"matrix.org",
115+
"mozilla.org",
116+
"mullvad.net",
117+
"nixos.org",
118+
"noyb.eu",
119+
"privacyinternational.org",
120+
"posteo.de",
121+
"proton.me",
122+
"openstreetmap.org",
38123
"ping.archlinux.org",
124+
"privacyguides.org",
39125
"quad9.net",
126+
"riseup.net",
127+
"signal.org",
128+
"torproject.org",
129+
"tuta.com",
130+
"wikimedia.org",
40131
];
41132

42133
private static readonly List<string> HttpUrls =
43134
[
44135
"https://checkonline.home-assistant.io",
136+
"https://bitwarden.com",
137+
"https://codeberg.org",
45138
"https://cloudflare.com",
139+
"https://discourse.privacyguides.net",
140+
"https://disroot.org",
141+
"https://debian.org",
142+
"https://duckduckgo.com",
143+
"https://epic.org",
46144
"https://eff.org",
145+
"https://fdroid.org",
146+
"https://fsf.org",
147+
"https://fsfe.org",
148+
"https://gnu.org",
149+
"https://infomaniak.com",
150+
"https://ivpn.net",
151+
"https://libreoffice.org",
152+
"https://mailbox.org",
153+
"https://matrix.org",
154+
"https://mozilla.org",
155+
"https://mullvad.net",
156+
"https://nixos.org",
157+
"https://noyb.eu",
158+
"https://privacyinternational.org",
159+
"https://posteo.de",
160+
"https://proton.me",
161+
"https://openstreetmap.org",
47162
"https://ping.archlinux.org",
163+
"https://privacyguides.org",
164+
"https://riseup.net",
165+
"https://signal.org",
166+
"https://torproject.org",
167+
"https://tuta.com",
168+
"https://wikimedia.org",
48169
"https://wikipedia.org",
49170
];
50171

51172
private static readonly List<string> PublicIpSources =
52173
[
174+
"https://4.ident.me",
175+
"https://am.i.mullvad.net/ip",
176+
"https://api-ipv4.ip.sb/ip",
177+
"https://api.ip.sb/ip",
53178
"https://api.ipify.org",
179+
"https://api.my-ip.io/ip",
180+
"https://api4.ipify.org",
181+
"https://bot.whatismyipaddress.com",
54182
"https://checkip.amazonaws.com",
55183
"https://icanhazip.com",
184+
"https://ident.me",
185+
"https://ifconfig.co/ip",
186+
"https://ifconfig.eu",
187+
"https://ifconfig.io/ip",
56188
"https://ifconfig.me/ip",
189+
"https://ip.sb",
190+
"https://ip.seeip.org",
191+
"https://ip.tyk.nu",
192+
"https://ipecho.io/plain",
193+
"https://ipecho.net/plain",
194+
"https://ipinfo.io/ip",
195+
"https://ipv4.icanhazip.com",
196+
"https://ipv4.seeip.org",
197+
"https://ipv4bot.whatismyipaddress.com",
198+
"https://l2.io/ip",
199+
"https://myexternalip.com/raw",
200+
"https://myip.dnsomatic.com",
201+
"https://myip.wtf/text",
202+
"https://v4.ident.me",
203+
"https://v4.ifconfig.co/ip",
204+
"https://whatismyip.akamai.com",
205+
"https://wtfismyip.com/text",
57206
];
58207

59208
/// <summary>
@@ -115,12 +264,11 @@ public static string GetPublicIpAddress()
115264
string response = HttpClient
116265
.GetStringAsync(source)
117266
.GetAwaiter()
118-
.GetResult()
119-
.Trim();
267+
.GetResult();
120268

121-
if (!string.IsNullOrWhiteSpace(response))
269+
if (TryNormalizePublicIpAddress(response, out string publicIpAddress))
122270
{
123-
return response;
271+
return publicIpAddress;
124272
}
125273
}
126274
catch (Exception ex)
@@ -134,6 +282,32 @@ public static string GetPublicIpAddress()
134282
new AggregateException(errors));
135283
}
136284

285+
private static bool TryNormalizePublicIpAddress(string response, out string publicIpAddress)
286+
{
287+
publicIpAddress = string.Empty;
288+
289+
if (string.IsNullOrWhiteSpace(response))
290+
{
291+
return false;
292+
}
293+
294+
string candidate = response.Trim();
295+
296+
if (!IPAddress.TryParse(candidate, out IPAddress parsedIpAddress))
297+
{
298+
return false;
299+
}
300+
301+
if (parsedIpAddress.AddressFamily != AddressFamily.InterNetwork)
302+
{
303+
return false;
304+
}
305+
306+
publicIpAddress = parsedIpAddress.ToString();
307+
308+
return true;
309+
}
310+
137311
/// <summary>
138312
/// Gets the known hostnames associated with the specified IP address using reverse DNS lookup.
139313
/// </summary>

NuciWeb.HTTP/NuciWeb.HTTP.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<TargetFramework>net10.0</TargetFramework>
55
<RootNamespace>NuciWeb.HTTP</RootNamespace>
6-
<Version>1.6.0</Version>
6+
<Version>1.6.1</Version>
77
<Description>HttpClient wrapper</Description>
88
<Authors>Horațiu Mlendea</Authors>
99
<Copyright>Copyright 2026 © Horațiu Mlendea</Copyright>

0 commit comments

Comments
 (0)