From 06636d04ac433f157498de86337787c1445c2563 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Sun, 17 Aug 2025 11:00:13 +0800 Subject: [PATCH] PacHandler is changed to singleton mode --- v2rayN/ServiceLib/Handler/PacHandler.cs | 27 ++++++++++--------- .../Handler/SysProxy/SysProxyHandler.cs | 4 +-- .../ViewModels/CheckUpdateViewModel.cs | 5 +--- .../ViewModels/StatusBarViewModel.cs | 5 +--- 4 files changed, 19 insertions(+), 22 deletions(-) diff --git a/v2rayN/ServiceLib/Handler/PacHandler.cs b/v2rayN/ServiceLib/Handler/PacHandler.cs index 6d2e1f19..51febada 100644 --- a/v2rayN/ServiceLib/Handler/PacHandler.cs +++ b/v2rayN/ServiceLib/Handler/PacHandler.cs @@ -5,15 +5,18 @@ namespace ServiceLib.Handler; public class PacHandler { - private static string _configPath; - private static int _httpPort; - private static int _pacPort; - private static TcpListener? _tcpListener; - private static byte[] _writeContent; - private static bool _isRunning; - private static bool _needRestart = true; + private static readonly Lazy _instance = new(() => new PacHandler()); + public static PacHandler Instance => _instance.Value; - public static async Task Start(string configPath, int httpPort, int pacPort) + private string _configPath; + private int _httpPort; + private int _pacPort; + private TcpListener? _tcpListener; + private byte[] _writeContent; + private bool _isRunning; + private bool _needRestart = true; + + public async Task StartAsync(string configPath, int httpPort, int pacPort) { _needRestart = configPath != _configPath || httpPort != _httpPort || pacPort != _pacPort || !_isRunning; @@ -30,7 +33,7 @@ public class PacHandler } } - private static async Task InitText() + private async Task InitText() { var path = Path.Combine(_configPath, "pac.txt"); @@ -59,7 +62,7 @@ public class PacHandler _writeContent = Encoding.UTF8.GetBytes(sb.ToString()); } - private static void RunListener() + private void RunListener() { _tcpListener = TcpListener.Create(_pacPort); _isRunning = true; @@ -87,14 +90,14 @@ public class PacHandler }, TaskCreationOptions.LongRunning); } - private static void WriteContent(TcpClient client) + private void WriteContent(TcpClient client) { var stream = client.GetStream(); stream.Write(_writeContent, 0, _writeContent.Length); stream.Flush(); } - public static void Stop() + public void Stop() { if (_tcpListener == null) { diff --git a/v2rayN/ServiceLib/Handler/SysProxy/SysProxyHandler.cs b/v2rayN/ServiceLib/Handler/SysProxy/SysProxyHandler.cs index 01361a92..ebb81325 100644 --- a/v2rayN/ServiceLib/Handler/SysProxy/SysProxyHandler.cs +++ b/v2rayN/ServiceLib/Handler/SysProxy/SysProxyHandler.cs @@ -56,7 +56,7 @@ public static class SysProxyHandler if (type != ESysProxyType.Pac && Utils.IsWindows()) { - PacHandler.Stop(); + PacHandler.Instance.Stop(); } } catch (Exception ex) @@ -91,7 +91,7 @@ public static class SysProxyHandler private static async Task SetWindowsProxyPac(int port) { var portPac = AppHandler.Instance.GetLocalPort(EInboundProtocol.pac); - await PacHandler.Start(Utils.GetConfigPath(), port, portPac); + await PacHandler.Instance.StartAsync(Utils.GetConfigPath(), port, portPac); var strProxy = $"{Global.HttpProtocol}{Global.Loopback}:{portPac}/pac?t={DateTime.Now.Ticks}"; ProxySettingWindows.SetProxy(strProxy, "", 4); } diff --git a/v2rayN/ServiceLib/ViewModels/CheckUpdateViewModel.cs b/v2rayN/ServiceLib/ViewModels/CheckUpdateViewModel.cs index f3247798..1c93a677 100644 --- a/v2rayN/ServiceLib/ViewModels/CheckUpdateViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/CheckUpdateViewModel.cs @@ -75,10 +75,7 @@ public class CheckUpdateViewModel : MyReactiveObject private async Task CheckUpdate() { - await Task.Run(async () => - { - await CheckUpdateTask(); - }); + await Task.Run(CheckUpdateTask); } private async Task CheckUpdateTask() diff --git a/v2rayN/ServiceLib/ViewModels/StatusBarViewModel.cs b/v2rayN/ServiceLib/ViewModels/StatusBarViewModel.cs index 5c2acab6..211317a6 100644 --- a/v2rayN/ServiceLib/ViewModels/StatusBarViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/StatusBarViewModel.cs @@ -334,10 +334,7 @@ public class StatusBarViewModel : MyReactiveObject _updateView?.Invoke(EViewAction.DispatcherServerAvailability, ResUI.Speedtesting); - var msg = await Task.Run(async () => - { - return await ConnectionHandler.Instance.RunAvailabilityCheck(); - }); + var msg = await Task.Run(ConnectionHandler.Instance.RunAvailabilityCheck); NoticeHandler.Instance.SendMessageEx(msg); _updateView?.Invoke(EViewAction.DispatcherServerAvailability, msg);