Publish Zonghengjia Windows 0.3.13 client source

This commit is contained in:
2026-11-13 10:35:04 +08:00
parent 1590443809
commit 839434a313
55 changed files with 2324 additions and 963 deletions
+90 -44
View File
@@ -1,61 +1,107 @@
using System.Net;
using System.Net.Sockets;
using System.Text.Json;
namespace UCVL.VPNPlus;
// Offline only: no real DNS, socket, request, authentication or client startup.
internal static class PlatformBootstrapTransportSelfTest
{
internal static async Task<int> RunAsync()
{
var checks = new List<bool>();
var checks = new Dictionary<string, bool>();
using var handler = PlatformBootstrapTransport.CreateHandler();
checks.Add(!handler.UseProxy && !handler.AllowAutoRedirect && !handler.UseCookies);
checks.Add(handler.SslOptions.RemoteCertificateValidationCallback is null);
PlatformBootstrapTransport.ValidateRequest(new Uri("https://vpn.example.invalid/api/v1/client/login/"));
foreach (var uri in new[] { "http://vpn.example.invalid/", "https://vpn.example.invalid:444/", "https://192.0.2.1/",
"https://other.invalid/", "https://vpn.example.invalid.evil.invalid/", "https://user@vpn.example.invalid/" })
checks.Add(Rejects(() => PlatformBootstrapTransport.ValidateRequest(new Uri(uri))));
checks.Add(Rejects(() => PlatformBootstrapTransport.ValidateRequest(new Uri("https://vpn.example.invalid/"), "other.invalid")));
foreach (var ip in new[] { "198.18.0.0", "198.19.255.255", "::ffff:198.18.0.41" })
checks.Add(PlatformBootstrapTransport.IsFakeAddress(IPAddress.Parse(ip)));
foreach (var ip in new[] { "198.17.255.255", "198.20.0.0", "192.0.2.1", "::1" })
checks.Add(!PlatformBootstrapTransport.IsFakeAddress(IPAddress.Parse(ip)));
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<string>();
ValueTask<Stream> Dial(IPAddress ip, int port, CancellationToken ct)
{ ct.ThrowIfCancellationRequested(); attempts.Add(ip + ":" + port); return ValueTask.FromResult<Stream>(new MemoryStream()); }
Task<IPAddress[]> Normal(string host, CancellationToken ct) => Task.FromResult(new[] { IPAddress.Parse("203.0.113.1") });
using (await PlatformBootstrapTransport.ConnectAsync(endpoint, CancellationToken.None, Normal, Dial)) { }
checks.Add(attempts.SequenceEqual(new[] { "203.0.113.1:443" }));
attempts.Clear();
using (await PlatformBootstrapTransport.ConnectAsync(endpoint, CancellationToken.None,
(_, _) => Task.FromResult(new[] { IPAddress.Parse("198.18.0.41") }), Dial)) { }
checks.Add(attempts.SequenceEqual(new[] { "192.0.2.1:443" }));
attempts.Clear();
using (await PlatformBootstrapTransport.ConnectAsync(endpoint, CancellationToken.None,
(_, _) => Task.FromException<IPAddress[]>(new SocketException((int)SocketError.HostNotFound)), Dial)) { }
checks.Add(attempts.SequenceEqual(new[] { "192.0.2.1:443" }));
attempts.Clear();
using (await PlatformBootstrapTransport.ConnectAsync(endpoint, CancellationToken.None, Normal, (ip, port, ct) =>
var attempts = new List<IPEndPoint>();
ValueTask<Stream> Dial(IPAddress ip, int port, CancellationToken token)
{
if (ip.ToString() == "203.0.113.1") { attempts.Add("normal-failed"); throw new SocketException((int)SocketError.ConnectionRefused); }
return Dial(ip, port, ct);
})) { }
checks.Add(attempts.SequenceEqual(new[] { "normal-failed", "192.0.2.1:443" }));
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]);
attempts.Clear();
using var canceled = new CancellationTokenSource(); canceled.Cancel();
checks.Add(await RejectsAsync(() => PlatformBootstrapTransport.ConnectAsync(endpoint, canceled.Token, Normal, Dial).AsTask()));
checks.Add(attempts.Count == 0);
checks.Add(await RejectsAsync(() => PlatformBootstrapTransport.ConnectAsync(new DnsEndPoint("other.invalid", 443), CancellationToken.None, Normal, Dial).AsTask()));
checks.Add(attempts.Count == 0);
checks.Add(await RejectsAsync(() => PlatformBootstrapTransport.ConnectAsync(endpoint, CancellationToken.None, Normal,
(_, _, _) => throw new System.Security.Authentication.AuthenticationException("fixture TLS failure")).AsTask()));
checks.Add(attempts.Count == 0); // Non-connect failures do not select bootstrap.
return checks.All(pass => pass) ? 0 : 83;
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 });
}
private static bool Rejects(Action action) { try { action(); return false; } catch { return true; } }
private static async Task<bool> RejectsAsync(Func<Task> action) { try { await action(); return false; } catch { return true; } }
// 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 });
}
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; }
}
}