Improve the code

pull/6045/head
2dust 2024-11-11 16:56:08 +08:00
parent 61e9101851
commit a1c8bc0e61
6 changed files with 41 additions and 43 deletions

View File

@ -145,7 +145,7 @@ namespace ServiceLib.Handler
private string CoreFindExe(CoreInfo coreInfo) private string CoreFindExe(CoreInfo coreInfo)
{ {
string fileName = string.Empty; var fileName = string.Empty;
foreach (var name in coreInfo.CoreExes) foreach (var name in coreInfo.CoreExes)
{ {
var vName = Utils.GetBinPath(Utils.GetExeName(name), coreInfo.CoreType.ToString()); var vName = Utils.GetBinPath(Utils.GetExeName(name), coreInfo.CoreType.ToString());
@ -157,7 +157,7 @@ namespace ServiceLib.Handler
} }
if (Utils.IsNullOrEmpty(fileName)) if (Utils.IsNullOrEmpty(fileName))
{ {
string msg = string.Format(ResUI.NotFoundCore, Utils.GetBinPath("", coreInfo.CoreType.ToString()), string.Join(", ", coreInfo.CoreExes.ToArray()), coreInfo.Url); var msg = string.Format(ResUI.NotFoundCore, Utils.GetBinPath("", coreInfo.CoreType.ToString()), string.Join(", ", coreInfo.CoreExes.ToArray()), coreInfo.Url);
Logging.SaveLog(msg); Logging.SaveLog(msg);
ShowMsg(false, msg); ShowMsg(false, msg);
} }
@ -183,7 +183,7 @@ namespace ServiceLib.Handler
var coreInfo = CoreInfoHandler.Instance.GetCoreInfo(coreType); var coreInfo = CoreInfoHandler.Instance.GetCoreInfo(coreType);
var displayLog = node.ConfigType != EConfigType.Custom || node.DisplayLog; var displayLog = node.ConfigType != EConfigType.Custom || node.DisplayLog;
var proc = await RunProcess(node, coreInfo, "", displayLog); var proc = await RunProcess(coreInfo, Global.CoreConfigFileName, displayLog);
if (proc is null) if (proc is null)
{ {
return; return;
@ -220,12 +220,12 @@ namespace ServiceLib.Handler
} }
if (itemSocks != null) if (itemSocks != null)
{ {
string fileName2 = Utils.GetConfigPath(Global.CorePreConfigFileName); var fileName2 = Utils.GetConfigPath(Global.CorePreConfigFileName);
var result = await CoreConfigHandler.GenerateClientConfig(itemSocks, fileName2); var result = await CoreConfigHandler.GenerateClientConfig(itemSocks, fileName2);
if (result.Success) if (result.Success)
{ {
var coreInfo2 = CoreInfoHandler.Instance.GetCoreInfo(preCoreType); var coreInfo2 = CoreInfoHandler.Instance.GetCoreInfo(preCoreType);
var proc2 = await RunProcess(node, coreInfo2, $" -c {Global.CorePreConfigFileName}", true); var proc2 = await RunProcess(coreInfo2, Global.CorePreConfigFileName, true);
if (proc2 is not null) if (proc2 is not null)
{ {
_processPre = proc2; _processPre = proc2;
@ -243,7 +243,7 @@ namespace ServiceLib.Handler
try try
{ {
var coreInfo = CoreInfoHandler.Instance.GetCoreInfo(coreType); var coreInfo = CoreInfoHandler.Instance.GetCoreInfo(coreType);
var proc = await RunProcess(new(), coreInfo, $" -c {Global.CoreSpeedtestConfigFileName}", true); var proc = await RunProcess(coreInfo, Global.CoreSpeedtestConfigFileName, true);
if (proc is null) if (proc is null)
{ {
return -1; return -1;
@ -254,8 +254,7 @@ namespace ServiceLib.Handler
catch (Exception ex) catch (Exception ex)
{ {
Logging.SaveLog(ex.Message, ex); Logging.SaveLog(ex.Message, ex);
string msg = ex.Message; ShowMsg(false, ex.Message);
ShowMsg(false, msg);
return -1; return -1;
} }
} }
@ -269,15 +268,15 @@ namespace ServiceLib.Handler
#region Process #region Process
private async Task<Process?> RunProcess(ProfileItem node, CoreInfo coreInfo, string configPath, bool displayLog) private async Task<Process?> RunProcess(CoreInfo coreInfo, string configPath, bool displayLog)
{ {
try var fileName = CoreFindExe(coreInfo);
{
string fileName = CoreFindExe(coreInfo);
if (Utils.IsNullOrEmpty(fileName)) if (Utils.IsNullOrEmpty(fileName))
{ {
return null; return null;
} }
try
{
Process proc = new() Process proc = new()
{ {
StartInfo = new() StartInfo = new()
@ -299,23 +298,17 @@ namespace ServiceLib.Handler
{ {
proc.OutputDataReceived += (sender, e) => proc.OutputDataReceived += (sender, e) =>
{ {
if (Utils.IsNotEmpty(e.Data)) if (Utils.IsNullOrEmpty(e.Data)) return;
{ ShowMsg(false, e.Data + Environment.NewLine);
string msg = e.Data + Environment.NewLine;
ShowMsg(false, msg);
}
}; };
proc.ErrorDataReceived += (sender, e) => proc.ErrorDataReceived += (sender, e) =>
{ {
if (Utils.IsNotEmpty(e.Data)) if (Utils.IsNullOrEmpty(e.Data)) return;
{ ShowMsg(false, e.Data + Environment.NewLine);
string msg = e.Data + Environment.NewLine;
ShowMsg(false, msg);
if (!startUpSuccessful) if (!startUpSuccessful)
{ {
startUpErrorMessage.Append(msg); startUpErrorMessage.Append(e.Data + Environment.NewLine);
}
} }
}; };
} }
@ -342,8 +335,7 @@ namespace ServiceLib.Handler
catch (Exception ex) catch (Exception ex)
{ {
Logging.SaveLog(ex.Message, ex); Logging.SaveLog(ex.Message, ex);
string msg = ex.Message; ShowMsg(true, ex.Message);
ShowMsg(true, msg);
return null; return null;
} }
} }
@ -354,19 +346,25 @@ namespace ServiceLib.Handler
{ {
return; return;
} }
var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(1));
try try
{
await proc.WaitForExitAsync(timeout.Token);
}
catch (OperationCanceledException)
{ {
proc.Kill(); proc.Kill();
proc.WaitForExit(100); }
if (!proc.HasExited) if (!proc.HasExited)
{ {
proc.Kill(); try
proc.WaitForExit(100);
}
}
catch (Exception ex)
{ {
Logging.SaveLog(ex.Message, ex); await proc.WaitForExitAsync(timeout.Token);
}
catch (Exception)
{
proc.Kill();
}
} }
} }

View File

@ -72,7 +72,7 @@
{ {
CoreType = ECoreType.Xray, CoreType = ECoreType.Xray,
CoreExes = new List<string> { "xray", "wxray" }, CoreExes = new List<string> { "xray", "wxray" },
Arguments = "run {0}", Arguments = "run -c {0}",
Url = Global.XrayCoreUrl, Url = Global.XrayCoreUrl,
ReleaseApiUrl = Global.XrayCoreUrl.Replace(Global.GithubUrl, Global.GithubApiUrl), ReleaseApiUrl = Global.XrayCoreUrl.Replace(Global.GithubUrl, Global.GithubApiUrl),
DownloadUrlWin64 = Global.XrayCoreUrl + "/download/{0}/Xray-windows-64.zip", DownloadUrlWin64 = Global.XrayCoreUrl + "/download/{0}/Xray-windows-64.zip",
@ -132,7 +132,7 @@
{ {
CoreType = ECoreType.sing_box, CoreType = ECoreType.sing_box,
CoreExes = new List<string> { "sing-box-client", "sing-box" }, CoreExes = new List<string> { "sing-box-client", "sing-box" },
Arguments = "run {0} --disable-color", Arguments = "run -c {0} --disable-color",
Url = Global.SingboxCoreUrl, Url = Global.SingboxCoreUrl,
RedirectInfo = true, RedirectInfo = true,
ReleaseApiUrl = Global.SingboxCoreUrl.Replace(Global.GithubUrl, Global.GithubApiUrl), ReleaseApiUrl = Global.SingboxCoreUrl.Replace(Global.GithubUrl, Global.GithubApiUrl),

View File

@ -1271,7 +1271,7 @@
<value>显示或隐藏主界面</value> <value>显示或隐藏主界面</value>
</data> </data>
<data name="UpdateStandalonePackageTip" xml:space="preserve"> <data name="UpdateStandalonePackageTip" xml:space="preserve">
<value>您当前运行的是独立包,请手动下载 SelfContained.7z文件解压覆盖</value> <value>您当前运行的是独立包,请手动下载 SelfContained.7z文件解压覆盖</value>
</data> </data>
<data name="TbPreSocksPort4Sub" xml:space="preserve"> <data name="TbPreSocksPort4Sub" xml:space="preserve">
<value>自定义配置的Socks端口</value> <value>自定义配置的Socks端口</value>

View File

@ -1151,7 +1151,7 @@
<value>顯示或隱藏主介面</value> <value>顯示或隱藏主介面</value>
</data> </data>
<data name="UpdateStandalonePackageTip" xml:space="preserve"> <data name="UpdateStandalonePackageTip" xml:space="preserve">
<value>您目前運行的是獨立包,請手動下載 SelfContained.7z檔案解壓縮覆蓋</value> <value>您目前運行的是獨立包,請手動下載 SelfContained.7z檔案解壓縮覆蓋</value>
</data> </data>
<data name="TbPreSocksPort4Sub" xml:space="preserve"> <data name="TbPreSocksPort4Sub" xml:space="preserve">
<value>自訂設定的Socks端口</value> <value>自訂設定的Socks端口</value>

View File

@ -86,7 +86,7 @@ namespace ServiceLib.ViewModels
return; return;
} }
//NoticeHandler.Instance.Enqueue(ResUI.OperationSuccess); //NoticeHandler.Instance.Enqueue(ResUI.OperationSuccess);
_updateView?.Invoke(EViewAction.CloseWindow, null); await _updateView?.Invoke(EViewAction.CloseWindow, null);
} }
} }
} }

View File

@ -43,11 +43,11 @@ namespace v2rayN.Desktop.Views
{ {
rulesItem.Protocol?.ForEach(it => rulesItem.Protocol?.ForEach(it =>
{ {
clbProtocol.SelectedItems.Add(it); clbProtocol?.SelectedItems?.Add(it);
}); });
rulesItem.InboundTag?.ForEach(it => rulesItem.InboundTag?.ForEach(it =>
{ {
clbInboundTag.SelectedItems.Add(it); clbInboundTag?.SelectedItems?.Add(it);
}); });
} }