From fea7c9fbd70cb81c5ccd57e11554d317a1fea97b Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Tue, 8 Oct 2024 20:45:56 +0800 Subject: [PATCH] Improved CliWrap --- v2rayN/ServiceLib/Common/Utils.cs | 49 ++++++++++++++----- v2rayN/ServiceLib/Services/UpdateService.cs | 25 +++------- .../Common/ProxySettingLinux.cs | 16 +----- 3 files changed, 45 insertions(+), 45 deletions(-) diff --git a/v2rayN/ServiceLib/Common/Utils.cs b/v2rayN/ServiceLib/Common/Utils.cs index 1fbe830f..3414a0e0 100644 --- a/v2rayN/ServiceLib/Common/Utils.cs +++ b/v2rayN/ServiceLib/Common/Utils.cs @@ -691,6 +691,41 @@ namespace ServiceLib.Common return systemHosts; } + public static async Task GetCliWrapOutput(string filePath, string? arg) + { + return await GetCliWrapOutput(filePath, arg != null ? [arg] : null); + } + + public static async Task GetCliWrapOutput(string filePath, IEnumerable? args) + { + try + { + var cmd = Cli.Wrap(filePath); + if (args != null) + { + if (args.Count() == 1) + { + cmd = cmd.WithArguments(args.First()); + } + else + { + cmd = cmd.WithArguments(args); + } + } + var result = await cmd.ExecuteBufferedAsync(); + if (result.IsSuccess) + { + return result.StandardOutput.ToString(); + } + Logging.SaveLog(result.ToString() ?? ""); + } + catch (Exception ex) + { + Logging.SaveLog("GetCliWrapOutput", ex); + } + return null; + } + #endregion 杂项 #region TempPath @@ -879,19 +914,7 @@ namespace ServiceLib.Common private static async Task GetLinuxUserId() { - try - { - var result = await Cli.Wrap("/bin/bash") - .WithArguments(["-c", "id -u"]) - .WithValidation(CommandResultValidation.None) - .ExecuteBufferedAsync(); - - return result.StandardOutput.ToString(); - } - catch - { - return null; - } + return await GetCliWrapOutput("/bin/bash", ["-c", "id -u"]); } #endregion Platform diff --git a/v2rayN/ServiceLib/Services/UpdateService.cs b/v2rayN/ServiceLib/Services/UpdateService.cs index 02d54f06..c5034b6b 100644 --- a/v2rayN/ServiceLib/Services/UpdateService.cs +++ b/v2rayN/ServiceLib/Services/UpdateService.cs @@ -1,6 +1,4 @@ -using System.Diagnostics; -using System.Runtime.InteropServices; -using System.Text; +using System.Runtime.InteropServices; using System.Text.RegularExpressions; namespace ServiceLib.Services @@ -298,7 +296,7 @@ namespace ServiceLib.Services /// /// 获取Core版本 /// - private SemanticVersion GetCoreVersion(ECoreType type) + private async Task GetCoreVersion(ECoreType type) { try { @@ -322,17 +320,8 @@ namespace ServiceLib.Services return new SemanticVersion(""); } - using Process p = new(); - p.StartInfo.FileName = filePath; - p.StartInfo.Arguments = coreInfo.VersionArg; - p.StartInfo.WorkingDirectory = Utils.GetConfigPath(); - p.StartInfo.UseShellExecute = false; - p.StartInfo.RedirectStandardOutput = true; - p.StartInfo.CreateNoWindow = true; - p.StartInfo.StandardOutputEncoding = Encoding.UTF8; - p.Start(); - p.WaitForExit(5000); - string echo = p.StandardOutput.ReadToEnd(); + var result = await Utils.GetCliWrapOutput(filePath, coreInfo.VersionArg); + var echo = result ?? ""; string version = string.Empty; switch (type) { @@ -379,21 +368,21 @@ namespace ServiceLib.Services case ECoreType.Xray: case ECoreType.v2fly_v5: { - curVersion = GetCoreVersion(type); + curVersion = await GetCoreVersion(type); message = string.Format(ResUI.IsLatestCore, type, curVersion.ToVersionString("v")); url = string.Format(GetUrlFromCore(coreInfo), version.ToVersionString("v")); break; } case ECoreType.mihomo: { - curVersion = GetCoreVersion(type); + curVersion = await GetCoreVersion(type); message = string.Format(ResUI.IsLatestCore, type, curVersion); url = string.Format(GetUrlFromCore(coreInfo), version.ToVersionString("v")); break; } case ECoreType.sing_box: { - curVersion = GetCoreVersion(type); + curVersion = await GetCoreVersion(type); message = string.Format(ResUI.IsLatestCore, type, curVersion.ToVersionString("v")); url = string.Format(GetUrlFromCore(coreInfo), version.ToVersionString("v"), version); break; diff --git a/v2rayN/v2rayN.Desktop/Common/ProxySettingLinux.cs b/v2rayN/v2rayN.Desktop/Common/ProxySettingLinux.cs index 5abc9636..f4c81ec1 100644 --- a/v2rayN/v2rayN.Desktop/Common/ProxySettingLinux.cs +++ b/v2rayN/v2rayN.Desktop/Common/ProxySettingLinux.cs @@ -1,7 +1,4 @@ -using CliWrap; -using CliWrap.Buffered; - -namespace v2rayN.Desktop.Common +namespace v2rayN.Desktop.Common { public class ProxySettingLinux { @@ -25,17 +22,8 @@ namespace v2rayN.Desktop.Common { if (cmd is null || cmd.Cmd.IsNullOrEmpty() || cmd.Arguments.IsNullOrEmpty()) { continue; } - await Task.Delay(10); - var result = await Cli.Wrap(cmd.Cmd) - .WithArguments(cmd.Arguments) - .ExecuteBufferedAsync(); - - if (result.ExitCode != 0) - { - //Logging.SaveLog($"Command failed {cmd.Cmd},{cmd.Arguments}"); - Logging.SaveLog(result.ToString() ?? ""); - } + await Utils.GetCliWrapOutput(cmd.Cmd, cmd.Arguments); } }