2026-10-11 23:23:20 +00:00
using System.Diagnostics ;
using System.Net ;
using System.Net.Sockets ;
using System.Text.Json ;
using System.Text.Json.Nodes ;
namespace UCVL.VPNPlus ;
internal static class LearningTransportSelfTest
{
internal static async Task < int > RunAsync ()
{
var folder = Path . Combine ( Path . GetTempPath (), "ucvl-learning-transport-" + Guid . NewGuid (). ToString ( "N" ));
Directory . CreateDirectory ( folder );
var configPath = Path . Combine ( folder , "fixture.json" );
Process ? process = null ;
var report = new Dictionary < string , object > { [ "scope" ] = "isolated real proxy DoH + destination TLS; not TUN, business login or automatic fallback acceptance" ,
["os_network_modified"] = false , [ "production_observations_submitted" ] = false };
try
{
using var deadline = new CancellationTokenSource ( TimeSpan . FromSeconds ( 75 ));
using var api = new VpnApiClient ();
var token = TokenStore . Load ();
if ( string . IsNullOrWhiteSpace ( token )) throw new IOException ( "No app session available for read-only configuration." );
var profile = await api . GetProfileAsync ( token , deadline . Token );
var node = profile . Nodes . First ();
report [ "node" ] = node . Name ;
var response = await api . GetConfigAsync ( token , node . Slug , "smart" , deadline . Token );
var config = LearningPolicy . DirectFirst ( response . GetRuntimeConfigJson ());
var addresses = new List < IPAddress >();
foreach ( var host in TunConfigBuilder . GetProxyServerHosts ( config )) addresses . AddRange ( await Dns . GetHostAddressesAsync ( host , deadline . Token ));
var physical = TunRouting . GetPhysicalNetwork ( addresses );
using var reservation = new TcpListener ( IPAddress . Loopback , 0 );
reservation . Start ();
var port = (( IPEndPoint ) reservation . LocalEndpoint ). Port ;
reservation . Stop ();
var root = JsonNode . Parse ( config )!. AsObject ();
root [ "inbounds" ] = new JsonArray ();
root = JsonNode . Parse ( TunConfigBuilder . Build ( root . ToJsonString (), physical . InterfaceName ,
includeLocalLearning : false , includeTun : false , learningProbePort : port ))!. AsObject ();
root [ "log" ] = new JsonObject { [ "loglevel" ] = "none" , [ "access" ] = "none" };
File . WriteAllText ( configPath , root . ToJsonString ());
var start = new ProcessStartInfo ( Path . Combine ( AppContext . BaseDirectory , "xray" , "xray.exe" )) {
UseShellExecute = false , CreateNoWindow = true , RedirectStandardOutput = true , RedirectStandardError = true };
foreach ( var arg in new [] { "run" , "-c" , configPath }) start . ArgumentList . Add ( arg );
process = Process . Start ( start )!;
_ = process . StandardOutput . ReadToEndAsync (); _ = process . StandardError . ReadToEndAsync ();
await Task . Delay ( 900 , deadline . Token );
var checks = new Dictionary < string , bool >();
foreach ( var target in new [] { "www.google.com" , "chatgpt.com" })
checks [ target ] = await ProxyProbe . ProbeTlsAsync ( new () { TargetType = "domain" , Target = target , Port = 443 , Network = "tcp" }, port , deadline . Token ) == true ;
2026-11-13 10:35:04 +08:00
checks [ "private_destination_rejected" ] = await ProxyProbe . ProbeTlsAsync ( new () { TargetType = "ip" , Target = "172.16.0.111" , Port = 443 , Network = "tcp" }, port , deadline . Token ) is null ;
2026-10-11 23:23:20 +00:00
report [ "checks" ] = checks ;
report [ "passed" ] = checks . Values . All ( value => value );
return checks . Values . All ( value => value ) ? 0 : 32 ;
}
catch ( Exception error ) { report [ "error_type" ] = error . GetType (). Name ; report [ "passed" ] = false ; return 32 ; }
finally
{
if ( process is not null ) { try { if (! process . HasExited ) process . Kill ( true ); await process . WaitForExitAsync (); } finally { process . Dispose (); } }
if ( File . Exists ( configPath )) File . Delete ( configPath );
Directory . Delete ( folder );
File . WriteAllText ( Path . Combine ( AppContext . BaseDirectory , "learning-transport-self-test.json" ), JsonSerializer . Serialize ( report , new JsonSerializerOptions { WriteIndented = true }));
}
}
}