Migrate geosite & geoip to rule sets (sing-box)

pull/4991/head
2dust 8 months ago
parent bba93a0fb7
commit 5c0c07c78f

@ -23,6 +23,7 @@ namespace v2rayN
public const string SpeedPingTestUrl = @"https://www.google.com/generate_204"; public const string SpeedPingTestUrl = @"https://www.google.com/generate_204";
public const string JuicityCoreUrl = "https://github.com/juicity/juicity/releases"; public const string JuicityCoreUrl = "https://github.com/juicity/juicity/releases";
public const string CustomRoutingListUrl = @"https://raw.githubusercontent.com/2dust/v2rayCustomRoutingList/master/"; 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 PromotionUrl = @"aHR0cHM6Ly85LjIzNDQ1Ni54eXovYWJjLmh0bWw=";
public const string ConfigFileName = "guiNConfig.json"; public const string ConfigFileName = "guiNConfig.json";

@ -56,6 +56,8 @@ namespace v2rayN.Handler
GenStatistic(singboxConfig); GenStatistic(singboxConfig);
ConvertGeo2Ruleset(singboxConfig);
msg = string.Format(ResUI.SuccessfulConfiguration, ""); msg = string.Format(ResUI.SuccessfulConfiguration, "");
} }
catch (Exception ex) catch (Exception ex)
@ -653,6 +655,7 @@ namespace v2rayN.Handler
{ {
rule.inbound = item.inboundTag; rule.inbound = item.inboundTag;
} }
var rule1 = JsonUtils.DeepCopy(rule);
var rule2 = JsonUtils.DeepCopy(rule); var rule2 = JsonUtils.DeepCopy(rule);
var rule3 = JsonUtils.DeepCopy(rule); var rule3 = JsonUtils.DeepCopy(rule);
@ -662,11 +665,11 @@ namespace v2rayN.Handler
var countDomain = 0; var countDomain = 0;
foreach (var it in item.domain) foreach (var it in item.domain)
{ {
if (ParseV2Domain(it, rule)) countDomain++; if (ParseV2Domain(it, rule1)) countDomain++;
} }
if (countDomain > 0) if (countDomain > 0)
{ {
rules.Add(rule); rules.Add(rule1);
hasDomainIp = true; hasDomainIp = true;
} }
} }
@ -756,6 +759,10 @@ namespace v2rayN.Handler
{ {
return false; return false;
} }
else if (address.Equals("geoip:private"))
{
rule.ip_is_private = true;
}
else if (address.StartsWith("geoip:")) else if (address.StartsWith("geoip:"))
{ {
if (rule.geoip is null) { rule.geoip = new(); } if (rule.geoip is null) { rule.geoip = new(); }
@ -772,29 +779,19 @@ namespace v2rayN.Handler
private int GenDns(ProfileItem node, SingboxConfig singboxConfig) private int GenDns(ProfileItem node, SingboxConfig singboxConfig)
{ {
try try
{
Dns4Sbox? dns4Sbox;
if (_config.tunModeItem.enableTun)
{ {
var item = LazyConfig.Instance.GetDNSItem(ECoreType.sing_box); var item = LazyConfig.Instance.GetDNSItem(ECoreType.sing_box);
var tunDNS = item?.tunDNS; var strDNS = string.Empty;
if (Utils.IsNullOrEmpty(tunDNS)) if (_config.tunModeItem.enableTun)
{ {
tunDNS = Utils.GetEmbedText(Global.TunSingboxDNSFileName); strDNS = Utils.IsNullOrEmpty(item?.tunDNS) ? Utils.GetEmbedText(Global.TunSingboxDNSFileName) : item?.tunDNS;
}
dns4Sbox = JsonUtils.Deserialize<Dns4Sbox>(tunDNS);
} }
else else
{ {
var item = LazyConfig.Instance.GetDNSItem(ECoreType.sing_box); strDNS = Utils.IsNullOrEmpty(item?.normalDNS) ? Utils.GetEmbedText(Global.DNSSingboxNormalFileName) : item?.normalDNS;
var normalDNS = item?.normalDNS;
if (Utils.IsNullOrEmpty(normalDNS))
{
normalDNS = "{\"servers\":[{\"address\":\"tcp://8.8.8.8\"}]}";
} }
dns4Sbox = JsonUtils.Deserialize<Dns4Sbox>(normalDNS); var dns4Sbox = JsonUtils.Deserialize<Dns4Sbox>(strDNS);
}
if (dns4Sbox is null) if (dns4Sbox is null)
{ {
return 0; return 0;
@ -831,10 +828,10 @@ namespace v2rayN.Handler
{ {
singboxConfig.experimental = new Experimental4Sbox() singboxConfig.experimental = new Experimental4Sbox()
{ {
//cache_file = new CacheFile4Sbox() cache_file = new CacheFile4Sbox()
//{ {
// enabled = true enabled = true
//}, },
//v2ray_api = new V2ray_Api4Sbox() //v2ray_api = new V2ray_Api4Sbox()
//{ //{
// listen = $"{Global.Loopback}:{Global.StatePort}", // listen = $"{Global.Loopback}:{Global.StatePort}",
@ -852,6 +849,59 @@ namespace v2rayN.Handler
return 0; return 0;
} }
private int ConvertGeo2Ruleset(SingboxConfig singboxConfig)
{
var geosite = "geosite";
var geoip = "geoip";
var ruleSets = new List<string>();
//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<string>(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 #endregion private gen function
#region Gen speedtest config #region Gen speedtest config

@ -730,7 +730,7 @@ namespace v2rayN.Handler
var domainStrategy4Freedom = item?.domainStrategy4Freedom; var domainStrategy4Freedom = item?.domainStrategy4Freedom;
if (Utils.IsNullOrEmpty(normalDNS)) if (Utils.IsNullOrEmpty(normalDNS))
{ {
normalDNS = "1.1.1.1,8.8.8.8"; normalDNS = Utils.GetEmbedText(Global.DNSV2rayNormalFileName);
} }
//Outbound Freedom domainStrategy //Outbound Freedom domainStrategy

@ -35,6 +35,7 @@
{ {
public bool? auto_detect_interface { get; set; } public bool? auto_detect_interface { get; set; }
public List<Rule4Sbox> rules { get; set; } public List<Rule4Sbox> rules { get; set; }
public List<Ruleset4Sbox>? rule_set { get; set; }
} }
[Serializable] [Serializable]
@ -48,6 +49,7 @@
public string type { get; set; } public string type { get; set; }
public string mode { get; set; } public string mode { get; set; }
public string network { get; set; } public string network { get; set; }
public bool? ip_is_private { get; set; }
public List<int>? port { get; set; } public List<int>? port { get; set; }
public List<string>? port_range { get; set; } public List<string>? port_range { get; set; }
public List<string>? geosite { get; set; } public List<string>? geosite { get; set; }
@ -58,8 +60,8 @@
public List<string>? geoip { get; set; } public List<string>? geoip { get; set; }
public List<string>? ip_cidr { get; set; } public List<string>? ip_cidr { get; set; }
public List<string>? source_ip_cidr { get; set; } public List<string>? source_ip_cidr { get; set; }
public List<string>? process_name { get; set; } public List<string>? process_name { get; set; }
public List<string>? rule_set { get; set; }
} }
[Serializable] [Serializable]
@ -230,4 +232,14 @@
public string? cache_id { get; set; } public string? cache_id { get; set; }
public bool? store_fakeip { 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; }
}
} }

@ -19,14 +19,14 @@
], ],
"rules": [ "rules": [
{ {
"geosite": [ "rule_set": [
"geolocation-!cn" "geosite-geolocation-!cn"
], ],
"server": "remote" "server": "remote"
}, },
{ {
"geosite": [ "rule_set": [
"category-ads-all" "geosite-category-ads-all"
], ],
"server": "block" "server": "block"
} }

@ -19,14 +19,14 @@
], ],
"rules": [ "rules": [
{ {
"geosite": [ "rule_set": [
"geolocation-!cn" "geosite-geolocation-!cn"
], ],
"server": "remote" "server": "remote"
}, },
{ {
"geosite": [ "rule_set": [
"category-ads-all" "geosite-category-ads-all"
], ],
"server": "block" "server": "block"
} }

Loading…
Cancel
Save