Compare commits

..

2 commits

Author SHA1 Message Date
2dust
d3a0b44247 Bug fix
Some checks are pending
release macos / build (Release) (push) Waiting to run
release / build (Release) (push) Waiting to run
https://github.com/2dust/v2rayN/issues/6182
2024-11-30 13:49:26 +08:00
2dust
e4d3a98aa8 Fixed the problem of traffic statistics api url 2024-11-30 10:58:53 +08:00
6 changed files with 23 additions and 17 deletions

View file

@ -27,7 +27,7 @@
get get
{ {
_statePort2 ??= Utils.GetFreePort(GetLocalPort(EInboundProtocol.api2)); _statePort2 ??= Utils.GetFreePort(GetLocalPort(EInboundProtocol.api2));
return _statePort2.Value; return _statePort2.Value + (_config.TunModeItem.EnableTun ? 1 : 0);
} }
} }
@ -79,6 +79,13 @@
return true; return true;
} }
public bool Reset()
{
_statePort = null;
_statePort2 = null;
return true;
}
#endregion Init #endregion Init
#region Config #region Config

View file

@ -97,12 +97,12 @@ namespace ServiceLib.Handler
{ {
if (_process != null) if (_process != null)
{ {
await KillProcess(_process); _process = await KillProcess(_process);
} }
if (_processPre != null) if (_processPre != null)
{ {
await KillProcess(_processPre); _processPre = await KillProcess(_processPre);
} }
if (_linuxSudoPid > 0) if (_linuxSudoPid > 0)
@ -321,17 +321,18 @@ namespace ServiceLib.Handler
} }
} }
private async Task KillProcess(Process? proc) private async Task<Process?> KillProcess(Process? proc)
{ {
if (proc is null) if (proc is null)
{ {
return; return null;
} }
try { proc?.Kill(true); } catch { } try { proc?.Kill(true); } catch { }
try { proc?.Close(); } catch { } try { proc?.Close(); } catch { }
try { proc?.Dispose(); } catch { } try { proc?.Dispose(); } catch { }
proc = null;
await Task.Delay(100); await Task.Delay(100);
return null;
} }
#endregion Process #endregion Process
@ -394,7 +395,7 @@ namespace ServiceLib.Handler
private async Task<string> CreateLinuxShellFile(string cmdLine, string fileName) private async Task<string> CreateLinuxShellFile(string cmdLine, string fileName)
{ {
//Shell scripts //Shell scripts
var shFilePath = Utils.GetBinPath(fileName); var shFilePath = Utils.GetBinPath(AppHandler.Instance.IsAdministrator ? "root_" + fileName : fileName);
File.Delete(shFilePath); File.Delete(shFilePath);
var sb = new StringBuilder(); var sb = new StringBuilder();
sb.AppendLine("#!/bin/sh"); sb.AppendLine("#!/bin/sh");

View file

@ -66,10 +66,10 @@
private static void GetWindowsProxyString(Config config, int port, int portSocks, out string strProxy, out string strExceptions) private static void GetWindowsProxyString(Config config, int port, int portSocks, out string strProxy, out string strExceptions)
{ {
strExceptions = ""; strExceptions = $"{config.ConstItem.DefIEProxyExceptions};{config.SystemProxyItem.SystemProxyExceptions}";
if (config.SystemProxyItem.NotProxyLocalAddress) if (config.SystemProxyItem.NotProxyLocalAddress)
{ {
strExceptions = $"<local>;{config.ConstItem.DefIEProxyExceptions};{config.SystemProxyItem.SystemProxyExceptions}"; strExceptions = $"<local>;{strExceptions}";
} }
strProxy = string.Empty; strProxy = string.Empty;

View file

@ -8,9 +8,9 @@ namespace ServiceLib.Services.Statistics
private Config _config; private Config _config;
private bool _exitFlag; private bool _exitFlag;
private ClientWebSocket? webSocket; private ClientWebSocket? webSocket;
private string url = string.Empty;
private Action<ServerSpeedItem>? _updateFunc; private Action<ServerSpeedItem>? _updateFunc;
private string Url => $"ws://{Global.Loopback}:{AppHandler.Instance.StatePort2}/traffic";
public StatisticsSingboxService(Config config, Action<ServerSpeedItem> updateFunc) public StatisticsSingboxService(Config config, Action<ServerSpeedItem> updateFunc)
{ {
_config = config; _config = config;
@ -26,12 +26,10 @@ namespace ServiceLib.Services.Statistics
try try
{ {
url = $"ws://{Global.Loopback}:{AppHandler.Instance.StatePort2}/traffic";
if (webSocket == null) if (webSocket == null)
{ {
webSocket = new ClientWebSocket(); webSocket = new ClientWebSocket();
await webSocket.ConnectAsync(new Uri(url), CancellationToken.None); await webSocket.ConnectAsync(new Uri(Url), CancellationToken.None);
} }
} }
catch { } catch { }

View file

@ -3,18 +3,17 @@
public class StatisticsXrayService public class StatisticsXrayService
{ {
private const long linkBase = 1024; private const long linkBase = 1024;
private string _url;
private ServerSpeedItem _serverSpeedItem = new(); private ServerSpeedItem _serverSpeedItem = new();
private Config _config; private Config _config;
private bool _exitFlag; private bool _exitFlag;
private Action<ServerSpeedItem>? _updateFunc; private Action<ServerSpeedItem>? _updateFunc;
private string Url => $"{Global.HttpProtocol}{Global.Loopback}:{AppHandler.Instance.StatePort}/debug/vars";
public StatisticsXrayService(Config config, Action<ServerSpeedItem> updateFunc) public StatisticsXrayService(Config config, Action<ServerSpeedItem> updateFunc)
{ {
_config = config; _config = config;
_updateFunc = updateFunc; _updateFunc = updateFunc;
_exitFlag = false; _exitFlag = false;
_url = $"{Global.HttpProtocol}{Global.Loopback}:{AppHandler.Instance.StatePort}/debug/vars";
Task.Run(Run); Task.Run(Run);
} }
@ -36,7 +35,7 @@
continue; continue;
} }
var result = await HttpClientHelper.Instance.TryGetAsync(_url); var result = await HttpClientHelper.Instance.TryGetAsync(Url);
if (result != null) if (result != null)
{ {
var server = ParseOutput(result) ?? new ServerSpeedItem(); var server = ParseOutput(result) ?? new ServerSpeedItem();

View file

@ -356,6 +356,7 @@ namespace ServiceLib.ViewModels
if (await ConfigHandler.SaveConfig(_config) == 0) if (await ConfigHandler.SaveConfig(_config) == 0)
{ {
await AutoStartupHandler.UpdateTask(_config); await AutoStartupHandler.UpdateTask(_config);
AppHandler.Instance.Reset();
NoticeHandler.Instance.Enqueue(needReboot ? ResUI.NeedRebootTips : ResUI.OperationSuccess); NoticeHandler.Instance.Enqueue(needReboot ? ResUI.NeedRebootTips : ResUI.OperationSuccess);
_updateView?.Invoke(EViewAction.CloseWindow, null); _updateView?.Invoke(EViewAction.CloseWindow, null);