Add user pac settings

This commit is contained in:
NiuBiZi 2023-05-07 19:29:37 +08:00
parent c8b01a5530
commit 4d117334b9
9 changed files with 178 additions and 0 deletions

View file

@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading;
@ -43,6 +44,12 @@ public class PacHandler
}
_pacText = File.ReadAllText(path).Replace("__PROXY__", $"PROXY 127.0.0.1:{_httpPort};DIRECT;");
var userPac = LoadUserPac(_configPath);
if (userPac != "")
{
_pacText = _pacText.Replace(" [],\n []", userPac);
}
}
private static void RunListener()
@ -100,4 +107,32 @@ public class PacHandler
}
}
}
public static string LoadUserPac(string configPath)
{
var userPac = "";
var userPacPath = Path.Combine(_configPath, "pacUser.txt");
if (File.Exists(userPacPath))
{
userPac = File.ReadAllText(userPacPath);
}
return userPac;
}
public static void SaveUserPac(string directDomains, string proxyDomains, string configPath)
{
var directDomainsArray = directDomains.Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
var proxyDomainsArray = proxyDomains.Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
var userPac = directDomainsArray.Aggregate("[\n", (current, directDomain) => $"{current} \"{directDomain}\",\n");
userPac += " ],\n [\n";
userPac = proxyDomainsArray.Aggregate(userPac, (current, proxyDomain) => $"{current} \"{proxyDomain}\",\n");
userPac += "]";
var path = Path.Combine(configPath, "pacUser.txt");
File.WriteAllText(path, userPac);
}
}

View file

@ -2869,6 +2869,33 @@ namespace v2rayN.Resx {
}
}
/// <summary>
/// 查找类似 User Pac settings 的本地化字符串。
/// </summary>
public static string TbSettingsUserPac {
get {
return ResourceManager.GetString("TbSettingsUserPac", resourceCulture);
}
}
/// <summary>
/// 查找类似 Direct domains(One at a line): 的本地化字符串。
/// </summary>
public static string TbSettingsUserPacDirectDomains {
get {
return ResourceManager.GetString("TbSettingsUserPacDirectDomains", resourceCulture);
}
}
/// <summary>
/// 查找类似 Proxy domains(One at a line): 的本地化字符串。
/// </summary>
public static string TbSettingsUserPacProxyDomains {
get {
return ResourceManager.GetString("TbSettingsUserPacProxyDomains", resourceCulture);
}
}
/// <summary>
/// 查找类似 ShortId 的本地化字符串。
/// </summary>

View file

@ -1009,4 +1009,13 @@
<data name="TbSettingsEnableHWA" xml:space="preserve">
<value>فعال‌سازی شتاب‌دهنده سخت‌افزاری (نیاز به راه‌اندازی مجدد)</value>
</data>
<data name="TbSettingsUserPac" xml:space="preserve">
<value>User Pac settings</value>
</data>
<data name="TbSettingsUserPacProxyDomains" xml:space="preserve">
<value>Proxy domains(One at a line):</value>
</data>
<data name="TbSettingsUserPacDirectDomains" xml:space="preserve">
<value>Direct domains(One at a line):</value>
</data>
</root>

View file

@ -814,6 +814,15 @@
<data name="TbSettingsUser" xml:space="preserve">
<value>Auth user</value>
</data>
<data name="TbSettingsUserPac" xml:space="preserve">
<value>User Pac settings</value>
</data>
<data name="TbSettingsUserPacDirectDomains" xml:space="preserve">
<value>Direct domains(One at a line):</value>
</data>
<data name="TbSettingsUserPacProxyDomains" xml:space="preserve">
<value>Proxy domains(One at a line):</value>
</data>
<data name="TbClearSystemProxy" xml:space="preserve">
<value>Clear system proxy</value>
</data>

View file

@ -1051,4 +1051,13 @@
<data name="TbSettingsEnableHWA" xml:space="preserve">
<value>Включить аппаратное ускорение (требуется перезагрузка)</value>
</data>
<data name="TbSettingsUserPac" xml:space="preserve">
<value>User Pac settings</value>
</data>
<data name="TbSettingsUserPacProxyDomains" xml:space="preserve">
<value>Proxy domains(One at a line):</value>
</data>
<data name="TbSettingsUserPacDirectDomains" xml:space="preserve">
<value>Direct domains(One at a line):</value>
</data>
</root>

View file

@ -1129,4 +1129,13 @@
<data name="TbSettingsMux4SboxProtocol" xml:space="preserve">
<value>sing-box Mux 多路复用协议</value>
</data>
<data name="TbSettingsUserPac" xml:space="preserve">
<value>用户Pac设置</value>
</data>
<data name="TbSettingsUserPacDirectDomains" xml:space="preserve">
<value>直连的域名(每行一个):</value>
</data>
<data name="TbSettingsUserPacProxyDomains" xml:space="preserve">
<value>需要代理的域名(每行一个):</value>
</data>
</root>

View file

@ -3,6 +3,7 @@ using ReactiveUI.Fody.Helpers;
using Splat;
using System.Reactive;
using System.Windows;
using PacLib;
using v2rayN.Handler;
using v2rayN.Mode;
using v2rayN.Resx;
@ -107,6 +108,13 @@ namespace v2rayN.ViewModels
#endregion CoreType
#region User Pac
[Reactive] public string userPacDirectDomains { get; set; }
[Reactive] public string userPacProxyDomains { get; set; }
#endregion System proxy
public ReactiveCommand<Unit, Unit> SaveCmd { get; }
public OptionSettingViewModel(Window view)
@ -201,6 +209,8 @@ namespace v2rayN.ViewModels
InitCoreType();
InitUserPac();
SaveCmd = ReactiveCommand.Create(() =>
{
SaveSetting();
@ -367,6 +377,8 @@ namespace v2rayN.ViewModels
{
UI.ShowWarning(ResUI.OperationFailed);
}
SaveUserPac();
}
private int SaveCoreType()
@ -405,5 +417,25 @@ namespace v2rayN.ViewModels
}
return 0;
}
private void InitUserPac()
{
var userPac = PacHandler.LoadUserPac(Utils.GetConfigPath());
if (userPac == "") return;
userPac = userPac.Replace(" \"", "").Replace("\",", "");
var arr = userPac.Split(" ],\n [\n", StringSplitOptions.RemoveEmptyEntries);
userPacDirectDomains = arr[0].Replace("[\n", "");
userPacProxyDomains = arr[1].Replace("]", "");
}
private int SaveUserPac()
{
var result = -1;
PacHandler.SaveUserPac(userPacDirectDomains, userPacProxyDomains, Utils.GetConfigPath());
return result;
}
}
}

View file

@ -1080,6 +1080,51 @@
Style="{StaticResource DefComboBox}" />
</Grid>
</TabItem>
<TabItem Header="{x:Static resx:ResUI.TbSettingsUserPac}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"></ColumnDefinition>
<ColumnDefinition Width="1*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<DockPanel Margin="{StaticResource SettingItemMargin}">
<TextBlock
Grid.Row="1"
Margin="{StaticResource SettingItemMargin}"
VerticalAlignment="Center"
DockPanel.Dock="Top"
Style="{StaticResource ToolbarTextBlock}"
Text="{x:Static resx:ResUI.TbSettingsUserPacDirectDomains}" />
<TextBox
x:Name="txtUserPacDirectDomains"
Margin="{StaticResource SettingItemMargin}"
VerticalAlignment="Stretch"
AcceptsReturn="True"
BorderThickness="1"
Style="{StaticResource DefTextBox}"
TextWrapping="Wrap"
VerticalScrollBarVisibility="Auto" />
</DockPanel>
<DockPanel Grid.Column="1" Margin="{StaticResource SettingItemMargin}">
<TextBlock
Grid.Row="1"
Margin="{StaticResource SettingItemMargin}"
VerticalAlignment="Center"
DockPanel.Dock="Top"
Style="{StaticResource ToolbarTextBlock}"
Text="{x:Static resx:ResUI.TbSettingsUserPacProxyDomains}" />
<TextBox
x:Name="txtUserPacProxyDomains"
Margin="{StaticResource SettingItemMargin}"
VerticalAlignment="Stretch"
AcceptsReturn="True"
BorderThickness="1"
Style="{StaticResource DefTextBox}"
TextWrapping="Wrap"
VerticalScrollBarVisibility="Auto" />
</DockPanel>
</Grid>
</TabItem>
</TabControl>
</DockPanel>
</reactiveui:ReactiveWindow>

View file

@ -190,6 +190,9 @@ namespace v2rayN.Views
this.Bind(ViewModel, vm => vm.CoreType5, v => v.cmbCoreType5.Text).DisposeWith(disposables);
this.Bind(ViewModel, vm => vm.CoreType6, v => v.cmbCoreType6.Text).DisposeWith(disposables);
this.Bind(ViewModel, vm => vm.userPacDirectDomains, v => v.txtUserPacDirectDomains.Text).DisposeWith(disposables);
this.Bind(ViewModel, vm => vm.userPacProxyDomains, v => v.txtUserPacProxyDomains.Text).DisposeWith(disposables);
this.BindCommand(ViewModel, vm => vm.SaveCmd, v => v.btnSave).DisposeWith(disposables);
});
}