2026-10-11 23:23:20 +00:00
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.Sockets;
|
2026-11-13 10:35:04 +08:00
|
|
|
using System.Text.Json;
|
2026-10-11 23:23:20 +00:00
|
|
|
|
|
|
|
|
namespace UCVL.VPNPlus;
|
|
|
|
|
|
|
|
|
|
internal static class PlatformBootstrapTransportSelfTest
|
|
|
|
|
{
|
|
|
|
|
internal static async Task<int> RunAsync()
|
|
|
|
|
{
|
2026-11-13 10:35:04 +08:00
|
|
|
var checks = new Dictionary<string, bool>();
|
2026-10-11 23:23:20 +00:00
|
|
|
using var handler = PlatformBootstrapTransport.CreateHandler();
|
2026-11-13 10:35:04 +08:00
|
|
|
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"));
|
2026-10-11 23:23:20 +00:00
|
|
|
|
|
|
|
|
var endpoint = new DnsEndPoint(PlatformBootstrapTransport.Host, 443);
|
2026-11-13 10:35:04 +08:00
|
|
|
var attempts = new List<IPEndPoint>();
|
|
|
|
|
ValueTask<Stream> Dial(IPAddress ip, int port, CancellationToken token)
|
2026-10-11 23:23:20 +00:00
|
|
|
{
|
2026-11-13 10:35:04 +08:00
|
|
|
token.ThrowIfCancellationRequested();
|
|
|
|
|
attempts.Add(new(ip, port));
|
|
|
|
|
return ValueTask.FromResult<Stream>(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]);
|
|
|
|
|
|
2026-10-11 23:23:20 +00:00
|
|
|
attempts.Clear();
|
2026-11-13 10:35:04 +08:00
|
|
|
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<Stream>(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<int> RunLiveAsync()
|
|
|
|
|
{
|
|
|
|
|
using var client = new HttpClient(PlatformBootstrapTransport.CreateHandler())
|
|
|
|
|
{
|
|
|
|
|
BaseAddress = new Uri(VpnApiClient.BaseAddress),
|
|
|
|
|
Timeout = TimeSpan.FromSeconds(12),
|
|
|
|
|
};
|
|
|
|
|
var statuses = new Dictionary<string, int>();
|
|
|
|
|
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<string, bool>
|
|
|
|
|
{
|
|
|
|
|
["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 });
|
2026-10-11 23:23:20 +00:00
|
|
|
}
|
|
|
|
|
|
2026-11-13 10:35:04 +08:00
|
|
|
private static int WriteReport(string filename, Dictionary<string, bool> 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; }
|
|
|
|
|
}
|
2026-10-11 23:23:20 +00:00
|
|
|
}
|