using System.Net; using System.Net.Sockets; namespace UCVL.VPNPlus; // Offline only: no real DNS, socket, request, authentication or client startup. internal static class PlatformBootstrapTransportSelfTest { internal static async Task RunAsync() { var checks = new List(); 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.toplc.cn/api/v1/client/login/")); foreach (var uri in new[] { "http://vpn.toplc.cn/", "https://vpn.toplc.cn:444/", "https://192.0.2.1/", "https://other.invalid/", "https://vpn.toplc.cn.evil.invalid/", "https://user@vpn.toplc.cn/" }) checks.Add(Rejects(() => PlatformBootstrapTransport.ValidateRequest(new Uri(uri)))); checks.Add(Rejects(() => PlatformBootstrapTransport.ValidateRequest(new Uri("https://vpn.toplc.cn/"), "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))); var endpoint = new DnsEndPoint(PlatformBootstrapTransport.Host, 443); var attempts = new List(); ValueTask Dial(IPAddress ip, int port, CancellationToken ct) { ct.ThrowIfCancellationRequested(); attempts.Add(ip + ":" + port); return ValueTask.FromResult(new MemoryStream()); } Task 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(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) => { 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" })); 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; } private static bool Rejects(Action action) { try { action(); return false; } catch { return true; } } private static async Task RejectsAsync(Func action) { try { await action(); return false; } catch { return true; } } }