using System.ComponentModel; using System.Diagnostics; using System.Security; namespace UCVL.VPNPlus; internal enum ClientRuntimeFault { Missing, Unreadable, Empty, CannotStart } // An installation failure is local to every node. Never feed it into node or // transport health/cooldown, or retry it by changing the user's network route. internal sealed class ClientRuntimeException(string componentPath, ClientRuntimeFault fault, Exception? inner = null) : IOException(MessageFor(componentPath, fault), inner) { internal string ComponentPath { get; } = Path.GetFullPath(componentPath); internal ClientRuntimeFault Fault { get; } = fault; private static string MessageFor(string path, ClientRuntimeFault fault) { var issue = fault switch { ClientRuntimeFault.Missing => "缺失", ClientRuntimeFault.Empty => "为空或已损坏", ClientRuntimeFault.CannotStart => "无法启动(程序文件或依赖异常)", _ => "无法读取(权限或文件占用)", }; return $"本机客户端组件{issue}:{Path.GetFileName(path)}\n位置:{Path.GetFullPath(path)}\n\n" + "请重新运行纵横家最新完整安装包,在原安装目录修复。账号和配置会保留。"; } // Only local installation identity. No token, node config or credentials. internal string Diagnostic => $"client_runtime fault={Fault} component={Clean(ComponentPath)}" + $" process={Clean(Environment.ProcessPath ?? "unknown")} version={Clean(Application.ProductVersion)}"; private static string Clean(string value) => value.Replace('\r', ' ').Replace('\n', ' '); } internal static class ClientRuntime { internal static readonly string[] Components = ["xray.exe", "geoip.dat", "geosite.dat", "wintun.dll"]; internal static void Validate(string installationDirectory) { // Resolve exclusively beside this application, never the current // working directory, PATH, or an unrelated installed Xray binary. var directory = Path.Combine(Path.GetFullPath(installationDirectory), "xray"); foreach (var name in Components) { var path = Path.Combine(directory, name); try { using var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete); if (stream.Length == 0 || stream.ReadByte() < 0) throw new ClientRuntimeException(path, ClientRuntimeFault.Empty); } catch (ClientRuntimeException) { throw; } catch (Exception error) when (error is FileNotFoundException or DirectoryNotFoundException) { throw new ClientRuntimeException(path, ClientRuntimeFault.Missing, error); } catch (Exception error) when (error is IOException or UnauthorizedAccessException or SecurityException) { throw new ClientRuntimeException(path, ClientRuntimeFault.Unreadable, error); } } } internal static bool Start(Process process) { try { return process.Start(); } // The executable can disappear or become blocked after preflight. // Classify only known local CreateProcess failures, not socket errors, // arbitrary IO, or remote Reality/TLS/configuration failures. catch (Win32Exception error) when (error.NativeErrorCode is 2 or 3 or 5 or 32 or 33 or 126 or 193 or 216 or 225 or 740) { var fault = error.NativeErrorCode is 2 or 3 ? ClientRuntimeFault.Missing : error.NativeErrorCode is 5 or 32 or 33 ? ClientRuntimeFault.Unreadable : ClientRuntimeFault.CannotStart; throw new ClientRuntimeException(process.StartInfo.FileName, fault, error); } } }