30 lines
1.3 KiB
C#
30 lines
1.3 KiB
C#
namespace UCVL.VPNPlus;
|
|
|
|
// A bounded round tolerates isolated ICMP loss. This is address reachability,
|
|
// never a substitute for an authenticated proxy grant or a business-site test.
|
|
internal static class TailnetPingConfirmation
|
|
{
|
|
internal const int Attempts = 3;
|
|
internal static async Task<bool> ProbeAsync(Func<CancellationToken, Task<bool>> ping,
|
|
CancellationToken token, TimeSpan? budget = null)
|
|
{
|
|
token.ThrowIfCancellationRequested();
|
|
using var deadline = CancellationTokenSource.CreateLinkedTokenSource(token);
|
|
deadline.CancelAfter(budget ?? TimeSpan.FromSeconds(4));
|
|
for (var attempt = 0; attempt < Attempts; attempt++)
|
|
{
|
|
try
|
|
{
|
|
var reachable = await ping(deadline.Token).WaitAsync(deadline.Token).ConfigureAwait(false);
|
|
token.ThrowIfCancellationRequested();
|
|
if (reachable) return true;
|
|
}
|
|
catch (OperationCanceledException) when (!token.IsCancellationRequested && deadline.IsCancellationRequested) { return false; }
|
|
catch (OperationCanceledException) { throw; }
|
|
catch (Exception) { /* One failed ICMP attempt is inconclusive. */ }
|
|
token.ThrowIfCancellationRequested();
|
|
}
|
|
return false;
|
|
}
|
|
}
|