mirror of
https://github.com/2dust/v2rayN.git
synced 2025-05-02 13:18:50 +00:00
Merge pull request #4464 from ShiinaRinne/host
feat: 支持导入系统hosts追加至dns设置
This commit is contained in:
commit
843b881da6
9 changed files with 434 additions and 378 deletions
|
@ -1,5 +1,7 @@
|
||||||
using System.Net;
|
using System.IO;
|
||||||
|
using System.Net;
|
||||||
using System.Net.NetworkInformation;
|
using System.Net.NetworkInformation;
|
||||||
|
using Newtonsoft.Json.Linq;
|
||||||
using v2rayN.Base;
|
using v2rayN.Base;
|
||||||
using v2rayN.Mode;
|
using v2rayN.Mode;
|
||||||
using v2rayN.Resx;
|
using v2rayN.Resx;
|
||||||
|
@ -718,29 +720,50 @@ namespace v2rayN.Handler
|
||||||
outbound.settings.userLevel = 0;
|
outbound.settings.userLevel = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
var obj = Utils.ParseJson(normalDNS);
|
var obj = Utils.ParseJson(normalDNS) ?? new JObject();
|
||||||
if (obj?.ContainsKey("servers") == true)
|
|
||||||
{
|
if (!obj.ContainsKey("servers"))
|
||||||
v2rayConfig.dns = obj;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
List<string> servers = new();
|
List<string> servers = new();
|
||||||
|
|
||||||
string[] arrDNS = normalDNS.Split(',');
|
string[] arrDNS = normalDNS.Split(',');
|
||||||
foreach (string str in arrDNS)
|
foreach (string str in arrDNS)
|
||||||
{
|
{
|
||||||
//if (Utils.IsIP(str))
|
|
||||||
//{
|
|
||||||
servers.Add(str);
|
servers.Add(str);
|
||||||
//}
|
|
||||||
}
|
}
|
||||||
//servers.Add("localhost");
|
obj["servers"] = JArray.FromObject(servers);
|
||||||
v2rayConfig.dns = new Mode.Dns4Ray
|
|
||||||
{
|
|
||||||
servers = 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)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
|
|
@ -11,6 +11,7 @@ namespace v2rayN.Mode
|
||||||
public string remarks { get; set; }
|
public string remarks { get; set; }
|
||||||
public bool enabled { get; set; } = true;
|
public bool enabled { get; set; } = true;
|
||||||
public ECoreType coreType { get; set; }
|
public ECoreType coreType { get; set; }
|
||||||
|
public bool useSystemHosts { get; set; } = true;
|
||||||
public string? normalDNS { get; set; }
|
public string? normalDNS { get; set; }
|
||||||
public string? tunDNS { get; set; }
|
public string? tunDNS { get; set; }
|
||||||
public string? domainStrategy4Freedom { get; set; }
|
public string? domainStrategy4Freedom { get; set; }
|
||||||
|
|
722
v2rayN/v2rayN/Resx/ResUI.Designer.cs
generated
722
v2rayN/v2rayN/Resx/ResUI.Designer.cs
generated
File diff suppressed because it is too large
Load diff
|
@ -1120,7 +1120,7 @@
|
||||||
<data name="TbDnsSingboxObjectDoc" xml:space="preserve">
|
<data name="TbDnsSingboxObjectDoc" xml:space="preserve">
|
||||||
<value>Please fill in DNS Structure, Click to view the document</value>
|
<value>Please fill in DNS Structure, Click to view the document</value>
|
||||||
</data>
|
</data>
|
||||||
<data name="TBSettingDnsImportDefConfig" xml:space="preserve">
|
<data name="TbSettingDnsImportDefConfig" xml:space="preserve">
|
||||||
<value>Click to import default DNS config</value>
|
<value>Click to import default DNS config</value>
|
||||||
</data>
|
</data>
|
||||||
<data name="TbdomainStrategy4Singbox" xml:space="preserve">
|
<data name="TbdomainStrategy4Singbox" xml:space="preserve">
|
||||||
|
@ -1144,4 +1144,7 @@
|
||||||
<data name="TbSettingsHysteriaBandwidth" xml:space="preserve">
|
<data name="TbSettingsHysteriaBandwidth" xml:space="preserve">
|
||||||
<value>Hysteria Max bandwidth (Up/Dw)</value>
|
<value>Hysteria Max bandwidth (Up/Dw)</value>
|
||||||
</data>
|
</data>
|
||||||
|
<data name="TbSettingsUseSystemHosts" xml:space="preserve">
|
||||||
|
<value>Use System Hosts</value>
|
||||||
|
</data>
|
||||||
</root>
|
</root>
|
|
@ -1117,7 +1117,7 @@
|
||||||
<data name="TbDnsSingboxObjectDoc" xml:space="preserve">
|
<data name="TbDnsSingboxObjectDoc" xml:space="preserve">
|
||||||
<value>请填写 DNS JSON 结构,点击查看文档</value>
|
<value>请填写 DNS JSON 结构,点击查看文档</value>
|
||||||
</data>
|
</data>
|
||||||
<data name="TBSettingDnsImportDefConfig" xml:space="preserve">
|
<data name="TbSettingDnsImportDefConfig" xml:space="preserve">
|
||||||
<value>点击导入默认DNS配置</value>
|
<value>点击导入默认DNS配置</value>
|
||||||
</data>
|
</data>
|
||||||
<data name="TbdomainStrategy4Singbox" xml:space="preserve">
|
<data name="TbdomainStrategy4Singbox" xml:space="preserve">
|
||||||
|
@ -1141,4 +1141,7 @@
|
||||||
<data name="TbSettingsHysteriaBandwidth" xml:space="preserve">
|
<data name="TbSettingsHysteriaBandwidth" xml:space="preserve">
|
||||||
<value>Hysteria 最大带宽(Up/Dw)</value>
|
<value>Hysteria 最大带宽(Up/Dw)</value>
|
||||||
</data>
|
</data>
|
||||||
|
<data name="TbSettingsUseSystemHosts" xml:space="preserve">
|
||||||
|
<value>使用系统hosts</value>
|
||||||
|
</data>
|
||||||
</root>
|
</root>
|
|
@ -1117,7 +1117,7 @@
|
||||||
<data name="TbDnsSingboxObjectDoc" xml:space="preserve">
|
<data name="TbDnsSingboxObjectDoc" xml:space="preserve">
|
||||||
<value>請填寫 DNS JSON 結構,點擊查看文件</value>
|
<value>請填寫 DNS JSON 結構,點擊查看文件</value>
|
||||||
</data>
|
</data>
|
||||||
<data name="TBSettingDnsImportDefConfig" xml:space="preserve">
|
<data name="TbSettingDnsImportDefConfig" xml:space="preserve">
|
||||||
<value>點擊匯入預設DNS配置</value>
|
<value>點擊匯入預設DNS配置</value>
|
||||||
</data>
|
</data>
|
||||||
<data name="TbdomainStrategy4Singbox" xml:space="preserve">
|
<data name="TbdomainStrategy4Singbox" xml:space="preserve">
|
||||||
|
|
|
@ -15,6 +15,7 @@ namespace v2rayN.ViewModels
|
||||||
private NoticeHandler? _noticeHandler;
|
private NoticeHandler? _noticeHandler;
|
||||||
private Window _view;
|
private Window _view;
|
||||||
|
|
||||||
|
[Reactive] public bool useSystemHosts { get; set; }
|
||||||
[Reactive] public string domainStrategy4Freedom { get; set; }
|
[Reactive] public string domainStrategy4Freedom { get; set; }
|
||||||
[Reactive] public string normalDNS { get; set; }
|
[Reactive] public string normalDNS { get; set; }
|
||||||
[Reactive] public string normalDNS2 { get; set; }
|
[Reactive] public string normalDNS2 { get; set; }
|
||||||
|
@ -31,6 +32,7 @@ namespace v2rayN.ViewModels
|
||||||
_view = view;
|
_view = view;
|
||||||
|
|
||||||
var item = LazyConfig.Instance.GetDNSItem(ECoreType.Xray);
|
var item = LazyConfig.Instance.GetDNSItem(ECoreType.Xray);
|
||||||
|
useSystemHosts = item.useSystemHosts;
|
||||||
domainStrategy4Freedom = item?.domainStrategy4Freedom!;
|
domainStrategy4Freedom = item?.domainStrategy4Freedom!;
|
||||||
normalDNS = item?.normalDNS!;
|
normalDNS = item?.normalDNS!;
|
||||||
|
|
||||||
|
@ -95,6 +97,7 @@ namespace v2rayN.ViewModels
|
||||||
|
|
||||||
var item = LazyConfig.Instance.GetDNSItem(ECoreType.Xray);
|
var item = LazyConfig.Instance.GetDNSItem(ECoreType.Xray);
|
||||||
item.domainStrategy4Freedom = domainStrategy4Freedom;
|
item.domainStrategy4Freedom = domainStrategy4Freedom;
|
||||||
|
item.useSystemHosts = useSystemHosts;
|
||||||
item.normalDNS = normalDNS;
|
item.normalDNS = normalDNS;
|
||||||
ConfigHandler.SaveDNSItems(_config, item);
|
ConfigHandler.SaveDNSItems(_config, item);
|
||||||
|
|
||||||
|
|
|
@ -81,11 +81,25 @@
|
||||||
<Button
|
<Button
|
||||||
x:Name="btnImportDefConfig4V2ray"
|
x:Name="btnImportDefConfig4V2ray"
|
||||||
Margin="8,0,0,0"
|
Margin="8,0,0,0"
|
||||||
Content="{x:Static resx:ResUI.TBSettingDnsImportDefConfig}"
|
Content="{x:Static resx:ResUI.TbSettingDnsImportDefConfig}"
|
||||||
Cursor="Hand"
|
Cursor="Hand"
|
||||||
Style="{StaticResource DefButton}" />
|
Style="{StaticResource DefButton}" />
|
||||||
</StackPanel>
|
</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
|
<TextBox
|
||||||
x:Name="txtnormalDNS"
|
x:Name="txtnormalDNS"
|
||||||
Margin="{StaticResource SettingItemMargin}"
|
Margin="{StaticResource SettingItemMargin}"
|
||||||
|
@ -113,7 +127,7 @@
|
||||||
<Button
|
<Button
|
||||||
x:Name="btnImportDefConfig4Singbox"
|
x:Name="btnImportDefConfig4Singbox"
|
||||||
Margin="8,0,0,0"
|
Margin="8,0,0,0"
|
||||||
Content="{x:Static resx:ResUI.TBSettingDnsImportDefConfig}"
|
Content="{x:Static resx:ResUI.TbSettingDnsImportDefConfig}"
|
||||||
Cursor="Hand"
|
Cursor="Hand"
|
||||||
Style="{StaticResource DefButton}" />
|
Style="{StaticResource DefButton}" />
|
||||||
</StackPanel>
|
</StackPanel>
|
||||||
|
|
|
@ -37,6 +37,7 @@ namespace v2rayN.Views
|
||||||
|
|
||||||
this.WhenActivated(disposables =>
|
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.domainStrategy4Freedom, v => v.cmbdomainStrategy4Freedom.Text).DisposeWith(disposables);
|
||||||
this.Bind(ViewModel, vm => vm.normalDNS, v => v.txtnormalDNS.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);
|
this.Bind(ViewModel, vm => vm.normalDNS2, v => v.txtnormalDNS2.Text).DisposeWith(disposables);
|
||||||
|
|
Loading…
Reference in a new issue