Browse Source

Improved CliWrap

pull/5829/head
2dust 1 month ago
parent
commit
fea7c9fbd7
  1. 49
      v2rayN/ServiceLib/Common/Utils.cs
  2. 25
      v2rayN/ServiceLib/Services/UpdateService.cs
  3. 16
      v2rayN/v2rayN.Desktop/Common/ProxySettingLinux.cs

49
v2rayN/ServiceLib/Common/Utils.cs

@ -691,6 +691,41 @@ namespace ServiceLib.Common
return systemHosts; return systemHosts;
} }
public static async Task<string?> GetCliWrapOutput(string filePath, string? arg)
{
return await GetCliWrapOutput(filePath, arg != null ? [arg] : null);
}
public static async Task<string?> GetCliWrapOutput(string filePath, IEnumerable<string>? 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 杂项 #endregion 杂项
#region TempPath #region TempPath
@ -879,19 +914,7 @@ namespace ServiceLib.Common
private static async Task<string?> GetLinuxUserId() private static async Task<string?> GetLinuxUserId()
{ {
try return await GetCliWrapOutput("/bin/bash", ["-c", "id -u"]);
{
var result = await Cli.Wrap("/bin/bash")
.WithArguments(["-c", "id -u"])
.WithValidation(CommandResultValidation.None)
.ExecuteBufferedAsync();
return result.StandardOutput.ToString();
}
catch
{
return null;
}
} }
#endregion Platform #endregion Platform

25
v2rayN/ServiceLib/Services/UpdateService.cs

@ -1,6 +1,4 @@
using System.Diagnostics; using System.Runtime.InteropServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
namespace ServiceLib.Services namespace ServiceLib.Services
@ -298,7 +296,7 @@ namespace ServiceLib.Services
/// <summary> /// <summary>
/// 获取Core版本 /// 获取Core版本
/// </summary> /// </summary>
private SemanticVersion GetCoreVersion(ECoreType type) private async Task<SemanticVersion> GetCoreVersion(ECoreType type)
{ {
try try
{ {
@ -322,17 +320,8 @@ namespace ServiceLib.Services
return new SemanticVersion(""); return new SemanticVersion("");
} }
using Process p = new(); var result = await Utils.GetCliWrapOutput(filePath, coreInfo.VersionArg);
p.StartInfo.FileName = filePath; var echo = result ?? "";
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();
string version = string.Empty; string version = string.Empty;
switch (type) switch (type)
{ {
@ -379,21 +368,21 @@ namespace ServiceLib.Services
case ECoreType.Xray: case ECoreType.Xray:
case ECoreType.v2fly_v5: case ECoreType.v2fly_v5:
{ {
curVersion = GetCoreVersion(type); curVersion = await GetCoreVersion(type);
message = string.Format(ResUI.IsLatestCore, type, curVersion.ToVersionString("v")); message = string.Format(ResUI.IsLatestCore, type, curVersion.ToVersionString("v"));
url = string.Format(GetUrlFromCore(coreInfo), version.ToVersionString("v")); url = string.Format(GetUrlFromCore(coreInfo), version.ToVersionString("v"));
break; break;
} }
case ECoreType.mihomo: case ECoreType.mihomo:
{ {
curVersion = GetCoreVersion(type); curVersion = await GetCoreVersion(type);
message = string.Format(ResUI.IsLatestCore, type, curVersion); message = string.Format(ResUI.IsLatestCore, type, curVersion);
url = string.Format(GetUrlFromCore(coreInfo), version.ToVersionString("v")); url = string.Format(GetUrlFromCore(coreInfo), version.ToVersionString("v"));
break; break;
} }
case ECoreType.sing_box: case ECoreType.sing_box:
{ {
curVersion = GetCoreVersion(type); curVersion = await GetCoreVersion(type);
message = string.Format(ResUI.IsLatestCore, type, curVersion.ToVersionString("v")); message = string.Format(ResUI.IsLatestCore, type, curVersion.ToVersionString("v"));
url = string.Format(GetUrlFromCore(coreInfo), version.ToVersionString("v"), version); url = string.Format(GetUrlFromCore(coreInfo), version.ToVersionString("v"), version);
break; break;

16
v2rayN/v2rayN.Desktop/Common/ProxySettingLinux.cs

@ -1,7 +1,4 @@
using CliWrap; namespace v2rayN.Desktop.Common
using CliWrap.Buffered;
namespace v2rayN.Desktop.Common
{ {
public class ProxySettingLinux public class ProxySettingLinux
{ {
@ -25,17 +22,8 @@ namespace v2rayN.Desktop.Common
{ {
if (cmd is null || cmd.Cmd.IsNullOrEmpty() || cmd.Arguments.IsNullOrEmpty()) if (cmd is null || cmd.Cmd.IsNullOrEmpty() || cmd.Arguments.IsNullOrEmpty())
{ continue; } { continue; }
await Task.Delay(10); await Task.Delay(10);
var result = await Cli.Wrap(cmd.Cmd) await Utils.GetCliWrapOutput(cmd.Cmd, cmd.Arguments);
.WithArguments(cmd.Arguments)
.ExecuteBufferedAsync();
if (result.ExitCode != 0)
{
//Logging.SaveLog($"Command failed {cmd.Cmd},{cmd.Arguments}");
Logging.SaveLog(result.ToString() ?? "");
}
} }
} }

Loading…
Cancel
Save