From f685682214c7873ffaf9e19606afb7f36b123139 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Fri, 29 Nov 2024 19:21:09 +0800 Subject: [PATCH] Code clean --- .../ServiceLib/Handler/AutoStartupHandler.cs | 2 +- v2rayN/ServiceLib/Handler/CoreHandler.cs | 32 ++++--------------- v2rayN/ServiceLib/ServiceLib.csproj | 2 +- .../CoreConfig/CoreConfigSingboxService.cs | 4 +-- .../ServiceLib/Services/SpeedtestService.cs | 6 ++-- .../ViewModels/OptionSettingViewModel.cs | 2 +- 6 files changed, 14 insertions(+), 34 deletions(-) diff --git a/v2rayN/ServiceLib/Handler/AutoStartupHandler.cs b/v2rayN/ServiceLib/Handler/AutoStartupHandler.cs index 6708a38e..12dc2921 100644 --- a/v2rayN/ServiceLib/Handler/AutoStartupHandler.cs +++ b/v2rayN/ServiceLib/Handler/AutoStartupHandler.cs @@ -25,7 +25,7 @@ namespace ServiceLib.Handler await SetTaskLinux(); } } - else if(Utils.IsOSX()) + else if (Utils.IsOSX()) { //TODO } diff --git a/v2rayN/ServiceLib/Handler/CoreHandler.cs b/v2rayN/ServiceLib/Handler/CoreHandler.cs index 4710dc4c..a5073dc0 100644 --- a/v2rayN/ServiceLib/Handler/CoreHandler.cs +++ b/v2rayN/ServiceLib/Handler/CoreHandler.cs @@ -98,15 +98,11 @@ namespace ServiceLib.Handler if (_process != null) { await KillProcess(_process); - _process.Dispose(); - _process = null; } if (_processPre != null) { await KillProcess(_processPre); - _processPre.Dispose(); - _processPre = null; } if (_linuxSudoPid > 0) @@ -125,8 +121,7 @@ namespace ServiceLib.Handler { try { - var _p = Process.GetProcessById(pid); - await KillProcess(_p); + await KillProcess(Process.GetProcessById(pid)); } catch (Exception ex) { @@ -332,26 +327,11 @@ namespace ServiceLib.Handler { return; } - try - { - proc?.Kill(true); - } - catch (Exception) - { - // ignored - } + try { proc?.Kill(true); } catch { } + try { proc?.Close(); } catch { } + try { proc?.Dispose(); } catch { } + proc = null; await Task.Delay(100); - if (proc?.HasExited == false) - { - try - { - proc?.Kill(); - } - catch (Exception) - { - // ignored - } - } } #endregion Process @@ -408,7 +388,7 @@ namespace ServiceLib.Handler var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(10)); await proc.WaitForExitAsync(timeout.Token); - await Task.Delay(1000); + await Task.Delay(3000); } private async Task CreateLinuxShellFile(string cmdLine, string fileName) diff --git a/v2rayN/ServiceLib/ServiceLib.csproj b/v2rayN/ServiceLib/ServiceLib.csproj index 11735c0d..48f0e359 100644 --- a/v2rayN/ServiceLib/ServiceLib.csproj +++ b/v2rayN/ServiceLib/ServiceLib.csproj @@ -4,7 +4,7 @@ net8.0 enable enable - 7.2.3 + 7.2.2 diff --git a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs index 4f9245b4..3cdcbe3d 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs @@ -26,7 +26,7 @@ namespace ServiceLib.Services.CoreConfig ret.Msg = ResUI.CheckServerSettings; return ret; } - if (node.GetNetwork() is nameof(ETransport.kcp) or nameof(ETransport.xhttp)) + if (node.GetNetwork() is nameof(ETransport.kcp) or nameof(ETransport.xhttp)) { ret.Msg = ResUI.Incorrectconfiguration + $" - {node.GetNetwork()}"; return ret; @@ -548,7 +548,7 @@ namespace ServiceLib.Services.CoreConfig } var tunInbound = JsonUtils.Deserialize(Utils.GetEmbedText(Global.TunSingboxInboundFileName)) ?? new Inbound4Sbox { }; - tunInbound.interface_name = Utils.IsOSX()? $"utun{new Random().Next(99)}": "singbox_tun"; + tunInbound.interface_name = Utils.IsOSX() ? $"utun{new Random().Next(99)}" : "singbox_tun"; tunInbound.mtu = _config.TunModeItem.Mtu; tunInbound.strict_route = _config.TunModeItem.StrictRoute; tunInbound.stack = _config.TunModeItem.Stack; diff --git a/v2rayN/ServiceLib/Services/SpeedtestService.cs b/v2rayN/ServiceLib/Services/SpeedtestService.cs index e257d421..d0fdd775 100644 --- a/v2rayN/ServiceLib/Services/SpeedtestService.cs +++ b/v2rayN/ServiceLib/Services/SpeedtestService.cs @@ -185,7 +185,7 @@ namespace ServiceLib.Services { if (pid > 0) { - CoreHandler.Instance.CoreStopPid(pid); + await CoreHandler.Instance.CoreStopPid(pid); } await ProfileExHandler.Instance.SaveTo(); } @@ -252,7 +252,7 @@ namespace ServiceLib.Services if (pid > 0) { - CoreHandler.Instance.CoreStopPid(pid); + await CoreHandler.Instance.CoreStopPid(pid); } UpdateFunc("", ResUI.SpeedtestingCompleted); await ProfileExHandler.Instance.SaveTo(); @@ -317,7 +317,7 @@ namespace ServiceLib.Services if (pid > 0) { - CoreHandler.Instance.CoreStopPid(pid); + await CoreHandler.Instance.CoreStopPid(pid); } UpdateFunc("", ResUI.SpeedtestingCompleted); await ProfileExHandler.Instance.SaveTo(); diff --git a/v2rayN/ServiceLib/ViewModels/OptionSettingViewModel.cs b/v2rayN/ServiceLib/ViewModels/OptionSettingViewModel.cs index 87fc6237..53fe1df6 100644 --- a/v2rayN/ServiceLib/ViewModels/OptionSettingViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/OptionSettingViewModel.cs @@ -405,4 +405,4 @@ namespace ServiceLib.ViewModels } } } -} +} \ No newline at end of file