Skip to content

Commit

Permalink
Migrate geosite & geoip to rule sets (sing-box)
Browse files Browse the repository at this point in the history
  • Loading branch information
2dust committed Apr 10, 2024
1 parent bba93a0 commit 5c0c07c
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 33 deletions.
1 change: 1 addition & 0 deletions v2rayN/v2rayN/Global.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ internal class Global
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";
Expand Down
94 changes: 72 additions & 22 deletions v2rayN/v2rayN/Handler/CoreConfigSingbox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public int GenerateClientConfigContent(ProfileItem node, out SingboxConfig? sing

GenStatistic(singboxConfig);

ConvertGeo2Ruleset(singboxConfig);

msg = string.Format(ResUI.SuccessfulConfiguration, "");
}
catch (Exception ex)
Expand Down Expand Up @@ -653,6 +655,7 @@ private int GenRoutingUserRule(RulesItem item, List<Rule4Sbox> rules)
{
rule.inbound = item.inboundTag;
}
var rule1 = JsonUtils.DeepCopy(rule);
var rule2 = JsonUtils.DeepCopy(rule);
var rule3 = JsonUtils.DeepCopy(rule);

Expand All @@ -662,11 +665,11 @@ private int GenRoutingUserRule(RulesItem item, List<Rule4Sbox> rules)
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;
}
}
Expand Down Expand Up @@ -756,6 +759,10 @@ private bool ParseV2Address(string address, Rule4Sbox rule)
{
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(); }
Expand All @@ -773,28 +780,18 @@ private int GenDns(ProfileItem node, SingboxConfig singboxConfig)
{
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;
Expand Down Expand Up @@ -831,10 +828,10 @@ private int GenStatistic(SingboxConfig singboxConfig)
{
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}",
Expand All @@ -852,6 +849,59 @@ private int GenStatistic(SingboxConfig singboxConfig)
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
Expand Down
2 changes: 1 addition & 1 deletion v2rayN/v2rayN/Handler/CoreConfigV2ray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,7 @@ private int GenDns(V2rayConfig v2rayConfig)
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
Expand Down
16 changes: 14 additions & 2 deletions v2rayN/v2rayN/Models/SingboxConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class Route4Sbox
{
public bool? auto_detect_interface { get; set; }
public List<Rule4Sbox> rules { get; set; }
public List<Ruleset4Sbox>? rule_set { get; set; }
}

[Serializable]
Expand All @@ -48,6 +49,7 @@ public class Rule4Sbox
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; }
Expand All @@ -58,8 +60,8 @@ public class Rule4Sbox
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]
Expand Down Expand Up @@ -230,4 +232,14 @@ public class CacheFile4Sbox
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; }
}

}
8 changes: 4 additions & 4 deletions v2rayN/v2rayN/Sample/dns_singbox_normal
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
8 changes: 4 additions & 4 deletions v2rayN/v2rayN/Sample/tun_singbox_dns
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down

0 comments on commit 5c0c07c

Please sign in to comment.