Skip to content

Commit

Permalink
fix ping diagnose on Android
Browse files Browse the repository at this point in the history
  • Loading branch information
trudyhood committed Nov 30, 2023
1 parent 084f8ee commit 8465afc
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 13 deletions.
13 changes: 4 additions & 9 deletions VpnHood.Client/Diagnosing/DiagnoseUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ public class DiagnoseUtil
return WhenAnySuccess(tasks.ToArray());
}

public static Task<Exception?> CheckPing(IPAddress[] ipAddresses, int timeout, int pingTtl = 128, bool anonymize = false)
public static Task<Exception?> CheckPing(IPAddress[] ipAddresses, int timeout, bool anonymize = false)
{
var tasks = ipAddresses.Select(x => CheckPing(x, timeout, pingTtl, anonymize));
var tasks = ipAddresses.Select(x => CheckPing(x, timeout, anonymize));
return WhenAnySuccess(tasks.ToArray());
}

Expand Down Expand Up @@ -102,23 +102,18 @@ public class DiagnoseUtil
}
}

public static async Task<Exception?> CheckPing(IPAddress ipAddress, int timeout, int pingTtl = 128, bool anonymize = false)
public static async Task<Exception?> CheckPing(IPAddress ipAddress, int timeout, bool anonymize = false)
{
var logIpAddress = anonymize ? VhLogger.Format(ipAddress) : ipAddress.ToString();

try
{
using var ping = new Ping();
var pingOptions = new PingOptions { Ttl = pingTtl };
VhLogger.Instance.LogInformation(
"PingTest: {PingTestStatus}, RemoteAddress: {RemoteAddress}, Timeout: {Timeout}...",
"Started", logIpAddress, timeout);

var buf = new byte[40];
for (var i = 0; i < buf.Length; i++)
buf[i] = 5;

var pingReply = await ping.SendPingAsync(ipAddress, timeout, buf, pingOptions);
var pingReply = await ping.SendPingAsync(ipAddress, timeout);
if (pingReply.Status != IPStatus.Success)
throw new Exception($"Status: {pingReply.Status}");

Expand Down
7 changes: 3 additions & 4 deletions VpnHood.Client/Diagnosing/Diagnoser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public class Diagnoser
{new(IPAddress.Parse("8.8.8.8"), 53), new(IPAddress.Parse("1.1.1.1"), 53)};

public Uri[] TestHttpUris { get; set; } = { new("https://www.google.com"), new("https://www.quad9.net/"), new("https://www.microsoft.com/") };
public int PingTtl { get; set; } = 128;
public int HttpTimeout { get; set; } = 10 * 1000;
public int NsTimeout { get; set; } = 10 * 1000;
public event EventHandler? StateChanged;
Expand Down Expand Up @@ -63,11 +62,11 @@ public async Task Diagnose(VpnHoodConnect clientConnect)
// ping server
VhLogger.Instance.LogTrace("Checking the VpnServer ping...");
var hostEndPoint = await clientConnect.Client.Token.ResolveHostEndPointAsync();
var pingRes = await DiagnoseUtil.CheckPing(new[] { hostEndPoint.Address }, NsTimeout, 128, true);
var pingRes = await DiagnoseUtil.CheckPing(new[] { hostEndPoint.Address }, NsTimeout, true);
if (pingRes == null)
VhLogger.Instance.LogTrace("Pinging server is OK.");
else
VhLogger.Instance.LogWarning($"Could not ping server! EndPoint: {VhLogger.Format(hostEndPoint)}, Error: {pingRes.Message}");
VhLogger.Instance.LogWarning($"Could not ping server! EndPoint: {VhLogger.Format(hostEndPoint.Address)}, Error: {pingRes.Message}");

// VpnConnect
IsWorking = false;
Expand All @@ -88,7 +87,7 @@ public async Task Diagnose(VpnHoodConnect clientConnect)
private async Task<bool> NetworkCheck(bool checkPing = true, bool checkUdp = true)
{
var taskPing = checkPing
? DiagnoseUtil.CheckPing(TestPingIpAddresses, NsTimeout, PingTtl)
? DiagnoseUtil.CheckPing(TestPingIpAddresses, NsTimeout)
: Task.FromResult((Exception?)null);

var taskUdp = checkUdp
Expand Down

0 comments on commit 8465afc

Please sign in to comment.