mirror of
https://github.com/2dust/v2rayN.git
synced 2025-08-23 03:16:53 +00:00
PacHandler is changed to singleton mode
This commit is contained in:
parent
6979e21628
commit
06636d04ac
4 changed files with 19 additions and 22 deletions
|
@ -5,15 +5,18 @@ namespace ServiceLib.Handler;
|
||||||
|
|
||||||
public class PacHandler
|
public class PacHandler
|
||||||
{
|
{
|
||||||
private static string _configPath;
|
private static readonly Lazy<PacHandler> _instance = new(() => new PacHandler());
|
||||||
private static int _httpPort;
|
public static PacHandler Instance => _instance.Value;
|
||||||
private static int _pacPort;
|
|
||||||
private static TcpListener? _tcpListener;
|
|
||||||
private static byte[] _writeContent;
|
|
||||||
private static bool _isRunning;
|
|
||||||
private static bool _needRestart = true;
|
|
||||||
|
|
||||||
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;
|
_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");
|
var path = Path.Combine(_configPath, "pac.txt");
|
||||||
|
|
||||||
|
@ -59,7 +62,7 @@ public class PacHandler
|
||||||
_writeContent = Encoding.UTF8.GetBytes(sb.ToString());
|
_writeContent = Encoding.UTF8.GetBytes(sb.ToString());
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void RunListener()
|
private void RunListener()
|
||||||
{
|
{
|
||||||
_tcpListener = TcpListener.Create(_pacPort);
|
_tcpListener = TcpListener.Create(_pacPort);
|
||||||
_isRunning = true;
|
_isRunning = true;
|
||||||
|
@ -87,14 +90,14 @@ public class PacHandler
|
||||||
}, TaskCreationOptions.LongRunning);
|
}, TaskCreationOptions.LongRunning);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void WriteContent(TcpClient client)
|
private void WriteContent(TcpClient client)
|
||||||
{
|
{
|
||||||
var stream = client.GetStream();
|
var stream = client.GetStream();
|
||||||
stream.Write(_writeContent, 0, _writeContent.Length);
|
stream.Write(_writeContent, 0, _writeContent.Length);
|
||||||
stream.Flush();
|
stream.Flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void Stop()
|
public void Stop()
|
||||||
{
|
{
|
||||||
if (_tcpListener == null)
|
if (_tcpListener == null)
|
||||||
{
|
{
|
||||||
|
|
|
@ -56,7 +56,7 @@ public static class SysProxyHandler
|
||||||
|
|
||||||
if (type != ESysProxyType.Pac && Utils.IsWindows())
|
if (type != ESysProxyType.Pac && Utils.IsWindows())
|
||||||
{
|
{
|
||||||
PacHandler.Stop();
|
PacHandler.Instance.Stop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
@ -91,7 +91,7 @@ public static class SysProxyHandler
|
||||||
private static async Task SetWindowsProxyPac(int port)
|
private static async Task SetWindowsProxyPac(int port)
|
||||||
{
|
{
|
||||||
var portPac = AppHandler.Instance.GetLocalPort(EInboundProtocol.pac);
|
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}";
|
var strProxy = $"{Global.HttpProtocol}{Global.Loopback}:{portPac}/pac?t={DateTime.Now.Ticks}";
|
||||||
ProxySettingWindows.SetProxy(strProxy, "", 4);
|
ProxySettingWindows.SetProxy(strProxy, "", 4);
|
||||||
}
|
}
|
||||||
|
|
|
@ -75,10 +75,7 @@ public class CheckUpdateViewModel : MyReactiveObject
|
||||||
|
|
||||||
private async Task CheckUpdate()
|
private async Task CheckUpdate()
|
||||||
{
|
{
|
||||||
await Task.Run(async () =>
|
await Task.Run(CheckUpdateTask);
|
||||||
{
|
|
||||||
await CheckUpdateTask();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task CheckUpdateTask()
|
private async Task CheckUpdateTask()
|
||||||
|
|
|
@ -334,10 +334,7 @@ public class StatusBarViewModel : MyReactiveObject
|
||||||
|
|
||||||
_updateView?.Invoke(EViewAction.DispatcherServerAvailability, ResUI.Speedtesting);
|
_updateView?.Invoke(EViewAction.DispatcherServerAvailability, ResUI.Speedtesting);
|
||||||
|
|
||||||
var msg = await Task.Run(async () =>
|
var msg = await Task.Run(ConnectionHandler.Instance.RunAvailabilityCheck);
|
||||||
{
|
|
||||||
return await ConnectionHandler.Instance.RunAvailabilityCheck();
|
|
||||||
});
|
|
||||||
|
|
||||||
NoticeHandler.Instance.SendMessageEx(msg);
|
NoticeHandler.Instance.SendMessageEx(msg);
|
||||||
_updateView?.Invoke(EViewAction.DispatcherServerAvailability, msg);
|
_updateView?.Invoke(EViewAction.DispatcherServerAvailability, msg);
|
||||||
|
|
Loading…
Reference in a new issue