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) { // 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. if (!allowCompatibilityMapping || response.HasTailscaleEndpoint || response.ConfigContract != 2 || response.ConnectionScope is not ("" or "public") || response.RouteMode is not ("smart" or "global")) return null; 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 }; 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(); if (root.ContainsKey(TailnetTransport.Marker) || PrivateAuxiliaryRuntime.IsPrivate(root)) return null; var proxy = TailnetTransport.Proxy(root); var server = proxy?["settings"]?["vnext"]?[0]; if (server?["address"]?.GetValue() != mapping.Host || server?["port"]?.GetValue() != mapping.PublicPort) return null; 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) { return null; } } }