Skip to content

Commit

Permalink
Merge pull request #4464 from ShiinaRinne/host
Browse files Browse the repository at this point in the history
feat: 支持导入系统hosts追加至dns设置
  • Loading branch information
2dust committed Dec 1, 2023
2 parents 3ca8acc + 63809d4 commit 843b881
Show file tree
Hide file tree
Showing 9 changed files with 433 additions and 377 deletions.
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

0 comments on commit 843b881

Please sign in to comment.