From e4d3a98aa814add980d575973306b61a76999695 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Sat, 30 Nov 2024 10:58:53 +0800 Subject: [PATCH] Fixed the problem of traffic statistics api url --- v2rayN/ServiceLib/Handler/AppHandler.cs | 9 ++++++++- v2rayN/ServiceLib/Handler/CoreHandler.cs | 13 +++++++------ .../Services/Statistics/StatisticsSingboxService.cs | 8 +++----- .../Services/Statistics/StatisticsXrayService.cs | 5 ++--- .../ServiceLib/ViewModels/OptionSettingViewModel.cs | 1 + 5 files changed, 21 insertions(+), 15 deletions(-) diff --git a/v2rayN/ServiceLib/Handler/AppHandler.cs b/v2rayN/ServiceLib/Handler/AppHandler.cs index 9615b73a..447c31f9 100644 --- a/v2rayN/ServiceLib/Handler/AppHandler.cs +++ b/v2rayN/ServiceLib/Handler/AppHandler.cs @@ -27,7 +27,7 @@ get { _statePort2 ??= Utils.GetFreePort(GetLocalPort(EInboundProtocol.api2)); - return _statePort2.Value; + return _statePort2.Value + (_config.TunModeItem.EnableTun ? 1 : 0); } } @@ -79,6 +79,13 @@ return true; } + public bool Reset() + { + _statePort = null; + _statePort2 = null; + return true; + } + #endregion Init #region Config diff --git a/v2rayN/ServiceLib/Handler/CoreHandler.cs b/v2rayN/ServiceLib/Handler/CoreHandler.cs index a5073dc0..78bc505e 100644 --- a/v2rayN/ServiceLib/Handler/CoreHandler.cs +++ b/v2rayN/ServiceLib/Handler/CoreHandler.cs @@ -97,12 +97,12 @@ namespace ServiceLib.Handler { if (_process != null) { - await KillProcess(_process); + _process = await KillProcess(_process); } if (_processPre != null) { - await KillProcess(_processPre); + _processPre = await KillProcess(_processPre); } if (_linuxSudoPid > 0) @@ -321,17 +321,18 @@ namespace ServiceLib.Handler } } - private async Task KillProcess(Process? proc) + private async Task KillProcess(Process? proc) { if (proc is null) { - return; + return null; } try { proc?.Kill(true); } catch { } try { proc?.Close(); } catch { } try { proc?.Dispose(); } catch { } - proc = null; + await Task.Delay(100); + return null; } #endregion Process @@ -394,7 +395,7 @@ namespace ServiceLib.Handler private async Task CreateLinuxShellFile(string cmdLine, string fileName) { //Shell scripts - var shFilePath = Utils.GetBinPath(fileName); + var shFilePath = Utils.GetBinPath(AppHandler.Instance.IsAdministrator ? "root_" + fileName : fileName); File.Delete(shFilePath); var sb = new StringBuilder(); sb.AppendLine("#!/bin/sh"); diff --git a/v2rayN/ServiceLib/Services/Statistics/StatisticsSingboxService.cs b/v2rayN/ServiceLib/Services/Statistics/StatisticsSingboxService.cs index a051fc95..e8f294e4 100644 --- a/v2rayN/ServiceLib/Services/Statistics/StatisticsSingboxService.cs +++ b/v2rayN/ServiceLib/Services/Statistics/StatisticsSingboxService.cs @@ -8,9 +8,9 @@ namespace ServiceLib.Services.Statistics private Config _config; private bool _exitFlag; private ClientWebSocket? webSocket; - private string url = string.Empty; private Action? _updateFunc; - + private string Url => $"ws://{Global.Loopback}:{AppHandler.Instance.StatePort2}/traffic"; + public StatisticsSingboxService(Config config, Action updateFunc) { _config = config; @@ -26,12 +26,10 @@ namespace ServiceLib.Services.Statistics try { - url = $"ws://{Global.Loopback}:{AppHandler.Instance.StatePort2}/traffic"; - if (webSocket == null) { webSocket = new ClientWebSocket(); - await webSocket.ConnectAsync(new Uri(url), CancellationToken.None); + await webSocket.ConnectAsync(new Uri(Url), CancellationToken.None); } } catch { } diff --git a/v2rayN/ServiceLib/Services/Statistics/StatisticsXrayService.cs b/v2rayN/ServiceLib/Services/Statistics/StatisticsXrayService.cs index 274e0f2e..fee5721f 100644 --- a/v2rayN/ServiceLib/Services/Statistics/StatisticsXrayService.cs +++ b/v2rayN/ServiceLib/Services/Statistics/StatisticsXrayService.cs @@ -3,18 +3,17 @@ public class StatisticsXrayService { private const long linkBase = 1024; - private string _url; private ServerSpeedItem _serverSpeedItem = new(); private Config _config; private bool _exitFlag; private Action? _updateFunc; + private string Url => $"{Global.HttpProtocol}{Global.Loopback}:{AppHandler.Instance.StatePort}/debug/vars"; public StatisticsXrayService(Config config, Action updateFunc) { _config = config; _updateFunc = updateFunc; _exitFlag = false; - _url = $"{Global.HttpProtocol}{Global.Loopback}:{AppHandler.Instance.StatePort}/debug/vars"; Task.Run(Run); } @@ -36,7 +35,7 @@ continue; } - var result = await HttpClientHelper.Instance.TryGetAsync(_url); + var result = await HttpClientHelper.Instance.TryGetAsync(Url); if (result != null) { var server = ParseOutput(result) ?? new ServerSpeedItem(); diff --git a/v2rayN/ServiceLib/ViewModels/OptionSettingViewModel.cs b/v2rayN/ServiceLib/ViewModels/OptionSettingViewModel.cs index 53fe1df6..1a8de48a 100644 --- a/v2rayN/ServiceLib/ViewModels/OptionSettingViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/OptionSettingViewModel.cs @@ -356,6 +356,7 @@ namespace ServiceLib.ViewModels if (await ConfigHandler.SaveConfig(_config) == 0) { await AutoStartupHandler.UpdateTask(_config); + AppHandler.Instance.Reset(); NoticeHandler.Instance.Enqueue(needReboot ? ResUI.NeedRebootTips : ResUI.OperationSuccess); _updateView?.Invoke(EViewAction.CloseWindow, null);