Added: 支持导入系统hosts添加至dns设置中

pull/4464/head
ShiinaRinne 2023-12-01 12:37:49 +08:00
parent 12be8bda52
commit 63809d4f16
9 changed files with 434 additions and 378 deletions

View File

@ -1,5 +1,7 @@
using System.Net;
using System.IO;
using System.Net;
using System.Net.NetworkInformation;
using Newtonsoft.Json.Linq;
using v2rayN.Base;
using v2rayN.Mode;
using v2rayN.Resx;
@ -718,29 +720,50 @@ namespace v2rayN.Handler
outbound.settings.userLevel = 0;
}
var obj = Utils.ParseJson(normalDNS);
if (obj?.ContainsKey("servers") == true)
{
v2rayConfig.dns = obj;
}
else
var obj = Utils.ParseJson(normalDNS) ?? new JObject();
if (!obj.ContainsKey("servers"))
{
List<string> servers = new();
string[] arrDNS = normalDNS.Split(',');
foreach (string str in arrDNS)
{
//if (Utils.IsIP(str))
//{
servers.Add(str);
//}
}
//servers.Add("localhost");
v2rayConfig.dns = new Mode.Dns4Ray
{
servers = servers
};
obj["servers"] = JArray.FromObject(servers);
}
if (item.useSystemHosts)
{
var hostfile = @"C:\Windows\System32\drivers\etc\hosts" ;
if (File.Exists(hostfile))
{
var hosts = File.ReadAllText(hostfile).Replace("\r", "");
var hostsList = hosts.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
// 获取系统hosts
var systemHosts = new Dictionary<string, string>();
foreach (var host in hostsList)
{
if (host.StartsWith("#")) continue;
var hostItem = host.Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
if (hostItem.Length < 2) continue;
systemHosts.Add(hostItem[1], hostItem[0]);
}
// 追加至 dns 设置
var normalHost = obj["hosts"] ?? new JObject();
foreach (var host in systemHosts)
{
if (normalHost[host.Key] != null) continue;
normalHost[host.Key] = host.Value;
}
obj["hosts"] = normalHost;
}
}
v2rayConfig.dns = obj;
}
catch (Exception ex)
{

View File

@ -11,6 +11,7 @@ namespace v2rayN.Mode
public string remarks { get; set; }
public bool enabled { get; set; } = true;
public ECoreType coreType { get; set; }
public bool useSystemHosts { get; set; } = true;
public string? normalDNS { get; set; }
public string? tunDNS { get; set; }
public string? domainStrategy4Freedom { get; set; }

File diff suppressed because it is too large Load Diff

View File

@ -1120,7 +1120,7 @@
<data name="TbDnsSingboxObjectDoc" xml:space="preserve">
<value>Please fill in DNS Structure, Click to view the document</value>
</data>
<data name="TBSettingDnsImportDefConfig" xml:space="preserve">
<data name="TbSettingDnsImportDefConfig" xml:space="preserve">
<value>Click to import default DNS config</value>
</data>
<data name="TbdomainStrategy4Singbox" xml:space="preserve">
@ -1144,4 +1144,7 @@
<data name="TbSettingsHysteriaBandwidth" xml:space="preserve">
<value>Hysteria Max bandwidth (Up/Dw)</value>
</data>
<data name="TbSettingsUseSystemHosts" xml:space="preserve">
<value>Use System Hosts</value>
</data>
</root>

View File

@ -1117,7 +1117,7 @@
<data name="TbDnsSingboxObjectDoc" xml:space="preserve">
<value>请填写 DNS JSON 结构,点击查看文档</value>
</data>
<data name="TBSettingDnsImportDefConfig" xml:space="preserve">
<data name="TbSettingDnsImportDefConfig" xml:space="preserve">
<value>点击导入默认DNS配置</value>
</data>
<data name="TbdomainStrategy4Singbox" xml:space="preserve">
@ -1141,4 +1141,7 @@
<data name="TbSettingsHysteriaBandwidth" xml:space="preserve">
<value>Hysteria 最大带宽(Up/Dw)</value>
</data>
<data name="TbSettingsUseSystemHosts" xml:space="preserve">
<value>使用系统hosts</value>
</data>
</root>

View File

@ -1117,7 +1117,7 @@
<data name="TbDnsSingboxObjectDoc" xml:space="preserve">
<value>請填寫 DNS JSON 結構,點擊查看文件</value>
</data>
<data name="TBSettingDnsImportDefConfig" xml:space="preserve">
<data name="TbSettingDnsImportDefConfig" xml:space="preserve">
<value>點擊匯入預設DNS配置</value>
</data>
<data name="TbdomainStrategy4Singbox" xml:space="preserve">

View File

@ -15,6 +15,7 @@ namespace v2rayN.ViewModels
private NoticeHandler? _noticeHandler;
private Window _view;
[Reactive] public bool useSystemHosts { get; set; }
[Reactive] public string domainStrategy4Freedom { get; set; }
[Reactive] public string normalDNS { get; set; }
[Reactive] public string normalDNS2 { get; set; }
@ -31,6 +32,7 @@ namespace v2rayN.ViewModels
_view = view;
var item = LazyConfig.Instance.GetDNSItem(ECoreType.Xray);
useSystemHosts = item.useSystemHosts;
domainStrategy4Freedom = item?.domainStrategy4Freedom!;
normalDNS = item?.normalDNS!;
@ -95,6 +97,7 @@ namespace v2rayN.ViewModels
var item = LazyConfig.Instance.GetDNSItem(ECoreType.Xray);
item.domainStrategy4Freedom = domainStrategy4Freedom;
item.useSystemHosts = useSystemHosts;
item.normalDNS = normalDNS;
ConfigHandler.SaveDNSItems(_config, item);

View File

@ -81,10 +81,24 @@
<Button
x:Name="btnImportDefConfig4V2ray"
Margin="8,0,0,0"
Content="{x:Static resx:ResUI.TBSettingDnsImportDefConfig}"
Content="{x:Static resx:ResUI.TbSettingDnsImportDefConfig}"
Cursor="Hand"
Style="{StaticResource DefButton}" />
</StackPanel>
<StackPanel DockPanel.Dock="Top" Orientation="Horizontal">
<TextBlock
Margin="{StaticResource SettingItemMargin}"
VerticalAlignment="Center"
Style="{StaticResource ToolbarTextBlock}"
Text="{x:Static resx:ResUI.TbSettingsUseSystemHosts}" />
<ToggleButton
x:Name="togUseSystemHosts"
Grid.Row="5"
Grid.Column="1"
Margin="{StaticResource SettingItemMargin}"
HorizontalAlignment="Left" />
</StackPanel>
<TextBox
x:Name="txtnormalDNS"
@ -113,7 +127,7 @@
<Button
x:Name="btnImportDefConfig4Singbox"
Margin="8,0,0,0"
Content="{x:Static resx:ResUI.TBSettingDnsImportDefConfig}"
Content="{x:Static resx:ResUI.TbSettingDnsImportDefConfig}"
Cursor="Hand"
Style="{StaticResource DefButton}" />
</StackPanel>

View File

@ -37,6 +37,7 @@ namespace v2rayN.Views
this.WhenActivated(disposables =>
{
this.Bind(ViewModel, vm=>vm.useSystemHosts, v=>v.togUseSystemHosts.IsChecked).DisposeWith(disposables);
this.Bind(ViewModel, vm => vm.domainStrategy4Freedom, v => v.cmbdomainStrategy4Freedom.Text).DisposeWith(disposables);
this.Bind(ViewModel, vm => vm.normalDNS, v => v.txtnormalDNS.Text).DisposeWith(disposables);
this.Bind(ViewModel, vm => vm.normalDNS2, v => v.txtnormalDNS2.Text).DisposeWith(disposables);