2026-10-11 23:23:20 +00:00
|
|
|
using System.Text.Json.Nodes;
|
|
|
|
|
|
|
|
|
|
namespace UCVL.VPNPlus;
|
|
|
|
|
|
|
|
|
|
// Compatibility addresses explicitly supplied by the operator and authenticated
|
|
|
|
|
// against these two existing nodes on 2026-09-26. Not new nodes or login grants.
|
|
|
|
|
// The authenticated API config remains the authority for credentials and identity.
|
|
|
|
|
internal static class KnownTailnetEndpoints
|
|
|
|
|
{
|
|
|
|
|
internal static TailnetEndpoint? Resolve(ConfigResponse response, bool allowCompatibilityMapping)
|
2026-10-22 05:43:39 +00:00
|
|
|
=> Resolve(response, allowCompatibilityMapping, out _);
|
|
|
|
|
|
|
|
|
|
internal static TailnetEndpoint? Resolve(ConfigResponse response, bool allowCompatibilityMapping, out string reason)
|
2026-10-11 23:23:20 +00:00
|
|
|
{
|
|
|
|
|
// This switch permits legacy endpoint mapping, not an employee or
|
|
|
|
|
// tailnet-membership policy. The API still supplies node authorization.
|
|
|
|
|
// An explicit field (including null/invalid metadata) ALWAYS wins. Never
|
|
|
|
|
// turn server rejection or an unknown contract into a compiled fallback.
|
2026-10-22 05:43:39 +00:00
|
|
|
reason = "compatibility_disabled";
|
|
|
|
|
if (!allowCompatibilityMapping) return null;
|
|
|
|
|
reason = "explicit_endpoint_present";
|
|
|
|
|
if (response.HasTailscaleEndpoint) return null;
|
|
|
|
|
reason = "config_contract_unsupported";
|
|
|
|
|
if (response.ConfigContract != 2) return null;
|
|
|
|
|
reason = "scope_unsupported";
|
|
|
|
|
if (response.ConnectionScope is not ("" or "public")) return null;
|
|
|
|
|
reason = "route_mode_unsupported";
|
|
|
|
|
if (response.RouteMode is not ("smart" or "global")) return null;
|
2026-10-11 23:23:20 +00:00
|
|
|
var mapping = response.Node.Slug switch
|
|
|
|
|
{
|
|
|
|
|
"aliyun-tokyo-002" => (Host: "vpn002.example.invalid", PublicPort: 11082, Address: "100.64.0.2", Port: 10803),
|
|
|
|
|
"aliyun-siliconvalley-003" => (Host: "vpn003.example.invalid", PublicPort: 11083, Address: "100.64.0.3", Port: 11083),
|
|
|
|
|
_ => default
|
|
|
|
|
};
|
2026-10-22 05:43:39 +00:00
|
|
|
reason = "known_node_unmapped";
|
2026-10-11 23:23:20 +00:00
|
|
|
if (mapping.Host is null) return null;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// Check the API's original node endpoint BEFORE the public-gateway
|
|
|
|
|
// rewrite. A reused slug/name must not send credentials to an old IP.
|
|
|
|
|
var root = JsonNode.Parse(response.Config.GetRawText())!.AsObject();
|
2026-10-22 05:43:39 +00:00
|
|
|
reason = "config_scope_or_marker_invalid";
|
2026-10-11 23:23:20 +00:00
|
|
|
if (root.ContainsKey(TailnetTransport.Marker) || PrivateAuxiliaryRuntime.IsPrivate(root)) return null;
|
|
|
|
|
var proxy = TailnetTransport.Proxy(root);
|
2026-10-22 05:43:39 +00:00
|
|
|
reason = "proxy_identity_unsupported";
|
|
|
|
|
if (proxy is null) return null;
|
2026-10-11 23:23:20 +00:00
|
|
|
var server = proxy?["settings"]?["vnext"]?[0];
|
2026-10-22 05:43:39 +00:00
|
|
|
reason = "known_original_ingress_mismatch";
|
2026-10-11 23:23:20 +00:00
|
|
|
if (server?["address"]?.GetValue<string>() != mapping.Host
|
|
|
|
|
|| server?["port"]?.GetValue<int>() != mapping.PublicPort) return null;
|
2026-10-22 05:43:39 +00:00
|
|
|
reason = "known_endpoint";
|
2026-10-11 23:23:20 +00:00
|
|
|
return new TailnetEndpoint { Contract = 1, NodeSlug = response.Node.Slug,
|
|
|
|
|
Address = mapping.Address, Port = mapping.Port };
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e) when (e is System.Text.Json.JsonException or InvalidOperationException or FormatException)
|
2026-10-22 05:43:39 +00:00
|
|
|
{ reason = "config_invalid"; return null; }
|
2026-10-11 23:23:20 +00:00
|
|
|
}
|
|
|
|
|
}
|