Skip to content

Commit

Permalink
fix NoInternetException
Browse files Browse the repository at this point in the history
  • Loading branch information
trudyhood committed Dec 12, 2024
1 parent a14052f commit 6331d2b
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions VpnHood.Core.Libs/VpnHood.Client/Diagnosing/Diagnoser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public async Task Connect(VpnHoodClient vpnHoodClient, CancellationToken cancell
catch (Exception) {
VhLogger.Instance.LogTrace("Checking the Internet connection...");
IsWorking = true;
if (!await NetworkCheck().VhConfigureAwait())
if (!await NetworkCheck(successOnAny: true).VhConfigureAwait())
throw new NoInternetException();

throw;
Expand All @@ -63,7 +63,7 @@ public async Task Diagnose(VpnHoodClient vpnHoodClient, CancellationToken cancel
try {
VhLogger.Instance.LogTrace("Checking the Internet connection...");
IsWorking = true;
if (!await NetworkCheck().VhConfigureAwait())
if (!await NetworkCheck(successOnAny: true).VhConfigureAwait())
throw new NoInternetException();

// ping server
Expand All @@ -88,9 +88,8 @@ public async Task Diagnose(VpnHoodClient vpnHoodClient, CancellationToken cancel

VhLogger.Instance.LogTrace("Checking the Vpn Connection...");
IsWorking = true;
await Task.Delay(2000, cancellationToken)
.VhConfigureAwait(); // connections can not be established on android immediately
if (!await NetworkCheck().VhConfigureAwait())
await Task.Delay(2000, cancellationToken).VhConfigureAwait(); // connections can not be established on android immediately
if (!await NetworkCheck(successOnAny: false).VhConfigureAwait())
throw new NoStableVpnException();
VhLogger.Instance.LogTrace("VPN has been established and tested successfully.");
}
Expand All @@ -99,7 +98,7 @@ await Task.Delay(2000, cancellationToken)
}
}

private async Task<bool> NetworkCheck(bool checkPing = true, bool checkUdp = true)
private async Task<bool> NetworkCheck(bool successOnAny, bool checkPing = true, bool checkUdp = true)
{
var taskPing = checkPing
? DiagnoseUtil.CheckPing(TestPingIpAddresses, NsTimeout)
Expand All @@ -113,7 +112,10 @@ private async Task<bool> NetworkCheck(bool checkPing = true, bool checkUdp = tru
var taskHttps = DiagnoseUtil.CheckHttps(TestHttpUris, HttpTimeout);

await Task.WhenAll(taskPing, taskUdp, taskHttps).VhConfigureAwait();
var hasInternet = taskPing.Result == null && taskUdp.Result == null && taskHttps.Result == null;
var hasInternet = successOnAny
? taskPing.Result == null || taskUdp.Result == null || taskHttps.Result == null
: taskPing.Result == null && taskUdp.Result == null && taskHttps.Result == null;

return hasInternet;
}
}

0 comments on commit 6331d2b

Please sign in to comment.