diff --git a/v2rayN/v2rayN/Common/Utils.cs b/v2rayN/v2rayN/Common/Utils.cs index 53f3511d..abffbec1 100644 --- a/v2rayN/v2rayN/Common/Utils.cs +++ b/v2rayN/v2rayN/Common/Utils.cs @@ -481,7 +481,7 @@ namespace v2rayN /// 验证Domain地址是否合法 /// /// - public static bool IsDomain(string domain) + public static bool IsDomain(string? domain) { //如果为空 if (IsNullOrEmpty(domain)) diff --git a/v2rayN/v2rayN/Handler/CoreConfigSingbox.cs b/v2rayN/v2rayN/Handler/CoreConfigSingbox.cs index 12b3aaae..c07d16d0 100644 --- a/v2rayN/v2rayN/Handler/CoreConfigSingbox.cs +++ b/v2rayN/v2rayN/Handler/CoreConfigSingbox.cs @@ -810,24 +810,9 @@ namespace v2rayN.Handler { return 0; } - //Add the dns of the remote server domain - if (dns4Sbox.rules is null) - { - dns4Sbox.rules = new(); - } - dns4Sbox.servers.Add(new() - { - tag = "local_local", - address = "223.5.5.5", - detour = Global.DirectTag, - }); - dns4Sbox.rules.Add(new() - { - server = "local_local", - outbound = "any" - }); - singboxConfig.dns = dns4Sbox; + + GenDnsDomains(singboxConfig); } catch (Exception ex) { @@ -836,6 +821,35 @@ namespace v2rayN.Handler return 0; } + private int GenDnsDomains(SingboxConfig singboxConfig) + { + var dns4Sbox = singboxConfig.dns ?? new(); + dns4Sbox.servers ??= []; + dns4Sbox.rules ??= []; + + var lstDomain = singboxConfig.outbounds + .Where(t => !Utils.IsNullOrEmpty(t.server) && Utils.IsDomain(t.server)) + .Select(t => t.server) + .ToList(); + if (lstDomain != null && lstDomain.Count > 0) + { + var tag = "local_local"; + dns4Sbox.servers.Insert(0, new() + { + tag = tag, + address = "223.5.5.5", + detour = Global.DirectTag, + }); + dns4Sbox.rules.Insert(0, new() + { + server = tag, + domain = lstDomain + }); + } + singboxConfig.dns = dns4Sbox; + return 0; + } + private int GenExperimental(SingboxConfig singboxConfig) { if (_config.guiItem.enableStatistics) @@ -1076,18 +1090,18 @@ namespace v2rayN.Handler singboxConfig.route.rules.Add(rule); } - GenDns(new(), singboxConfig); - var dnsServer = singboxConfig.dns?.servers.FirstOrDefault(); - if (dnsServer != null) - { - dnsServer.detour = singboxConfig.route.rules.LastOrDefault()?.outbound; - } - var dnsRule = singboxConfig.dns?.rules.Where(t => t.outbound != null).FirstOrDefault(); - if (dnsRule != null) - { - singboxConfig.dns.rules = []; - singboxConfig.dns.rules.Add(dnsRule); - } + GenDnsDomains(singboxConfig); + //var dnsServer = singboxConfig.dns?.servers.FirstOrDefault(); + //if (dnsServer != null) + //{ + // dnsServer.detour = singboxConfig.route.rules.LastOrDefault()?.outbound; + //} + //var dnsRule = singboxConfig.dns?.rules.Where(t => t.outbound != null).FirstOrDefault(); + //if (dnsRule != null) + //{ + // singboxConfig.dns.rules = []; + // singboxConfig.dns.rules.Add(dnsRule); + //} //msg = string.Format(ResUI.SuccessfulConfiguration"), node.getSummary()); return 0; diff --git a/v2rayN/v2rayN/Models/SingboxConfig.cs b/v2rayN/v2rayN/Models/SingboxConfig.cs index b157a468..45012e68 100644 --- a/v2rayN/v2rayN/Models/SingboxConfig.cs +++ b/v2rayN/v2rayN/Models/SingboxConfig.cs @@ -95,7 +95,7 @@ { public string type { get; set; } public string tag { get; set; } - public string server { get; set; } + public string? server { get; set; } public int? server_port { get; set; } public string uuid { get; set; } public string security { get; set; } diff --git a/v2rayN/v2rayN/Sample/dns_singbox_normal b/v2rayN/v2rayN/Sample/dns_singbox_normal index d8c5c74e..b323520c 100644 --- a/v2rayN/v2rayN/Sample/dns_singbox_normal +++ b/v2rayN/v2rayN/Sample/dns_singbox_normal @@ -30,5 +30,6 @@ ], "server": "block" } - ] + ], + "final": "local" } \ No newline at end of file diff --git a/v2rayN/v2rayN/Sample/tun_singbox_dns b/v2rayN/v2rayN/Sample/tun_singbox_dns index d8c5c74e..b323520c 100644 --- a/v2rayN/v2rayN/Sample/tun_singbox_dns +++ b/v2rayN/v2rayN/Sample/tun_singbox_dns @@ -30,5 +30,6 @@ ], "server": "block" } - ] + ], + "final": "local" } \ No newline at end of file