From a1c8bc0e618b6bf4c46c7b2d41bb1fb2564b8db9 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Mon, 11 Nov 2024 16:56:08 +0800 Subject: [PATCH] Improve the code --- v2rayN/ServiceLib/Handler/CoreHandler.cs | 70 +++++++++---------- v2rayN/ServiceLib/Handler/CoreInfoHandler.cs | 4 +- v2rayN/ServiceLib/Resx/ResUI.zh-Hans.resx | 2 +- v2rayN/ServiceLib/Resx/ResUI.zh-Hant.resx | 2 +- .../ViewModels/RoutingRuleDetailsViewModel.cs | 2 +- .../Views/RoutingRuleDetailsWindow.axaml.cs | 4 +- 6 files changed, 41 insertions(+), 43 deletions(-) diff --git a/v2rayN/ServiceLib/Handler/CoreHandler.cs b/v2rayN/ServiceLib/Handler/CoreHandler.cs index acb91c20..77d91597 100644 --- a/v2rayN/ServiceLib/Handler/CoreHandler.cs +++ b/v2rayN/ServiceLib/Handler/CoreHandler.cs @@ -145,7 +145,7 @@ namespace ServiceLib.Handler private string CoreFindExe(CoreInfo coreInfo) { - string fileName = string.Empty; + var fileName = string.Empty; foreach (var name in coreInfo.CoreExes) { var vName = Utils.GetBinPath(Utils.GetExeName(name), coreInfo.CoreType.ToString()); @@ -157,7 +157,7 @@ namespace ServiceLib.Handler } 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); ShowMsg(false, msg); } @@ -183,7 +183,7 @@ namespace ServiceLib.Handler var coreInfo = CoreInfoHandler.Instance.GetCoreInfo(coreType); 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) { return; @@ -220,12 +220,12 @@ namespace ServiceLib.Handler } if (itemSocks != null) { - string fileName2 = Utils.GetConfigPath(Global.CorePreConfigFileName); + var fileName2 = Utils.GetConfigPath(Global.CorePreConfigFileName); var result = await CoreConfigHandler.GenerateClientConfig(itemSocks, fileName2); if (result.Success) { 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) { _processPre = proc2; @@ -243,7 +243,7 @@ namespace ServiceLib.Handler try { 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) { return -1; @@ -254,8 +254,7 @@ namespace ServiceLib.Handler catch (Exception ex) { Logging.SaveLog(ex.Message, ex); - string msg = ex.Message; - ShowMsg(false, msg); + ShowMsg(false, ex.Message); return -1; } } @@ -269,15 +268,15 @@ namespace ServiceLib.Handler #region Process - private async Task RunProcess(ProfileItem node, CoreInfo coreInfo, string configPath, bool displayLog) + private async Task RunProcess(CoreInfo coreInfo, string configPath, bool displayLog) { + var fileName = CoreFindExe(coreInfo); + if (Utils.IsNullOrEmpty(fileName)) + { + return null; + } try { - string fileName = CoreFindExe(coreInfo); - if (Utils.IsNullOrEmpty(fileName)) - { - return null; - } Process proc = new() { StartInfo = new() @@ -299,23 +298,17 @@ namespace ServiceLib.Handler { proc.OutputDataReceived += (sender, e) => { - if (Utils.IsNotEmpty(e.Data)) - { - string msg = e.Data + Environment.NewLine; - ShowMsg(false, msg); - } + if (Utils.IsNullOrEmpty(e.Data)) return; + ShowMsg(false, e.Data + Environment.NewLine); }; proc.ErrorDataReceived += (sender, e) => { - if (Utils.IsNotEmpty(e.Data)) - { - string msg = e.Data + Environment.NewLine; - ShowMsg(false, msg); + if (Utils.IsNullOrEmpty(e.Data)) return; + ShowMsg(false, e.Data + Environment.NewLine); - if (!startUpSuccessful) - { - startUpErrorMessage.Append(msg); - } + if (!startUpSuccessful) + { + startUpErrorMessage.Append(e.Data + Environment.NewLine); } }; } @@ -342,8 +335,7 @@ namespace ServiceLib.Handler catch (Exception ex) { Logging.SaveLog(ex.Message, ex); - string msg = ex.Message; - ShowMsg(true, msg); + ShowMsg(true, ex.Message); return null; } } @@ -354,20 +346,26 @@ namespace ServiceLib.Handler { return; } + var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(1)); try + { + await proc.WaitForExitAsync(timeout.Token); + } + catch (OperationCanceledException) { proc.Kill(); - proc.WaitForExit(100); - if (!proc.HasExited) + } + if (!proc.HasExited) + { + try + { + await proc.WaitForExitAsync(timeout.Token); + } + catch (Exception) { proc.Kill(); - proc.WaitForExit(100); } } - catch (Exception ex) - { - Logging.SaveLog(ex.Message, ex); - } } #endregion Process diff --git a/v2rayN/ServiceLib/Handler/CoreInfoHandler.cs b/v2rayN/ServiceLib/Handler/CoreInfoHandler.cs index 346aa957..aeb5e57d 100644 --- a/v2rayN/ServiceLib/Handler/CoreInfoHandler.cs +++ b/v2rayN/ServiceLib/Handler/CoreInfoHandler.cs @@ -72,7 +72,7 @@ { CoreType = ECoreType.Xray, CoreExes = new List { "xray", "wxray" }, - Arguments = "run {0}", + Arguments = "run -c {0}", Url = Global.XrayCoreUrl, ReleaseApiUrl = Global.XrayCoreUrl.Replace(Global.GithubUrl, Global.GithubApiUrl), DownloadUrlWin64 = Global.XrayCoreUrl + "/download/{0}/Xray-windows-64.zip", @@ -132,7 +132,7 @@ { CoreType = ECoreType.sing_box, CoreExes = new List { "sing-box-client", "sing-box" }, - Arguments = "run {0} --disable-color", + Arguments = "run -c {0} --disable-color", Url = Global.SingboxCoreUrl, RedirectInfo = true, ReleaseApiUrl = Global.SingboxCoreUrl.Replace(Global.GithubUrl, Global.GithubApiUrl), diff --git a/v2rayN/ServiceLib/Resx/ResUI.zh-Hans.resx b/v2rayN/ServiceLib/Resx/ResUI.zh-Hans.resx index 54951788..7494aa4d 100644 --- a/v2rayN/ServiceLib/Resx/ResUI.zh-Hans.resx +++ b/v2rayN/ServiceLib/Resx/ResUI.zh-Hans.resx @@ -1271,7 +1271,7 @@ 显示或隐藏主界面 - 您当前运行的是独立包,请手动下载 SelfContained.7z文件解压覆盖 + 您当前运行的是独立包,请手动下载 SelfContained.7z文件解压覆盖! 自定义配置的Socks端口 diff --git a/v2rayN/ServiceLib/Resx/ResUI.zh-Hant.resx b/v2rayN/ServiceLib/Resx/ResUI.zh-Hant.resx index 64f7fd70..d9c8d58d 100644 --- a/v2rayN/ServiceLib/Resx/ResUI.zh-Hant.resx +++ b/v2rayN/ServiceLib/Resx/ResUI.zh-Hant.resx @@ -1151,7 +1151,7 @@ 顯示或隱藏主介面 - 您目前運行的是獨立包,請手動下載 SelfContained.7z檔案解壓縮覆蓋 + 您目前運行的是獨立包,請手動下載 SelfContained.7z檔案解壓縮覆蓋! 自訂設定的Socks端口 diff --git a/v2rayN/ServiceLib/ViewModels/RoutingRuleDetailsViewModel.cs b/v2rayN/ServiceLib/ViewModels/RoutingRuleDetailsViewModel.cs index 950b12e2..ec02b941 100644 --- a/v2rayN/ServiceLib/ViewModels/RoutingRuleDetailsViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/RoutingRuleDetailsViewModel.cs @@ -86,7 +86,7 @@ namespace ServiceLib.ViewModels return; } //NoticeHandler.Instance.Enqueue(ResUI.OperationSuccess); - _updateView?.Invoke(EViewAction.CloseWindow, null); + await _updateView?.Invoke(EViewAction.CloseWindow, null); } } } \ No newline at end of file diff --git a/v2rayN/v2rayN.Desktop/Views/RoutingRuleDetailsWindow.axaml.cs b/v2rayN/v2rayN.Desktop/Views/RoutingRuleDetailsWindow.axaml.cs index 5d1aeb25..3c2c4508 100644 --- a/v2rayN/v2rayN.Desktop/Views/RoutingRuleDetailsWindow.axaml.cs +++ b/v2rayN/v2rayN.Desktop/Views/RoutingRuleDetailsWindow.axaml.cs @@ -43,11 +43,11 @@ namespace v2rayN.Desktop.Views { rulesItem.Protocol?.ForEach(it => { - clbProtocol.SelectedItems.Add(it); + clbProtocol?.SelectedItems?.Add(it); }); rulesItem.InboundTag?.ForEach(it => { - clbInboundTag.SelectedItems.Add(it); + clbInboundTag?.SelectedItems?.Add(it); }); }