2026-10-22 05:43:39 +00:00
|
|
|
using System.Text.Json;
|
|
|
|
|
|
|
|
|
|
namespace UCVL.VPNPlus;
|
|
|
|
|
|
|
|
|
|
internal static class PublicTailnetSelectionSelfTest
|
|
|
|
|
{
|
|
|
|
|
internal static async Task<int> RunAsync()
|
|
|
|
|
{
|
|
|
|
|
var checks = new Dictionary<string, bool>();
|
2026-11-13 10:35:04 +08:00
|
|
|
NodeInfo[] profile = [new() { Slug = "primary" }, new() { Slug = "backup" }, new PrivateAuxiliaryNode { Slug = "private" }];
|
|
|
|
|
var auto = ConnectionSelection.Create(false, null, profile.Select(n => n.Slug));
|
|
|
|
|
var auxiliary = new PrivateAuxiliaryNode { Slug = "private", ProbeIpv4 = "100.84.199.104" };
|
|
|
|
|
var failed = new Dictionary<string, DateTimeOffset>();
|
|
|
|
|
var requests = new List<string>();
|
|
|
|
|
var privateSearches = 0;
|
|
|
|
|
Task<IReadOnlyList<PrivateAuxiliaryNode>> RankPrivate(CancellationToken _)
|
2026-10-22 05:43:39 +00:00
|
|
|
{
|
2026-11-13 10:35:04 +08:00
|
|
|
privateSearches++;
|
|
|
|
|
return Task.FromResult<IReadOnlyList<PrivateAuxiliaryNode>>([auxiliary]);
|
2026-10-22 05:43:39 +00:00
|
|
|
}
|
2026-11-13 10:35:04 +08:00
|
|
|
ConfigResponse Response(string slug, string route = "smart", string scope = "public") =>
|
|
|
|
|
new() { Node = new() { Slug = slug }, RouteMode = route, ConnectionScope = scope };
|
2026-10-22 05:43:39 +00:00
|
|
|
|
2026-11-13 10:35:04 +08:00
|
|
|
await foreach (var attempt in TransportPlan.AttemptsAsync(auto, "smart", 8, null, RankPrivate, default))
|
2026-10-22 05:43:39 +00:00
|
|
|
{
|
2026-11-13 10:35:04 +08:00
|
|
|
requests.Add(VpnApiClient.ConfigPath(attempt.Slug, "smart", failed.Keys));
|
|
|
|
|
auto.ValidateConfig(attempt, Response("primary"), "smart", profile, failed);
|
|
|
|
|
break;
|
2026-10-22 05:43:39 +00:00
|
|
|
}
|
2026-11-13 10:35:04 +08:00
|
|
|
checks["healthy_primary_consumes_one_sticky_dispatch"] = privateSearches == 0 && requests.SequenceEqual(
|
|
|
|
|
["api/v1/client/config/?node=auto&route=smart"]);
|
2026-10-22 05:43:39 +00:00
|
|
|
|
2026-11-13 10:35:04 +08:00
|
|
|
requests.Clear();
|
|
|
|
|
var attempted = new List<string>();
|
|
|
|
|
await foreach (var attempt in TransportPlan.AttemptsAsync(auto, "smart", 8, null, RankPrivate, default))
|
2026-10-22 05:43:39 +00:00
|
|
|
{
|
2026-11-13 10:35:04 +08:00
|
|
|
if (attempt.IsPrivate) { attempted.Add(attempt.Slug); continue; }
|
|
|
|
|
requests.Add(VpnApiClient.ConfigPath(attempt.Slug, "smart", failed.Keys));
|
|
|
|
|
var node = failed.ContainsKey("primary") ? "backup" : "primary";
|
|
|
|
|
auto.ValidateConfig(attempt, Response(node), "smart", profile, failed);
|
|
|
|
|
attempted.Add(node);
|
|
|
|
|
if (node == "backup") break;
|
|
|
|
|
failed[node] = DateTimeOffset.UtcNow.AddMinutes(5);
|
2026-10-22 05:43:39 +00:00
|
|
|
}
|
2026-11-13 10:35:04 +08:00
|
|
|
checks["primary_failure_defers_private_search_and_backup_dispatch"] = privateSearches == 1
|
|
|
|
|
&& attempted.SequenceEqual(["primary", "private", "backup"])
|
|
|
|
|
&& requests.SequenceEqual(["api/v1/client/config/?node=auto&route=smart", "api/v1/client/config/?node=auto&route=smart&exclude=primary"]);
|
2026-10-22 05:43:39 +00:00
|
|
|
|
2026-11-13 10:35:04 +08:00
|
|
|
attempted.Clear();
|
|
|
|
|
await foreach (var attempt in TransportPlan.AttemptsAsync(auto, "global", 99, "primary", RankPrivate, default))
|
|
|
|
|
attempted.Add(attempt.Slug);
|
|
|
|
|
checks["same_node_retry_precedes_discovery_and_two_public_backups"] =
|
|
|
|
|
attempted.SequenceEqual(["primary", "private", "auto", "auto"]);
|
|
|
|
|
checks["same_node_config_does_not_request_rebalance_or_exclusions"] =
|
|
|
|
|
VpnApiClient.ConfigPath("primary", "global", failed.Keys) == "api/v1/client/config/?node=primary&route=global";
|
2026-10-22 05:43:39 +00:00
|
|
|
|
2026-11-13 10:35:04 +08:00
|
|
|
var manual = ConnectionSelection.Create(true, "private", profile.Select(n => n.Slug));
|
|
|
|
|
attempted.Clear();
|
|
|
|
|
await foreach (var attempt in TransportPlan.AttemptsAsync(manual, "smart", 3, "primary", RankPrivate, default))
|
|
|
|
|
attempted.Add(attempt.Slug);
|
|
|
|
|
checks["manual_stays_at_selected_site"] = attempted.SequenceEqual(["private"]);
|
|
|
|
|
var before = privateSearches;
|
|
|
|
|
attempted.Clear();
|
|
|
|
|
await foreach (var attempt in TransportPlan.AttemptsAsync(auto, "direct", 3, null, RankPrivate, default))
|
|
|
|
|
attempted.Add(attempt.Slug);
|
|
|
|
|
checks["direct_has_no_endpoint_discovery"] = privateSearches == before && attempted.SequenceEqual(["auto", "auto", "auto"]);
|
2026-10-22 05:43:39 +00:00
|
|
|
|
2026-11-13 10:35:04 +08:00
|
|
|
bool Rejected(ConfigResponse response, string requested = "auto")
|
|
|
|
|
{
|
|
|
|
|
try { auto.ValidateConfig(new(requested, null), response, "smart", profile, failed); return false; }
|
|
|
|
|
catch (InvalidOperationException) { return true; }
|
|
|
|
|
}
|
|
|
|
|
checks["auto_cannot_repeat_excluded_node"] = Rejected(Response("primary"));
|
|
|
|
|
checks["auto_cannot_probe_node_outside_authorized_profile"] = Rejected(Response("unexpected"));
|
|
|
|
|
checks["public_config_cannot_use_private_scope_or_private_slug"] =
|
|
|
|
|
Rejected(Response("backup", scope: PrivateAuxiliaryRouting.Scope)) && Rejected(Response("private"));
|
|
|
|
|
checks["mismatched_route_and_explicit_node_rejected"] =
|
|
|
|
|
Rejected(Response("backup", "global")) && Rejected(Response("backup"), "primary");
|
2026-11-03 15:15:23 +00:00
|
|
|
|
2026-11-13 10:35:04 +08:00
|
|
|
using var cancelled = new CancellationTokenSource();
|
|
|
|
|
var entered = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
|
|
|
|
|
var never = new TaskCompletionSource<IReadOnlyList<PrivateAuxiliaryNode>>(TaskCreationOptions.RunContinuationsAsynchronously);
|
|
|
|
|
async Task ConsumeAsync()
|
2026-11-03 15:15:23 +00:00
|
|
|
{
|
2026-11-13 10:35:04 +08:00
|
|
|
await foreach (var _ in TransportPlan.AttemptsAsync(auto, "smart", 0, null, _ =>
|
|
|
|
|
{ entered.SetResult(); return never.Task; }, cancelled.Token)) { }
|
2026-11-03 15:15:23 +00:00
|
|
|
}
|
2026-11-13 10:35:04 +08:00
|
|
|
var pending = ConsumeAsync();
|
|
|
|
|
await entered.Task.WaitAsync(TimeSpan.FromSeconds(1));
|
|
|
|
|
cancelled.Cancel();
|
|
|
|
|
try
|
2026-11-03 15:15:23 +00:00
|
|
|
{
|
2026-11-13 10:35:04 +08:00
|
|
|
await pending.WaitAsync(TimeSpan.FromSeconds(1));
|
|
|
|
|
checks["cancellation_interrupts_discovery"] = false;
|
2026-11-03 15:15:23 +00:00
|
|
|
}
|
2026-11-13 10:35:04 +08:00
|
|
|
catch (OperationCanceledException) { checks["cancellation_interrupts_discovery"] = true; }
|
|
|
|
|
|
|
|
|
|
var report = new { passed = checks.Values.All(v => v), checks, simulated_transport = true, network_modified = false };
|
|
|
|
|
File.WriteAllText(Path.Combine(AppContext.BaseDirectory, "public-tailnet-selection-self-test.json"),
|
|
|
|
|
JsonSerializer.Serialize(report, new JsonSerializerOptions { WriteIndented = true }));
|
2026-10-22 05:43:39 +00:00
|
|
|
return report.passed ? 0 : 83;
|
|
|
|
|
}
|
|
|
|
|
}
|