2025-04-02 03:44:23 +00:00
|
|
|
namespace ServiceLib.Handler.SysProxy;
|
|
|
|
|
|
|
|
|
|
public static class SysProxyHandler
|
2024-08-29 07:48:51 +00:00
|
|
|
{
|
2025-04-02 03:44:23 +00:00
|
|
|
private static readonly string _tag = "SysProxyHandler";
|
|
|
|
|
|
|
|
|
|
public static async Task<bool> UpdateSysProxy(Config config, bool forceDisable)
|
2024-08-29 07:48:51 +00:00
|
|
|
{
|
2025-04-02 03:44:23 +00:00
|
|
|
var type = config.SystemProxyItem.SysProxyType;
|
2025-01-05 06:50:31 +00:00
|
|
|
|
2025-04-02 03:44:23 +00:00
|
|
|
if (forceDisable && type != ESysProxyType.Unchanged)
|
2024-08-29 07:48:51 +00:00
|
|
|
{
|
2025-04-02 03:44:23 +00:00
|
|
|
type = ESysProxyType.ForcedClear;
|
|
|
|
|
}
|
2024-08-29 07:48:51 +00:00
|
|
|
|
2025-04-02 03:44:23 +00:00
|
|
|
try
|
|
|
|
|
{
|
2025-08-17 09:31:55 +00:00
|
|
|
var port = AppManager.Instance.GetLocalPort(EInboundProtocol.socks);
|
2025-04-02 03:44:23 +00:00
|
|
|
var exceptions = config.SystemProxyItem.SystemProxyExceptions.Replace(" ", "");
|
|
|
|
|
if (port <= 0)
|
2024-08-29 07:48:51 +00:00
|
|
|
{
|
2025-04-02 03:44:23 +00:00
|
|
|
return false;
|
2024-08-29 07:48:51 +00:00
|
|
|
}
|
2025-04-02 03:44:23 +00:00
|
|
|
switch (type)
|
2024-08-29 07:48:51 +00:00
|
|
|
{
|
2025-04-02 03:44:23 +00:00
|
|
|
case ESysProxyType.ForcedChange when Utils.IsWindows():
|
|
|
|
|
{
|
|
|
|
|
GetWindowsProxyString(config, port, out var strProxy, out var strExceptions);
|
|
|
|
|
ProxySettingWindows.SetProxy(strProxy, strExceptions, 2);
|
2024-11-15 03:59:22 +00:00
|
|
|
break;
|
2025-04-02 03:44:23 +00:00
|
|
|
}
|
|
|
|
|
case ESysProxyType.ForcedChange when Utils.IsLinux():
|
|
|
|
|
await ProxySettingLinux.SetProxy(Global.Loopback, port, exceptions);
|
|
|
|
|
break;
|
2024-10-16 02:52:45 +00:00
|
|
|
|
2025-11-09 02:52:46 +00:00
|
|
|
case ESysProxyType.ForcedChange when Utils.IsMacOS():
|
2025-04-02 03:44:23 +00:00
|
|
|
await ProxySettingOSX.SetProxy(Global.Loopback, port, exceptions);
|
|
|
|
|
break;
|
2024-10-16 02:52:45 +00:00
|
|
|
|
2025-04-02 03:44:23 +00:00
|
|
|
case ESysProxyType.ForcedClear when Utils.IsWindows():
|
|
|
|
|
ProxySettingWindows.UnsetProxy();
|
|
|
|
|
break;
|
2024-10-16 02:52:45 +00:00
|
|
|
|
2025-04-02 03:44:23 +00:00
|
|
|
case ESysProxyType.ForcedClear when Utils.IsLinux():
|
|
|
|
|
await ProxySettingLinux.UnsetProxy();
|
|
|
|
|
break;
|
2024-10-16 02:52:45 +00:00
|
|
|
|
2025-11-09 02:52:46 +00:00
|
|
|
case ESysProxyType.ForcedClear when Utils.IsMacOS():
|
2025-04-02 03:44:23 +00:00
|
|
|
await ProxySettingOSX.UnsetProxy();
|
|
|
|
|
break;
|
2024-09-28 02:41:30 +00:00
|
|
|
|
2025-04-02 03:44:23 +00:00
|
|
|
case ESysProxyType.Pac when Utils.IsWindows():
|
|
|
|
|
await SetWindowsProxyPac(port);
|
|
|
|
|
break;
|
2024-08-29 07:48:51 +00:00
|
|
|
}
|
2025-04-02 03:44:23 +00:00
|
|
|
|
|
|
|
|
if (type != ESysProxyType.Pac && Utils.IsWindows())
|
2024-08-29 07:48:51 +00:00
|
|
|
{
|
2025-08-17 09:31:55 +00:00
|
|
|
PacManager.Instance.Stop();
|
2024-08-29 07:48:51 +00:00
|
|
|
}
|
|
|
|
|
}
|
2025-04-02 03:44:23 +00:00
|
|
|
catch (Exception ex)
|
2024-10-16 02:52:45 +00:00
|
|
|
{
|
2025-04-02 03:44:23 +00:00
|
|
|
Logging.SaveLog(_tag, ex);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2024-10-16 02:52:45 +00:00
|
|
|
|
2025-04-02 03:44:23 +00:00
|
|
|
private static void GetWindowsProxyString(Config config, int port, out string strProxy, out string strExceptions)
|
|
|
|
|
{
|
|
|
|
|
strExceptions = config.SystemProxyItem.SystemProxyExceptions.Replace(" ", "");
|
|
|
|
|
if (config.SystemProxyItem.NotProxyLocalAddress)
|
|
|
|
|
{
|
|
|
|
|
strExceptions = $"<local>;{strExceptions}";
|
2024-10-16 02:52:45 +00:00
|
|
|
}
|
2024-11-15 03:59:22 +00:00
|
|
|
|
2025-04-02 03:44:23 +00:00
|
|
|
strProxy = string.Empty;
|
|
|
|
|
if (config.SystemProxyItem.SystemProxyAdvancedProtocol.IsNullOrEmpty())
|
|
|
|
|
{
|
|
|
|
|
strProxy = $"{Global.Loopback}:{port}";
|
|
|
|
|
}
|
|
|
|
|
else
|
2024-11-15 03:59:22 +00:00
|
|
|
{
|
2025-04-02 03:44:23 +00:00
|
|
|
strProxy = config.SystemProxyItem.SystemProxyAdvancedProtocol
|
|
|
|
|
.Replace("{ip}", Global.Loopback)
|
|
|
|
|
.Replace("{http_port}", port.ToString())
|
|
|
|
|
.Replace("{socks_port}", port.ToString());
|
2024-11-15 03:59:22 +00:00
|
|
|
}
|
2024-08-29 07:48:51 +00:00
|
|
|
}
|
2025-04-02 03:44:23 +00:00
|
|
|
|
|
|
|
|
private static async Task SetWindowsProxyPac(int port)
|
|
|
|
|
{
|
2025-08-17 09:31:55 +00:00
|
|
|
var portPac = AppManager.Instance.GetLocalPort(EInboundProtocol.pac);
|
2025-11-07 11:28:16 +00:00
|
|
|
await PacManager.Instance.StartAsync(port, portPac);
|
2025-04-02 03:44:23 +00:00
|
|
|
var strProxy = $"{Global.HttpProtocol}{Global.Loopback}:{portPac}/pac?t={DateTime.Now.Ticks}";
|
|
|
|
|
ProxySettingWindows.SetProxy(strProxy, "", 4);
|
|
|
|
|
}
|
|
|
|
|
}
|