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 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";

@ -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<Dns4Sbox>(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<Dns4Sbox>(normalDNS);
strDNS = Utils.IsNullOrEmpty(item?.normalDNS) ? Utils.GetEmbedText(Global.DNSSingboxNormalFileName) : item?.normalDNS;
}
var dns4Sbox = JsonUtils.Deserialize<Dns4Sbox>(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<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
#region Gen speedtest config

@ -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

@ -35,6 +35,7 @@
{
public bool? auto_detect_interface { get; set; }
public List<Rule4Sbox> rules { get; set; }
public List<Ruleset4Sbox>? 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<int>? port { get; set; }
public List<string>? port_range { get; set; }
public List<string>? geosite { get; set; }
@ -58,8 +60,8 @@
public List<string>? geoip { get; set; }
public List<string>? 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]
@ -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; }
}
}

@ -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"
}

@ -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"
}

Loading…
Cancel
Save