using System.Net; using System.Net.Sockets; using System.Text.Json; namespace UCVL.VPNPlus; internal static class PlatformBootstrapTransportSelfTest { internal static async Task RunAsync() { var checks = new Dictionary(); using var handler = PlatformBootstrapTransport.CreateHandler(); checks["system_proxy_redirect_and_cookies_disabled"] = !handler.UseProxy && !handler.AllowAutoRedirect && !handler.UseCookies; checks["normal_tls_certificate_validation"] = handler.SslOptions.RemoteCertificateValidationCallback is null; var origin = new Uri("https://vpn.ucvl.cn/api/v1/client/login/"); checks["management_https_origin_accepted"] = !Rejects(() => PlatformBootstrapTransport.ValidateRequest(origin, "vpn.ucvl.cn:443")); checks["alternate_or_insecure_authorities_rejected"] = new[] { "http://vpn.ucvl.cn/", "https://vpn.ucvl.cn:444/", "https://47.104.134.190/", "https://vpn.toplc.cn/", "https://vpn.ucvl.cn.evil.invalid/", "https://user@vpn.ucvl.cn/" } .All(uri => Rejects(() => PlatformBootstrapTransport.ValidateRequest(new Uri(uri)))); checks["different_http_host_rejected"] = Rejects(() => PlatformBootstrapTransport.ValidateRequest(origin, "other.invalid")); var endpoint = new DnsEndPoint(PlatformBootstrapTransport.Host, 443); var attempts = new List(); ValueTask Dial(IPAddress ip, int port, CancellationToken token) { token.ThrowIfCancellationRequested(); attempts.Add(new(ip, port)); return ValueTask.FromResult(new MemoryStream()); } using (await PlatformBootstrapTransport.ConnectAsync(endpoint, CancellationToken.None, Dial)) { } var expected = new IPEndPoint(IPAddress.Parse("47.104.134.190"), 443); checks["dials_only_qingdao_ipv4"] = attempts.SequenceEqual([expected]); attempts.Clear(); using var canceled = new CancellationTokenSource(); canceled.Cancel(); checks["pre_cancel_prevents_socket"] = Rejects(() => PlatformBootstrapTransport.ConnectAsync(endpoint, canceled.Token, Dial)) && attempts.Count == 0; checks["mismatched_socket_host_and_port_do_not_dial"] = Rejects(() => PlatformBootstrapTransport.ConnectAsync(new DnsEndPoint("other.invalid", 443), default, Dial)) && Rejects(() => PlatformBootstrapTransport.ConnectAsync(new DnsEndPoint(endpoint.Host, 444), default, Dial)) && attempts.Count == 0; var refused = new SocketException((int)SocketError.ConnectionRefused); try { using var _ = await PlatformBootstrapTransport.ConnectAsync(endpoint, default, (ip, port, token) => { attempts.Add(new(ip, port)); return ValueTask.FromException(refused); }); checks["socket_error_propagates_without_alternative"] = false; } catch (SocketException failure) { checks["socket_error_propagates_without_alternative"] = ReferenceEquals(failure, refused) && attempts.SequenceEqual([expected]); } return WriteReport("platform-bootstrap-self-test.json", checks, new { network_modified = false, live = false }); } // Optional read-only production smoke with the exact management handler. // No credentials, session establishment, browser launch, TUN or proxy. internal static async Task RunLiveAsync() { using var client = new HttpClient(PlatformBootstrapTransport.CreateHandler()) { BaseAddress = new Uri(VpnApiClient.BaseAddress), Timeout = TimeSpan.FromSeconds(12), }; var statuses = new Dictionary(); foreach (var path in new[] { "health/", "api/v1/client/config/?node=auto&route=smart", "register/" }) { try { using var response = await client.GetAsync(path, HttpCompletionOption.ResponseHeadersRead); statuses[path] = (int)response.StatusCode; } catch (Exception e) when (e is HttpRequestException or TaskCanceledException) { statuses[path] = 0; } } var checks = new Dictionary { ["health_uses_pinned_ipv4_with_valid_tls"] = statuses["health/"] == 200, ["config_preserves_authentication"] = statuses["api/v1/client/config/?node=auto&route=smart"] is 401 or 403, ["registration_portal_origin_reachable"] = statuses["register/"] is >= 200 and < 400, }; return WriteReport("platform-bootstrap-live-test.json", checks, new { network_modified = false, live = true, authority = PlatformBootstrapTransport.Host, ipv4 = PlatformBootstrapTransport.ManagementAddress.ToString(), statuses }); } private static int WriteReport(string filename, Dictionary checks, object details) { var passed = checks.Values.All(value => value); File.WriteAllText(Path.Combine(AppContext.BaseDirectory, filename), JsonSerializer.Serialize(new { passed, checks, details }, new JsonSerializerOptions { WriteIndented = true })); return passed ? 0 : 83; } private static bool Rejects(Action action) { try { action(); return false; } catch (Exception e) when (e is InvalidOperationException or OperationCanceledException) { return true; } } }