Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 支持导入系统hosts追加至dns设置 #4464

Merged
merged 1 commit into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 38 additions & 15 deletions v2rayN/v2rayN/Handler/CoreConfigV2ray.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -718,29 +720,50 @@ private int dns(V2rayConfig v2rayConfig)
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
obj["servers"] = JArray.FromObject(servers);
}

if (item.useSystemHosts)
{
var hostfile = @"C:\Windows\System32\drivers\etc\hosts" ;

if (File.Exists(hostfile))
{
servers = servers
};
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)
{
Expand Down
1 change: 1 addition & 0 deletions v2rayN/v2rayN/Mode/DNSItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class DNSItem
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; }
Expand Down
Loading
Loading