Refactor system proxy

This commit is contained in:
2dust 2024-10-16 09:20:05 +08:00
parent 35e5475255
commit 74f980aab1
14 changed files with 26 additions and 110 deletions

View file

@ -7,7 +7,6 @@
SaveFileDialog, SaveFileDialog,
AddBatchRoutingRulesYesNo, AddBatchRoutingRulesYesNo,
AdjustMainLvColWidth, AdjustMainLvColWidth,
UpdateSysProxy,
SetClipboardData, SetClipboardData,
AddServerViaClipboard, AddServerViaClipboard,
ImportRulesFromClipboard, ImportRulesFromClipboard,

View file

@ -7,4 +7,5 @@ global using ServiceLib.Services;
global using ServiceLib.Services.Statistics; global using ServiceLib.Services.Statistics;
global using ServiceLib.Services.CoreConfig; global using ServiceLib.Services.CoreConfig;
global using ServiceLib.Models; global using ServiceLib.Models;
global using ServiceLib.Resx; global using ServiceLib.Resx;
global using ServiceLib.Handler.SysProxy;

View file

@ -1,4 +1,4 @@
namespace v2rayN.Desktop.Common namespace ServiceLib.Handler.SysProxy
{ {
public class ProxySettingLinux public class ProxySettingLinux
{ {

View file

@ -1,4 +1,4 @@
namespace v2rayN.Desktop.Common namespace ServiceLib.Handler.SysProxy
{ {
public class ProxySettingOSX public class ProxySettingOSX
{ {

View file

@ -1,10 +1,10 @@
using Microsoft.Win32; 
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using static PacLib.ProxySettingWindows.InternetConnectionOption; using static ServiceLib.Handler.SysProxy.ProxySettingWindows.InternetConnectionOption;
namespace PacLib namespace ServiceLib.Handler.SysProxy
{ {
public class ProxySettingWindows public class ProxySettingWindows
{ {
@ -362,10 +362,10 @@ namespace PacLib
private static void RegWriteValue(string path, string name, object value) private static void RegWriteValue(string path, string name, object value)
{ {
RegistryKey? regKey = null; Microsoft.Win32.RegistryKey? regKey = null;
try try
{ {
regKey = Registry.CurrentUser.CreateSubKey(path); regKey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(path);
if (string.IsNullOrEmpty(value.ToString())) if (string.IsNullOrEmpty(value.ToString()))
{ {
regKey?.DeleteValue(name, false); regKey?.DeleteValue(name, false);

View file

@ -1,7 +1,6 @@
using PacLib; using PacLib;
using v2rayN.Desktop.Common;
namespace v2rayN.Desktop.Handler namespace ServiceLib.Handler.SysProxy
{ {
public static class SysProxyHandler public static class SysProxyHandler
{ {
@ -16,9 +15,9 @@ namespace v2rayN.Desktop.Handler
try try
{ {
int port = AppHandler.Instance.GetLocalPort(EInboundProtocol.http); var port = AppHandler.Instance.GetLocalPort(EInboundProtocol.http);
int portSocks = AppHandler.Instance.GetLocalPort(EInboundProtocol.socks); var portSocks = AppHandler.Instance.GetLocalPort(EInboundProtocol.socks);
int portPac = AppHandler.Instance.GetLocalPort(EInboundProtocol.pac); var portPac = AppHandler.Instance.GetLocalPort(EInboundProtocol.pac);
if (port <= 0) if (port <= 0)
{ {
return false; return false;
@ -73,12 +72,15 @@ namespace v2rayN.Desktop.Handler
} }
else if (type == ESysProxyType.Pac) else if (type == ESysProxyType.Pac)
{ {
PacHandler.Start(Utils.GetConfigPath(), port, portPac);
var strProxy = $"{Global.HttpProtocol}{Global.Loopback}:{portPac}/pac?t={DateTime.Now.Ticks}";
ProxySettingWindows.SetProxy(strProxy, "", 4);
} }
//if (type != ESysProxyType.Pac) if (type != ESysProxyType.Pac)
//{ {
// PacHandler.Stop(); PacHandler.Stop();
//} }
} }
catch (Exception ex) catch (Exception ex)
{ {

View file

@ -43,6 +43,7 @@
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\PacLib\PacLib.csproj" />
<ProjectReference Include="..\ProtosLib\ProtosLib.csproj" /> <ProjectReference Include="..\ProtosLib\ProtosLib.csproj" />
</ItemGroup> </ItemGroup>

View file

@ -263,7 +263,7 @@ namespace ServiceLib.ViewModels
{ {
Logging.SaveLog("MyAppExit Begin"); Logging.SaveLog("MyAppExit Begin");
//if (blWindowsShutDown) //if (blWindowsShutDown)
await _updateView?.Invoke(EViewAction.UpdateSysProxy, true); await SysProxyHandler.UpdateSysProxy(_config, true);
ConfigHandler.SaveConfig(_config); ConfigHandler.SaveConfig(_config);
ProfileExHandler.Instance.SaveTo(); ProfileExHandler.Instance.SaveTo();
@ -492,12 +492,13 @@ namespace ServiceLib.ViewModels
await LoadCore(); await LoadCore();
Locator.Current.GetService<StatusBarViewModel>()?.TestServerAvailability(); Locator.Current.GetService<StatusBarViewModel>()?.TestServerAvailability();
await SysProxyHandler.UpdateSysProxy(_config, false);
_updateView?.Invoke(EViewAction.DispatcherReload, null); _updateView?.Invoke(EViewAction.DispatcherReload, null);
} }
public void ReloadResult() public void ReloadResult()
{ {
//ChangeSystemProxyStatusAsync(_config.systemProxyItem.sysProxyType, false); //Locator.Current.GetService<StatusBarViewModel>()?.ChangeSystemProxyAsync(_config.systemProxyItem.sysProxyType, false);
BlReloadEnabled = true; BlReloadEnabled = true;
ShowClashUI = _config.IsRunningCore(ECoreType.sing_box); ShowClashUI = _config.IsRunningCore(ECoreType.sing_box);
if (ShowClashUI) if (ShowClashUI)

View file

@ -3,7 +3,6 @@ using ReactiveUI;
using ReactiveUI.Fody.Helpers; using ReactiveUI.Fody.Helpers;
using Splat; using Splat;
using System.Reactive; using System.Reactive;
using System.Reactive.Linq;
using System.Text; using System.Text;
namespace ServiceLib.ViewModels namespace ServiceLib.ViewModels
@ -318,10 +317,9 @@ namespace ServiceLib.ViewModels
ConfigHandler.SaveConfig(_config, false); ConfigHandler.SaveConfig(_config, false);
} }
private async Task ChangeSystemProxyAsync(ESysProxyType type, bool blChange) public async Task ChangeSystemProxyAsync(ESysProxyType type, bool blChange)
{ {
//await _updateView?.Invoke(EViewAction.UpdateSysProxy, _config.tunModeItem.enableTun ? true : false); await SysProxyHandler.UpdateSysProxy(_config, false);
_updateView?.Invoke(EViewAction.UpdateSysProxy, false);
BlSystemProxyClear = (type == ESysProxyType.ForcedClear); BlSystemProxyClear = (type == ESysProxyType.ForcedClear);
BlSystemProxySet = (type == ESysProxyType.ForcedChange); BlSystemProxySet = (type == ESysProxyType.ForcedChange);

View file

@ -6,7 +6,6 @@ using ReactiveUI;
using Splat; using Splat;
using System.Reactive.Disposables; using System.Reactive.Disposables;
using v2rayN.Desktop.Common; using v2rayN.Desktop.Common;
using v2rayN.Desktop.Handler;
namespace v2rayN.Desktop.Views namespace v2rayN.Desktop.Views
{ {
@ -91,11 +90,6 @@ namespace v2rayN.Desktop.Views
{ {
switch (action) switch (action)
{ {
case EViewAction.UpdateSysProxy:
if (obj is null) return false;
await SysProxyHandler.UpdateSysProxy(_config, (bool)obj);
break;
case EViewAction.DispatcherServerAvailability: case EViewAction.DispatcherServerAvailability:
if (obj is null) return false; if (obj is null) return false;
Dispatcher.UIThread.Post(() => Dispatcher.UIThread.Post(() =>

View file

@ -33,7 +33,6 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\PacLib\PacLib.csproj" />
<ProjectReference Include="..\ServiceLib\ServiceLib.csproj" /> <ProjectReference Include="..\ServiceLib\ServiceLib.csproj" />
</ItemGroup> </ItemGroup>

View file

@ -1,73 +0,0 @@
using PacLib;
namespace v2rayN.Handler
{
public static class SysProxyHandler
{
public static bool UpdateSysProxy(Config config, bool forceDisable)
{
var type = config.systemProxyItem.sysProxyType;
if (forceDisable && type != ESysProxyType.Unchanged)
{
type = ESysProxyType.ForcedClear;
}
try
{
int port = AppHandler.Instance.GetLocalPort(EInboundProtocol.http);
int portSocks = AppHandler.Instance.GetLocalPort(EInboundProtocol.socks);
int portPac = AppHandler.Instance.GetLocalPort(EInboundProtocol.pac);
if (port <= 0)
{
return false;
}
if (type == ESysProxyType.ForcedChange)
{
var strExceptions = "";
if (config.systemProxyItem.notProxyLocalAddress)
{
strExceptions = $"<local>;{config.constItem.defIEProxyExceptions};{config.systemProxyItem.systemProxyExceptions}";
}
var strProxy = string.Empty;
if (Utils.IsNullOrEmpty(config.systemProxyItem.systemProxyAdvancedProtocol))
{
strProxy = $"{Global.Loopback}:{port}";
}
else
{
strProxy = config.systemProxyItem.systemProxyAdvancedProtocol
.Replace("{ip}", Global.Loopback)
.Replace("{http_port}", port.ToString())
.Replace("{socks_port}", portSocks.ToString());
}
ProxySettingWindows.SetProxy(strProxy, strExceptions, 2);
}
else if (type == ESysProxyType.ForcedClear)
{
ProxySettingWindows.UnsetProxy();
}
else if (type == ESysProxyType.Unchanged)
{
}
else if (type == ESysProxyType.Pac)
{
PacHandler.Start(Utils.GetConfigPath(), port, portPac);
var strProxy = $"{Global.HttpProtocol}{Global.Loopback}:{portPac}/pac?t={DateTime.Now.Ticks}";
ProxySettingWindows.SetProxy(strProxy, "", 4);
}
if (type != ESysProxyType.Pac)
{
PacHandler.Stop();
}
}
catch (Exception ex)
{
Logging.SaveLog(ex.Message, ex);
}
return true;
}
}
}

View file

@ -96,11 +96,6 @@ namespace v2rayN.Views
Application.Current.MainWindow.Icon = WindowsHandler.Instance.GetAppIcon(_config); Application.Current.MainWindow.Icon = WindowsHandler.Instance.GetAppIcon(_config);
}), DispatcherPriority.Normal); }), DispatcherPriority.Normal);
break; break;
case EViewAction.UpdateSysProxy:
if (obj is null) return false;
SysProxyHandler.UpdateSysProxy(_config, (bool)obj);
break;
} }
return await Task.FromResult(true); return await Task.FromResult(true);
} }

View file

@ -36,7 +36,6 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\PacLib\PacLib.csproj" />
<ProjectReference Include="..\ServiceLib\ServiceLib.csproj" /> <ProjectReference Include="..\ServiceLib\ServiceLib.csproj" />
</ItemGroup> </ItemGroup>