2026-10-11 23:23:20 +00:00
|
|
|
using System.Net;
|
|
|
|
|
|
|
|
|
|
namespace UCVL.VPNPlus;
|
|
|
|
|
|
|
|
|
|
// An advisory probe for the explicitly retained automatic failover feature.
|
|
|
|
|
// It is NOT part of 0.2.5's connection transaction and never changes routes.
|
|
|
|
|
internal static class NodeHealthProbe
|
|
|
|
|
{
|
|
|
|
|
internal static bool Accepts(int target, int status) => target == 0
|
|
|
|
|
? status is 200 or 204
|
|
|
|
|
: status is >= 200 and < 500;
|
|
|
|
|
|
|
|
|
|
internal static bool OverallHealthy(IReadOnlyList<bool> results) =>
|
|
|
|
|
results.Count >= 3 && results[2] && (results[0] || results[1]);
|
|
|
|
|
|
2026-11-02 04:13:58 +00:00
|
|
|
internal static async Task<bool> AggregateAsync(IReadOnlyList<Task<bool>> probes,
|
|
|
|
|
CancellationToken token, Action cancelRemaining)
|
|
|
|
|
{
|
|
|
|
|
async Task<bool> ResultAsync(Task<bool> probe)
|
|
|
|
|
{
|
|
|
|
|
try { return await probe.ConfigureAwait(false); }
|
|
|
|
|
catch { return false; }
|
|
|
|
|
}
|
|
|
|
|
// Observe every probe even after an early verdict. Cancellation or a
|
|
|
|
|
// late HTTP exception from a losing probe must not escape unobserved.
|
|
|
|
|
var observations = probes.Select(ResultAsync).ToArray();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
token.ThrowIfCancellationRequested();
|
|
|
|
|
if (observations.Length < 3) return false;
|
|
|
|
|
var results = new bool?[3];
|
|
|
|
|
var pending = Enumerable.Range(0, 3).ToList();
|
|
|
|
|
while (pending.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
var completed = await Task.WhenAny(pending.Select(index => observations[index]))
|
|
|
|
|
.WaitAsync(token).ConfigureAwait(false);
|
|
|
|
|
var index = pending.First(index => observations[index] == completed);
|
|
|
|
|
results[index] = await completed.ConfigureAwait(false);
|
|
|
|
|
pending.Remove(index);
|
|
|
|
|
token.ThrowIfCancellationRequested();
|
|
|
|
|
// Codex is mandatory; either independent target is sufficient.
|
|
|
|
|
// Stop only once that same predicate is proved or impossible.
|
|
|
|
|
if (results[2] == false || results[0] == false && results[1] == false) return false;
|
|
|
|
|
if (results[2] == true && (results[0] == true || results[1] == true)) return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
cancelRemaining();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-11 23:23:20 +00:00
|
|
|
internal static async Task<bool> CheckAsync(CancellationToken token, int proxyPort = 0)
|
|
|
|
|
{
|
|
|
|
|
using var timeout = CancellationTokenSource.CreateLinkedTokenSource(token);
|
|
|
|
|
timeout.CancelAfter(TimeSpan.FromSeconds(15));
|
|
|
|
|
using var client = new HttpClient(new SocketsHttpHandler {
|
|
|
|
|
UseProxy = true, Proxy = new WebProxy(proxyPort > 0 ? $"socks5://127.0.0.1:{proxyPort}" : "http://127.0.0.1:10809"),
|
|
|
|
|
ConnectTimeout = TimeSpan.FromSeconds(8), AllowAutoRedirect = false,
|
|
|
|
|
}) { Timeout = Timeout.InfiniteTimeSpan };
|
|
|
|
|
var urls = new[] { "https://www.google.com/generate_204", "https://api.openai.com/v1/models",
|
|
|
|
|
"https://chatgpt.com/backend-api/codex/models" };
|
2026-11-02 04:13:58 +00:00
|
|
|
var probes = urls.Select(async (url, index) => {
|
2026-10-11 23:23:20 +00:00
|
|
|
try {
|
|
|
|
|
using var response = await client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead, timeout.Token);
|
|
|
|
|
return Accepts(index, (int)response.StatusCode);
|
|
|
|
|
}
|
|
|
|
|
catch (OperationCanceledException) when (token.IsCancellationRequested) { throw; }
|
|
|
|
|
catch { return false; }
|
2026-11-02 04:13:58 +00:00
|
|
|
}).ToArray();
|
2026-10-11 23:23:20 +00:00
|
|
|
// This client exists primarily for ChatGPT/Codex. Google or the OpenAI
|
|
|
|
|
// REST API remaining reachable must not mask a broken Codex transport.
|
|
|
|
|
// Require the Codex endpoint plus one independent Internet target.
|
2026-11-02 04:13:58 +00:00
|
|
|
return await AggregateAsync(probes, token, timeout.Cancel);
|
2026-10-11 23:23:20 +00:00
|
|
|
}
|
|
|
|
|
}
|