From e54dfe57b520f95f112e413f5872f085ca148408 Mon Sep 17 00:00:00 2001 From: DHR60 Date: Sun, 6 Apr 2025 21:34:23 +0800 Subject: [PATCH 01/36] Migrating to singbox 1.11 support --- v2rayN/ServiceLib/Models/SingboxConfig.cs | 32 +++++++++- .../Sample/SingboxSampleClientConfig | 14 +---- v2rayN/ServiceLib/Sample/tun_singbox_rules | 4 +- .../CoreConfig/CoreConfigSingboxService.cs | 63 ++++++++++++------- 4 files changed, 74 insertions(+), 39 deletions(-) diff --git a/v2rayN/ServiceLib/Models/SingboxConfig.cs b/v2rayN/ServiceLib/Models/SingboxConfig.cs index 9ea1157d..cbb2cb29 100644 --- a/v2rayN/ServiceLib/Models/SingboxConfig.cs +++ b/v2rayN/ServiceLib/Models/SingboxConfig.cs @@ -67,6 +67,9 @@ public class Rule4Sbox public List? process_name { get; set; } public List? rule_set { get; set; } public List? rules { get; set; } + public string? action { get; set; } + public string? strategy { get; set; } + public List? sniffer { get; set; } } [Serializable] @@ -76,7 +79,6 @@ public class Inbound4Sbox public string tag { get; set; } public string listen { get; set; } public int? listen_port { get; set; } - public string? domain_strategy { get; set; } public string interface_name { get; set; } public List? address { get; set; } public int? mtu { get; set; } @@ -84,8 +86,6 @@ public class Inbound4Sbox public bool? strict_route { get; set; } public bool? endpoint_independent_nat { get; set; } public string? stack { get; set; } - public bool? sniff { get; set; } - public bool? sniff_override_destination { get; set; } public List users { get; set; } } @@ -136,6 +136,32 @@ public class Outbound4Sbox public bool? interrupt_exist_connections { get; set; } } +public class Endpoints4Sbox +{ + public string type { get; set; } + public string tag { get; set; } + public bool? system { get; set; } + public string? name { get; set; } + public int? mtu { get; set; } + public List address { get; set; } + public string private_key { get; set; } + public int listen_port { get; set; } + public string? udp_timeout { get; set; } + public int? workers { get; set; } + public List peers { get; set; } +} + +public class Peer4Sbox +{ + public string address { get; set; } + public int port { get; set; } + public string public_key { get; set; } + public string? pre_shared_key { get; set; } + public List allowed_ips { get; set; } + public int? persistent_keepalive_interval { get; set; } + public List reserved { get; set; } +} + public class Tls4Sbox { public bool enabled { get; set; } diff --git a/v2rayN/ServiceLib/Sample/SingboxSampleClientConfig b/v2rayN/ServiceLib/Sample/SingboxSampleClientConfig index f88422a1..b07fd72c 100644 --- a/v2rayN/ServiceLib/Sample/SingboxSampleClientConfig +++ b/v2rayN/ServiceLib/Sample/SingboxSampleClientConfig @@ -1,4 +1,4 @@ -{ +{ "log": { "level": "debug", "timestamp": true @@ -14,22 +14,10 @@ { "type": "direct", "tag": "direct" - }, - { - "type": "block", - "tag": "block" - }, - { - "tag": "dns_out", - "type": "dns" } ], "route": { "rules": [ - { - "protocol": [ "dns" ], - "outbound": "dns_out" - } ] } } \ No newline at end of file diff --git a/v2rayN/ServiceLib/Sample/tun_singbox_rules b/v2rayN/ServiceLib/Sample/tun_singbox_rules index df1dc4ec..a4276134 100644 --- a/v2rayN/ServiceLib/Sample/tun_singbox_rules +++ b/v2rayN/ServiceLib/Sample/tun_singbox_rules @@ -8,13 +8,13 @@ 139, 5353 ], - "outbound": "block" + "action": "reject" }, { "ip_cidr": [ "224.0.0.0/3", "ff00::/8" ], - "outbound": "block" + "action": "reject" } ] \ No newline at end of file diff --git a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs index fdc03058..a66ddff6 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs @@ -1,6 +1,7 @@ using System.Data; using System.Net; using System.Net.NetworkInformation; +using DynamicData; namespace ServiceLib.Services.CoreConfig; @@ -534,15 +535,6 @@ public class CoreConfigSingboxService singboxConfig.inbounds.Add(inbound); inbound.listen_port = AppHandler.Instance.GetLocalPort(EInboundProtocol.socks); - inbound.sniff = _config.Inbound.First().SniffingEnabled; - inbound.sniff_override_destination = _config.Inbound.First().RouteOnly ? false : _config.Inbound.First().SniffingEnabled; - inbound.domain_strategy = _config.RoutingBasicItem.DomainStrategy4Singbox.IsNullOrEmpty() ? null : _config.RoutingBasicItem.DomainStrategy4Singbox; - - var routing = await ConfigHandler.GetDefaultRouting(_config); - if (routing.DomainStrategy4Singbox.IsNotEmpty()) - { - inbound.domain_strategy = routing.DomainStrategy4Singbox; - } if (_config.Inbound.First().SecondLocalPortEnabled) { @@ -587,8 +579,6 @@ public class CoreConfigSingboxService tunInbound.mtu = _config.TunModeItem.Mtu; tunInbound.strict_route = _config.TunModeItem.StrictRoute; tunInbound.stack = _config.TunModeItem.Stack; - tunInbound.sniff = _config.Inbound.First().SniffingEnabled; - //tunInbound.sniff_override_destination = _config.inbound.First().routeOnly ? false : _config.inbound.First().sniffingEnabled; if (_config.TunModeItem.EnableIPv6Address == false) { tunInbound.address = ["172.18.0.1/30"]; @@ -1110,8 +1100,6 @@ public class CoreConfigSingboxService { try { - var dnsOutbound = "dns_out"; - if (_config.TunModeItem.EnableTun) { singboxConfig.route.auto_detect_interface = true; @@ -1126,7 +1114,7 @@ public class CoreConfigSingboxService singboxConfig.route.rules.Add(new() { port = new() { 53 }, - outbound = dnsOutbound, + action = "hijack-dns", process_name = lstDnsExe }); @@ -1137,13 +1125,26 @@ public class CoreConfigSingboxService }); } - if (!_config.Inbound.First().SniffingEnabled) + if (_config.Inbound.First().SniffingEnabled) { singboxConfig.route.rules.Add(new() { - port = [53], - network = ["udp"], - outbound = dnsOutbound + action = "sniff", + sniffer = new() { "dns", _config.Inbound.First().DestOverride } + }); + singboxConfig.route.rules.Add(new() + { + protocol = new() { "dns" }, + action = "hijack-dns" + }); + } + else + { + singboxConfig.route.rules.Add(new() + { + port = new() { 53 }, + network = new() { "udp" }, + action = "hijack-dns" }); } @@ -1158,6 +1159,21 @@ public class CoreConfigSingboxService clash_mode = ERuleMode.Global.ToString() }); + if (!(_config.Inbound.First().RouteOnly || _config.TunModeItem.EnableTun)) + { + var domainStrategy = _config.RoutingBasicItem.DomainStrategy4Singbox.IsNullOrEmpty() ? null : _config.RoutingBasicItem.DomainStrategy4Singbox; + var defaultRouting = await ConfigHandler.GetDefaultRouting(_config); + if (defaultRouting.DomainStrategy4Singbox.IsNotEmpty()) + { + domainStrategy = defaultRouting.DomainStrategy4Singbox; + } + singboxConfig.route.rules.Add(new() + { + action = "resolve", + strategy = domainStrategy + }); + } + var routing = await ConfigHandler.GetDefaultRouting(_config); if (routing != null) { @@ -1217,10 +1233,15 @@ public class CoreConfigSingboxService item.OutboundTag = await GenRoutingUserRuleOutbound(item.OutboundTag, singboxConfig); var rules = singboxConfig.route.rules; - var rule = new Rule4Sbox() + var rule = new Rule4Sbox(); + if (item.OutboundTag == "block") { - outbound = item.OutboundTag, - }; + rule.action = "reject"; + } + else + { + rule.outbound = item.OutboundTag; + } if (item.Port.IsNotEmpty()) { From e21dab1d4d2a31ade9d9bdc032df3f9992027941 Mon Sep 17 00:00:00 2001 From: DHR60 Date: Sun, 6 Apr 2025 23:01:16 +0800 Subject: [PATCH 02/36] Removes unnecessary sniffer --- .../ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs index a66ddff6..8ebe7d15 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs @@ -1129,8 +1129,7 @@ public class CoreConfigSingboxService { singboxConfig.route.rules.Add(new() { - action = "sniff", - sniffer = new() { "dns", _config.Inbound.First().DestOverride } + action = "sniff" }); singboxConfig.route.rules.Add(new() { From 849752abcecc3401f76f4bfcc9cd8b7146cca676 Mon Sep 17 00:00:00 2001 From: DHR60 Date: Mon, 7 Apr 2025 19:24:30 +0800 Subject: [PATCH 03/36] Migrating to singbox 1.12 support --- v2rayN/ServiceLib/Models/SingboxConfig.cs | 19 ++-- v2rayN/ServiceLib/Sample/dns_singbox_normal | 13 ++- v2rayN/ServiceLib/Sample/tun_singbox_dns | 13 ++- .../CoreConfig/CoreConfigSingboxService.cs | 90 +++++++++++++++---- 4 files changed, 88 insertions(+), 47 deletions(-) diff --git a/v2rayN/ServiceLib/Models/SingboxConfig.cs b/v2rayN/ServiceLib/Models/SingboxConfig.cs index cbb2cb29..1d3249c9 100644 --- a/v2rayN/ServiceLib/Models/SingboxConfig.cs +++ b/v2rayN/ServiceLib/Models/SingboxConfig.cs @@ -29,7 +29,6 @@ public class Dns4Sbox public bool? independent_cache { get; set; } public bool? reverse_mapping { get; set; } public string? client_subnet { get; set; } - public Fakeip4Sbox? fakeip { get; set; } } public class Route4Sbox @@ -134,6 +133,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 +220,14 @@ 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? inet4_range { get; set; } + public string? inet6_range { 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 @@ -255,13 +257,6 @@ public class Stats4Sbox public List? users { get; set; } } -public class Fakeip4Sbox -{ - public bool enabled { get; set; } - public string inet4_range { get; set; } - public string inet6_range { get; set; } -} - public class CacheFile4Sbox { public bool enabled { get; set; } 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 8ebe7d15..84b77840 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; @@ -611,6 +612,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: @@ -1448,17 +1459,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() { @@ -1466,27 +1531,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 }); } From c15fa64bad30f7f1927c341795074c478c42d517 Mon Sep 17 00:00:00 2001 From: DHR60 Date: Tue, 8 Apr 2025 20:05:31 +0800 Subject: [PATCH 04/36] Adds Google cn dns rules --- v2rayN/ServiceLib/Sample/dns_singbox_normal | 7 +++++++ v2rayN/ServiceLib/Sample/tun_singbox_dns | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/v2rayN/ServiceLib/Sample/dns_singbox_normal b/v2rayN/ServiceLib/Sample/dns_singbox_normal index 13aa7ead..19b2ceae 100644 --- a/v2rayN/ServiceLib/Sample/dns_singbox_normal +++ b/v2rayN/ServiceLib/Sample/dns_singbox_normal @@ -15,6 +15,13 @@ } ], "rules": [ + { + "domain_suffix": [ + "googleapis.cn", + "gstatic.com" + ], + "server": "remote" + }, { "rule_set": [ "geosite-cn" diff --git a/v2rayN/ServiceLib/Sample/tun_singbox_dns b/v2rayN/ServiceLib/Sample/tun_singbox_dns index e20c5cd5..31a9af33 100644 --- a/v2rayN/ServiceLib/Sample/tun_singbox_dns +++ b/v2rayN/ServiceLib/Sample/tun_singbox_dns @@ -15,6 +15,13 @@ } ], "rules": [ + { + "domain_suffix": [ + "googleapis.cn", + "gstatic.com" + ], + "server": "remote" + }, { "rule_set": [ "geosite-cn", From 0fc5ce16bf44683fd4e3c27b994b4951587e8f79 Mon Sep 17 00:00:00 2001 From: DHR60 Date: Tue, 8 Apr 2025 20:12:03 +0800 Subject: [PATCH 05/36] Improves geoip rule handling in singbox --- .../CoreConfig/CoreConfigSingboxService.cs | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs index 84b77840..2f32f6ea 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs @@ -1375,24 +1375,28 @@ public class CoreConfigSingboxService { return false; } - else if (address.StartsWith("geoip:!")) - { - return false; - } else if (address.Equals("geoip:private")) { rule.ip_is_private = true; } else if (address.StartsWith("geoip:")) { - if (rule.geoip is null) - { rule.geoip = new(); } + rule.geoip ??= new(); rule.geoip?.Add(address.Substring(6)); } + else if (address.Equals("geoip:!private")) + { + rule.ip_is_private = false; + } + else if (address.StartsWith("geoip:!")) + { + rule.geoip ??= new(); + rule.geoip?.Add(address.Substring(6)); + rule.invert = true; + } else { - if (rule.ip_cidr is null) - { rule.ip_cidr = new(); } + rule.ip_cidr ??= new(); rule.ip_cidr?.Add(address); } return true; From 9bc7c7be3aace60e6b551fc685a94b9eb974a720 Mon Sep 17 00:00:00 2001 From: DHR60 Date: Thu, 10 Apr 2025 23:04:54 +0800 Subject: [PATCH 06/36] add anytls support --- v2rayN/ServiceLib/Enums/EConfigType.cs | 3 +- v2rayN/ServiceLib/Global.cs | 6 ++- v2rayN/ServiceLib/Handler/ConfigHandler.cs | 31 +++++++++++ v2rayN/ServiceLib/Handler/CoreHandler.cs | 2 +- v2rayN/ServiceLib/Handler/Fmt/AnytlsFmt.cs | 54 +++++++++++++++++++ v2rayN/ServiceLib/Handler/Fmt/FmtHandler.cs | 5 ++ v2rayN/ServiceLib/Resx/ResUI.Designer.cs | 9 ++++ v2rayN/ServiceLib/Resx/ResUI.fa-Ir.resx | 3 ++ v2rayN/ServiceLib/Resx/ResUI.hu.resx | 3 ++ v2rayN/ServiceLib/Resx/ResUI.resx | 3 ++ v2rayN/ServiceLib/Resx/ResUI.ru.resx | 3 ++ v2rayN/ServiceLib/Resx/ResUI.zh-Hans.resx | 3 ++ v2rayN/ServiceLib/Resx/ResUI.zh-Hant.resx | 3 ++ .../CoreConfig/CoreConfigSingboxService.cs | 5 ++ .../CoreConfig/CoreConfigV2rayService.cs | 3 +- .../ServiceLib/Services/SpeedtestService.cs | 4 +- .../ViewModels/MainWindowViewModel.cs | 5 ++ .../Views/AddServerWindow.axaml | 20 +++++++ .../Views/AddServerWindow.axaml.cs | 9 ++++ v2rayN/v2rayN.Desktop/Views/MainWindow.axaml | 1 + .../v2rayN.Desktop/Views/MainWindow.axaml.cs | 1 + v2rayN/v2rayN/Views/AddServerWindow.xaml | 29 ++++++++++ v2rayN/v2rayN/Views/AddServerWindow.xaml.cs | 8 +++ v2rayN/v2rayN/Views/MainWindow.xaml | 4 ++ v2rayN/v2rayN/Views/MainWindow.xaml.cs | 1 + 25 files changed, 211 insertions(+), 7 deletions(-) create mode 100644 v2rayN/ServiceLib/Handler/Fmt/AnytlsFmt.cs diff --git a/v2rayN/ServiceLib/Enums/EConfigType.cs b/v2rayN/ServiceLib/Enums/EConfigType.cs index f56d0e0f..6698f962 100644 --- a/v2rayN/ServiceLib/Enums/EConfigType.cs +++ b/v2rayN/ServiceLib/Enums/EConfigType.cs @@ -11,5 +11,6 @@ public enum EConfigType Hysteria2 = 7, TUIC = 8, WireGuard = 9, - HTTP = 10 + HTTP = 10, + Anytls = 11 } diff --git a/v2rayN/ServiceLib/Global.cs b/v2rayN/ServiceLib/Global.cs index 5592fa2e..4948f111 100644 --- a/v2rayN/ServiceLib/Global.cs +++ b/v2rayN/ServiceLib/Global.cs @@ -167,7 +167,8 @@ public class Global { EConfigType.Trojan, "trojan://" }, { EConfigType.Hysteria2, "hysteria2://" }, { EConfigType.TUIC, "tuic://" }, - { EConfigType.WireGuard, "wireguard://" } + { EConfigType.WireGuard, "wireguard://" }, + { EConfigType.Anytls, "anytls://" } }; public static readonly Dictionary ProtocolTypes = new() @@ -180,7 +181,8 @@ public class Global { EConfigType.Trojan, "trojan" }, { EConfigType.Hysteria2, "hysteria2" }, { EConfigType.TUIC, "tuic" }, - { EConfigType.WireGuard, "wireguard" } + { EConfigType.WireGuard, "wireguard" }, + { EConfigType.Anytls, "anytls" } }; public static readonly List VmessSecurities = diff --git a/v2rayN/ServiceLib/Handler/ConfigHandler.cs b/v2rayN/ServiceLib/Handler/ConfigHandler.cs index c08ca28e..6005e0c6 100644 --- a/v2rayN/ServiceLib/Handler/ConfigHandler.cs +++ b/v2rayN/ServiceLib/Handler/ConfigHandler.cs @@ -261,6 +261,7 @@ public class ConfigHandler EConfigType.Hysteria2 => await AddHysteria2Server(config, item), EConfigType.TUIC => await AddTuicServer(config, item), EConfigType.WireGuard => await AddWireguardServer(config, item), + EConfigType.Anytls => await AddAnytlsServer(config, item), _ => -1, }; return ret; @@ -785,6 +786,35 @@ public class ConfigHandler return 0; } + /// + /// Add or edit a Anytls server + /// Validates and processes Anytls-specific settings + /// + /// Current configuration + /// Anytls profile to add + /// Whether to save to file + /// 0 if successful, -1 if failed + public static async Task AddAnytlsServer(Config config, ProfileItem profileItem, bool toFile = true) + { + profileItem.ConfigType = EConfigType.Anytls; + profileItem.CoreType = ECoreType.sing_box; + + profileItem.Address = profileItem.Address.TrimEx(); + profileItem.Id = profileItem.Id.TrimEx(); + profileItem.Security = profileItem.Security.TrimEx(); + profileItem.Network = string.Empty; + if (profileItem.StreamSecurity.IsNullOrEmpty()) + { + profileItem.StreamSecurity = Global.StreamSecurity; + } + if (profileItem.Id.IsNullOrEmpty()) + { + return -1; + } + await AddServerCommon(config, profileItem, toFile); + return 0; + } + /// /// Sort the server list by the specified column /// Updates the sort order in the profile extension data @@ -1294,6 +1324,7 @@ public class ConfigHandler EConfigType.Hysteria2 => await AddHysteria2Server(config, profileItem, false), EConfigType.TUIC => await AddTuicServer(config, profileItem, false), EConfigType.WireGuard => await AddWireguardServer(config, profileItem, false), + EConfigType.Anytls => await AddAnytlsServer(config, profileItem, false), _ => -1, }; diff --git a/v2rayN/ServiceLib/Handler/CoreHandler.cs b/v2rayN/ServiceLib/Handler/CoreHandler.cs index 92c30171..f66d3700 100644 --- a/v2rayN/ServiceLib/Handler/CoreHandler.cs +++ b/v2rayN/ServiceLib/Handler/CoreHandler.cs @@ -101,7 +101,7 @@ public class CoreHandler public async Task LoadCoreConfigSpeedtest(List selecteds) { - var coreType = selecteds.Exists(t => t.ConfigType is EConfigType.Hysteria2 or EConfigType.TUIC) ? ECoreType.sing_box : ECoreType.Xray; + var coreType = selecteds.Exists(t => t.ConfigType is EConfigType.Hysteria2 or EConfigType.TUIC or EConfigType.Anytls) ? ECoreType.sing_box : ECoreType.Xray; var fileName = string.Format(Global.CoreSpeedtestConfigFileName, Utils.GetGuid(false)); var configPath = Utils.GetBinConfigPath(fileName); var result = await CoreConfigHandler.GenerateClientSpeedtestConfig(_config, configPath, selecteds, coreType); diff --git a/v2rayN/ServiceLib/Handler/Fmt/AnytlsFmt.cs b/v2rayN/ServiceLib/Handler/Fmt/AnytlsFmt.cs new file mode 100644 index 00000000..bad36b19 --- /dev/null +++ b/v2rayN/ServiceLib/Handler/Fmt/AnytlsFmt.cs @@ -0,0 +1,54 @@ +using static QRCoder.PayloadGenerator; + +namespace ServiceLib.Handler.Fmt; +public class AnytlsFmt : BaseFmt +{ + public static ProfileItem? Resolve(string str, out string msg) + { + msg = ResUI.ConfigurationFormatIncorrect; + + var parsedUrl = Utils.TryUri(str); + if (parsedUrl == null) + { + return null; + } + + ProfileItem item = new() + { + ConfigType = EConfigType.Anytls, + Remarks = parsedUrl.GetComponents(UriComponents.Fragment, UriFormat.Unescaped), + Address = parsedUrl.IdnHost, + Port = parsedUrl.Port, + }; + var rawUserInfo = Utils.UrlDecode(parsedUrl.UserInfo); + item.Id = rawUserInfo; + + var query = Utils.ParseQueryString(parsedUrl.Query); + item.Sni = query["sni"] ?? Global.None; + item.AllowInsecure = (query["insecure"] ?? "") == "1" ? "true" : "false"; + + return item; + } + + public static string? ToUri(ProfileItem? item) + { + if (item == null) + { + return null; + } + var remark = string.Empty; + if (item.Remarks.IsNotEmpty()) + { + remark = "#" + Utils.UrlEncode(item.Remarks); + } + var pw = item.Id; + var dicQuery = new Dictionary(); + if (item.Sni.IsNotEmpty()) + { + dicQuery.Add("sni", item.Sni); + } + dicQuery.Add("insecure", item.AllowInsecure.ToLower() == "true" ? "1" : "0"); + + return ToUri(EConfigType.Anytls, item.Address, item.Port, pw, dicQuery, remark); + } +} diff --git a/v2rayN/ServiceLib/Handler/Fmt/FmtHandler.cs b/v2rayN/ServiceLib/Handler/Fmt/FmtHandler.cs index 3e8ab2ae..814d753d 100644 --- a/v2rayN/ServiceLib/Handler/Fmt/FmtHandler.cs +++ b/v2rayN/ServiceLib/Handler/Fmt/FmtHandler.cs @@ -18,6 +18,7 @@ public class FmtHandler EConfigType.Hysteria2 => Hysteria2Fmt.ToUri(item), EConfigType.TUIC => TuicFmt.ToUri(item), EConfigType.WireGuard => WireguardFmt.ToUri(item), + EConfigType.Anytls => AnytlsFmt.ToUri(item), _ => null, }; @@ -75,6 +76,10 @@ public class FmtHandler { return WireguardFmt.Resolve(str, out msg); } + else if (str.StartsWith(Global.ProtocolShares[EConfigType.Anytls])) + { + return AnytlsFmt.Resolve(str, out msg); + } else { msg = ResUI.NonvmessOrssProtocol; diff --git a/v2rayN/ServiceLib/Resx/ResUI.Designer.cs b/v2rayN/ServiceLib/Resx/ResUI.Designer.cs index 6167fd75..b7d28383 100644 --- a/v2rayN/ServiceLib/Resx/ResUI.Designer.cs +++ b/v2rayN/ServiceLib/Resx/ResUI.Designer.cs @@ -654,6 +654,15 @@ namespace ServiceLib.Resx { } } + /// + /// 查找类似 Add [Anytls] Configuration 的本地化字符串。 + /// + public static string menuAddAnytlsServer { + get { + return ResourceManager.GetString("menuAddAnytlsServer", resourceCulture); + } + } + /// /// 查找类似 Add a custom configuration Configuration 的本地化字符串。 /// diff --git a/v2rayN/ServiceLib/Resx/ResUI.fa-Ir.resx b/v2rayN/ServiceLib/Resx/ResUI.fa-Ir.resx index 3bc07d9f..d336da09 100644 --- a/v2rayN/ServiceLib/Resx/ResUI.fa-Ir.resx +++ b/v2rayN/ServiceLib/Resx/ResUI.fa-Ir.resx @@ -1395,4 +1395,7 @@ Can fill in the configuration remarks, please make sure it exist and are unique + + Add [Anytls] Configuration + \ No newline at end of file diff --git a/v2rayN/ServiceLib/Resx/ResUI.hu.resx b/v2rayN/ServiceLib/Resx/ResUI.hu.resx index 114e1a21..48abd20c 100644 --- a/v2rayN/ServiceLib/Resx/ResUI.hu.resx +++ b/v2rayN/ServiceLib/Resx/ResUI.hu.resx @@ -1395,4 +1395,7 @@ Can fill in the configuration remarks, please make sure it exist and are unique + + Add [Anytls] Configuration + \ No newline at end of file diff --git a/v2rayN/ServiceLib/Resx/ResUI.resx b/v2rayN/ServiceLib/Resx/ResUI.resx index 4be88f75..151e0998 100644 --- a/v2rayN/ServiceLib/Resx/ResUI.resx +++ b/v2rayN/ServiceLib/Resx/ResUI.resx @@ -1395,4 +1395,7 @@ Can fill in the configuration remarks, please make sure it exist and are unique + + Add [Anytls] Configuration + \ No newline at end of file diff --git a/v2rayN/ServiceLib/Resx/ResUI.ru.resx b/v2rayN/ServiceLib/Resx/ResUI.ru.resx index b987c364..a0ccb5b3 100644 --- a/v2rayN/ServiceLib/Resx/ResUI.ru.resx +++ b/v2rayN/ServiceLib/Resx/ResUI.ru.resx @@ -1395,4 +1395,7 @@ Can fill in the configuration remarks, please make sure it exist and are unique + + Add [Anytls] Configuration + \ No newline at end of file diff --git a/v2rayN/ServiceLib/Resx/ResUI.zh-Hans.resx b/v2rayN/ServiceLib/Resx/ResUI.zh-Hans.resx index 27240b80..4d33a507 100644 --- a/v2rayN/ServiceLib/Resx/ResUI.zh-Hans.resx +++ b/v2rayN/ServiceLib/Resx/ResUI.zh-Hans.resx @@ -1392,4 +1392,7 @@ 可以填写配置文件别名,请确保存在并唯一 + + 添加 [Anytls] 配置文件 + \ No newline at end of file diff --git a/v2rayN/ServiceLib/Resx/ResUI.zh-Hant.resx b/v2rayN/ServiceLib/Resx/ResUI.zh-Hant.resx index 4ea33211..1b7a647c 100644 --- a/v2rayN/ServiceLib/Resx/ResUI.zh-Hant.resx +++ b/v2rayN/ServiceLib/Resx/ResUI.zh-Hant.resx @@ -1392,4 +1392,7 @@ 可以填寫設定檔別名,請確保存在並唯一 + + 新增 [Anytls] 設定檔 + \ No newline at end of file diff --git a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs index 2f32f6ea..dec0d3ff 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs @@ -735,6 +735,11 @@ public class CoreConfigSingboxService outbound.mtu = node.ShortId.IsNullOrEmpty() ? Global.TunMtus.First() : node.ShortId.ToInt(); break; } + case EConfigType.Anytls: + { + outbound.password = node.Id; + break; + } } await GenOutboundTls(node, outbound); diff --git a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigV2rayService.cs b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigV2rayService.cs index 9be1acf0..97e8e6aa 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigV2rayService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigV2rayService.cs @@ -1349,7 +1349,8 @@ public class CoreConfigV2rayService if (prevNode is not null && prevNode.ConfigType != EConfigType.Custom && prevNode.ConfigType != EConfigType.Hysteria2 - && prevNode.ConfigType != EConfigType.TUIC) + && prevNode.ConfigType != EConfigType.TUIC + && prevNode.ConfigType != EConfigType.Anytls) { var prevOutbound = JsonUtils.Deserialize(txtOutbound); await GenOutbound(prevNode, prevOutbound); diff --git a/v2rayN/ServiceLib/Services/SpeedtestService.cs b/v2rayN/ServiceLib/Services/SpeedtestService.cs index 998cedcc..9c97a217 100644 --- a/v2rayN/ServiceLib/Services/SpeedtestService.cs +++ b/v2rayN/ServiceLib/Services/SpeedtestService.cs @@ -358,8 +358,8 @@ public class SpeedtestService private List> GetTestBatchItem(List lstSelected, int pageSize) { List> lstTest = new(); - var lst1 = lstSelected.Where(t => t.ConfigType is not (EConfigType.Hysteria2 or EConfigType.TUIC)).ToList(); - var lst2 = lstSelected.Where(t => t.ConfigType is EConfigType.Hysteria2 or EConfigType.TUIC).ToList(); + var lst1 = lstSelected.Where(t => t.ConfigType is not (EConfigType.Hysteria2 or EConfigType.TUIC or EConfigType.Anytls)).ToList(); + var lst2 = lstSelected.Where(t => t.ConfigType is EConfigType.Hysteria2 or EConfigType.TUIC or EConfigType.Anytls).ToList(); for (var num = 0; num < (int)Math.Ceiling(lst1.Count * 1.0 / pageSize); num++) { diff --git a/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs b/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs index 7bae19be..36e20a87 100644 --- a/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs @@ -20,6 +20,7 @@ public class MainWindowViewModel : MyReactiveObject public ReactiveCommand AddHysteria2ServerCmd { get; } public ReactiveCommand AddTuicServerCmd { get; } public ReactiveCommand AddWireguardServerCmd { get; } + public ReactiveCommand AddAnytlsServerCmd { get; } public ReactiveCommand AddCustomServerCmd { get; } public ReactiveCommand AddServerViaClipboardCmd { get; } public ReactiveCommand AddServerViaScanCmd { get; } @@ -111,6 +112,10 @@ public class MainWindowViewModel : MyReactiveObject { await AddServerAsync(true, EConfigType.WireGuard); }); + AddAnytlsServerCmd = ReactiveCommand.CreateFromTask(async () => + { + await AddServerAsync(true, EConfigType.Anytls); + }); AddCustomServerCmd = ReactiveCommand.CreateFromTask(async () => { await AddServerAsync(true, EConfigType.Custom); diff --git a/v2rayN/v2rayN.Desktop/Views/AddServerWindow.axaml b/v2rayN/v2rayN.Desktop/Views/AddServerWindow.axaml index ad30f985..78bd2595 100644 --- a/v2rayN/v2rayN.Desktop/Views/AddServerWindow.axaml +++ b/v2rayN/v2rayN.Desktop/Views/AddServerWindow.axaml @@ -533,6 +533,26 @@ HorizontalAlignment="Left" Watermark="1500" /> + + + + + gridTls.IsVisible = false; break; + + case EConfigType.Anytls: + gridAnytls.IsVisible = true; + cmbCoreType.IsEnabled = false; + break; } cmbStreamSecurity.ItemsSource = lstStreamSecurity; @@ -167,6 +172,10 @@ public partial class AddServerWindow : WindowBase this.Bind(ViewModel, vm => vm.SelectedSource.RequestHost, v => v.txtRequestHost9.Text).DisposeWith(disposables); this.Bind(ViewModel, vm => vm.SelectedSource.ShortId, v => v.txtShortId9.Text).DisposeWith(disposables); break; + + case EConfigType.Anytls: + this.Bind(ViewModel, vm => vm.SelectedSource.Id, v => v.txtId10.Text).DisposeWith(disposables); + break; } this.Bind(ViewModel, vm => vm.SelectedSource.Network, v => v.cmbNetwork.SelectedValue).DisposeWith(disposables); this.Bind(ViewModel, vm => vm.SelectedSource.HeaderType, v => v.cmbHeaderType.SelectedValue).DisposeWith(disposables); diff --git a/v2rayN/v2rayN.Desktop/Views/MainWindow.axaml b/v2rayN/v2rayN.Desktop/Views/MainWindow.axaml index bbc78a72..af4ae529 100644 --- a/v2rayN/v2rayN.Desktop/Views/MainWindow.axaml +++ b/v2rayN/v2rayN.Desktop/Views/MainWindow.axaml @@ -46,6 +46,7 @@ + diff --git a/v2rayN/v2rayN.Desktop/Views/MainWindow.axaml.cs b/v2rayN/v2rayN.Desktop/Views/MainWindow.axaml.cs index ea95ef0e..2584310b 100644 --- a/v2rayN/v2rayN.Desktop/Views/MainWindow.axaml.cs +++ b/v2rayN/v2rayN.Desktop/Views/MainWindow.axaml.cs @@ -83,6 +83,7 @@ public partial class MainWindow : WindowBase this.BindCommand(ViewModel, vm => vm.AddHysteria2ServerCmd, v => v.menuAddHysteria2Server).DisposeWith(disposables); this.BindCommand(ViewModel, vm => vm.AddTuicServerCmd, v => v.menuAddTuicServer).DisposeWith(disposables); this.BindCommand(ViewModel, vm => vm.AddWireguardServerCmd, v => v.menuAddWireguardServer).DisposeWith(disposables); + this.BindCommand(ViewModel, vm => vm.AddAnytlsServerCmd, v => v.menuAddAnytlsServer).DisposeWith(disposables); this.BindCommand(ViewModel, vm => vm.AddCustomServerCmd, v => v.menuAddCustomServer).DisposeWith(disposables); this.BindCommand(ViewModel, vm => vm.AddServerViaClipboardCmd, v => v.menuAddServerViaClipboard).DisposeWith(disposables); this.BindCommand(ViewModel, vm => vm.AddServerViaScanCmd, v => v.menuAddServerViaScan).DisposeWith(disposables); diff --git a/v2rayN/v2rayN/Views/AddServerWindow.xaml b/v2rayN/v2rayN/Views/AddServerWindow.xaml index 3b7be680..a1cb0fc7 100644 --- a/v2rayN/v2rayN/Views/AddServerWindow.xaml +++ b/v2rayN/v2rayN/Views/AddServerWindow.xaml @@ -707,6 +707,35 @@ materialDesign:HintAssist.Hint="1500" Style="{StaticResource DefTextBox}" /> + + + + + + + + + + + + + + vm.SelectedSource.RequestHost, v => v.txtRequestHost9.Text).DisposeWith(disposables); this.Bind(ViewModel, vm => vm.SelectedSource.ShortId, v => v.txtShortId9.Text).DisposeWith(disposables); break; + + case EConfigType.Anytls: + this.Bind(ViewModel, vm => vm.SelectedSource.Id, v => v.txtId10.Text).DisposeWith(disposables); + break; } this.Bind(ViewModel, vm => vm.SelectedSource.Network, v => v.cmbNetwork.Text).DisposeWith(disposables); this.Bind(ViewModel, vm => vm.SelectedSource.HeaderType, v => v.cmbHeaderType.Text).DisposeWith(disposables); diff --git a/v2rayN/v2rayN/Views/MainWindow.xaml b/v2rayN/v2rayN/Views/MainWindow.xaml index 55384453..0b8c30b3 100644 --- a/v2rayN/v2rayN/Views/MainWindow.xaml +++ b/v2rayN/v2rayN/Views/MainWindow.xaml @@ -108,6 +108,10 @@ x:Name="menuAddTuicServer" Height="{StaticResource MenuItemHeight}" Header="{x:Static resx:ResUI.menuAddTuicServer}" /> + diff --git a/v2rayN/v2rayN/Views/MainWindow.xaml.cs b/v2rayN/v2rayN/Views/MainWindow.xaml.cs index 88af6334..08bdd90b 100644 --- a/v2rayN/v2rayN/Views/MainWindow.xaml.cs +++ b/v2rayN/v2rayN/Views/MainWindow.xaml.cs @@ -80,6 +80,7 @@ public partial class MainWindow this.BindCommand(ViewModel, vm => vm.AddHysteria2ServerCmd, v => v.menuAddHysteria2Server).DisposeWith(disposables); this.BindCommand(ViewModel, vm => vm.AddTuicServerCmd, v => v.menuAddTuicServer).DisposeWith(disposables); this.BindCommand(ViewModel, vm => vm.AddWireguardServerCmd, v => v.menuAddWireguardServer).DisposeWith(disposables); + this.BindCommand(ViewModel, vm => vm.AddAnytlsServerCmd, v => v.menuAddAnytlsServer).DisposeWith(disposables); this.BindCommand(ViewModel, vm => vm.AddCustomServerCmd, v => v.menuAddCustomServer).DisposeWith(disposables); this.BindCommand(ViewModel, vm => vm.AddServerViaClipboardCmd, v => v.menuAddServerViaClipboard).DisposeWith(disposables); this.BindCommand(ViewModel, vm => vm.AddServerViaScanCmd, v => v.menuAddServerViaScan).DisposeWith(disposables); From 6e7b6aed965f36ace89bc013c2eb83e311a8f4db Mon Sep 17 00:00:00 2001 From: DHR60 Date: Thu, 10 Apr 2025 23:19:56 +0800 Subject: [PATCH 07/36] Simplifies local DNS address handling --- .../CoreConfig/CoreConfigSingboxService.cs | 40 +++++++------------ 1 file changed, 15 insertions(+), 25 deletions(-) diff --git a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs index dec0d3ff..6e87b6d8 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs @@ -1476,46 +1476,36 @@ public class CoreConfigSingboxService localDnsType = "local"; localDnsAddress = null; } - else if (localDnsAddress.StartsWith("dhcp")) + else if (localDnsAddress.StartsWith("dhcp") && localDnsAddress.Length > 7) { localDnsType = "dhcp"; - //if (localDnsAddress.Length > 7) // dhcp:// - //{ - // localDnsAddress = localDnsAddress.Substring(7); - //} + // // dhcp:// + // dhcpDnsInterface = localDnsAddress.Substring(7); localDnsAddress = null; } - else if (localDnsAddress.StartsWith("tcp")) + else if (localDnsAddress.StartsWith("tcp") && localDnsAddress.Length > 6) { localDnsType = "tcp"; - if (localDnsAddress.Length > 6) // tcp:// - { - localDnsAddress = localDnsAddress.Substring(6); - } + // tcp:// + localDnsAddress = localDnsAddress.Substring(6); } - else if (localDnsAddress.StartsWith("tls")) + else if (localDnsAddress.StartsWith("tls") && localDnsAddress.Length > 6) { localDnsType = "tls"; - if (localDnsAddress.Length > 6) // tls:// - { - localDnsAddress = localDnsAddress.Substring(6); - } + // tls:// + localDnsAddress = localDnsAddress.Substring(6); } - else if (localDnsAddress.StartsWith("https")) + else if (localDnsAddress.StartsWith("https") && localDnsAddress.Length > 8) { localDnsType = "https"; - if (localDnsAddress.Length > 8) // https:// - { - localDnsAddress = localDnsAddress.Substring(8); - } + // https:// + localDnsAddress = localDnsAddress.Substring(8); } - else if (localDnsAddress.StartsWith("quic")) + else if (localDnsAddress.StartsWith("quic") && localDnsAddress.Length > 7) { localDnsType = "quic"; - if (localDnsAddress.Length > 7) // quic:// - { - localDnsAddress = localDnsAddress.Substring(7); - } + // quic:// + localDnsAddress = localDnsAddress.Substring(7); } else { From 85a5769b96ce09a73029ae13010f3cb5596f6644 Mon Sep 17 00:00:00 2001 From: DHR60 Date: Fri, 11 Apr 2025 18:30:45 +0800 Subject: [PATCH 08/36] Enables dhcp interface configuration --- v2rayN/ServiceLib/Models/SingboxConfig.cs | 4 +++- .../Services/CoreConfig/CoreConfigSingboxService.cs | 13 +++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/v2rayN/ServiceLib/Models/SingboxConfig.cs b/v2rayN/ServiceLib/Models/SingboxConfig.cs index 1d3249c9..d5dd3038 100644 --- a/v2rayN/ServiceLib/Models/SingboxConfig.cs +++ b/v2rayN/ServiceLib/Models/SingboxConfig.cs @@ -1,3 +1,5 @@ +using System.Text.Json.Serialization; + namespace ServiceLib.Models; public class SingboxConfig @@ -227,7 +229,7 @@ public class Server4Sbox public string? type { get; set; } public string? server { get; set; } public string? server_resolver { get; set; } - //public string? interface { get; set; } + [JsonPropertyName("interface")] public string? Interface { get; set; } } public class Experimental4Sbox diff --git a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs index 6e87b6d8..99170558 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs @@ -1470,7 +1470,7 @@ public class CoreConfigSingboxService var tag = "local_local"; var localDnsAddress = string.IsNullOrEmpty(dNSItem?.DomainDNSAddress) ? Global.SingboxDomainDNSAddress.FirstOrDefault() : dNSItem?.DomainDNSAddress; string? localDnsType = null; - //string? dhcpDnsInterface = null; + string? dhcpDnsInterface = null; if (localDnsAddress == "local") { localDnsType = "local"; @@ -1479,8 +1479,12 @@ public class CoreConfigSingboxService else if (localDnsAddress.StartsWith("dhcp") && localDnsAddress.Length > 7) { localDnsType = "dhcp"; - // // dhcp:// - // dhcpDnsInterface = localDnsAddress.Substring(7); + // dhcp:// + dhcpDnsInterface = localDnsAddress.Substring(7); + if (dhcpDnsInterface == "auto") + { + dhcpDnsInterface = null; + } localDnsAddress = null; } else if (localDnsAddress.StartsWith("tcp") && localDnsAddress.Length > 6) @@ -1516,7 +1520,8 @@ public class CoreConfigSingboxService { tag = tag, type = localDnsType, - server = localDnsAddress + server = localDnsAddress, + Interface = dhcpDnsInterface }); dns4Sbox.rules.Insert(0, new() { From d57ffbba669a3e0635569dfe3e4dbf299fe97757 Mon Sep 17 00:00:00 2001 From: DHR60 Date: Fri, 11 Apr 2025 19:03:52 +0800 Subject: [PATCH 09/36] Fetches DNS strategy for domain resolution --- .../Services/CoreConfig/CoreConfigSingboxService.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs index 99170558..469e7762 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs @@ -614,11 +614,11 @@ public class CoreConfigSingboxService if (Utils.IsDomain(node.Address)) { + var item = await AppHandler.Instance.GetDNSItem(ECoreType.sing_box); outbound.domain_resolver = new() { server = "local_local", - // TODO - //strategy = string.IsNullOrEmpty(dNSItem?.DomainStrategy4Freedom) ? null : dNSItem?.DomainStrategy4Freedom + strategy = string.IsNullOrEmpty(item?.DomainStrategy4Freedom) ? null : item?.DomainStrategy4Freedom }; } From 98d3ba38cb4935a6df884c8ecd95e3f466d19939 Mon Sep 17 00:00:00 2001 From: DHR60 Date: Tue, 8 Apr 2025 21:23:01 +0800 Subject: [PATCH 10/36] support Wireguard endpoint Refactors Singbox config classes for dial fields --- v2rayN/ServiceLib/Models/SingboxConfig.cs | 55 ++-- .../CoreConfig/CoreConfigSingboxService.cs | 236 ++++++++++++++---- 2 files changed, 221 insertions(+), 70 deletions(-) diff --git a/v2rayN/ServiceLib/Models/SingboxConfig.cs b/v2rayN/ServiceLib/Models/SingboxConfig.cs index d5dd3038..6d464b2c 100644 --- a/v2rayN/ServiceLib/Models/SingboxConfig.cs +++ b/v2rayN/ServiceLib/Models/SingboxConfig.cs @@ -8,6 +8,7 @@ public class SingboxConfig public Dns4Sbox? dns { get; set; } public List inbounds { get; set; } public List outbounds { get; set; } + public List? endpoints { get; set; } public Route4Sbox route { get; set; } public Experimental4Sbox? experimental { get; set; } } @@ -96,10 +97,8 @@ public class User4Sbox public string password { get; set; } } -public class Outbound4Sbox +public class Outbound4Sbox : BaseServer4Sbox { - public string type { get; set; } - public string tag { get; set; } public string? server { get; set; } public int? server_port { get; set; } public List? server_ports { get; set; } @@ -114,7 +113,6 @@ public class Outbound4Sbox public int? recv_window_conn { get; set; } public int? recv_window { get; set; } public bool? disable_mtu_discovery { get; set; } - public string? detour { get; set; } public string? method { get; set; } public string? username { get; set; } public string? password { get; set; } @@ -122,26 +120,14 @@ public class Outbound4Sbox public string? version { get; set; } public string? network { get; set; } public string? packet_encoding { get; set; } - public List? local_address { get; set; } - public string? private_key { get; set; } - public string? peer_public_key { get; set; } - public List? reserved { get; set; } - public int? mtu { get; set; } public string? plugin { get; set; } public string? plugin_opts { get; set; } - public Tls4Sbox? tls { get; set; } - public Multiplex4Sbox? multiplex { get; set; } - public Transport4Sbox? transport { get; set; } - 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 +public class Endpoints4Sbox : BaseServer4Sbox { - public string type { get; set; } - public string tag { get; set; } public bool? system { get; set; } public string? name { get; set; } public int? mtu { get; set; } @@ -219,14 +205,11 @@ public class HyObfs4Sbox public string? password { get; set; } } -public class Server4Sbox +public class Server4Sbox : BaseServer4Sbox { - public string? tag { get; set; } - public string? detour { get; set; } public string? inet4_range { get; set; } public string? inet6_range { get; set; } public string? client_subnet { get; set; } - public string? type { get; set; } public string? server { get; set; } public string? server_resolver { get; set; } [JsonPropertyName("interface")] public string? Interface { get; set; } @@ -277,3 +260,33 @@ public class Ruleset4Sbox public string? download_detour { get; set; } public string? update_interval { get; set; } } + +public abstract class DialFields4Sbox +{ + public string? detour { get; set; } + public string? bind_interface { get; set; } + public string? inet4_bind_address { get; set; } + public string? inet6_bind_address { get; set; } + public int? routing_mark { get; set; } + public bool? reuse_addr { get; set; } + public string? netns { get; set; } + public string? connect_timeout { get; set; } + public bool? tcp_fast_open { get; set; } + public bool? tcp_multi_path { get; set; } + public bool? udp_fragment { get; set; } + public Rule4Sbox? domain_resolver { get; set; } // or string + public string? network_strategy { get; set; } + public List? network_type { get; set; } + public List? fallback_network_type { get; set; } + public string? fallback_delay { get; set; } + public Tls4Sbox? tls { get; set; } + public Multiplex4Sbox? multiplex { get; set; } + public Transport4Sbox? transport { get; set; } + public HyObfs4Sbox? obfs { get; set; } +} + +public abstract class BaseServer4Sbox : DialFields4Sbox +{ + public string type { get; set; } + public string tag { get; set; } +} diff --git a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs index 469e7762..5b87fdcd 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs @@ -1,6 +1,7 @@ using System.Data; using System.Net; using System.Net.NetworkInformation; +using System.Reactive; using DynamicData; using ServiceLib.Models; @@ -55,7 +56,18 @@ public class CoreConfigSingboxService await GenInbounds(singboxConfig); - await GenOutbound(node, singboxConfig.outbounds.First()); + if (node.ConfigType == EConfigType.WireGuard) + { + singboxConfig.outbounds.RemoveAt(0); + var endpoints = new Endpoints4Sbox(); + await GenEndpoint(node, endpoints); + endpoints.tag = Global.ProxyTag; + singboxConfig.endpoints = new() { endpoints }; + } + else + { + await GenOutbound(node, singboxConfig.outbounds.First()); + } await GenMoreOutbounds(node, singboxConfig); @@ -204,16 +216,29 @@ public class CoreConfigSingboxService continue; } - var outbound = JsonUtils.Deserialize(txtOutbound); - await GenOutbound(item, outbound); - outbound.tag = Global.ProxyTag + inbound.listen_port.ToString(); - singboxConfig.outbounds.Add(outbound); + var server = await GenServer(item); + if (server is null) + { + ret.Msg = ResUI.FailedGenDefaultConfiguration; + return ret; + } + var tag = Global.ProxyTag + inbound.listen_port.ToString(); + server.tag = tag; + if (server is Endpoints4Sbox endpoint) + { + singboxConfig.endpoints ??= new(); + singboxConfig.endpoints.Add(endpoint); + } + else if (server is Outbound4Sbox outbound) + { + singboxConfig.outbounds.Add(outbound); + } //rule Rule4Sbox rule = new() { inbound = new List { inbound.tag }, - outbound = outbound.tag + outbound = tag }; singboxConfig.route.rules.Add(rule); } @@ -277,7 +302,18 @@ public class CoreConfigSingboxService } await GenLog(singboxConfig); - await GenOutbound(node, singboxConfig.outbounds.First()); + if (node.ConfigType == EConfigType.WireGuard) + { + singboxConfig.outbounds.RemoveAt(0); + var endpoints = new Endpoints4Sbox(); + await GenEndpoint(node, endpoints); + endpoints.tag = Global.ProxyTag; + singboxConfig.endpoints = new() { endpoints }; + } + else + { + await GenOutbound(node, singboxConfig.outbounds.First()); + } await GenMoreOutbounds(node, singboxConfig); await GenDnsDomains(null, singboxConfig, null); @@ -726,15 +762,6 @@ public class CoreConfigSingboxService outbound.congestion_control = node.HeaderType; break; } - case EConfigType.WireGuard: - { - outbound.private_key = node.Id; - outbound.peer_public_key = node.PublicKey; - outbound.reserved = Utils.String2List(node.Path)?.Select(int.Parse).ToList(); - outbound.local_address = Utils.String2List(node.RequestHost); - outbound.mtu = node.ShortId.IsNullOrEmpty() ? Global.TunMtus.First() : node.ShortId.ToInt(); - break; - } case EConfigType.Anytls: { outbound.password = node.Id; @@ -753,6 +780,76 @@ public class CoreConfigSingboxService return 0; } + private async Task GenEndpoint(ProfileItem node, Endpoints4Sbox endpoint) + { + try + { + endpoint.address = Utils.String2List(node.RequestHost); + // Utils.GetFreePort() 9090 ? + endpoint.listen_port = Utils.GetFreePort(); + endpoint.type = Global.ProtocolTypes[node.ConfigType]; + + if (Utils.IsDomain(node.Address)) + { + var item = await AppHandler.Instance.GetDNSItem(ECoreType.sing_box); + endpoint.domain_resolver = new() + { + server = "local_local", + strategy = string.IsNullOrEmpty(item?.DomainStrategy4Freedom) ? null : item?.DomainStrategy4Freedom + }; + } + + switch (node.ConfigType) + { + case EConfigType.WireGuard: + { + var peer = new Peer4Sbox + { + public_key = node.PublicKey, + reserved = Utils.String2List(node.Path)?.Select(int.Parse).ToList(), + address = node.Address, + port = node.Port, + // TODO default ["0.0.0.0/0", "::/0"] + allowed_ips = new() { "0.0.0.0/0", "::/0" }, + }; + endpoint.private_key = node.Id; + endpoint.mtu = node.ShortId.IsNullOrEmpty() ? Global.TunMtus.First() : node.ShortId.ToInt(); + break; + } + } + } + catch (Exception ex) + { + Logging.SaveLog(_tag, ex); + } + return await Task.FromResult(0); + } + + private async Task GenServer(ProfileItem node) + { + try + { + var txtOutbound = EmbedUtils.GetEmbedText(Global.SingboxSampleOutbound); + if (node.ConfigType == EConfigType.WireGuard) + { + var endpoint = JsonUtils.Deserialize(txtOutbound); + await GenEndpoint(node, endpoint); + return endpoint; + } + else + { + var outbound = JsonUtils.Deserialize(txtOutbound); + await GenOutbound(node, outbound); + return outbound; + } + } + catch (Exception ex) + { + Logging.SaveLog(_tag, ex); + } + return await Task.FromResult(null); + } + private async Task GenOutboundMux(ProfileItem node, Outbound4Sbox outbound) { try @@ -919,7 +1016,8 @@ public class CoreConfigSingboxService } //current proxy - var outbound = singboxConfig.outbounds.First(); + BaseServer4Sbox? outbound = singboxConfig.endpoints?.FirstOrDefault(t => t.tag == Global.ProxyTag) == null ? singboxConfig.outbounds.First() : null; + var txtOutbound = EmbedUtils.GetEmbedText(Global.SingboxSampleOutbound); //Previous proxy @@ -928,17 +1026,32 @@ public class CoreConfigSingboxService if (prevNode is not null && prevNode.ConfigType != EConfigType.Custom) { - var prevOutbound = JsonUtils.Deserialize(txtOutbound); - await GenOutbound(prevNode, prevOutbound); prevOutboundTag = $"prev-{Global.ProxyTag}"; - prevOutbound.tag = prevOutboundTag; - singboxConfig.outbounds.Add(prevOutbound); + var prevServer = await GenServer(prevNode); + prevServer.tag = prevOutboundTag; + if (prevServer is Endpoints4Sbox endpoint) + { + singboxConfig.endpoints ??= new(); + singboxConfig.endpoints.Add(endpoint); + } + else if (prevServer is Outbound4Sbox outboundPrev) + { + singboxConfig.outbounds.Add(outboundPrev); + } } - var nextOutbound = await GenChainOutbounds(subItem, outbound, prevOutboundTag); + var nextServer = await GenChainOutbounds(subItem, outbound, prevOutboundTag); - if (nextOutbound is not null) + if (nextServer is not null) { - singboxConfig.outbounds.Insert(0, nextOutbound); + if (nextServer is Endpoints4Sbox endpoint) + { + singboxConfig.endpoints ??= new(); + singboxConfig.endpoints.Insert(0, endpoint); + } + else if (nextServer is Outbound4Sbox outboundNext) + { + singboxConfig.outbounds.Insert(0, outboundNext); + } } } catch (Exception ex) @@ -961,11 +1074,13 @@ public class CoreConfigSingboxService } var resultOutbounds = new List(); + var resultEndpoints = new List(); // For endpoints var prevOutbounds = new List(); // Separate list for prev outbounds + var prevEndpoints = new List(); // Separate list for prev endpoints var proxyTags = new List(); // For selector and urltest outbounds // Cache for chain proxies to avoid duplicate generation - var nextProxyCache = new Dictionary(); + var nextProxyCache = new Dictionary(); var prevProxyTags = new Dictionary(); // Map from profile name to tag int prevIndex = 0; // Index for prev outbounds @@ -977,19 +1092,18 @@ public class CoreConfigSingboxService // Handle proxy chain string? prevTag = null; - var currentOutbound = JsonUtils.Deserialize(txtOutbound); - var nextOutbound = nextProxyCache.GetValueOrDefault(node.Subid, null); - if (nextOutbound != null) + var currentServer = await GenServer(node); + var nextServer = nextProxyCache.GetValueOrDefault(node.Subid, null); + if (nextServer != null) { - nextOutbound = JsonUtils.DeepCopy(nextOutbound); + nextServer = JsonUtils.DeepCopy(nextServer); } var subItem = await AppHandler.Instance.GetSubItem(node.Subid); // current proxy - await GenOutbound(node, currentOutbound); - currentOutbound.tag = $"{Global.ProxyTag}-{index}"; - proxyTags.Add(currentOutbound.tag); + currentServer.tag = $"{Global.ProxyTag}-{index}"; + proxyTags.Add(currentServer.tag); if (!node.Subid.IsNullOrEmpty()) { @@ -1012,18 +1126,32 @@ public class CoreConfigSingboxService prevProxyTags[node.Subid] = prevTag; } - nextOutbound = await GenChainOutbounds(subItem, currentOutbound, prevTag, nextOutbound); + nextServer = await GenChainOutbounds(subItem, currentServer, prevTag, nextServer); if (!nextProxyCache.ContainsKey(node.Subid)) { - nextProxyCache[node.Subid] = nextOutbound; + nextProxyCache[node.Subid] = nextServer; } } - if (nextOutbound is not null) + if (nextServer is not null) { - resultOutbounds.Add(nextOutbound); + if (nextServer is Endpoints4Sbox nextEndpoint) + { + resultEndpoints.Add(nextEndpoint); + } + else if (nextServer is Outbound4Sbox nextOutbound) + { + resultOutbounds.Add(nextOutbound); + } + } + if (currentServer is Endpoints4Sbox currentEndpoint) + { + resultEndpoints.Add(currentEndpoint); + } + else if (currentServer is Outbound4Sbox currentOutbound) + { + resultOutbounds.Add(currentOutbound); } - resultOutbounds.Add(currentOutbound); } // Add urltest outbound (auto selection based on latency) @@ -1056,6 +1184,9 @@ public class CoreConfigSingboxService resultOutbounds.AddRange(prevOutbounds); resultOutbounds.AddRange(singboxConfig.outbounds); singboxConfig.outbounds = resultOutbounds; + singboxConfig.endpoints ??= new List(); + resultEndpoints.AddRange(singboxConfig.endpoints); + singboxConfig.endpoints = resultEndpoints; } catch (Exception ex) { @@ -1077,7 +1208,7 @@ public class CoreConfigSingboxService /// /// The outbound configuration for the next proxy in the chain, or null if no next proxy exists. /// - private async Task GenChainOutbounds(SubItem subItem, Outbound4Sbox outbound, string? prevOutboundTag, Outbound4Sbox? nextOutbound = null) + private async Task GenChainOutbounds(SubItem subItem, BaseServer4Sbox outbound, string? prevOutboundTag, BaseServer4Sbox? nextOutbound = null) { try { @@ -1093,11 +1224,7 @@ public class CoreConfigSingboxService if (nextNode is not null && nextNode.ConfigType != EConfigType.Custom) { - if (nextOutbound == null) - { - nextOutbound = JsonUtils.Deserialize(txtOutbound); - await GenOutbound(nextNode, nextOutbound); - } + nextOutbound ??= await GenServer(nextNode); nextOutbound.tag = outbound.tag; outbound.tag = $"mid-{outbound.tag}"; @@ -1421,13 +1548,24 @@ public class CoreConfigSingboxService return Global.ProxyTag; } - var txtOutbound = EmbedUtils.GetEmbedText(Global.SingboxSampleOutbound); - var outbound = JsonUtils.Deserialize(txtOutbound); - await GenOutbound(node, outbound); - outbound.tag = Global.ProxyTag + node.IndexId.ToString(); - singboxConfig.outbounds.Add(outbound); + var server = await GenServer(node); + if (server is null) + { + return Global.ProxyTag; + } - return outbound.tag; + server.tag = Global.ProxyTag + node.IndexId.ToString(); + if (server is Endpoints4Sbox endpoint) + { + singboxConfig.endpoints ??= new(); + singboxConfig.endpoints.Add(endpoint); + } + else if (server is Outbound4Sbox outbound) + { + singboxConfig.outbounds.Add(outbound); + } + + return server.tag; } private async Task GenDns(ProfileItem? node, SingboxConfig singboxConfig) From 6311a72387b90fc4d2904949154f856de3d74418 Mon Sep 17 00:00:00 2001 From: DHR60 Date: Fri, 11 Apr 2025 19:33:51 +0800 Subject: [PATCH 11/36] Utils.GetFreePort() default port to be zero --- v2rayN/ServiceLib/Common/Utils.cs | 4 ++-- .../Services/CoreConfig/CoreConfigSingboxService.cs | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/v2rayN/ServiceLib/Common/Utils.cs b/v2rayN/ServiceLib/Common/Utils.cs index c5b670cf..b0d3827b 100644 --- a/v2rayN/ServiceLib/Common/Utils.cs +++ b/v2rayN/ServiceLib/Common/Utils.cs @@ -435,11 +435,11 @@ public class Utils return false; } - public static int GetFreePort(int defaultPort = 9090) + public static int GetFreePort(int defaultPort = 0) { try { - if (!Utils.PortInUse(defaultPort)) + if (!(defaultPort == 0 || Utils.PortInUse(defaultPort))) { return defaultPort; } diff --git a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs index 5b87fdcd..eee6a0cd 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs @@ -785,7 +785,6 @@ public class CoreConfigSingboxService try { endpoint.address = Utils.String2List(node.RequestHost); - // Utils.GetFreePort() 9090 ? endpoint.listen_port = Utils.GetFreePort(); endpoint.type = Global.ProtocolTypes[node.ConfigType]; From 1e537aaf3f3538ff23e79463a9d569a7904d2889 Mon Sep 17 00:00:00 2001 From: DHR60 Date: Fri, 11 Apr 2025 19:59:20 +0800 Subject: [PATCH 12/36] 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 6d464b2c..e53cfd8f 100644 --- a/v2rayN/ServiceLib/Models/SingboxConfig.cs +++ b/v2rayN/ServiceLib/Models/SingboxConfig.cs @@ -213,6 +213,12 @@ public class Server4Sbox : BaseServer4Sbox 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 eee6a0cd..0b00a194 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs @@ -1589,7 +1589,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) { @@ -1687,6 +1694,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) From 032ebded8a7e5b9307e8b7e47c4cfbb1efb8a943 Mon Sep 17 00:00:00 2001 From: DHR60 Date: Fri, 11 Apr 2025 20:10:49 +0800 Subject: [PATCH 13/36] Adds IPv4 preference to DNS configurations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 对应原dns.servers[].strategy = prefer_ipv4 --- v2rayN/ServiceLib/Sample/dns_singbox_normal | 9 ++++++--- v2rayN/ServiceLib/Sample/tun_singbox_dns | 9 ++++++--- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/v2rayN/ServiceLib/Sample/dns_singbox_normal b/v2rayN/ServiceLib/Sample/dns_singbox_normal index 69367e86..b32b439c 100644 --- a/v2rayN/ServiceLib/Sample/dns_singbox_normal +++ b/v2rayN/ServiceLib/Sample/dns_singbox_normal @@ -18,14 +18,17 @@ "googleapis.cn", "gstatic.com" ], - "server": "remote" + "server": "remote", + "strategy": "prefer_ipv4" }, { "rule_set": [ "geosite-cn" ], - "server": "local" + "server": "local", + "strategy": "prefer_ipv4" } ], - "final": "remote" + "final": "remote", + "strategy": "prefer_ipv4" } diff --git a/v2rayN/ServiceLib/Sample/tun_singbox_dns b/v2rayN/ServiceLib/Sample/tun_singbox_dns index 530267b0..39bf43ac 100644 --- a/v2rayN/ServiceLib/Sample/tun_singbox_dns +++ b/v2rayN/ServiceLib/Sample/tun_singbox_dns @@ -18,15 +18,18 @@ "googleapis.cn", "gstatic.com" ], - "server": "remote" + "server": "remote", + "strategy": "prefer_ipv4" }, { "rule_set": [ "geosite-cn", "geosite-geolocation-cn" ], - "server": "local" + "server": "local", + "strategy": "prefer_ipv4" } ], - "final": "remote" + "final": "remote", + "strategy": "prefer_ipv4" } From aca6e2b5b9ff23bcee075ed4add406830ebed1de Mon Sep 17 00:00:00 2001 From: DHR60 Date: Fri, 11 Apr 2025 20:55:15 +0800 Subject: [PATCH 14/36] Refactors DNS address parsing --- .../CoreConfig/CoreConfigSingboxService.cs | 67 ++++++++++--------- 1 file changed, 36 insertions(+), 31 deletions(-) diff --git a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs index 0b00a194..f0bbc44e 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs @@ -1615,45 +1615,50 @@ public class CoreConfigSingboxService var localDnsAddress = string.IsNullOrEmpty(dNSItem?.DomainDNSAddress) ? Global.SingboxDomainDNSAddress.FirstOrDefault() : dNSItem?.DomainDNSAddress; string? localDnsType = null; string? dhcpDnsInterface = null; + var dnsProtocols = new List + { + "dhcp", + "https", + "tcp", + "tls", + "quic", + "h3", + "udp" + }; if (localDnsAddress == "local") { localDnsType = "local"; localDnsAddress = null; } - else if (localDnsAddress.StartsWith("dhcp") && localDnsAddress.Length > 7) + else if (dnsProtocols.Any(protocol => localDnsAddress.StartsWith(protocol))) { - localDnsType = "dhcp"; - // dhcp:// - dhcpDnsInterface = localDnsAddress.Substring(7); - if (dhcpDnsInterface == "auto") + var protocol = dnsProtocols.First(p => localDnsAddress.StartsWith(p)); + localDnsType = protocol; + // +3 for "://" + if (localDnsAddress.Length > protocol.Length + 3) { - dhcpDnsInterface = null; + localDnsAddress = localDnsAddress.Substring(protocol.Length + 3); + if (protocol == "dhcp") + { + dhcpDnsInterface = localDnsAddress; + if (dhcpDnsInterface == "auto") + { + dhcpDnsInterface = null; + } + localDnsAddress = null; + } + else if (protocol is "https" or "h3") + { + if (localDnsAddress.Contains('/')) + { + localDnsAddress = localDnsAddress.Substring(0, localDnsAddress.IndexOf('/')); + } + } + } + else + { + localDnsAddress = null; } - localDnsAddress = null; - } - else if (localDnsAddress.StartsWith("tcp") && localDnsAddress.Length > 6) - { - localDnsType = "tcp"; - // tcp:// - localDnsAddress = localDnsAddress.Substring(6); - } - else if (localDnsAddress.StartsWith("tls") && localDnsAddress.Length > 6) - { - localDnsType = "tls"; - // tls:// - localDnsAddress = localDnsAddress.Substring(6); - } - else if (localDnsAddress.StartsWith("https") && localDnsAddress.Length > 8) - { - localDnsType = "https"; - // https:// - localDnsAddress = localDnsAddress.Substring(8); - } - else if (localDnsAddress.StartsWith("quic") && localDnsAddress.Length > 7) - { - localDnsType = "quic"; - // quic:// - localDnsAddress = localDnsAddress.Substring(7); } else { From e1a2e9c6317fe512416cb1c047ebbbbe6af6998b Mon Sep 17 00:00:00 2001 From: DHR60 Date: Sat, 12 Apr 2025 12:05:47 +0800 Subject: [PATCH 15/36] Fixes config generation --- .../Services/CoreConfig/CoreConfigSingboxService.cs | 1 + .../Services/CoreConfig/CoreConfigV2rayService.cs | 6 ++++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs index f0bbc44e..5aa05b3b 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs @@ -813,6 +813,7 @@ public class CoreConfigSingboxService }; endpoint.private_key = node.Id; endpoint.mtu = node.ShortId.IsNullOrEmpty() ? Global.TunMtus.First() : node.ShortId.ToInt(); + endpoint.peers = new() { peer }; break; } } diff --git a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigV2rayService.cs b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigV2rayService.cs index 97e8e6aa..d4db6d8e 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigV2rayService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigV2rayService.cs @@ -1425,7 +1425,8 @@ public class CoreConfigV2rayService if (prevNode is not null && prevNode.ConfigType != EConfigType.Custom && prevNode.ConfigType != EConfigType.Hysteria2 - && prevNode.ConfigType != EConfigType.TUIC) + && prevNode.ConfigType != EConfigType.TUIC + && prevNode.ConfigType != EConfigType.Anytls) { var prevOutbound = JsonUtils.Deserialize(txtOutbound); await GenOutbound(prevNode, prevOutbound); @@ -1494,7 +1495,8 @@ public class CoreConfigV2rayService if (nextNode is not null && nextNode.ConfigType != EConfigType.Custom && nextNode.ConfigType != EConfigType.Hysteria2 - && nextNode.ConfigType != EConfigType.TUIC) + && nextNode.ConfigType != EConfigType.TUIC + && nextNode.ConfigType != EConfigType.Anytls) { if (nextOutbound == null) { From aa5b7abbe38d396e9a9a5a6e74635fe07e2a5c53 Mon Sep 17 00:00:00 2001 From: DHR60 Date: Sat, 12 Apr 2025 13:37:51 +0800 Subject: [PATCH 16/36] fix singbox endpoints proxy chain not work --- v2rayN/ServiceLib/Models/SingboxConfig.cs | 1 + .../ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs | 2 ++ 2 files changed, 3 insertions(+) diff --git a/v2rayN/ServiceLib/Models/SingboxConfig.cs b/v2rayN/ServiceLib/Models/SingboxConfig.cs index e53cfd8f..f9657901 100644 --- a/v2rayN/ServiceLib/Models/SingboxConfig.cs +++ b/v2rayN/ServiceLib/Models/SingboxConfig.cs @@ -39,6 +39,7 @@ public class Route4Sbox public bool? auto_detect_interface { get; set; } public List rules { get; set; } public List? rule_set { get; set; } + public string? final { get; set; } } [Serializable] diff --git a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs index 5aa05b3b..bd623c40 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs @@ -1243,6 +1243,8 @@ public class CoreConfigSingboxService { try { + singboxConfig.route.final = Global.ProxyTag; + if (_config.TunModeItem.EnableTun) { singboxConfig.route.auto_detect_interface = true; From a725ad80be64c2f2cd9335aec4bfca3c8b4849db Mon Sep 17 00:00:00 2001 From: DHR60 Date: Mon, 5 May 2025 16:27:41 +0800 Subject: [PATCH 17/36] Fixes wrong field --- v2rayN/ServiceLib/Models/SingboxConfig.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/v2rayN/ServiceLib/Models/SingboxConfig.cs b/v2rayN/ServiceLib/Models/SingboxConfig.cs index f9657901..7c279b0f 100644 --- a/v2rayN/ServiceLib/Models/SingboxConfig.cs +++ b/v2rayN/ServiceLib/Models/SingboxConfig.cs @@ -212,7 +212,7 @@ public class Server4Sbox : BaseServer4Sbox public string? inet6_range { get; set; } public string? client_subnet { get; set; } public string? server { get; set; } - public string? server_resolver { get; set; } + public new string? domain_resolver { get; set; } [JsonPropertyName("interface")] public string? Interface { get; set; } // Deprecated public string? address { get; set; } From ab17487f891dd8817a45aff3ebf91fe11bb05b8b Mon Sep 17 00:00:00 2001 From: DHR60 Date: Mon, 5 May 2025 16:41:27 +0800 Subject: [PATCH 18/36] Removes direct clash_mode domain strategy --- .../ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs index bd623c40..898b7698 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs @@ -1678,8 +1678,7 @@ public class CoreConfigSingboxService dns4Sbox.rules.Insert(0, new() { server = tag, - clash_mode = ERuleMode.Direct.ToString(), - strategy = string.IsNullOrEmpty(dNSItem?.DomainStrategy4Freedom) ? null : dNSItem?.DomainStrategy4Freedom + clash_mode = ERuleMode.Direct.ToString() }); dns4Sbox.rules.Insert(0, new() { From 9214f74bda763d4903fb33357f83f5d90da607ce Mon Sep 17 00:00:00 2001 From: DHR60 Date: Mon, 5 May 2025 17:03:22 +0800 Subject: [PATCH 19/36] Improves DNS address parsing in Singbox DNS type, host, port, and path --- v2rayN/ServiceLib/Models/SingboxConfig.cs | 3 + .../CoreConfig/CoreConfigSingboxService.cs | 147 +++++++++++------- 2 files changed, 96 insertions(+), 54 deletions(-) diff --git a/v2rayN/ServiceLib/Models/SingboxConfig.cs b/v2rayN/ServiceLib/Models/SingboxConfig.cs index 7c279b0f..8b087894 100644 --- a/v2rayN/ServiceLib/Models/SingboxConfig.cs +++ b/v2rayN/ServiceLib/Models/SingboxConfig.cs @@ -214,6 +214,9 @@ public class Server4Sbox : BaseServer4Sbox public string? server { get; set; } public new string? domain_resolver { get; set; } [JsonPropertyName("interface")] public string? Interface { get; set; } + public int? server_port { get; set; } + public string? path { get; set; } + public Headers4Sbox? headers { get; set; } // Deprecated public string? address { get; set; } public string? address_resolver { get; set; } diff --git a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs index 898b7698..76af2a64 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs @@ -1616,65 +1616,19 @@ public class CoreConfigSingboxService var tag = "local_local"; var localDnsAddress = string.IsNullOrEmpty(dNSItem?.DomainDNSAddress) ? Global.SingboxDomainDNSAddress.FirstOrDefault() : dNSItem?.DomainDNSAddress; - string? localDnsType = null; - string? dhcpDnsInterface = null; - var dnsProtocols = new List - { - "dhcp", - "https", - "tcp", - "tls", - "quic", - "h3", - "udp" - }; - if (localDnsAddress == "local") - { - localDnsType = "local"; - localDnsAddress = null; - } - else if (dnsProtocols.Any(protocol => localDnsAddress.StartsWith(protocol))) - { - var protocol = dnsProtocols.First(p => localDnsAddress.StartsWith(p)); - localDnsType = protocol; - // +3 for "://" - if (localDnsAddress.Length > protocol.Length + 3) - { - localDnsAddress = localDnsAddress.Substring(protocol.Length + 3); - if (protocol == "dhcp") - { - dhcpDnsInterface = localDnsAddress; - if (dhcpDnsInterface == "auto") - { - dhcpDnsInterface = null; - } - localDnsAddress = null; - } - else if (protocol is "https" or "h3") - { - if (localDnsAddress.Contains('/')) - { - localDnsAddress = localDnsAddress.Substring(0, localDnsAddress.IndexOf('/')); - } - } - } - else - { - localDnsAddress = null; - } - } - else - { - localDnsType = "udp"; - } + + var (dnsType, dnsHost, dnsPort, dnsPath) = ParseDnsAddress(localDnsAddress); dns4Sbox.servers.Add(new() { tag = tag, - type = localDnsType, - server = localDnsAddress, - Interface = dhcpDnsInterface + type = dnsType, + server = dnsHost, + Interface = dnsType == "dhcp" ? dnsHost : null, + server_port = dnsPort, + path = dnsPath }); + dns4Sbox.rules.Insert(0, new() { server = tag, @@ -1754,6 +1708,91 @@ public class CoreConfigSingboxService return await Task.FromResult(0); } + private (string type, string? host, int? port, string? path) ParseDnsAddress(string address) + { + string type = "udp"; + string? host = null; + int? port = null; + string? path = null; + + if (address is "local" or "localhost") + { + return ("local", null, null, null); + } + + if (address.StartsWith("dhcp://", StringComparison.OrdinalIgnoreCase)) + { + string interface_name = address.Substring(7); + return ("dhcp", interface_name == "auto" ? null : interface_name, null, null); + } + + if (!address.Contains("://")) + { + // udp dns + host = address; + return (type, host, port, path); + } + + try + { + int protocolEndIndex = address.IndexOf("://", StringComparison.Ordinal); + type = address.Substring(0, protocolEndIndex).ToLower(); + + var uri = new Uri(address); + host = uri.Host; + + if (!uri.IsDefaultPort) + { + port = uri.Port; + } + + if ((type == "https" || type == "h3") && !string.IsNullOrEmpty(uri.AbsolutePath) && uri.AbsolutePath != "/") + { + path = uri.AbsolutePath; + } + } + catch (UriFormatException) + { + int protocolEndIndex = address.IndexOf("://", StringComparison.Ordinal); + if (protocolEndIndex > 0) + { + type = address.Substring(0, protocolEndIndex).ToLower(); + string remaining = address.Substring(protocolEndIndex + 3); + + int portIndex = remaining.IndexOf(':'); + int pathIndex = remaining.IndexOf('/'); + + if (portIndex > 0) + { + host = remaining.Substring(0, portIndex); + string portPart = pathIndex > portIndex + ? remaining.Substring(portIndex + 1, pathIndex - portIndex - 1) + : remaining.Substring(portIndex + 1); + + if (int.TryParse(portPart, out int parsedPort)) + { + port = parsedPort; + } + } + else if (pathIndex > 0) + { + host = remaining.Substring(0, pathIndex); + } + else + { + host = remaining; + } + + if (pathIndex > 0 && (type == "https" || type == "h3")) + { + path = remaining.Substring(pathIndex); + } + } + } + + return (type, host, port, path); + } + private async Task GenExperimental(SingboxConfig singboxConfig) { //if (_config.guiItem.enableStatistics) From 1c4d13ffed3ebaafeb11cf42c4c93aac027bdd38 Mon Sep 17 00:00:00 2001 From: DHR60 Date: Mon, 5 May 2025 18:21:40 +0800 Subject: [PATCH 20/36] Adds properties to Rule4Sbox class --- v2rayN/ServiceLib/Models/SingboxConfig.cs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/v2rayN/ServiceLib/Models/SingboxConfig.cs b/v2rayN/ServiceLib/Models/SingboxConfig.cs index 8b087894..93393476 100644 --- a/v2rayN/ServiceLib/Models/SingboxConfig.cs +++ b/v2rayN/ServiceLib/Models/SingboxConfig.cs @@ -52,6 +52,7 @@ public class Rule4Sbox public string? mode { get; set; } public bool? ip_is_private { get; set; } public string? client_subnet { get; set; } + public int? rewrite_ttl { get; set; } public bool? invert { get; set; } public string? clash_mode { get; set; } public List? inbound { get; set; } @@ -73,6 +74,24 @@ public class Rule4Sbox public string? action { get; set; } public string? strategy { get; set; } public List? sniffer { get; set; } + public string? rcode { get; set; } + public List? query_type { get; set; } + public List? answer { get; set; } + public List? ns { get; set; } + public List? extra { get; set; } + public string? method { get; set; } + public bool? no_drop { get; set; } + public bool? source_ip_is_private { get; set; } + public bool? ip_accept_any { get; set; } + public int? source_port { get; set; } + public List? source_port_range { get; set; } + public List? network_type { get; set; } + public bool? network_is_expensive { get; set; } + public bool? network_is_constrained { get; set; } + public List? wifi_ssid { get; set; } + public List? wifi_bssid { get; set; } + public bool? rule_set_ip_cidr_match_source { get; set; } + public bool? rule_set_ip_cidr_accept_empty { get; set; } } [Serializable] From 09250ae30480289fa76509c653c9639c8c638cf9 Mon Sep 17 00:00:00 2001 From: DHR60 Date: Wed, 28 May 2025 20:39:20 +0800 Subject: [PATCH 21/36] Removes Wireguard listen port --- v2rayN/ServiceLib/Models/SingboxConfig.cs | 2 +- .../ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/v2rayN/ServiceLib/Models/SingboxConfig.cs b/v2rayN/ServiceLib/Models/SingboxConfig.cs index 93393476..e8166482 100644 --- a/v2rayN/ServiceLib/Models/SingboxConfig.cs +++ b/v2rayN/ServiceLib/Models/SingboxConfig.cs @@ -153,7 +153,7 @@ public class Endpoints4Sbox : BaseServer4Sbox public int? mtu { get; set; } public List address { get; set; } public string private_key { get; set; } - public int listen_port { get; set; } + public int? listen_port { get; set; } public string? udp_timeout { get; set; } public int? workers { get; set; } public List peers { get; set; } diff --git a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs index 76af2a64..0fe12b8d 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs @@ -785,7 +785,6 @@ public class CoreConfigSingboxService try { endpoint.address = Utils.String2List(node.RequestHost); - endpoint.listen_port = Utils.GetFreePort(); endpoint.type = Global.ProtocolTypes[node.ConfigType]; if (Utils.IsDomain(node.Address)) From c50f0cfc26938ac62a1ba903d5e9a76e85542132 Mon Sep 17 00:00:00 2001 From: DHR60 Date: Tue, 17 Jun 2025 18:52:12 +0800 Subject: [PATCH 22/36] Support sing-box hosts --- v2rayN/ServiceLib/Models/SingboxConfig.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/v2rayN/ServiceLib/Models/SingboxConfig.cs b/v2rayN/ServiceLib/Models/SingboxConfig.cs index e8166482..c63acc4a 100644 --- a/v2rayN/ServiceLib/Models/SingboxConfig.cs +++ b/v2rayN/ServiceLib/Models/SingboxConfig.cs @@ -236,6 +236,8 @@ public class Server4Sbox : BaseServer4Sbox public int? server_port { get; set; } public string? path { get; set; } public Headers4Sbox? headers { get; set; } + // public List? path { get; set; } // hosts + public Dictionary? predefined { get; set; } // Deprecated public string? address { get; set; } public string? address_resolver { get; set; } From 95c32977fa81cc59a6775a07faac81eb6e038200 Mon Sep 17 00:00:00 2001 From: DHR60 Date: Tue, 17 Jun 2025 21:58:52 +0800 Subject: [PATCH 23/36] Adds tag resolver supports --- .../CoreConfig/CoreConfigSingboxService.cs | 58 +++++++++++++------ 1 file changed, 41 insertions(+), 17 deletions(-) diff --git a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs index 0fe12b8d..ea3bf117 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs @@ -651,9 +651,10 @@ public class CoreConfigSingboxService if (Utils.IsDomain(node.Address)) { var item = await AppHandler.Instance.GetDNSItem(ECoreType.sing_box); + var localDnsAddress = string.IsNullOrEmpty(item?.DomainDNSAddress) ? Global.SingboxDomainDNSAddress.FirstOrDefault() : item?.DomainDNSAddress; outbound.domain_resolver = new() { - server = "local_local", + server = localDnsAddress.StartsWith("tag://") ? localDnsAddress.Substring(6) : "local_resolver", strategy = string.IsNullOrEmpty(item?.DomainStrategy4Freedom) ? null : item?.DomainStrategy4Freedom }; } @@ -790,9 +791,10 @@ public class CoreConfigSingboxService if (Utils.IsDomain(node.Address)) { var item = await AppHandler.Instance.GetDNSItem(ECoreType.sing_box); + var localDnsAddress = string.IsNullOrEmpty(item?.DomainDNSAddress) ? Global.SingboxDomainDNSAddress.FirstOrDefault() : item?.DomainDNSAddress; endpoint.domain_resolver = new() { - server = "local_local", + server = localDnsAddress.StartsWith("tag://") ? localDnsAddress.Substring(6) : "local_resolver", strategy = string.IsNullOrEmpty(item?.DomainStrategy4Freedom) ? null : item?.DomainStrategy4Freedom }; } @@ -1613,26 +1615,48 @@ public class CoreConfigSingboxService dns4Sbox.servers ??= []; dns4Sbox.rules ??= []; - var tag = "local_local"; + var tag = "local_resolver"; var localDnsAddress = string.IsNullOrEmpty(dNSItem?.DomainDNSAddress) ? Global.SingboxDomainDNSAddress.FirstOrDefault() : dNSItem?.DomainDNSAddress; - var (dnsType, dnsHost, dnsPort, dnsPath) = ParseDnsAddress(localDnsAddress); - - dns4Sbox.servers.Add(new() + if (localDnsAddress.StartsWith("tag://")) { - tag = tag, - type = dnsType, - server = dnsHost, - Interface = dnsType == "dhcp" ? dnsHost : null, - server_port = dnsPort, - path = dnsPath - }); + tag = localDnsAddress.Substring(6); - dns4Sbox.rules.Insert(0, new() + var localDnsTag = "local_local"; + + dns4Sbox.servers.Add(new() + { + tag = localDnsTag, + type = "local" + }); + + dns4Sbox.rules.Insert(0, new() + { + server = localDnsTag, + clash_mode = ERuleMode.Direct.ToString() + }); + } + else { - server = tag, - clash_mode = ERuleMode.Direct.ToString() - }); + var (dnsType, dnsHost, dnsPort, dnsPath) = ParseDnsAddress(localDnsAddress); + + dns4Sbox.servers.Add(new() + { + tag = tag, + type = dnsType, + server = dnsHost, + Interface = dnsType == "dhcp" ? dnsHost : null, + server_port = dnsPort, + path = dnsPath + }); + + 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", From 29a5c18214dce81f1f961fc5dc375d7193655078 Mon Sep 17 00:00:00 2001 From: DHR60 Date: Tue, 1 Jul 2025 21:45:01 +0800 Subject: [PATCH 24/36] Adds sing-box DomainStrategy support --- .../CoreConfig/CoreConfigSingboxService.cs | 38 +++++++++++++------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs index ea3bf117..d03288b6 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs @@ -1304,22 +1304,24 @@ public class CoreConfigSingboxService clash_mode = ERuleMode.Global.ToString() }); - if (!(_config.Inbound.First().RouteOnly || _config.TunModeItem.EnableTun)) + var domainStrategy = _config.RoutingBasicItem.DomainStrategy4Singbox.IsNullOrEmpty() ? null : _config.RoutingBasicItem.DomainStrategy4Singbox; + var defaultRouting = await ConfigHandler.GetDefaultRouting(_config); + if (defaultRouting.DomainStrategy4Singbox.IsNotEmpty()) { - var domainStrategy = _config.RoutingBasicItem.DomainStrategy4Singbox.IsNullOrEmpty() ? null : _config.RoutingBasicItem.DomainStrategy4Singbox; - var defaultRouting = await ConfigHandler.GetDefaultRouting(_config); - if (defaultRouting.DomainStrategy4Singbox.IsNotEmpty()) - { - domainStrategy = defaultRouting.DomainStrategy4Singbox; - } - singboxConfig.route.rules.Add(new() - { - action = "resolve", - strategy = domainStrategy - }); + domainStrategy = defaultRouting.DomainStrategy4Singbox; + } + var resolveRule = new Rule4Sbox + { + action = "resolve", + strategy = domainStrategy + }; + if (_config.RoutingBasicItem.DomainStrategy == "IPOnDemand") + { + singboxConfig.route.rules.Add(resolveRule); } var routing = await ConfigHandler.GetDefaultRouting(_config); + var ipRules = new List(); if (routing != null) { var rules = JsonUtils.Deserialize>(routing.RuleSet); @@ -1328,9 +1330,21 @@ public class CoreConfigSingboxService if (item.Enabled) { await GenRoutingUserRule(item, singboxConfig); + if (item.Ip != null && item.Ip.Count > 0) + { + ipRules.Add(item); + } } } } + if (_config.RoutingBasicItem.DomainStrategy == "IPIfNonMatch") + { + singboxConfig.route.rules.Add(resolveRule); + foreach (var item in ipRules) + { + await GenRoutingUserRule(item, singboxConfig); + } + } } catch (Exception ex) { From 9d1e9f4ef094f763e2ce2c47f08f627cfeee5b38 Mon Sep 17 00:00:00 2001 From: DHR60 Date: Tue, 1 Jul 2025 23:56:45 +0800 Subject: [PATCH 25/36] Deletes Duplicate Rules --- v2rayN/ServiceLib/Sample/tun_singbox_dns | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/v2rayN/ServiceLib/Sample/tun_singbox_dns b/v2rayN/ServiceLib/Sample/tun_singbox_dns index 39bf43ac..3dd55eef 100644 --- a/v2rayN/ServiceLib/Sample/tun_singbox_dns +++ b/v2rayN/ServiceLib/Sample/tun_singbox_dns @@ -23,8 +23,7 @@ }, { "rule_set": [ - "geosite-cn", - "geosite-geolocation-cn" + "geosite-cn" ], "server": "local", "strategy": "prefer_ipv4" @@ -32,4 +31,4 @@ ], "final": "remote", "strategy": "prefer_ipv4" -} +} \ No newline at end of file From 2b32f3705704bc1d3f6799a86ec4c5025e75017a Mon Sep 17 00:00:00 2001 From: DHR60 Date: Mon, 7 Jul 2025 11:38:06 +0800 Subject: [PATCH 26/36] Adds anytls reality support --- v2rayN/ServiceLib/Handler/Fmt/AnytlsFmt.cs | 9 ++------- v2rayN/v2rayN.Desktop/Views/AddServerWindow.axaml.cs | 1 + v2rayN/v2rayN/Views/AddServerWindow.xaml.cs | 1 + 3 files changed, 4 insertions(+), 7 deletions(-) diff --git a/v2rayN/ServiceLib/Handler/Fmt/AnytlsFmt.cs b/v2rayN/ServiceLib/Handler/Fmt/AnytlsFmt.cs index bad36b19..3f1d40dc 100644 --- a/v2rayN/ServiceLib/Handler/Fmt/AnytlsFmt.cs +++ b/v2rayN/ServiceLib/Handler/Fmt/AnytlsFmt.cs @@ -24,8 +24,7 @@ public class AnytlsFmt : BaseFmt item.Id = rawUserInfo; var query = Utils.ParseQueryString(parsedUrl.Query); - item.Sni = query["sni"] ?? Global.None; - item.AllowInsecure = (query["insecure"] ?? "") == "1" ? "true" : "false"; + _ = ResolveStdTransport(query, ref item); return item; } @@ -43,11 +42,7 @@ public class AnytlsFmt : BaseFmt } var pw = item.Id; var dicQuery = new Dictionary(); - if (item.Sni.IsNotEmpty()) - { - dicQuery.Add("sni", item.Sni); - } - dicQuery.Add("insecure", item.AllowInsecure.ToLower() == "true" ? "1" : "0"); + _ = GetStdTransport(item, Global.None, ref dicQuery); return ToUri(EConfigType.Anytls, item.Address, item.Port, pw, dicQuery, remark); } diff --git a/v2rayN/v2rayN.Desktop/Views/AddServerWindow.axaml.cs b/v2rayN/v2rayN.Desktop/Views/AddServerWindow.axaml.cs index dbb9d85b..1eef0404 100644 --- a/v2rayN/v2rayN.Desktop/Views/AddServerWindow.axaml.cs +++ b/v2rayN/v2rayN.Desktop/Views/AddServerWindow.axaml.cs @@ -105,6 +105,7 @@ public partial class AddServerWindow : WindowBase case EConfigType.Anytls: gridAnytls.IsVisible = true; + cmbStreamSecurity.Items.Add(Global.StreamSecurityReality); cmbCoreType.IsEnabled = false; break; } diff --git a/v2rayN/v2rayN/Views/AddServerWindow.xaml.cs b/v2rayN/v2rayN/Views/AddServerWindow.xaml.cs index 4fdbbdac..4d58a2ac 100644 --- a/v2rayN/v2rayN/Views/AddServerWindow.xaml.cs +++ b/v2rayN/v2rayN/Views/AddServerWindow.xaml.cs @@ -99,6 +99,7 @@ public partial class AddServerWindow case EConfigType.Anytls: gridAnytls.Visibility = Visibility.Visible; cmbCoreType.IsEnabled = false; + cmbStreamSecurity.Items.Add(Global.StreamSecurityReality); break; } cmbStreamSecurity.ItemsSource = lstStreamSecurity; From dff67e9d7811bd81bcd14ff7ab85adb3260effa3 Mon Sep 17 00:00:00 2001 From: DHR60 Date: Thu, 10 Jul 2025 21:42:53 +0800 Subject: [PATCH 27/36] Simplify DNS Settings --- v2rayN/ServiceLib/Global.cs | 52 ++- v2rayN/ServiceLib/Handler/AppHandler.cs | 10 - v2rayN/ServiceLib/Handler/ConfigHandler.cs | 116 +---- v2rayN/ServiceLib/Models/Config.cs | 1 + v2rayN/ServiceLib/Models/ConfigItems.cs | 17 + v2rayN/ServiceLib/Models/DNSItem.cs | 19 - v2rayN/ServiceLib/Models/SingboxConfig.cs | 11 +- v2rayN/ServiceLib/Models/V2rayConfig.cs | 5 +- v2rayN/ServiceLib/Resx/ResUI.Designer.cs | 164 +++++++- v2rayN/ServiceLib/Resx/ResUI.fa-Ir.resx | 54 +++ v2rayN/ServiceLib/Resx/ResUI.hu.resx | 54 +++ v2rayN/ServiceLib/Resx/ResUI.resx | 54 +++ v2rayN/ServiceLib/Resx/ResUI.ru.resx | 114 +++-- v2rayN/ServiceLib/Resx/ResUI.zh-Hans.resx | 54 +++ v2rayN/ServiceLib/Resx/ResUI.zh-Hant.resx | 54 +++ .../CoreConfig/CoreConfigSingboxService.cs | 395 ++++++++++-------- .../CoreConfig/CoreConfigV2rayService.cs | 278 ++++++++---- .../ViewModels/DNSSettingViewModel.cs | 138 +++--- .../ViewModels/MainWindowViewModel.cs | 1 - .../Views/DNSSettingWindow.axaml | 321 ++++++++------ .../Views/DNSSettingWindow.axaml.cs | 32 +- v2rayN/v2rayN/Views/DNSSettingWindow.xaml | 368 ++++++++++------ v2rayN/v2rayN/Views/DNSSettingWindow.xaml.cs | 32 +- 23 files changed, 1532 insertions(+), 812 deletions(-) delete mode 100644 v2rayN/ServiceLib/Models/DNSItem.cs diff --git a/v2rayN/ServiceLib/Global.cs b/v2rayN/ServiceLib/Global.cs index 4948f111..a667a335 100644 --- a/v2rayN/ServiceLib/Global.cs +++ b/v2rayN/ServiceLib/Global.cs @@ -349,25 +349,42 @@ public class Global public static readonly List SingboxDomainStrategy4Out = [ - "ipv4_only", + "", + "ipv4_only", "prefer_ipv4", "prefer_ipv6", - "ipv6_only", - "" + "ipv6_only" ]; - public static readonly List DomainDNSAddress = + public static readonly List DomainDirectDNSAddress = [ - "223.5.5.5", - "223.6.6.6", + "https://dns.alidns.com/dns-query", + "https://doh.pub/dns-query", + "223.5.5.5", + "119.29.29.29", "localhost" ]; - public static readonly List SingboxDomainDNSAddress = + public static readonly List DomainRemoteDNSAddress = + [ + "https://cloudflare-dns.com/dns-query", + "https://dns.cloudflare.com/dns-query", + "https://dns.google/dns-query", + "https://doh.dns.sb/dns-query", + "https://doh.opendns.com/dns-query", + "https://common.dot.dns.yandex.net", + "8.8.8.8", + "1.1.1.1", + "185.222.222.222", + "208.67.222.222", + "77.88.8.8" + ]; + + public static readonly List DomainPureIPDNSAddress = [ "223.5.5.5", - "223.6.6.6", - "dhcp://auto" + "119.29.29.29", + "localhost" ]; public static readonly List Languages = @@ -537,5 +554,22 @@ public class Global BlockTag ]; + public static readonly Dictionary> PredefinedHosts = new() + { + { "dns.google", new List { "8.8.8.8", "8.8.4.4", "2001:4860:4860::8888", "2001:4860:4860::8844" } }, + { "dns.alidns.com", new List { "223.5.5.5", "223.6.6.6", "2400:3200::1", "2400:3200:baba::1" } }, + { "one.one.one.one", new List { "1.1.1.1", "1.0.0.1", "2606:4700:4700::1111", "2606:4700:4700::1001" } }, + { "1dot1dot1dot1.cloudflare-dns.com", new List { "1.1.1.1", "1.0.0.1", "2606:4700:4700::1111", "2606:4700:4700::1001" } }, + { "cloudflare-dns.com", new List { "104.16.249.249", "104.16.248.249", "2606:4700::6810:f8f9", "2606:4700::6810:f9f9" } }, + { "dns.cloudflare.com", new List { "104.16.132.229", "104.16.133.229", "2606:4700::6810:84e5", "2606:4700::6810:85e5" } }, + { "dot.pub", new List { "1.12.12.12", "120.53.53.53" } }, + { "dns.quad9.net", new List { "9.9.9.9", "149.112.112.112", "2620:fe::fe", "2620:fe::9" } }, + { "dns.yandex.net", new List { "77.88.8.8", "77.88.8.1", "2a02:6b8::feed:0ff", "2a02:6b8:0:1::feed:0ff" } }, + { "dns.sb", new List { "185.222.222.222", "2a09::" } }, + { "dns.umbrella.com", new List { "208.67.220.220", "208.67.222.222", "2620:119:35::35", "2620:119:53::53" } }, + { "dns.sse.cisco.com", new List { "208.67.220.220", "208.67.222.222", "2620:119:35::35", "2620:119:53::53" } }, + { "engage.cloudflareclient.com", new List { "162.159.192.1", "2606:4700:d0::a29f:c001" } } + }; + #endregion const } diff --git a/v2rayN/ServiceLib/Handler/AppHandler.cs b/v2rayN/ServiceLib/Handler/AppHandler.cs index ad9a4029..496173f3 100644 --- a/v2rayN/ServiceLib/Handler/AppHandler.cs +++ b/v2rayN/ServiceLib/Handler/AppHandler.cs @@ -193,16 +193,6 @@ public sealed class AppHandler return await SQLiteHelper.Instance.TableAsync().FirstOrDefaultAsync(it => it.Id == id); } - public async Task?> DNSItems() - { - return await SQLiteHelper.Instance.TableAsync().ToListAsync(); - } - - public async Task GetDNSItem(ECoreType eCoreType) - { - return await SQLiteHelper.Instance.TableAsync().FirstOrDefaultAsync(it => it.CoreType == eCoreType); - } - #endregion SqliteHelper #region Core Type diff --git a/v2rayN/ServiceLib/Handler/ConfigHandler.cs b/v2rayN/ServiceLib/Handler/ConfigHandler.cs index 6005e0c6..3667d7d2 100644 --- a/v2rayN/ServiceLib/Handler/ConfigHandler.cs +++ b/v2rayN/ServiceLib/Handler/ConfigHandler.cs @@ -112,6 +112,18 @@ public class ConfigHandler config.ConstItem ??= new ConstItem(); + config.DNSItem ??= new DNSItem() + { + UseSystemHosts = false, + AddCommonHosts = true, + FakeIP = false, + BlockBindingQuery = true, + DirectDNS = Global.DomainDirectDNSAddress.FirstOrDefault(), + RemoteDNS = Global.DomainRemoteDNSAddress.FirstOrDefault(), + SingboxOutboundsResolveDNS = Global.DomainDirectDNSAddress.FirstOrDefault(), + SingboxFinalResolveDNS = Global.DomainPureIPDNSAddress.FirstOrDefault() + }; + config.SpeedTestItem ??= new(); if (config.SpeedTestItem.SpeedTestTimeout < 10) { @@ -2088,101 +2100,6 @@ public class ConfigHandler #endregion Routing - #region DNS - - /// - /// Initialize built-in DNS configurations - /// Creates default DNS items for V2Ray and sing-box - /// - /// Current configuration - /// 0 if successful - public static async Task InitBuiltinDNS(Config config) - { - var items = await AppHandler.Instance.DNSItems(); - if (items.Count <= 0) - { - var item = new DNSItem() - { - Remarks = "V2ray", - CoreType = ECoreType.Xray, - }; - await SaveDNSItems(config, item); - - var item2 = new DNSItem() - { - Remarks = "sing-box", - CoreType = ECoreType.sing_box, - }; - await SaveDNSItems(config, item2); - } - - return 0; - } - - /// - /// Save a DNS item to the database - /// - /// Current configuration - /// DNS item to save - /// 0 if successful, -1 if failed - public static async Task SaveDNSItems(Config config, DNSItem item) - { - if (item == null) - { - return -1; - } - - if (item.Id.IsNullOrEmpty()) - { - item.Id = Utils.GetGuid(false); - } - - if (await SQLiteHelper.Instance.ReplaceAsync(item) > 0) - { - return 0; - } - else - { - return -1; - } - } - - /// - /// Get an external DNS configuration from URL - /// Downloads and processes DNS templates - /// - /// Core type (Xray or sing-box) - /// URL of the DNS template - /// DNS item with configuration from the URL - public static async Task GetExternalDNSItem(ECoreType type, string url) - { - var currentItem = await AppHandler.Instance.GetDNSItem(type); - - var downloadHandle = new DownloadService(); - var templateContent = await downloadHandle.TryDownloadString(url, true, ""); - if (templateContent.IsNullOrEmpty()) - return currentItem; - - var template = JsonUtils.Deserialize(templateContent); - if (template == null) - return currentItem; - - if (!template.NormalDNS.IsNullOrEmpty()) - template.NormalDNS = await downloadHandle.TryDownloadString(template.NormalDNS, true, ""); - - if (!template.TunDNS.IsNullOrEmpty()) - template.TunDNS = await downloadHandle.TryDownloadString(template.TunDNS, true, ""); - - template.Id = currentItem.Id; - template.Enabled = currentItem.Enabled; - template.Remarks = currentItem.Remarks; - template.CoreType = type; - - return template; - } - - #endregion DNS - #region Regional Presets /// @@ -2202,7 +2119,6 @@ public class ConfigHandler config.ConstItem.RouteRulesTemplateSourceUrl = ""; await SQLiteHelper.Instance.DeleteAllAsync(); - await InitBuiltinDNS(config); return true; @@ -2211,8 +2127,8 @@ public class ConfigHandler config.ConstItem.SrsSourceUrl = Global.SingboxRulesetSources[1]; config.ConstItem.RouteRulesTemplateSourceUrl = Global.RoutingRulesSources[1]; - await SaveDNSItems(config, await GetExternalDNSItem(ECoreType.Xray, Global.DNSTemplateSources[1] + "v2ray.json")); - await SaveDNSItems(config, await GetExternalDNSItem(ECoreType.sing_box, Global.DNSTemplateSources[1] + "sing_box.json")); + //await SaveDNSItems(config, await GetExternalDNSItem(ECoreType.Xray, Global.DNSTemplateSources[1] + "v2ray.json")); + //await SaveDNSItems(config, await GetExternalDNSItem(ECoreType.sing_box, Global.DNSTemplateSources[1] + "sing_box.json")); return true; @@ -2221,8 +2137,8 @@ public class ConfigHandler config.ConstItem.SrsSourceUrl = Global.SingboxRulesetSources[2]; config.ConstItem.RouteRulesTemplateSourceUrl = Global.RoutingRulesSources[2]; - await SaveDNSItems(config, await GetExternalDNSItem(ECoreType.Xray, Global.DNSTemplateSources[2] + "v2ray.json")); - await SaveDNSItems(config, await GetExternalDNSItem(ECoreType.sing_box, Global.DNSTemplateSources[2] + "sing_box.json")); + //await SaveDNSItems(config, await GetExternalDNSItem(ECoreType.Xray, Global.DNSTemplateSources[2] + "v2ray.json")); + //await SaveDNSItems(config, await GetExternalDNSItem(ECoreType.sing_box, Global.DNSTemplateSources[2] + "sing_box.json")); return true; } diff --git a/v2rayN/ServiceLib/Models/Config.cs b/v2rayN/ServiceLib/Models/Config.cs index 15996608..ca032c2e 100644 --- a/v2rayN/ServiceLib/Models/Config.cs +++ b/v2rayN/ServiceLib/Models/Config.cs @@ -48,6 +48,7 @@ public class Config public List Inbound { get; set; } public List GlobalHotkeys { get; set; } public List CoreTypeItem { get; set; } + public DNSItem DNSItem { get; set; } #endregion other entities } diff --git a/v2rayN/ServiceLib/Models/ConfigItems.cs b/v2rayN/ServiceLib/Models/ConfigItems.cs index c6254b9f..8847782b 100644 --- a/v2rayN/ServiceLib/Models/ConfigItems.cs +++ b/v2rayN/ServiceLib/Models/ConfigItems.cs @@ -253,3 +253,20 @@ public class WindowSizeItem public int Width { get; set; } public int Height { get; set; } } + +[Serializable] +public class DNSItem +{ + public bool? UseSystemHosts { get; set; } + public bool? AddCommonHosts { get; set; } + public bool? FakeIP { get; set; } + public bool? BlockBindingQuery { get; set; } + public string? DirectDNS { get; set; } + public string? RemoteDNS { get; set; } + public string? SingboxOutboundsResolveDNS { get; set; } + public string? SingboxFinalResolveDNS { get; set; } + public string? RayStrategy4Freedom { get; set; } + public string? SingboxStrategy4Direct { get; set; } + public string? SingboxStrategy4Proxy { get; set; } + public string? Hosts { get; set; } +} diff --git a/v2rayN/ServiceLib/Models/DNSItem.cs b/v2rayN/ServiceLib/Models/DNSItem.cs deleted file mode 100644 index 59ab9c9b..00000000 --- a/v2rayN/ServiceLib/Models/DNSItem.cs +++ /dev/null @@ -1,19 +0,0 @@ -using SQLite; - -namespace ServiceLib.Models; - -[Serializable] -public class DNSItem -{ - [PrimaryKey] - public string Id { get; set; } - - public string Remarks { get; set; } - public bool Enabled { get; set; } = true; - public ECoreType CoreType { get; set; } - public bool UseSystemHosts { get; set; } - public string? NormalDNS { get; set; } - public string? TunDNS { get; set; } - public string? DomainStrategy4Freedom { get; set; } - public string? DomainDNSAddress { get; set; } -} diff --git a/v2rayN/ServiceLib/Models/SingboxConfig.cs b/v2rayN/ServiceLib/Models/SingboxConfig.cs index c63acc4a..5fd91408 100644 --- a/v2rayN/ServiceLib/Models/SingboxConfig.cs +++ b/v2rayN/ServiceLib/Models/SingboxConfig.cs @@ -36,6 +36,7 @@ public class Dns4Sbox public class Route4Sbox { + public Rule4Sbox? default_domain_resolver { get; set; } // or string public bool? auto_detect_interface { get; set; } public List rules { get; set; } public List? rule_set { get; set; } @@ -75,7 +76,7 @@ public class Rule4Sbox public string? strategy { get; set; } public List? sniffer { get; set; } public string? rcode { get; set; } - public List? query_type { get; set; } + public List? query_type { get; set; } public List? answer { get; set; } public List? ns { get; set; } public List? extra { get; set; } @@ -237,13 +238,7 @@ public class Server4Sbox : BaseServer4Sbox public string? path { get; set; } public Headers4Sbox? headers { get; set; } // public List? path { get; set; } // hosts - public Dictionary? predefined { 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 Dictionary>? predefined { get; set; } } public class Experimental4Sbox diff --git a/v2rayN/ServiceLib/Models/V2rayConfig.cs b/v2rayN/ServiceLib/Models/V2rayConfig.cs index 2e5ed4b4..a18c7e34 100644 --- a/v2rayN/ServiceLib/Models/V2rayConfig.cs +++ b/v2rayN/ServiceLib/Models/V2rayConfig.cs @@ -5,7 +5,7 @@ namespace ServiceLib.Models; public class V2rayConfig { public Log4Ray log { get; set; } - public object dns { get; set; } + public Dns4Ray dns { get; set; } public List inbounds { get; set; } public List outbounds { get; set; } public Routing4Ray routing { get; set; } @@ -203,7 +203,8 @@ public class Response4Ray public class Dns4Ray { - public List servers { get; set; } + public Dictionary>? hosts { get; set; } + public List servers { get; set; } } public class DnsServer4Ray diff --git a/v2rayN/ServiceLib/Resx/ResUI.Designer.cs b/v2rayN/ServiceLib/Resx/ResUI.Designer.cs index b7d28383..589ff0a5 100644 --- a/v2rayN/ServiceLib/Resx/ResUI.Designer.cs +++ b/v2rayN/ServiceLib/Resx/ResUI.Designer.cs @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // 此代码由工具生成。 // 运行时版本:4.0.30319.42000 @@ -2211,6 +2211,15 @@ namespace ServiceLib.Resx { } } + /// + /// 查找类似 Add Common DNS Hosts 的本地化字符串。 + /// + public static string TbAddCommonDNSHosts { + get { + return ResourceManager.GetString("TbAddCommonDNSHosts", resourceCulture); + } + } + /// /// 查找类似 Address 的本地化字符串。 /// @@ -2247,6 +2256,15 @@ namespace ServiceLib.Resx { } } + /// + /// 查找类似 Apply to Proxy Domains Only 的本地化字符串。 + /// + public static string TbApplyProxyDomainsOnly { + get { + return ResourceManager.GetString("TbApplyProxyDomainsOnly", resourceCulture); + } + } + /// /// 查找类似 Auto refresh 的本地化字符串。 /// @@ -2274,6 +2292,15 @@ namespace ServiceLib.Resx { } } + /// + /// 查找类似 Block SVCB and HTTPS Queries 的本地化字符串。 + /// + public static string TbBlockSVCBHTTPSQueries { + get { + return ResourceManager.GetString("TbBlockSVCBHTTPSQueries", resourceCulture); + } + } + /// /// 查找类似 Browse 的本地化字符串。 /// @@ -2346,6 +2373,15 @@ namespace ServiceLib.Resx { } } + /// + /// 查找类似 DNS Hosts: ("domain1 ip1 ip2" per line) 的本地化字符串。 + /// + public static string TbDNSHostsConfig { + get { + return ResourceManager.GetString("TbDNSHostsConfig", resourceCulture); + } + } + /// /// 查找类似 Supports DNS Object; Click to view documentation 的本地化字符串。 /// @@ -2391,6 +2427,15 @@ namespace ServiceLib.Resx { } } + /// + /// 查找类似 Domestic DNS 的本地化字符串。 + /// + public static string TbDomesticDNS { + get { + return ResourceManager.GetString("TbDomesticDNS", resourceCulture); + } + } + /// /// 查找类似 Edit 的本地化字符串。 /// @@ -2409,6 +2454,15 @@ namespace ServiceLib.Resx { } } + /// + /// 查找类似 FakeIP 的本地化字符串。 + /// + public static string TbFakeIP { + get { + return ResourceManager.GetString("TbFakeIP", resourceCulture); + } + } + /// /// 查找类似 Fingerprint 的本地化字符串。 /// @@ -2598,6 +2652,15 @@ namespace ServiceLib.Resx { } } + /// + /// 查找类似 Prevent DNS Leaks 的本地化字符串。 + /// + public static string TbPreventDNSLeaks { + get { + return ResourceManager.GetString("TbPreventDNSLeaks", resourceCulture); + } + } + /// /// 查找类似 Private Key 的本地化字符串。 /// @@ -2634,6 +2697,15 @@ namespace ServiceLib.Resx { } } + /// + /// 查找类似 Remote DNS 的本地化字符串。 + /// + public static string TbRemoteDNS { + get { + return ResourceManager.GetString("TbRemoteDNS", resourceCulture); + } + } + /// /// 查找类似 Camouflage domain(host) 的本地化字符串。 /// @@ -2742,6 +2814,69 @@ namespace ServiceLib.Resx { } } + /// + /// 查找类似 sing-box Direct Resolution Strategy 的本地化字符串。 + /// + public static string TbSBDirectResolveStrategy { + get { + return ResourceManager.GetString("TbSBDirectResolveStrategy", resourceCulture); + } + } + + /// + /// 查找类似 Enable to Override sing-box DoH Resolver 的本地化字符串。 + /// + public static string TbSBDoHOverride { + get { + return ResourceManager.GetString("TbSBDoHOverride", resourceCulture); + } + } + + /// + /// 查找类似 sing-box DoH Resolver Server 的本地化字符串。 + /// + public static string TbSBDoHResolverServer { + get { + return ResourceManager.GetString("TbSBDoHResolverServer", resourceCulture); + } + } + + /// + /// 查找类似 Fallback DNS Resolution, Suggest IP 的本地化字符串。 + /// + public static string TbSBFallbackDNSResolve { + get { + return ResourceManager.GetString("TbSBFallbackDNSResolve", resourceCulture); + } + } + + /// + /// 查找类似 Resolve Outbound Domains 的本地化字符串。 + /// + public static string TbSBOutboundDomainResolve { + get { + return ResourceManager.GetString("TbSBOutboundDomainResolve", resourceCulture); + } + } + + /// + /// 查找类似 Outbound DNS Resolution (sing-box) 的本地化字符串。 + /// + public static string TbSBOutboundsResolverDNS { + get { + return ResourceManager.GetString("TbSBOutboundsResolverDNS", resourceCulture); + } + } + + /// + /// 查找类似 sing-box Remote Resolution Strategy 的本地化字符串。 + /// + public static string TbSBRemoteResolveStrategy { + get { + return ResourceManager.GetString("TbSBRemoteResolveStrategy", resourceCulture); + } + } + /// /// 查找类似 Encryption method (security) 的本地化字符串。 /// @@ -3687,6 +3822,15 @@ namespace ServiceLib.Resx { } } + /// + /// 查找类似 xray Freedom Resolution Strategy 的本地化字符串。 + /// + public static string TbXrayFreedomResolveStrategy { + get { + return ResourceManager.GetString("TbXrayFreedomResolveStrategy", resourceCulture); + } + } + /// /// 查找类似 The delay: {0} ms, {1} 的本地化字符串。 /// @@ -3696,6 +3840,24 @@ namespace ServiceLib.Resx { } } + /// + /// 查找类似 Advanced DNS Settings 的本地化字符串。 + /// + public static string ThAdvancedDNSSettings { + get { + return ResourceManager.GetString("ThAdvancedDNSSettings", resourceCulture); + } + } + + /// + /// 查找类似 Basic DNS Settings 的本地化字符串。 + /// + public static string ThBasicDNSSettings { + get { + return ResourceManager.GetString("ThBasicDNSSettings", resourceCulture); + } + } + /// /// 查找类似 Active 的本地化字符串。 /// diff --git a/v2rayN/ServiceLib/Resx/ResUI.fa-Ir.resx b/v2rayN/ServiceLib/Resx/ResUI.fa-Ir.resx index d336da09..6ebf5697 100644 --- a/v2rayN/ServiceLib/Resx/ResUI.fa-Ir.resx +++ b/v2rayN/ServiceLib/Resx/ResUI.fa-Ir.resx @@ -1398,4 +1398,58 @@ Add [Anytls] Configuration + + Remote DNS + + + Domestic DNS + + + Outbound DNS Resolution (sing-box) + + + Resolve Outbound Domains + + + sing-box DoH Resolver Server + + + Fallback DNS Resolution, Suggest IP + + + xray Freedom Resolution Strategy + + + sing-box Direct Resolution Strategy + + + sing-box Remote Resolution Strategy + + + Add Common DNS Hosts + + + Enable to Override sing-box DoH Resolver + + + FakeIP + + + Block SVCB and HTTPS Queries + + + Prevent DNS Leaks + + + DNS Hosts: ("domain1 ip1 ip2" per line) + + + Apply to Proxy Domains Only + + + Basic DNS Settings + + + Advanced DNS Settings + \ No newline at end of file diff --git a/v2rayN/ServiceLib/Resx/ResUI.hu.resx b/v2rayN/ServiceLib/Resx/ResUI.hu.resx index 48abd20c..44504ffd 100644 --- a/v2rayN/ServiceLib/Resx/ResUI.hu.resx +++ b/v2rayN/ServiceLib/Resx/ResUI.hu.resx @@ -1398,4 +1398,58 @@ Add [Anytls] Configuration + + Remote DNS + + + Domestic DNS + + + Outbound DNS Resolution (sing-box) + + + Resolve Outbound Domains + + + sing-box DoH Resolver Server + + + Fallback DNS Resolution, Suggest IP + + + xray Freedom Resolution Strategy + + + sing-box Direct Resolution Strategy + + + sing-box Remote Resolution Strategy + + + Add Common DNS Hosts + + + Enable to Override sing-box DoH Resolver + + + FakeIP + + + Block SVCB and HTTPS Queries + + + Prevent DNS Leaks + + + DNS Hosts: ("domain1 ip1 ip2" per line) + + + Apply to Proxy Domains Only + + + Basic DNS Settings + + + Advanced DNS Settings + \ No newline at end of file diff --git a/v2rayN/ServiceLib/Resx/ResUI.resx b/v2rayN/ServiceLib/Resx/ResUI.resx index 151e0998..54a24060 100644 --- a/v2rayN/ServiceLib/Resx/ResUI.resx +++ b/v2rayN/ServiceLib/Resx/ResUI.resx @@ -1398,4 +1398,58 @@ Add [Anytls] Configuration + + Remote DNS + + + Domestic DNS + + + Outbound DNS Resolution (sing-box) + + + Resolve Outbound Domains + + + sing-box DoH Resolver Server + + + Fallback DNS Resolution, Suggest IP + + + xray Freedom Resolution Strategy + + + sing-box Direct Resolution Strategy + + + sing-box Remote Resolution Strategy + + + Add Common DNS Hosts + + + Enable to Override sing-box DoH Resolver + + + FakeIP + + + Block SVCB and HTTPS Queries + + + Prevent DNS Leaks + + + DNS Hosts: ("domain1 ip1 ip2" per line) + + + Apply to Proxy Domains Only + + + Basic DNS Settings + + + Advanced DNS Settings + \ No newline at end of file diff --git a/v2rayN/ServiceLib/Resx/ResUI.ru.resx b/v2rayN/ServiceLib/Resx/ResUI.ru.resx index a0ccb5b3..1eb7ed28 100644 --- a/v2rayN/ServiceLib/Resx/ResUI.ru.resx +++ b/v2rayN/ServiceLib/Resx/ResUI.ru.resx @@ -1,17 +1,17 @@ - @@ -807,9 +807,6 @@ Вверх (U) - - Переместить вверх/вниз - Фильтр, поддерживает regex @@ -969,6 +966,9 @@ URL для тестирования скорости + + Переместить вверх/вниз + PublicKey @@ -1398,4 +1398,58 @@ Add [Anytls] Configuration + + Remote DNS + + + Domestic DNS + + + Outbound DNS Resolution (sing-box) + + + Resolve Outbound Domains + + + sing-box DoH Resolver Server + + + Fallback DNS Resolution, Suggest IP + + + xray Freedom Resolution Strategy + + + sing-box Direct Resolution Strategy + + + sing-box Remote Resolution Strategy + + + Add Common DNS Hosts + + + Enable to Override sing-box DoH Resolver + + + FakeIP + + + Block SVCB and HTTPS Queries + + + Prevent DNS Leaks + + + DNS Hosts: ("domain1 ip1 ip2" per line) + + + Apply to Proxy Domains Only + + + Basic DNS Settings + + + Advanced DNS Settings + \ No newline at end of file diff --git a/v2rayN/ServiceLib/Resx/ResUI.zh-Hans.resx b/v2rayN/ServiceLib/Resx/ResUI.zh-Hans.resx index 4d33a507..04d0b16f 100644 --- a/v2rayN/ServiceLib/Resx/ResUI.zh-Hans.resx +++ b/v2rayN/ServiceLib/Resx/ResUI.zh-Hans.resx @@ -1395,4 +1395,58 @@ 添加 [Anytls] 配置文件 + + 远程 DNS + + + 直连 DNS + + + 出站 DNS 解析 + + + 解析出站域名 + + + sing-box DoH 解析服务器 + + + 兜底解析其他 DNS 域名,建议设为 ip + + + xray freedom 解析策略 + + + sing-box 直连解析策略 + + + sing-box 远程解析策略 + + + 添加常用 DNS Hosts + + + 开启后可覆盖 sing-box DoH 解析服务器 + + + FakeIP + + + 阻止 SVCB 和 HTTPS 查询 + + + 避免 DNS 泄漏 + + + DNS Hosts:(“域名1 ip1 ip2” 一行一个) + + + 仅对代理域名生效 + + + DNS 基础设置 + + + DNS 进阶设置 + \ No newline at end of file diff --git a/v2rayN/ServiceLib/Resx/ResUI.zh-Hant.resx b/v2rayN/ServiceLib/Resx/ResUI.zh-Hant.resx index 1b7a647c..5a9e9a02 100644 --- a/v2rayN/ServiceLib/Resx/ResUI.zh-Hant.resx +++ b/v2rayN/ServiceLib/Resx/ResUI.zh-Hant.resx @@ -1395,4 +1395,58 @@ 新增 [Anytls] 設定檔 + + Remote DNS + + + Domestic DNS + + + Outbound DNS Resolution (sing-box) + + + Resolve Outbound Domains + + + sing-box DoH Resolver Server + + + Fallback DNS Resolution, Suggest IP + + + xray Freedom Resolution Strategy + + + sing-box Direct Resolution Strategy + + + sing-box Remote Resolution Strategy + + + Add Common DNS Hosts + + + Enable to Override sing-box DoH Resolver + + + FakeIP + + + Block SVCB and HTTPS Queries + + + Prevent DNS Leaks + + + DNS Hosts: ("domain1 ip1 ip2" per line) + + + Apply to Proxy Domains Only + + + Basic DNS Settings + + + Advanced DNS Settings + \ No newline at end of file diff --git a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs index d03288b6..7f29ce5d 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs @@ -1,7 +1,9 @@ +using System.Collections.Generic; using System.Data; using System.Net; using System.Net.NetworkInformation; using System.Reactive; +using System.Text; using DynamicData; using ServiceLib.Models; @@ -73,7 +75,7 @@ public class CoreConfigSingboxService await GenRouting(singboxConfig); - await GenDns(node, singboxConfig); + await GenDns(singboxConfig); await GenExperimental(singboxConfig); @@ -243,7 +245,7 @@ public class CoreConfigSingboxService singboxConfig.route.rules.Add(rule); } - await GenDnsDomains(null, singboxConfig, null); + await GenDns(singboxConfig); //var dnsServer = singboxConfig.dns?.servers.FirstOrDefault(); //if (dnsServer != null) //{ @@ -315,7 +317,7 @@ public class CoreConfigSingboxService await GenOutbound(node, singboxConfig.outbounds.First()); } await GenMoreOutbounds(node, singboxConfig); - await GenDnsDomains(null, singboxConfig, null); + await GenDns(singboxConfig); singboxConfig.route.rules.Clear(); singboxConfig.inbounds.Clear(); @@ -417,7 +419,7 @@ public class CoreConfigSingboxService } await GenOutboundsList(proxyProfiles, singboxConfig); - await GenDns(null, singboxConfig); + await GenDns(singboxConfig); await ConvertGeo2Ruleset(singboxConfig); ret.Success = true; @@ -648,17 +650,6 @@ public class CoreConfigSingboxService outbound.server_port = node.Port; outbound.type = Global.ProtocolTypes[node.ConfigType]; - if (Utils.IsDomain(node.Address)) - { - var item = await AppHandler.Instance.GetDNSItem(ECoreType.sing_box); - var localDnsAddress = string.IsNullOrEmpty(item?.DomainDNSAddress) ? Global.SingboxDomainDNSAddress.FirstOrDefault() : item?.DomainDNSAddress; - outbound.domain_resolver = new() - { - server = localDnsAddress.StartsWith("tag://") ? localDnsAddress.Substring(6) : "local_resolver", - strategy = string.IsNullOrEmpty(item?.DomainStrategy4Freedom) ? null : item?.DomainStrategy4Freedom - }; - } - switch (node.ConfigType) { case EConfigType.VMess: @@ -788,17 +779,6 @@ public class CoreConfigSingboxService endpoint.address = Utils.String2List(node.RequestHost); endpoint.type = Global.ProtocolTypes[node.ConfigType]; - if (Utils.IsDomain(node.Address)) - { - var item = await AppHandler.Instance.GetDNSItem(ECoreType.sing_box); - var localDnsAddress = string.IsNullOrEmpty(item?.DomainDNSAddress) ? Global.SingboxDomainDNSAddress.FirstOrDefault() : item?.DomainDNSAddress; - endpoint.domain_resolver = new() - { - server = localDnsAddress.StartsWith("tag://") ? localDnsAddress.Substring(6) : "local_resolver", - strategy = string.IsNullOrEmpty(item?.DomainStrategy4Freedom) ? null : item?.DomainStrategy4Freedom - }; - } - switch (node.ConfigType) { case EConfigType.WireGuard: @@ -1245,6 +1225,13 @@ public class CoreConfigSingboxService try { singboxConfig.route.final = Global.ProxyTag; + var item = _config.DNSItem; + singboxConfig.route.default_domain_resolver = new() + { + server = "outbound_resolver", + strategy = item.SingboxStrategy4Direct + } + ; if (_config.TunModeItem.EnableTun) { @@ -1325,14 +1312,14 @@ public class CoreConfigSingboxService if (routing != null) { var rules = JsonUtils.Deserialize>(routing.RuleSet); - foreach (var item in rules ?? []) + foreach (var item1 in rules ?? []) { - if (item.Enabled) + if (item1.Enabled) { - await GenRoutingUserRule(item, singboxConfig); - if (item.Ip != null && item.Ip.Count > 0) + await GenRoutingUserRule(item1, singboxConfig); + if (item1.Ip != null && item1.Ip.Count > 0) { - ipRules.Add(item); + ipRules.Add(item1); } } } @@ -1340,9 +1327,9 @@ public class CoreConfigSingboxService if (_config.RoutingBasicItem.DomainStrategy == "IPIfNonMatch") { singboxConfig.route.rules.Add(resolveRule); - foreach (var item in ipRules) + foreach (var item2 in ipRules) { - await GenRoutingUserRule(item, singboxConfig); + await GenRoutingUserRule(item2, singboxConfig); } } } @@ -1585,36 +1572,17 @@ public class CoreConfigSingboxService return server.tag; } - private async Task GenDns(ProfileItem? node, SingboxConfig singboxConfig) + private async Task GenDns(SingboxConfig singboxConfig) { try { - var item = await AppHandler.Instance.GetDNSItem(ECoreType.sing_box); - var strDNS = string.Empty; - if (_config.TunModeItem.EnableTun) - { - strDNS = string.IsNullOrEmpty(item?.TunDNS) ? EmbedUtils.GetEmbedText(Global.TunSingboxDNSFileName) : item?.TunDNS; - } - else - { - strDNS = string.IsNullOrEmpty(item?.NormalDNS) ? EmbedUtils.GetEmbedText(Global.DNSSingboxNormalFileName) : item?.NormalDNS; - } + var dNSItem = _config.DNSItem; + await GenDnsServers(singboxConfig, dNSItem); + await GenDnsRules(singboxConfig, dNSItem); - var dns4Sbox = JsonUtils.Deserialize(strDNS); - if (dns4Sbox is null) - { - return 0; - } - singboxConfig.dns = dns4Sbox; - - if (dns4Sbox.servers != null && dns4Sbox.servers.Count > 0 && dns4Sbox.servers.First().address.IsNullOrEmpty()) - { - await GenDnsDomains(node, singboxConfig, item); - } - else - { - await GenDnsDomainsLegacy(node, singboxConfig, item); - } + singboxConfig.dns ??= new Dns4Sbox(); + singboxConfig.dns.independent_cache = true; + singboxConfig.dns.final = "dns_remote"; // TODO } catch (Exception ex) { @@ -1623,211 +1591,280 @@ public class CoreConfigSingboxService return 0; } - private async Task GenDnsDomains(ProfileItem? node, SingboxConfig singboxConfig, DNSItem? dNSItem) + private async Task GenDnsServers(SingboxConfig singboxConfig, DNSItem dNSItem) { - var dns4Sbox = singboxConfig.dns ?? new(); - dns4Sbox.servers ??= []; - dns4Sbox.rules ??= []; + var finalDns = ParseDnsAddress(dNSItem.SingboxFinalResolveDNS); + finalDns.tag = "final_resolver"; - var tag = "local_resolver"; - var localDnsAddress = string.IsNullOrEmpty(dNSItem?.DomainDNSAddress) ? Global.SingboxDomainDNSAddress.FirstOrDefault() : dNSItem?.DomainDNSAddress; + var directDns = ParseDnsAddress(dNSItem.DirectDNS); + directDns.tag = "dns_direct"; + directDns.domain_resolver = "final_resolver"; - if (localDnsAddress.StartsWith("tag://")) + var remoteDns = ParseDnsAddress(dNSItem.RemoteDNS); + remoteDns.tag = "dns_remote"; + remoteDns.detour = Global.ProxyTag; + remoteDns.domain_resolver = "final_resolver"; + + var resolverDns = ParseDnsAddress(dNSItem.SingboxOutboundsResolveDNS); + resolverDns.tag = "outbound_resolver"; + resolverDns.domain_resolver = "final_resolver"; + + var hostsDns = new Server4Sbox { - tag = localDnsAddress.Substring(6); - - var localDnsTag = "local_local"; - - dns4Sbox.servers.Add(new() - { - tag = localDnsTag, - type = "local" - }); - - dns4Sbox.rules.Insert(0, new() - { - server = localDnsTag, - clash_mode = ERuleMode.Direct.ToString() - }); + tag = "dns_hosts", + type = "hosts", + }; + if (dNSItem.AddCommonHosts == true) + { + hostsDns.predefined = Global.PredefinedHosts; } - else + var userHostsMap = dNSItem.Hosts? + .Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries) + .Where(line => !string.IsNullOrWhiteSpace(line)) + .Where(line => line.Contains(' ')) + .ToDictionary( + line => + { + var parts = line.Trim().Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries); + return parts[0]; + }, + line => + { + var parts = line.Trim().Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries); + var values = parts.Skip(1).ToList(); + return values; + } + ); + + if (userHostsMap != null) { - var (dnsType, dnsHost, dnsPort, dnsPath) = ParseDnsAddress(localDnsAddress); - - dns4Sbox.servers.Add(new() + foreach (var kvp in userHostsMap) { - tag = tag, - type = dnsType, - server = dnsHost, - Interface = dnsType == "dhcp" ? dnsHost : null, - server_port = dnsPort, - path = dnsPath - }); - - dns4Sbox.rules.Insert(0, new() - { - server = tag, - clash_mode = ERuleMode.Direct.ToString() - }); + hostsDns.predefined[kvp.Key] = kvp.Value; + } } - dns4Sbox.rules.Insert(0, new() + foreach (var host in hostsDns.predefined) { - server = dns4Sbox.servers.Where(t => t.detour == Global.ProxyTag).Select(t => t.tag).FirstOrDefault() ?? "remote", - clash_mode = ERuleMode.Global.ToString() - }); - - //Tun2SocksAddress - if (_config.TunModeItem.EnableTun && node?.ConfigType == EConfigType.SOCKS && Utils.IsDomain(node?.Sni)) - { - dns4Sbox.rules.Insert(0, new() + if (finalDns.server == host.Key) { - server = tag, - domain = [node?.Sni], - strategy = string.IsNullOrEmpty(dNSItem?.DomainStrategy4Freedom) ? null : dNSItem?.DomainStrategy4Freedom - }); + finalDns.domain_resolver = "dns_hosts"; + } + if (remoteDns.server == host.Key) + { + remoteDns.domain_resolver = "dns_hosts"; + } + if (resolverDns.server == host.Key) + { + resolverDns.domain_resolver = "dns_hosts"; + } + if (directDns.server == host.Key) + { + directDns.domain_resolver = "dns_hosts"; + } + } + + singboxConfig.dns ??= new Dns4Sbox(); + singboxConfig.dns.servers ??= new List(); + singboxConfig.dns.servers.Add(remoteDns); + singboxConfig.dns.servers.Add(directDns); + singboxConfig.dns.servers.Add(finalDns); + singboxConfig.dns.servers.Add(resolverDns); + singboxConfig.dns.servers.Add(hostsDns); + + // fake ip + if (dNSItem.FakeIP == true) + { + var fakeip = new Server4Sbox + { + tag = "dns-fake", + type = "fakeip", + inet4_range = "198.18.0.0/15", + inet6_range = "fc00::/18", + }; + singboxConfig.dns.servers.Add(fakeip); } - singboxConfig.dns = dns4Sbox; return await Task.FromResult(0); } - private async Task GenDnsDomainsLegacy(ProfileItem? node, SingboxConfig singboxConfig, DNSItem? dNSItem) + private async Task GenDnsRules(SingboxConfig singboxConfig, DNSItem dNSItem) { - var dns4Sbox = singboxConfig.dns ?? new(); - dns4Sbox.servers ??= []; - dns4Sbox.rules ??= []; - - var tag = "local_local"; - dns4Sbox.servers.Add(new() + singboxConfig.dns ??= new Dns4Sbox(); + singboxConfig.dns.rules ??= new List(); + // hosts + singboxConfig.dns.rules.Add(new Rule4Sbox { - tag = tag, - address = string.IsNullOrEmpty(dNSItem?.DomainDNSAddress) ? Global.SingboxDomainDNSAddress.FirstOrDefault() : dNSItem?.DomainDNSAddress, - detour = Global.DirectTag, - strategy = string.IsNullOrEmpty(dNSItem?.DomainStrategy4Freedom) ? null : dNSItem?.DomainStrategy4Freedom, + ip_accept_any = true, + server = "dns_hosts", }); - dns4Sbox.rules.Insert(0, new() + // clash mode + singboxConfig.dns.rules.Add(new Rule4Sbox { - server = tag, + server = "dns_remote", + strategy = dNSItem.SingboxStrategy4Proxy.IsNullOrEmpty() ? null : dNSItem.SingboxStrategy4Proxy, + clash_mode = ERuleMode.Global.ToString() + }); + singboxConfig.dns.rules.Add(new Rule4Sbox + { + server = "dns_direct", + strategy = dNSItem.SingboxStrategy4Direct.IsNullOrEmpty() ? null : dNSItem.SingboxStrategy4Direct, clash_mode = ERuleMode.Direct.ToString() }); - dns4Sbox.rules.Insert(0, new() + // block binding query + singboxConfig.dns.rules.Add(new Rule4Sbox { - server = dns4Sbox.servers.Where(t => t.detour == Global.ProxyTag).Select(t => t.tag).FirstOrDefault() ?? "remote", - clash_mode = ERuleMode.Global.ToString() + query_type = new List { 64, 65 }, + action = "predefined", + rcode = "NOTIMP" }); - 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) + var routing = await ConfigHandler.GetDefaultRouting(_config); + if (routing != null) { - dns4Sbox.rules.Insert(0, new() + var rules = JsonUtils.Deserialize>(routing.RuleSet); + foreach (var item in rules ?? []) { - server = tag, - domain = lstDomain - }); + if (!item.Enabled) + { + continue; + } + if (item.Domain == null || item.Domain.Count == 0) + { + continue; + } + var rule = new Rule4Sbox(); + + var countDomain = 0; + foreach (var it in item.Domain) + { + if (ParseV2Domain(it, rule)) + { + countDomain++; + } + } + if (countDomain <= 0) + { + continue; + } + if (item.OutboundTag == Global.DirectTag) + { + rule.server = "dns_direct"; + rule.strategy = dNSItem.SingboxStrategy4Direct.IsNullOrEmpty() ? null : dNSItem.SingboxStrategy4Direct; + } + else if (item.OutboundTag == Global.ProxyTag) + { + if (dNSItem.FakeIP == true) + { + var rule4Fake = JsonUtils.DeepCopy(rule); + rule4Fake.server = "dns-fake"; + singboxConfig.dns.rules.Add(rule4Fake); + } + rule.server = "dns_remote"; + rule.strategy = dNSItem.SingboxStrategy4Proxy.IsNullOrEmpty() ? null : dNSItem.SingboxStrategy4Proxy; + } + else if (item.OutboundTag == Global.BlockTag) + { + rule.action = "predefined"; + rule.rcode = "NOERROR"; + rule.answer = new List { "A" }; + } + singboxConfig.dns.rules.Add(rule); + } } - //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 (string type, string? host, int? port, string? path) ParseDnsAddress(string address) + private static Server4Sbox? ParseDnsAddress(string address) { - string type = "udp"; - string? host = null; - int? port = null; - string? path = null; + if (string.IsNullOrEmpty(address)) + { + return null; + } + + var server = new Server4Sbox(); if (address is "local" or "localhost") { - return ("local", null, null, null); + server.type = "local"; + return server; } if (address.StartsWith("dhcp://", StringComparison.OrdinalIgnoreCase)) { - string interface_name = address.Substring(7); - return ("dhcp", interface_name == "auto" ? null : interface_name, null, null); + var interface_name = address.Substring(7); + server.type = "dhcp"; + server.Interface = interface_name == "auto" ? null : interface_name; + return server; } if (!address.Contains("://")) { // udp dns - host = address; - return (type, host, port, path); + server.type = "udp"; + server.server = address; + return server; } try { - int protocolEndIndex = address.IndexOf("://", StringComparison.Ordinal); - type = address.Substring(0, protocolEndIndex).ToLower(); + var protocolEndIndex = address.IndexOf("://", StringComparison.Ordinal); + server.type = address.Substring(0, protocolEndIndex).ToLower(); var uri = new Uri(address); - host = uri.Host; + server.server = uri.Host; if (!uri.IsDefaultPort) { - port = uri.Port; + server.server_port = uri.Port; } - if ((type == "https" || type == "h3") && !string.IsNullOrEmpty(uri.AbsolutePath) && uri.AbsolutePath != "/") + if ((server.type == "https" || server.type == "h3") && !string.IsNullOrEmpty(uri.AbsolutePath) && uri.AbsolutePath != "/") { - path = uri.AbsolutePath; + server.path = uri.AbsolutePath; } } catch (UriFormatException) { - int protocolEndIndex = address.IndexOf("://", StringComparison.Ordinal); + var protocolEndIndex = address.IndexOf("://", StringComparison.Ordinal); if (protocolEndIndex > 0) { - type = address.Substring(0, protocolEndIndex).ToLower(); - string remaining = address.Substring(protocolEndIndex + 3); + server.type = address.Substring(0, protocolEndIndex).ToLower(); + var remaining = address.Substring(protocolEndIndex + 3); - int portIndex = remaining.IndexOf(':'); - int pathIndex = remaining.IndexOf('/'); + var portIndex = remaining.IndexOf(':'); + var pathIndex = remaining.IndexOf('/'); if (portIndex > 0) { - host = remaining.Substring(0, portIndex); - string portPart = pathIndex > portIndex + server.server = remaining.Substring(0, portIndex); + var portPart = pathIndex > portIndex ? remaining.Substring(portIndex + 1, pathIndex - portIndex - 1) : remaining.Substring(portIndex + 1); - if (int.TryParse(portPart, out int parsedPort)) + if (int.TryParse(portPart, out var parsedPort)) { - port = parsedPort; + server.server_port = parsedPort; } } else if (pathIndex > 0) { - host = remaining.Substring(0, pathIndex); + server.server = remaining.Substring(0, pathIndex); } else { - host = remaining; + server.server = remaining; } - if (pathIndex > 0 && (type == "https" || type == "h3")) + if (pathIndex > 0 && (server.type == "https" || server.type == "h3")) { - path = remaining.Substring(pathIndex); + server.path = remaining.Substring(pathIndex); } } } - return (type, host, port, path); + return server; } private async Task GenExperimental(SingboxConfig singboxConfig) diff --git a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigV2rayService.cs b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigV2rayService.cs index d4db6d8e..9f3c2b65 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigV2rayService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigV2rayService.cs @@ -1135,13 +1135,8 @@ public class CoreConfigV2rayService { try { - var item = await AppHandler.Instance.GetDNSItem(ECoreType.Xray); - var normalDNS = item?.NormalDNS; - var domainStrategy4Freedom = item?.DomainStrategy4Freedom; - if (normalDNS.IsNullOrEmpty()) - { - normalDNS = EmbedUtils.GetEmbedText(Global.DNSV2rayNormalFileName); - } + var dNSItem = _config.DNSItem; + var domainStrategy4Freedom = dNSItem?.RayStrategy4Freedom; //Outbound Freedom domainStrategy if (domainStrategy4Freedom.IsNotEmpty()) @@ -1149,47 +1144,16 @@ public class CoreConfigV2rayService var outbound = v2rayConfig.outbounds.FirstOrDefault(t => t is { protocol: "freedom", tag: Global.DirectTag }); if (outbound != null) { - outbound.settings = new(); - outbound.settings.domainStrategy = domainStrategy4Freedom; - outbound.settings.userLevel = 0; - } - } - - var obj = JsonUtils.ParseJson(normalDNS); - if (obj is null) - { - List servers = []; - string[] arrDNS = normalDNS.Split(','); - foreach (string str in arrDNS) - { - servers.Add(str); - } - obj = JsonUtils.ParseJson("{}"); - obj["servers"] = JsonUtils.SerializeToNode(servers); - } - - // Append to dns settings - if (item.UseSystemHosts) - { - var systemHosts = Utils.GetSystemHosts(); - if (systemHosts.Count > 0) - { - var normalHost = obj["hosts"]; - if (normalHost != null) + outbound.settings = new() { - foreach (var host in systemHosts) - { - if (normalHost[host.Key] != null) - continue; - normalHost[host.Key] = host.Value; - } - } + domainStrategy = domainStrategy4Freedom, + userLevel = 0 + }; } } - await GenDnsDomains(node, obj, item); - - v2rayConfig.dns = obj; + await GenDnsServers(node, v2rayConfig, dNSItem); + await GenDnsHosts(v2rayConfig, dNSItem); } catch (Exception ex) { @@ -1198,54 +1162,200 @@ public class CoreConfigV2rayService return 0; } - private async Task GenDnsDomains(ProfileItem? node, JsonNode dns, DNSItem? dNSItem) + private async Task GenDnsServers(ProfileItem? node, V2rayConfig v2rayConfig, DNSItem dNSItem) { - if (node == null) + var directDomainList = new List(); + var directGeositeList = new List(); + var proxyDomainList = new List(); + var proxyGeositeList = new List(); + var routing = await ConfigHandler.GetDefaultRouting(_config); + if (routing != null) { - return 0; + var rules = JsonUtils.Deserialize>(routing.RuleSet); + foreach (var item in rules ?? []) + { + if (!item.Enabled) + { + continue; + } + if (item.Domain == null || item.Domain.Count == 0) + { + continue; + } + if (item.OutboundTag == Global.DirectTag) + { + foreach (var domain in item.Domain) + { + if (domain.StartsWith('#')) + { + continue; + } + var domain1 = domain.Replace(Global.RoutingRuleComma, ","); + if (domain1.StartsWith("geosite:")) + { + directGeositeList.Add(domain1); + } + else + { + directDomainList.Add(domain1); + } + } + } + else if (item.OutboundTag == Global.ProxyTag) + { + foreach (var domain in item.Domain) + { + if (domain.StartsWith('#')) + { + continue; + } + var domain1 = domain.Replace(Global.RoutingRuleComma, ","); + if (domain1.StartsWith("geosite:")) + { + proxyGeositeList.Add(domain1); + } + else + { + proxyDomainList.Add(domain1); + } + } + } + } } - var servers = dns["servers"]; - if (servers != null) - { - var domainList = new List(); - if (Utils.IsDomain(node.Address)) - { - domainList.Add(node.Address); - } - var subItem = await AppHandler.Instance.GetSubItem(node.Subid); - if (subItem is not null) - { - // Previous proxy - var prevNode = await AppHandler.Instance.GetProfileItemViaRemarks(subItem.PrevProfile); - if (prevNode is not null - && prevNode.ConfigType != EConfigType.Custom - && prevNode.ConfigType != EConfigType.Hysteria2 - && prevNode.ConfigType != EConfigType.TUIC - && Utils.IsDomain(prevNode.Address)) - { - domainList.Add(prevNode.Address); - } - // Next proxy - var nextNode = await AppHandler.Instance.GetProfileItemViaRemarks(subItem.NextProfile); - if (nextNode is not null - && nextNode.ConfigType != EConfigType.Custom - && nextNode.ConfigType != EConfigType.Hysteria2 - && nextNode.ConfigType != EConfigType.TUIC - && Utils.IsDomain(nextNode.Address)) + if (Utils.IsDomain(node.Address)) + { + directDomainList.Add(node.Address); + } + var subItem = await AppHandler.Instance.GetSubItem(node.Subid); + if (subItem is not null) + { + // Previous proxy + var prevNode = await AppHandler.Instance.GetProfileItemViaRemarks(subItem.PrevProfile); + if (prevNode is not null + && prevNode.ConfigType != EConfigType.Custom + && prevNode.ConfigType != EConfigType.Hysteria2 + && prevNode.ConfigType != EConfigType.TUIC + && Utils.IsDomain(prevNode.Address)) + { + directDomainList.Add(prevNode.Address); + } + + // Next proxy + var nextNode = await AppHandler.Instance.GetProfileItemViaRemarks(subItem.NextProfile); + if (nextNode is not null + && nextNode.ConfigType != EConfigType.Custom + && nextNode.ConfigType != EConfigType.Hysteria2 + && nextNode.ConfigType != EConfigType.TUIC + && Utils.IsDomain(nextNode.Address)) + { + directDomainList.Add(nextNode.Address); + } + } + + v2rayConfig.dns ??= new Dns4Ray(); + v2rayConfig.dns.servers ??= new List(); + + if (proxyDomainList.Count > 0) + { + var dnsServer = new DnsServer4Ray() + { + address = dNSItem.RemoteDNS, + skipFallback = true, + domains = proxyDomainList + }; + v2rayConfig.dns.servers.Add(JsonUtils.SerializeToNode(dnsServer)); + } + if (proxyGeositeList.Count > 0) + { + var dnsServer = new DnsServer4Ray() + { + address = dNSItem.RemoteDNS, + skipFallback = true, + domains = proxyGeositeList + }; + v2rayConfig.dns.servers.Add(JsonUtils.SerializeToNode(dnsServer)); + } + if (directDomainList.Count > 0) + { + var dnsServer = new DnsServer4Ray() + { + address = dNSItem.DirectDNS, + skipFallback = true, + domains = directDomainList + }; + v2rayConfig.dns.servers.Add(JsonUtils.SerializeToNode(dnsServer)); + } + if (directGeositeList.Count > 0) + { + var dnsServer = new DnsServer4Ray() + { + address = dNSItem.DirectDNS, + skipFallback = true, + domains = directGeositeList + }; + v2rayConfig.dns.servers.Add(JsonUtils.SerializeToNode(dnsServer)); + } + v2rayConfig.dns.servers.Add(dNSItem.RemoteDNS); // fallback DNS server + return await Task.FromResult(0); + } + + private async Task GenDnsHosts(V2rayConfig v2rayConfig, DNSItem dNSItem) + { + if (dNSItem.AddCommonHosts == false && dNSItem.UseSystemHosts == false && dNSItem.Hosts.IsNullOrEmpty()) + { + return await Task.FromResult(0); + } + v2rayConfig.dns ??= new Dns4Ray(); + v2rayConfig.dns.hosts ??= new Dictionary>(); + if (dNSItem.AddCommonHosts == true) + { + v2rayConfig.dns.hosts = Global.PredefinedHosts; + } + + if (dNSItem.UseSystemHosts == true) + { + var systemHosts = Utils.GetSystemHosts(); + if (systemHosts.Count > 0) + { + var normalHost = v2rayConfig.dns.hosts; + if (normalHost != null) { - domainList.Add(nextNode.Address); + foreach (var host in systemHosts) + { + if (normalHost[host.Key] != null) + { + continue; + } + normalHost[host.Key] = new List { host.Value }; + } } } - if (domainList.Count > 0) - { - var dnsServer = new DnsServer4Ray() + } + + var userHostsMap = dNSItem.Hosts? + .Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries) + .Where(line => !string.IsNullOrWhiteSpace(line)) + .Where(line => line.Contains(' ')) + .ToDictionary( + line => { - address = string.IsNullOrEmpty(dNSItem?.DomainDNSAddress) ? Global.DomainDNSAddress.FirstOrDefault() : dNSItem?.DomainDNSAddress, - skipFallback = true, - domains = domainList - }; - servers.AsArray().Add(JsonUtils.SerializeToNode(dnsServer)); + var parts = line.Trim().Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries); + return parts[0]; + }, + line => + { + var parts = line.Trim().Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries); + var values = parts.Skip(1).ToList(); + return values; + } + ); + + if (userHostsMap != null) + { + foreach (var kvp in userHostsMap) + { + v2rayConfig.dns.hosts[kvp.Key] = kvp.Value; } } return await Task.FromResult(0); diff --git a/v2rayN/ServiceLib/ViewModels/DNSSettingViewModel.cs b/v2rayN/ServiceLib/ViewModels/DNSSettingViewModel.cs index 1d6829b8..734d97bf 100644 --- a/v2rayN/ServiceLib/ViewModels/DNSSettingViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/DNSSettingViewModel.cs @@ -6,111 +6,81 @@ namespace ServiceLib.ViewModels; public class DNSSettingViewModel : MyReactiveObject { - [Reactive] public bool UseSystemHosts { get; set; } - [Reactive] public string DomainStrategy4Freedom { get; set; } - [Reactive] public string DomainDNSAddress { get; set; } - [Reactive] public string NormalDNS { get; set; } - - [Reactive] public string DomainStrategy4Freedom2 { get; set; } - [Reactive] public string DomainDNSAddress2 { get; set; } - [Reactive] public string NormalDNS2 { get; set; } - [Reactive] public string TunDNS2 { get; set; } + [Reactive] public bool? UseSystemHosts { get; set; } + [Reactive] public bool? AddCommonHosts { get; set; } + [Reactive] public bool? FakeIP { get; set; } + [Reactive] public bool? BlockBindingQuery { get; set; } + [Reactive] public string? DirectDNS { get; set; } + [Reactive] public string? RemoteDNS { get; set; } + [Reactive] public string? SingboxOutboundsResolveDNS { get; set; } + [Reactive] public string? SingboxFinalResolveDNS { get; set; } + [Reactive] public string? RayStrategy4Freedom { get; set; } + [Reactive] public string? SingboxStrategy4Direct { get; set; } + [Reactive] public string? SingboxStrategy4Proxy { get; set; } + [Reactive] public string? Hosts { get; set; } public ReactiveCommand SaveCmd { get; } - public ReactiveCommand ImportDefConfig4V2rayCmd { get; } - public ReactiveCommand ImportDefConfig4SingboxCmd { get; } + //public ReactiveCommand ImportDefConfig4V2rayCmd { get; } + //public ReactiveCommand ImportDefConfig4SingboxCmd { get; } public DNSSettingViewModel(Func>? updateView) { _config = AppHandler.Instance.Config; _updateView = updateView; - SaveCmd = ReactiveCommand.CreateFromTask(async () => - { - await SaveSettingAsync(); - }); + SaveCmd = ReactiveCommand.CreateFromTask(SaveSettingAsync); - ImportDefConfig4V2rayCmd = ReactiveCommand.CreateFromTask(async () => - { - NormalDNS = EmbedUtils.GetEmbedText(Global.DNSV2rayNormalFileName); - await Task.CompletedTask; - }); + //ImportDefConfig4V2rayCmd = ReactiveCommand.CreateFromTask(async () => + //{ + // NormalDNS = EmbedUtils.GetEmbedText(Global.DNSV2rayNormalFileName); + // await Task.CompletedTask; + //}); - ImportDefConfig4SingboxCmd = ReactiveCommand.CreateFromTask(async () => - { - NormalDNS2 = EmbedUtils.GetEmbedText(Global.DNSSingboxNormalFileName); - TunDNS2 = EmbedUtils.GetEmbedText(Global.TunSingboxDNSFileName); - await Task.CompletedTask; - }); + //ImportDefConfig4SingboxCmd = ReactiveCommand.CreateFromTask(async () => + //{ + // NormalDNS2 = EmbedUtils.GetEmbedText(Global.DNSSingboxNormalFileName); + // TunDNS2 = EmbedUtils.GetEmbedText(Global.TunSingboxDNSFileName); + // await Task.CompletedTask; + //}); _ = Init(); } private async Task Init() { - var item = await AppHandler.Instance.GetDNSItem(ECoreType.Xray); + _config = AppHandler.Instance.Config; + var item = _config.DNSItem; UseSystemHosts = item.UseSystemHosts; - DomainStrategy4Freedom = item?.DomainStrategy4Freedom ?? string.Empty; - DomainDNSAddress = item?.DomainDNSAddress ?? string.Empty; - NormalDNS = item?.NormalDNS ?? string.Empty; - - var item2 = await AppHandler.Instance.GetDNSItem(ECoreType.sing_box); - DomainStrategy4Freedom2 = item2?.DomainStrategy4Freedom ?? string.Empty; - DomainDNSAddress2 = item2?.DomainDNSAddress ?? string.Empty; - NormalDNS2 = item2?.NormalDNS ?? string.Empty; - TunDNS2 = item2?.TunDNS ?? string.Empty; + AddCommonHosts = item.AddCommonHosts; + FakeIP = item.FakeIP; + BlockBindingQuery = item.BlockBindingQuery; + DirectDNS = item.DirectDNS; + RemoteDNS = item.RemoteDNS; + RayStrategy4Freedom = item.RayStrategy4Freedom; + SingboxOutboundsResolveDNS = item.SingboxOutboundsResolveDNS; + SingboxFinalResolveDNS = item.SingboxFinalResolveDNS; + SingboxStrategy4Direct = item.SingboxStrategy4Direct; + SingboxStrategy4Proxy = item.SingboxStrategy4Proxy; + Hosts = item.Hosts; } private async Task SaveSettingAsync() { - if (NormalDNS.IsNotEmpty()) + _config.DNSItem.UseSystemHosts = UseSystemHosts; + _config.DNSItem.AddCommonHosts = AddCommonHosts; + _config.DNSItem.FakeIP = FakeIP; + _config.DNSItem.BlockBindingQuery = BlockBindingQuery; + _config.DNSItem.DirectDNS = DirectDNS; + _config.DNSItem.RemoteDNS = RemoteDNS; + _config.DNSItem.RayStrategy4Freedom = RayStrategy4Freedom; + _config.DNSItem.SingboxOutboundsResolveDNS = SingboxOutboundsResolveDNS; + _config.DNSItem.SingboxFinalResolveDNS = SingboxFinalResolveDNS; + _config.DNSItem.SingboxStrategy4Direct = SingboxStrategy4Direct; + _config.DNSItem.SingboxStrategy4Proxy = SingboxStrategy4Proxy; + _config.DNSItem.Hosts = Hosts; + await ConfigHandler.SaveConfig(_config); + if (_updateView != null) { - var obj = JsonUtils.ParseJson(NormalDNS); - if (obj != null && obj["servers"] != null) - { - } - else - { - if (NormalDNS.Contains('{') || NormalDNS.Contains('}')) - { - NoticeHandler.Instance.Enqueue(ResUI.FillCorrectDNSText); - return; - } - } + await _updateView(EViewAction.CloseWindow, null); } - if (NormalDNS2.IsNotEmpty()) - { - var obj2 = JsonUtils.Deserialize(NormalDNS2); - if (obj2 == null) - { - NoticeHandler.Instance.Enqueue(ResUI.FillCorrectDNSText); - return; - } - } - if (TunDNS2.IsNotEmpty()) - { - var obj2 = JsonUtils.Deserialize(TunDNS2); - if (obj2 == null) - { - NoticeHandler.Instance.Enqueue(ResUI.FillCorrectDNSText); - return; - } - } - - var item = await AppHandler.Instance.GetDNSItem(ECoreType.Xray); - item.DomainStrategy4Freedom = DomainStrategy4Freedom; - item.DomainDNSAddress = DomainDNSAddress; - item.UseSystemHosts = UseSystemHosts; - item.NormalDNS = NormalDNS; - await ConfigHandler.SaveDNSItems(_config, item); - - var item2 = await AppHandler.Instance.GetDNSItem(ECoreType.sing_box); - item2.DomainStrategy4Freedom = DomainStrategy4Freedom2; - item2.DomainDNSAddress = DomainDNSAddress2; - item2.NormalDNS = JsonUtils.Serialize(JsonUtils.ParseJson(NormalDNS2)); - item2.TunDNS = JsonUtils.Serialize(JsonUtils.ParseJson(TunDNS2)); - await ConfigHandler.SaveDNSItems(_config, item2); - - NoticeHandler.Instance.Enqueue(ResUI.OperationSuccess); - _ = _updateView?.Invoke(EViewAction.CloseWindow, null); } } diff --git a/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs b/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs index 36e20a87..22d82895 100644 --- a/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs @@ -219,7 +219,6 @@ public class MainWindowViewModel : MyReactiveObject _config.UiItem.ShowInTaskbar = true; await ConfigHandler.InitBuiltinRouting(_config); - await ConfigHandler.InitBuiltinDNS(_config); await ProfileExHandler.Instance.Init(); await CoreHandler.Instance.Init(_config, UpdateHandler); TaskHandler.Instance.RegUpdateTask(_config, UpdateTaskHandler); diff --git a/v2rayN/v2rayN.Desktop/Views/DNSSettingWindow.axaml b/v2rayN/v2rayN.Desktop/Views/DNSSettingWindow.axaml index efdf4eda..59cffdc5 100644 --- a/v2rayN/v2rayN.Desktop/Views/DNSSettingWindow.axaml +++ b/v2rayN/v2rayN.Desktop/Views/DNSSettingWindow.axaml @@ -35,147 +35,220 @@ - - - + + + + + Text="{x:Static resx:ResUI.TbDomesticDNS}" /> + - - - - - -