Skip to content

Commit

Permalink
Refactor system proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
2dust committed Oct 16, 2024
1 parent 35e5475 commit 74f980a
Show file tree
Hide file tree
Showing 14 changed files with 26 additions and 110 deletions.
1 change: 0 additions & 1 deletion v2rayN/ServiceLib/Enums/EViewAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ public enum EViewAction
SaveFileDialog,
AddBatchRoutingRulesYesNo,
AdjustMainLvColWidth,
UpdateSysProxy,
SetClipboardData,
AddServerViaClipboard,
ImportRulesFromClipboard,
Expand Down
3 changes: 2 additions & 1 deletion v2rayN/ServiceLib/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
global using ServiceLib.Services.Statistics;
global using ServiceLib.Services.CoreConfig;
global using ServiceLib.Models;
global using ServiceLib.Resx;
global using ServiceLib.Resx;
global using ServiceLib.Handler.SysProxy;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace v2rayN.Desktop.Common
namespace ServiceLib.Handler.SysProxy
{
public class ProxySettingLinux
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace v2rayN.Desktop.Common
namespace ServiceLib.Handler.SysProxy
{
public class ProxySettingOSX
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using Microsoft.Win32;

using System;
using System.Collections.Generic;
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
{
Expand Down Expand Up @@ -362,10 +362,10 @@ ref int lpcEntries // Number of entries written to the buffer

private static void RegWriteValue(string path, string name, object value)
{
RegistryKey? regKey = null;
Microsoft.Win32.RegistryKey? regKey = null;
try
{
regKey = Registry.CurrentUser.CreateSubKey(path);
regKey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(path);
if (string.IsNullOrEmpty(value.ToString()))
{
regKey?.DeleteValue(name, false);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using PacLib;
using v2rayN.Desktop.Common;

namespace v2rayN.Desktop.Handler
namespace ServiceLib.Handler.SysProxy
{
public static class SysProxyHandler
{
Expand All @@ -16,9 +15,9 @@ public static async Task<bool> UpdateSysProxy(Config config, bool forceDisable)

try
{
int port = AppHandler.Instance.GetLocalPort(EInboundProtocol.http);
int portSocks = AppHandler.Instance.GetLocalPort(EInboundProtocol.socks);
int portPac = AppHandler.Instance.GetLocalPort(EInboundProtocol.pac);
var port = AppHandler.Instance.GetLocalPort(EInboundProtocol.http);
var portSocks = AppHandler.Instance.GetLocalPort(EInboundProtocol.socks);
var portPac = AppHandler.Instance.GetLocalPort(EInboundProtocol.pac);
if (port <= 0)
{
return false;
Expand Down Expand Up @@ -73,12 +72,15 @@ public static async Task<bool> UpdateSysProxy(Config config, bool forceDisable)
}
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();
//}
if (type != ESysProxyType.Pac)
{
PacHandler.Stop();
}
}
catch (Exception ex)
{
Expand Down
1 change: 1 addition & 0 deletions v2rayN/ServiceLib/ServiceLib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@


<ItemGroup>
<ProjectReference Include="..\PacLib\PacLib.csproj" />
<ProjectReference Include="..\ProtosLib\ProtosLib.csproj" />
</ItemGroup>

Expand Down
5 changes: 3 additions & 2 deletions v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ public async Task MyAppExitAsync(bool blWindowsShutDown)
{
Logging.SaveLog("MyAppExit Begin");
//if (blWindowsShutDown)
await _updateView?.Invoke(EViewAction.UpdateSysProxy, true);
await SysProxyHandler.UpdateSysProxy(_config, true);

ConfigHandler.SaveConfig(_config);
ProfileExHandler.Instance.SaveTo();
Expand Down Expand Up @@ -492,12 +492,13 @@ public async Task Reload()

await LoadCore();
Locator.Current.GetService<StatusBarViewModel>()?.TestServerAvailability();
await SysProxyHandler.UpdateSysProxy(_config, false);
_updateView?.Invoke(EViewAction.DispatcherReload, null);
}

public void ReloadResult()
{
//ChangeSystemProxyStatusAsync(_config.systemProxyItem.sysProxyType, false);
//Locator.Current.GetService<StatusBarViewModel>()?.ChangeSystemProxyAsync(_config.systemProxyItem.sysProxyType, false);
BlReloadEnabled = true;
ShowClashUI = _config.IsRunningCore(ECoreType.sing_box);
if (ShowClashUI)
Expand Down
6 changes: 2 additions & 4 deletions v2rayN/ServiceLib/ViewModels/StatusBarViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using ReactiveUI.Fody.Helpers;
using Splat;
using System.Reactive;
using System.Reactive.Linq;
using System.Text;

namespace ServiceLib.ViewModels
Expand Down Expand Up @@ -318,10 +317,9 @@ public async Task SetListenerType(ESysProxyType type)
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);
_updateView?.Invoke(EViewAction.UpdateSysProxy, false);
await SysProxyHandler.UpdateSysProxy(_config, false);

BlSystemProxyClear = (type == ESysProxyType.ForcedClear);
BlSystemProxySet = (type == ESysProxyType.ForcedChange);
Expand Down
6 changes: 0 additions & 6 deletions v2rayN/v2rayN.Desktop/Views/StatusBarView.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using Splat;
using System.Reactive.Disposables;
using v2rayN.Desktop.Common;
using v2rayN.Desktop.Handler;

namespace v2rayN.Desktop.Views
{
Expand Down Expand Up @@ -91,11 +90,6 @@ private async Task<bool> UpdateViewHandler(EViewAction action, object? obj)
{
switch (action)
{
case EViewAction.UpdateSysProxy:
if (obj is null) return false;
await SysProxyHandler.UpdateSysProxy(_config, (bool)obj);
break;

case EViewAction.DispatcherServerAvailability:
if (obj is null) return false;
Dispatcher.UIThread.Post(() =>
Expand Down
1 change: 0 additions & 1 deletion v2rayN/v2rayN.Desktop/v2rayN.Desktop.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\PacLib\PacLib.csproj" />
<ProjectReference Include="..\ServiceLib\ServiceLib.csproj" />
</ItemGroup>

Expand Down
73 changes: 0 additions & 73 deletions v2rayN/v2rayN/Handler/SysProxyHandler.cs

This file was deleted.

5 changes: 0 additions & 5 deletions v2rayN/v2rayN/Views/StatusBarView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,6 @@ private async Task<bool> UpdateViewHandler(EViewAction action, object? obj)
Application.Current.MainWindow.Icon = WindowsHandler.Instance.GetAppIcon(_config);
}), DispatcherPriority.Normal);
break;

case EViewAction.UpdateSysProxy:
if (obj is null) return false;
SysProxyHandler.UpdateSysProxy(_config, (bool)obj);
break;
}
return await Task.FromResult(true);
}
Expand Down
1 change: 0 additions & 1 deletion v2rayN/v2rayN/v2rayN.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\PacLib\PacLib.csproj" />
<ProjectReference Include="..\ServiceLib\ServiceLib.csproj" />
</ItemGroup>

Expand Down

0 comments on commit 74f980a

Please sign in to comment.