2026-10-11 23:23:20 +00:00
|
|
|
namespace UCVL.VPNPlus;
|
|
|
|
|
|
|
|
|
|
// Advisory after connection only; never an Internet-site gate in StartAsync.
|
|
|
|
|
// A delayed probe must not undo a user's disconnect or judge a different core.
|
|
|
|
|
internal sealed class ConnectionHealthPolicy
|
|
|
|
|
{
|
2026-11-13 10:35:04 +08:00
|
|
|
internal enum Decision { Ignore, Keep, Repair, PendingAdvisory, WebsiteUnavailable, TailnetEgressUnverified, Switch }
|
2026-10-11 23:23:20 +00:00
|
|
|
internal long Revision { get; private set; }
|
|
|
|
|
internal int Failures { get; private set; }
|
|
|
|
|
internal bool HasBeenHealthy { get; private set; }
|
|
|
|
|
private long _nextProbe;
|
2026-11-13 10:35:04 +08:00
|
|
|
private Decision? _advisoryCandidate;
|
|
|
|
|
internal void Invalidate()
|
|
|
|
|
{
|
|
|
|
|
Revision++;
|
|
|
|
|
_advisoryCandidate = null;
|
|
|
|
|
}
|
2026-10-11 23:23:20 +00:00
|
|
|
internal void BeginCore(long now)
|
|
|
|
|
{
|
|
|
|
|
Invalidate(); Failures = 0; HasBeenHealthy = false;
|
|
|
|
|
_nextProbe = now + 5_000;
|
|
|
|
|
}
|
|
|
|
|
internal void ResetFailures() => Failures = 0;
|
|
|
|
|
internal bool IsDue(long now, bool running) => !running || now >= _nextProbe;
|
|
|
|
|
internal Decision Observe(long revision, int? testedCore, int? currentCore,
|
2026-11-05 03:41:46 +00:00
|
|
|
bool wanted, bool busy, bool transitioning, bool exiting, bool healthy, long now, bool proxyHealthy = false,
|
|
|
|
|
bool activeTailnet = false, bool businessUnavailable = false)
|
2026-10-11 23:23:20 +00:00
|
|
|
{
|
|
|
|
|
if (revision != Revision || testedCore != currentCore || !wanted || busy || transitioning || exiting)
|
|
|
|
|
return Decision.Ignore;
|
|
|
|
|
if (healthy && currentCore is not null) {
|
2026-11-13 10:35:04 +08:00
|
|
|
_advisoryCandidate = null;
|
2026-10-11 23:23:20 +00:00
|
|
|
HasBeenHealthy = true; Failures = 0; _nextProbe = now + 20_000;
|
|
|
|
|
return Decision.Keep;
|
|
|
|
|
}
|
2026-11-05 03:41:46 +00:00
|
|
|
if (currentCore is not null && (businessUnavailable && proxyHealthy || activeTailnet && !proxyHealthy)) {
|
|
|
|
|
// Third-party HTTP diagnostics cannot identify an active Tailnet
|
|
|
|
|
// transport failure. The bounded periodic ICMP policy owns fallback.
|
|
|
|
|
Failures = 0; _nextProbe = now + 20_000;
|
2026-11-13 10:35:04 +08:00
|
|
|
var advisory = proxyHealthy ? Decision.WebsiteUnavailable : Decision.TailnetEgressUnverified;
|
|
|
|
|
var confirmed = _advisoryCandidate == advisory;
|
|
|
|
|
_advisoryCandidate = advisory;
|
|
|
|
|
return confirmed ? advisory : Decision.PendingAdvisory;
|
2026-11-05 03:41:46 +00:00
|
|
|
}
|
2026-11-13 10:35:04 +08:00
|
|
|
_advisoryCandidate = null;
|
2026-10-11 23:23:20 +00:00
|
|
|
if (proxyHealthy && currentCore is not null) {
|
|
|
|
|
// Do not punish/restart a working node for a local smart-rule fault.
|
|
|
|
|
// The UI must show degraded routing, not declare the business path healthy.
|
|
|
|
|
Failures = 0; _nextProbe = now + 20_000;
|
|
|
|
|
return Decision.Repair;
|
|
|
|
|
}
|
|
|
|
|
Failures++;
|
|
|
|
|
_nextProbe = now + (HasBeenHealthy ? 20_000 : 5_000);
|
|
|
|
|
// Established connections retain tolerance for two transient rounds.
|
|
|
|
|
// An unverified newly selected node gets one retry, not a minute of
|
|
|
|
|
// false connected state; a dead core does not wait for HTTP timeouts.
|
|
|
|
|
return currentCore is null || Failures >= (HasBeenHealthy ? 3 : 2)
|
|
|
|
|
? Decision.Switch : Decision.Keep;
|
|
|
|
|
}
|
|
|
|
|
}
|