From 1d2aa70fe943a8674254364a7d53c447a6f44b3b Mon Sep 17 00:00:00 2001 From: DHR60 Date: Mon, 7 Apr 2025 19:24:30 +0800 Subject: [PATCH] Migrating to singbox 1.12 support --- v2rayN/ServiceLib/Models/SingboxConfig.cs | 9 +- v2rayN/ServiceLib/Sample/dns_singbox_normal | 13 ++- v2rayN/ServiceLib/Sample/tun_singbox_dns | 13 ++- .../CoreConfig/CoreConfigSingboxService.cs | 90 +++++++++++++++---- 4 files changed, 86 insertions(+), 39 deletions(-) diff --git a/v2rayN/ServiceLib/Models/SingboxConfig.cs b/v2rayN/ServiceLib/Models/SingboxConfig.cs index cbb2cb29..2c616776 100644 --- a/v2rayN/ServiceLib/Models/SingboxConfig.cs +++ b/v2rayN/ServiceLib/Models/SingboxConfig.cs @@ -134,6 +134,7 @@ public class Outbound4Sbox public HyObfs4Sbox? obfs { get; set; } public List? outbounds { get; set; } public bool? interrupt_exist_connections { get; set; } + public Rule4Sbox? domain_resolver { get; set; } } public class Endpoints4Sbox @@ -220,12 +221,12 @@ public class HyObfs4Sbox public class Server4Sbox { public string? tag { get; set; } - public string? address { get; set; } - public string? address_resolver { get; set; } - public string? address_strategy { get; set; } - public string? strategy { get; set; } public string? detour { get; set; } public string? client_subnet { get; set; } + public string? type { get; set; } + public string? server { get; set; } + public string? server_resolver { get; set; } + //public string? interface { get; set; } } public class Experimental4Sbox diff --git a/v2rayN/ServiceLib/Sample/dns_singbox_normal b/v2rayN/ServiceLib/Sample/dns_singbox_normal index 0921fe64..13aa7ead 100644 --- a/v2rayN/ServiceLib/Sample/dns_singbox_normal +++ b/v2rayN/ServiceLib/Sample/dns_singbox_normal @@ -2,19 +2,16 @@ "servers": [ { "tag": "remote", - "address": "tcp://8.8.8.8", + "type": "tcp", + "server": "8.8.8.8", "strategy": "prefer_ipv4", "detour": "proxy" }, { "tag": "local", - "address": "223.5.5.5", - "strategy": "prefer_ipv4", - "detour": "direct" - }, - { - "tag": "block", - "address": "rcode://success" + "type": "udp", + "server": "223.5.5.5", + "strategy": "prefer_ipv4" } ], "rules": [ diff --git a/v2rayN/ServiceLib/Sample/tun_singbox_dns b/v2rayN/ServiceLib/Sample/tun_singbox_dns index d8ca9808..e20c5cd5 100644 --- a/v2rayN/ServiceLib/Sample/tun_singbox_dns +++ b/v2rayN/ServiceLib/Sample/tun_singbox_dns @@ -2,19 +2,16 @@ "servers": [ { "tag": "remote", - "address": "tcp://8.8.8.8", + "type": "tcp", + "server": "8.8.8.8", "strategy": "prefer_ipv4", "detour": "proxy" }, { "tag": "local", - "address": "223.5.5.5", - "strategy": "prefer_ipv4", - "detour": "direct" - }, - { - "tag": "block", - "address": "rcode://success" + "type": "udp", + "server": "223.5.5.5", + "strategy": "prefer_ipv4" } ], "rules": [ diff --git a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs index cb065be0..449a00fb 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs @@ -2,6 +2,7 @@ using System.Data; using System.Net; using System.Net.NetworkInformation; using DynamicData; +using ServiceLib.Models; namespace ServiceLib.Services.CoreConfig; @@ -635,6 +636,16 @@ public class CoreConfigSingboxService outbound.server_port = node.Port; outbound.type = Global.ProtocolTypes[node.ConfigType]; + if (Utils.IsDomain(node.Address)) + { + outbound.domain_resolver = new() + { + server = "local_local", + // TODO + //strategy = string.IsNullOrEmpty(dNSItem?.DomainStrategy4Freedom) ? null : dNSItem?.DomainStrategy4Freedom + }; + } + switch (node.ConfigType) { case EConfigType.VMess: @@ -1292,17 +1303,71 @@ public class CoreConfigSingboxService dns4Sbox.rules ??= []; var tag = "local_local"; + var localDnsAddress = string.IsNullOrEmpty(dNSItem?.DomainDNSAddress) ? Global.SingboxDomainDNSAddress.FirstOrDefault() : dNSItem?.DomainDNSAddress; + string? localDnsType = null; + //string? dhcpDnsInterface = null; + if (localDnsAddress == "local") + { + localDnsType = "local"; + localDnsAddress = null; + } + else if (localDnsAddress.StartsWith("dhcp")) + { + localDnsType = "dhcp"; + //if (localDnsAddress.Length > 7) // dhcp:// + //{ + // localDnsAddress = localDnsAddress.Substring(7); + //} + localDnsAddress = null; + } + else if (localDnsAddress.StartsWith("tcp")) + { + localDnsType = "tcp"; + if (localDnsAddress.Length > 6) // tcp:// + { + localDnsAddress = localDnsAddress.Substring(6); + } + } + else if (localDnsAddress.StartsWith("tls")) + { + localDnsType = "tls"; + if (localDnsAddress.Length > 6) // tls:// + { + localDnsAddress = localDnsAddress.Substring(6); + } + } + else if (localDnsAddress.StartsWith("https")) + { + localDnsType = "https"; + if (localDnsAddress.Length > 8) // https:// + { + localDnsAddress = localDnsAddress.Substring(8); + } + } + else if (localDnsAddress.StartsWith("quic")) + { + localDnsType = "quic"; + if (localDnsAddress.Length > 7) // quic:// + { + localDnsAddress = localDnsAddress.Substring(7); + } + } + else + { + localDnsType = "udp"; + } + 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, + type = localDnsType, + server = localDnsAddress }); dns4Sbox.rules.Insert(0, new() { server = tag, - clash_mode = ERuleMode.Direct.ToString() + clash_mode = ERuleMode.Direct.ToString(), + strategy = string.IsNullOrEmpty(dNSItem?.DomainStrategy4Freedom) ? null : dNSItem?.DomainStrategy4Freedom }); dns4Sbox.rules.Insert(0, new() { @@ -1310,27 +1375,14 @@ public class CoreConfigSingboxService 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] + domain = [node?.Sni], + strategy = string.IsNullOrEmpty(dNSItem?.DomainStrategy4Freedom) ? null : dNSItem?.DomainStrategy4Freedom }); }