From 60d40747a007cfbfcda1bdb752c2e9159727b8f1 Mon Sep 17 00:00:00 2001 From: DHR60 Date: Fri, 11 Apr 2025 19:59:20 +0800 Subject: [PATCH] Adds Sing-box legacy DNS config support --- v2rayN/ServiceLib/Models/SingboxConfig.cs | 6 ++ v2rayN/ServiceLib/Sample/dns_singbox_normal | 4 +- v2rayN/ServiceLib/Sample/tun_singbox_dns | 4 +- .../CoreConfig/CoreConfigSingboxService.cs | 62 ++++++++++++++++++- 4 files changed, 69 insertions(+), 7 deletions(-) diff --git a/v2rayN/ServiceLib/Models/SingboxConfig.cs b/v2rayN/ServiceLib/Models/SingboxConfig.cs index 6731611b..77c98db6 100644 --- a/v2rayN/ServiceLib/Models/SingboxConfig.cs +++ b/v2rayN/ServiceLib/Models/SingboxConfig.cs @@ -227,6 +227,12 @@ public class Server4Sbox public string? server { get; set; } public string? server_resolver { get; set; } [JsonPropertyName("interface")] public string? Interface { get; set; } + // Deprecated + public string? address { get; set; } + public string? address_resolver { get; set; } + public string? address_strategy { get; set; } + public string? strategy { get; set; } + // Deprecated End } public class Experimental4Sbox diff --git a/v2rayN/ServiceLib/Sample/dns_singbox_normal b/v2rayN/ServiceLib/Sample/dns_singbox_normal index 19b2ceae..69367e86 100644 --- a/v2rayN/ServiceLib/Sample/dns_singbox_normal +++ b/v2rayN/ServiceLib/Sample/dns_singbox_normal @@ -4,14 +4,12 @@ "tag": "remote", "type": "tcp", "server": "8.8.8.8", - "strategy": "prefer_ipv4", "detour": "proxy" }, { "tag": "local", "type": "udp", - "server": "223.5.5.5", - "strategy": "prefer_ipv4" + "server": "223.5.5.5" } ], "rules": [ diff --git a/v2rayN/ServiceLib/Sample/tun_singbox_dns b/v2rayN/ServiceLib/Sample/tun_singbox_dns index 31a9af33..530267b0 100644 --- a/v2rayN/ServiceLib/Sample/tun_singbox_dns +++ b/v2rayN/ServiceLib/Sample/tun_singbox_dns @@ -4,14 +4,12 @@ "tag": "remote", "type": "tcp", "server": "8.8.8.8", - "strategy": "prefer_ipv4", "detour": "proxy" }, { "tag": "local", "type": "udp", - "server": "223.5.5.5", - "strategy": "prefer_ipv4" + "server": "223.5.5.5" } ], "rules": [ diff --git a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs index fec5c524..f417ac1d 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs @@ -1427,7 +1427,14 @@ public class CoreConfigSingboxService } singboxConfig.dns = dns4Sbox; - await GenDnsDomains(node, singboxConfig, item); + if (dns4Sbox.servers != null && dns4Sbox.servers.Count > 0 && dns4Sbox.servers.First().address.IsNullOrEmpty()) + { + await GenDnsDomains(node, singboxConfig, item); + } + else + { + await GenDnsDomainsLegacy(node, singboxConfig, item); + } } catch (Exception ex) { @@ -1525,6 +1532,59 @@ public class CoreConfigSingboxService return await Task.FromResult(0); } + private async Task GenDnsDomainsLegacy(ProfileItem? node, SingboxConfig singboxConfig, DNSItem? dNSItem) + { + var dns4Sbox = singboxConfig.dns ?? new(); + dns4Sbox.servers ??= []; + dns4Sbox.rules ??= []; + + var tag = "local_local"; + dns4Sbox.servers.Add(new() + { + tag = tag, + address = string.IsNullOrEmpty(dNSItem?.DomainDNSAddress) ? Global.SingboxDomainDNSAddress.FirstOrDefault() : dNSItem?.DomainDNSAddress, + detour = Global.DirectTag, + strategy = string.IsNullOrEmpty(dNSItem?.DomainStrategy4Freedom) ? null : dNSItem?.DomainStrategy4Freedom, + }); + dns4Sbox.rules.Insert(0, new() + { + server = tag, + clash_mode = ERuleMode.Direct.ToString() + }); + dns4Sbox.rules.Insert(0, new() + { + server = dns4Sbox.servers.Where(t => t.detour == Global.ProxyTag).Select(t => t.tag).FirstOrDefault() ?? "remote", + clash_mode = ERuleMode.Global.ToString() + }); + + var lstDomain = singboxConfig.outbounds + .Where(t => t.server.IsNotEmpty() && Utils.IsDomain(t.server)) + .Select(t => t.server) + .Distinct() + .ToList(); + if (lstDomain != null && lstDomain.Count > 0) + { + dns4Sbox.rules.Insert(0, new() + { + server = tag, + domain = lstDomain + }); + } + + //Tun2SocksAddress + if (_config.TunModeItem.EnableTun && node?.ConfigType == EConfigType.SOCKS && Utils.IsDomain(node?.Sni)) + { + dns4Sbox.rules.Insert(0, new() + { + server = tag, + domain = [node?.Sni] + }); + } + + singboxConfig.dns = dns4Sbox; + return await Task.FromResult(0); + } + private async Task GenExperimental(SingboxConfig singboxConfig) { //if (_config.guiItem.enableStatistics)