From 5c0c07c78fecf4965aebdd93d3bdf8761cc29652 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Wed, 10 Apr 2024 08:42:53 +0800 Subject: [PATCH] Migrate geosite & geoip to rule sets (sing-box) --- v2rayN/v2rayN/Global.cs | 1 + v2rayN/v2rayN/Handler/CoreConfigSingbox.cs | 94 +++++++++++++++++----- v2rayN/v2rayN/Handler/CoreConfigV2ray.cs | 2 +- v2rayN/v2rayN/Models/SingboxConfig.cs | 16 +++- v2rayN/v2rayN/Sample/dns_singbox_normal | 8 +- v2rayN/v2rayN/Sample/tun_singbox_dns | 8 +- 6 files changed, 96 insertions(+), 33 deletions(-) diff --git a/v2rayN/v2rayN/Global.cs b/v2rayN/v2rayN/Global.cs index 19b3cf35..3d493309 100644 --- a/v2rayN/v2rayN/Global.cs +++ b/v2rayN/v2rayN/Global.cs @@ -23,6 +23,7 @@ namespace v2rayN public const string SpeedPingTestUrl = @"https://www.google.com/generate_204"; public const string JuicityCoreUrl = "https://github.com/juicity/juicity/releases"; public const string CustomRoutingListUrl = @"https://raw.githubusercontent.com/2dust/v2rayCustomRoutingList/master/"; + public const string SingboxRulesetUrl = @"https://raw.githubusercontent.com/SagerNet/sing-{0}/rule-set/{1}.srs"; public const string PromotionUrl = @"aHR0cHM6Ly85LjIzNDQ1Ni54eXovYWJjLmh0bWw="; public const string ConfigFileName = "guiNConfig.json"; diff --git a/v2rayN/v2rayN/Handler/CoreConfigSingbox.cs b/v2rayN/v2rayN/Handler/CoreConfigSingbox.cs index 635967c7..37b0a235 100644 --- a/v2rayN/v2rayN/Handler/CoreConfigSingbox.cs +++ b/v2rayN/v2rayN/Handler/CoreConfigSingbox.cs @@ -56,6 +56,8 @@ namespace v2rayN.Handler GenStatistic(singboxConfig); + ConvertGeo2Ruleset(singboxConfig); + msg = string.Format(ResUI.SuccessfulConfiguration, ""); } catch (Exception ex) @@ -653,6 +655,7 @@ namespace v2rayN.Handler { rule.inbound = item.inboundTag; } + var rule1 = JsonUtils.DeepCopy(rule); var rule2 = JsonUtils.DeepCopy(rule); var rule3 = JsonUtils.DeepCopy(rule); @@ -662,11 +665,11 @@ namespace v2rayN.Handler var countDomain = 0; foreach (var it in item.domain) { - if (ParseV2Domain(it, rule)) countDomain++; + if (ParseV2Domain(it, rule1)) countDomain++; } if (countDomain > 0) { - rules.Add(rule); + rules.Add(rule1); hasDomainIp = true; } } @@ -756,6 +759,10 @@ namespace v2rayN.Handler { 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(); } @@ -773,28 +780,18 @@ namespace v2rayN.Handler { try { - Dns4Sbox? dns4Sbox; + var item = LazyConfig.Instance.GetDNSItem(ECoreType.sing_box); + var strDNS = string.Empty; if (_config.tunModeItem.enableTun) { - var item = LazyConfig.Instance.GetDNSItem(ECoreType.sing_box); - var tunDNS = item?.tunDNS; - if (Utils.IsNullOrEmpty(tunDNS)) - { - tunDNS = Utils.GetEmbedText(Global.TunSingboxDNSFileName); - } - dns4Sbox = JsonUtils.Deserialize(tunDNS); + strDNS = Utils.IsNullOrEmpty(item?.tunDNS) ? Utils.GetEmbedText(Global.TunSingboxDNSFileName) : item?.tunDNS; } else { - var item = LazyConfig.Instance.GetDNSItem(ECoreType.sing_box); - var normalDNS = item?.normalDNS; - if (Utils.IsNullOrEmpty(normalDNS)) - { - normalDNS = "{\"servers\":[{\"address\":\"tcp://8.8.8.8\"}]}"; - } - - dns4Sbox = JsonUtils.Deserialize(normalDNS); + strDNS = Utils.IsNullOrEmpty(item?.normalDNS) ? Utils.GetEmbedText(Global.DNSSingboxNormalFileName) : item?.normalDNS; } + + var dns4Sbox = JsonUtils.Deserialize(strDNS); if (dns4Sbox is null) { return 0; @@ -831,10 +828,10 @@ namespace v2rayN.Handler { singboxConfig.experimental = new Experimental4Sbox() { - //cache_file = new CacheFile4Sbox() - //{ - // enabled = true - //}, + cache_file = new CacheFile4Sbox() + { + enabled = true + }, //v2ray_api = new V2ray_Api4Sbox() //{ // listen = $"{Global.Loopback}:{Global.StatePort}", @@ -852,6 +849,59 @@ namespace v2rayN.Handler return 0; } + private int ConvertGeo2Ruleset(SingboxConfig singboxConfig) + { + var geosite = "geosite"; + var geoip = "geoip"; + var ruleSets = new List(); + + //convert route geosite & geoip to ruleset + foreach (var rule in singboxConfig.route.rules.Where(t => t.geosite?.Count > 0).ToList() ?? []) + { + rule.rule_set = rule?.geosite?.Select(t => $"{geosite}-{t}").ToList(); + rule.geosite = null; + ruleSets.AddRange(rule.rule_set); + } + foreach (var rule in singboxConfig.route.rules.Where(t => t.geoip?.Count > 0).ToList() ?? []) + { + rule.rule_set = rule?.geoip?.Select(t => $"{geoip}-{t}").ToList(); + rule.geoip = null; + ruleSets.AddRange(rule.rule_set); + } + + //convert dns geosite & geoip to ruleset + foreach (var rule in singboxConfig.dns?.rules.Where(t => t.geosite?.Count > 0).ToList() ?? []) + { + rule.rule_set = rule?.geosite?.Select(t => $"{geosite}-{t}").ToList(); + rule.geosite = null; + } + foreach (var rule in singboxConfig.dns?.rules.Where(t => t.geoip?.Count > 0).ToList() ?? []) + { + rule.rule_set = rule?.geoip?.Select(t => $"{geoip}-{t}").ToList(); + rule.geoip = null; + } + foreach (var dnsRule in singboxConfig.dns?.rules.Where(t => t.rule_set?.Count > 0).ToList() ?? []) + { + ruleSets.AddRange(dnsRule.rule_set); + } + + //Add ruleset srs + singboxConfig.route.rule_set = []; + foreach (var item in new HashSet(ruleSets)) + { + singboxConfig.route.rule_set.Add(new() + { + type = "remote", + format = "binary", + tag = item, + url = string.Format(Global.SingboxRulesetUrl, item.StartsWith(geosite) ? geosite : geoip, item), + download_detour = Global.ProxyTag + }); + } + + return 0; + } + #endregion private gen function #region Gen speedtest config diff --git a/v2rayN/v2rayN/Handler/CoreConfigV2ray.cs b/v2rayN/v2rayN/Handler/CoreConfigV2ray.cs index 92252850..64e8e4da 100644 --- a/v2rayN/v2rayN/Handler/CoreConfigV2ray.cs +++ b/v2rayN/v2rayN/Handler/CoreConfigV2ray.cs @@ -730,7 +730,7 @@ namespace v2rayN.Handler var domainStrategy4Freedom = item?.domainStrategy4Freedom; if (Utils.IsNullOrEmpty(normalDNS)) { - normalDNS = "1.1.1.1,8.8.8.8"; + normalDNS = Utils.GetEmbedText(Global.DNSV2rayNormalFileName); } //Outbound Freedom domainStrategy diff --git a/v2rayN/v2rayN/Models/SingboxConfig.cs b/v2rayN/v2rayN/Models/SingboxConfig.cs index 476dcaef..013a883b 100644 --- a/v2rayN/v2rayN/Models/SingboxConfig.cs +++ b/v2rayN/v2rayN/Models/SingboxConfig.cs @@ -35,6 +35,7 @@ { public bool? auto_detect_interface { get; set; } public List rules { get; set; } + public List? rule_set { get; set; } } [Serializable] @@ -48,6 +49,7 @@ public string type { get; set; } public string mode { get; set; } public string network { get; set; } + public bool? ip_is_private { get; set; } public List? port { get; set; } public List? port_range { get; set; } public List? geosite { get; set; } @@ -58,8 +60,8 @@ public List? geoip { get; set; } public List? ip_cidr { get; set; } public List? source_ip_cidr { get; set; } - - public List? process_name { get; set; } + public List? process_name { get; set; } + public List? rule_set { get; set; } } [Serializable] @@ -230,4 +232,14 @@ public string? cache_id { get; set; } public bool? store_fakeip { get; set; } } + + public class Ruleset4Sbox + { + public string? tag { get; set; } + public string? type { get; set; } + public string? format { get; set; } + public string? url { get; set; } + public string? download_detour { get; set; } + } + } \ No newline at end of file diff --git a/v2rayN/v2rayN/Sample/dns_singbox_normal b/v2rayN/v2rayN/Sample/dns_singbox_normal index 0aa14b1f..d8c5c74e 100644 --- a/v2rayN/v2rayN/Sample/dns_singbox_normal +++ b/v2rayN/v2rayN/Sample/dns_singbox_normal @@ -19,14 +19,14 @@ ], "rules": [ { - "geosite": [ - "geolocation-!cn" + "rule_set": [ + "geosite-geolocation-!cn" ], "server": "remote" }, { - "geosite": [ - "category-ads-all" + "rule_set": [ + "geosite-category-ads-all" ], "server": "block" } diff --git a/v2rayN/v2rayN/Sample/tun_singbox_dns b/v2rayN/v2rayN/Sample/tun_singbox_dns index 0aa14b1f..d8c5c74e 100644 --- a/v2rayN/v2rayN/Sample/tun_singbox_dns +++ b/v2rayN/v2rayN/Sample/tun_singbox_dns @@ -19,14 +19,14 @@ ], "rules": [ { - "geosite": [ - "geolocation-!cn" + "rule_set": [ + "geosite-geolocation-!cn" ], "server": "remote" }, { - "geosite": [ - "category-ads-all" + "rule_set": [ + "geosite-category-ads-all" ], "server": "block" }