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;
}
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 杂项
#region TempPath
@ -879,19 +914,7 @@ namespace ServiceLib.Common
private static async Task<string?> 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

25
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
/// <summary>
/// 获取Core版本
/// </summary>
private SemanticVersion GetCoreVersion(ECoreType type)
private async Task<SemanticVersion> 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;

16
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);
}
}

Loading…
Cancel
Save