添加PAC模式

This commit is contained in:
jx
2022-10-26 21:36:40 +08:00
parent 830191ee97
commit 0c03e8b4f3
19 changed files with 142 additions and 13 deletions

View File

@@ -67,6 +67,11 @@ namespace clashN.Handler
config.APIPort = 9090;
}
if (config.PacPort == 0)
{
config.PacPort = 7990;
}
if (config.profileItems == null)
{
config.profileItems = new List<ProfileItem>();

View File

@@ -0,0 +1,44 @@
using System.IO;
using System.Windows;
using clashN.Mode;
using TouchSocket.Core.Config;
using TouchSocket.Core.Plugins;
using TouchSocket.Http;
using TouchSocket.Http.Plugins;
using TouchSocket.Sockets;
namespace clashN.Handler;
public class HttpHandler : HttpPluginBase
{
private static HttpService _httpService = new();
private static string _pacText;
public static void Start(Config config)
{
if (!File.Exists("./pac.txt"))
{
MessageBox.Show("pac file is not exists", "error", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
_pacText = File.ReadAllText("./pac.txt").Replace("__PROXY__", $"PROXY 127.0.0.1:{config.httpPort};DIRECT;");
Stop();
_httpService.Setup(config.PacPort).Start();
_httpService.AddPlugin<HttpHandler>();
}
public static void Stop()
{
if (_httpService.ServerState == ServerState.Running)
{
_httpService.Stop();
}
}
protected override void OnGet(ITcpClientBase client, HttpContextEventArgs e)
{
e.Context.Response.FromText(_pacText).Answer();
base.OnGet(client, e);
}
}

View File

@@ -75,6 +75,17 @@ namespace clashN.Handler
else if (type == ESysProxyType.Unchanged)
{
}
else if (type == ESysProxyType.Pac)
{
HttpHandler.Start(config);
var strProxy = $"http://127.0.0.1:{config.PacPort}/";
SetIEProxy(false, strProxy, "");
}
if (type != ESysProxyType.Pac)
{
HttpHandler.Stop();
}
}
catch (Exception ex)
{

View File

@@ -1,5 +1,4 @@
using clashN.Base;
using System.Drawing;
using System.Drawing;
using System.Windows.Forms;
namespace clashN.Mode
@@ -45,6 +44,11 @@ namespace clashN.Mode
public bool enableMixinContent { get; set; }
/// <summary>
/// Pac监听端口
/// </summary>
public int PacPort { get; set; }
#endregion
#region other entities

View File

@@ -5,6 +5,7 @@ namespace clashN.Mode
{
ForcedClear = 0,
ForcedChange = 1,
Unchanged = 2
Unchanged = 2,
Pac = 3
}
}

View File

@@ -537,6 +537,15 @@ namespace clashN.Resx {
}
}
/// <summary>
/// 查找类似 Pac Mode 的本地化字符串。
/// </summary>
public static string menuSystemProxyPac {
get {
return ResourceManager.GetString("menuSystemProxyPac", resourceCulture);
}
}
/// <summary>
/// 查找类似 Set system proxy 的本地化字符串。
/// </summary>
@@ -1420,6 +1429,15 @@ namespace clashN.Resx {
}
}
/// <summary>
/// 查找类似 Pac listen port 的本地化字符串。
/// </summary>
public static string TbSettingsPacListenPort {
get {
return ResourceManager.GetString("TbSettingsPacListenPort", resourceCulture);
}
}
/// <summary>
/// 查找类似 After modifying the following parameters, click Save to take effect 的本地化字符串。
/// </summary>

View File

@@ -640,4 +640,10 @@
<data name="TbSortingDefault" xml:space="preserve">
<value>Default</value>
</data>
<data name="menuSystemProxyPac" xml:space="preserve">
<value>Pac Mode</value>
</data>
<data name="TbSettingsPacListenPort" xml:space="preserve">
<value>Pac listen port</value>
</data>
</root>

View File

@@ -640,4 +640,13 @@
<data name="TbSortingDefault" xml:space="preserve">
<value>默认</value>
</data>
<data name="menuSystemProxyPac" xml:space="preserve">
<value>Pac模式</value>
</data>
<data name="pacListenPort" xml:space="preserve">
<value>Pac监听端口号</value>
</data>
<data name="TbSettingsPacListenPort" xml:space="preserve">
<value>Pac监听端口号</value>
</data>
</root>

View File

@@ -1,4 +1,3 @@
using clashN.Base;
using clashN.Handler;
using clashN.Mode;
using DynamicData;

View File

@@ -1,4 +1,3 @@
using clashN.Base;
using clashN.Handler;
using clashN.Mode;
using clashN.Views;
@@ -53,6 +52,8 @@ namespace clashN.ViewModels
public ReactiveCommand<Unit, Unit> SystemProxyClearCmd { get; }
public ReactiveCommand<Unit, Unit> SystemProxySetCmd { get; }
public ReactiveCommand<Unit, Unit> SystemProxyNothingCmd { get; }
public ReactiveCommand<Unit, Unit> SystemProxyPacCmd { get; }
#endregion
#region Rule mode
@@ -127,6 +128,10 @@ namespace clashN.ViewModels
{
SetListenerType(ESysProxyType.Unchanged);
});//, this.WhenAnyValue(x => x.BlSystemProxyNothing, y => !y));
SystemProxyPacCmd = ReactiveCommand.Create(() =>
{
SetListenerType(ESysProxyType.Pac);
});//, this.WhenAnyValue(x => x.BlSystemProxyNothing, y => !y));
//Rule mode
ModeRuleCmd = ReactiveCommand.Create(() =>

View File

@@ -1,4 +1,3 @@
using clashN.Base;
using clashN.Handler;
using clashN.Mode;
using clashN.Resx;

View File

@@ -1,4 +1,3 @@
using clashN.Base;
using clashN.Handler;
using clashN.Mode;
using clashN.Resx;

View File

@@ -1,4 +1,3 @@
using clashN.Base;
using clashN.Handler;
using clashN.Mode;
using clashN.Resx;

View File

@@ -1,4 +1,3 @@
using clashN.Base;
using clashN.Handler;
using clashN.Mode;
using clashN.Resx;
@@ -56,6 +55,8 @@ namespace clashN.ViewModels
public string SubConvertUrl { get; set; }
public ReactiveCommand<Unit, Unit> SetLoopbackCmd { get; }
public ReactiveCommand<Unit, Unit> SetGlobalHotkeyCmd { get; }
[Reactive]
public int PacListenPort { get; set; }
#endregion
#region System proxy
@@ -107,6 +108,7 @@ namespace clashN.ViewModels
autoUpdateSubInterval = _config.autoUpdateSubInterval;
autoDelayTestInterval = _config.autoDelayTestInterval;
SubConvertUrl = _config.constItem.subConvertUrl;
PacListenPort = _config.PacPort;
SetLoopbackCmd = ReactiveCommand.Create(() =>
{
Utils.ProcessStart(Utils.GetBinPath("EnableLoopback.exe"));
@@ -202,6 +204,7 @@ namespace clashN.ViewModels
_config.autoUpdateSubInterval = autoUpdateSubInterval;
_config.autoDelayTestInterval = autoDelayTestInterval;
_config.constItem.subConvertUrl = SubConvertUrl;
_config.PacPort = PacListenPort;
//System proxy
_config.systemProxyExceptions = systemProxyExceptions;

View File

@@ -242,6 +242,17 @@
</StackPanel>
</MenuItem.Header>
</MenuItem>
<MenuItem x:Name="menuSystemProxyPac">
<MenuItem.Header>
<StackPanel Orientation="Horizontal">
<materialDesign:PackIcon
x:Name="menuSystemProxyPac2"
Margin="0,0,8,0"
Kind="Check" />
<TextBlock Text="{x:Static resx:ResUI.menuSystemProxyPac}" />
</StackPanel>
</MenuItem.Header>
</MenuItem>
<Separator />
<MenuItem x:Name="menuModeRule">

View File

@@ -41,6 +41,7 @@ namespace clashN.Views
this.OneWayBind(ViewModel, vm => vm.BlSystemProxyNothing, v => v.menuSystemProxyNothing2.Visibility, conversionHint: BooleanToVisibilityHint.UseHidden, vmToViewConverterOverride: new BooleanToVisibilityTypeConverter()).DisposeWith(disposables);
this.BindCommand(ViewModel, vm => vm.SystemProxyClearCmd, v => v.menuSystemProxyClear).DisposeWith(disposables);
this.BindCommand(ViewModel, vm => vm.SystemProxySetCmd, v => v.menuSystemProxySet).DisposeWith(disposables);
this.BindCommand(ViewModel, vm => vm.SystemProxyPacCmd, v => v.menuSystemProxyPac).DisposeWith(disposables);
this.BindCommand(ViewModel, vm => vm.SystemProxyNothingCmd, v => v.menuSystemProxyNothing).DisposeWith(disposables);

View File

@@ -44,6 +44,7 @@
<ComboBoxItem Content="{x:Static resx:ResUI.menuSystemProxyClear}" />
<ComboBoxItem Content="{x:Static resx:ResUI.menuSystemProxySet}" />
<ComboBoxItem Content="{x:Static resx:ResUI.menuSystemProxyNothing}" />
<ComboBoxItem Content="{x:Static resx:ResUI.menuSystemProxyPac}" />
</ComboBox>
<Separator />
<TextBlock

View File

@@ -221,6 +221,7 @@
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
@@ -303,17 +304,29 @@
Grid.Column="1"
Width="300"
Margin="8" />
<TextBlock
Grid.Row="8"
Grid.Column="0"
Margin="8"
VerticalAlignment="Center"
Style="{StaticResource ListItemTitle}"
Text="{x:Static resx:ResUI.TbSettingsPacListenPort}" />
<TextBox
x:Name="txtPacPort"
Grid.Row="8"
Grid.Column="1"
Width="300"
Margin="8" />
<TextBlock
Grid.Row="9"
Grid.Column="0"
Margin="8"
VerticalAlignment="Center"
Style="{StaticResource ListItemTitle}"
Text="{x:Static resx:ResUI.TbSettingsSubConvert}" />
<ComboBox
x:Name="cmbSubConvertUrl"
Grid.Row="8"
Grid.Row="9"
Grid.Column="1"
Width="300"
Margin="8"
@@ -322,14 +335,14 @@
<Button
x:Name="btnSetLoopback"
Grid.Row="9"
Grid.Row="10"
Grid.Column="0"
Margin="8"
Content="{x:Static resx:ResUI.TbSettingsSetUWP}"
Style="{StaticResource DefButton}" />
<Button
x:Name="btnSetGlobalHotkey"
Grid.Row="10"
Grid.Row="11"
Grid.Column="0"
Margin="8"
Content="{x:Static resx:ResUI.TbGlobalHotkeySetting}"

View File

@@ -21,6 +21,7 @@
<PackageReference Include="NHotkey.Wpf" Version="2.1.0" />
<PackageReference Include="QRCoder.Xaml" Version="1.4.3" />
<PackageReference Include="TaskScheduler" Version="2.10.1" />
<PackageReference Include="TouchSocket" Version="0.7.4" />
<PackageReference Include="YamlDotNet" Version="12.0.2" />
<PackageReference Include="ZXing.Net.Bindings.Windows.Compatibility" Version="0.16.10" />
<PackageReference Include="ReactiveUI.Fody" Version="18.3.1" />