Skip to content

Commit

Permalink
Add custom sing-box rule-set support
Browse files Browse the repository at this point in the history
  • Loading branch information
2dust committed May 4, 2024
1 parent 8d21f9b commit 124cbfa
Show file tree
Hide file tree
Showing 15 changed files with 512 additions and 410 deletions.
7 changes: 5 additions & 2 deletions v2rayN/v2rayN/Common/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,14 @@ public static string GetEmbedText(string res)
/// 取得存储资源
/// </summary>
/// <returns></returns>
public static string? LoadResource(string res)
public static string? LoadResource(string? res)
{
try
{
if (!File.Exists(res)) return null;
if (!File.Exists(res))
{
return null;
}
return File.ReadAllText(res);
}
catch (Exception ex)
Expand Down
41 changes: 34 additions & 7 deletions v2rayN/v2rayN/Handler/CoreConfigSingbox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -895,18 +895,45 @@ private int ConvertGeo2Ruleset(SingboxConfig singboxConfig)
ruleSets.AddRange(dnsRule.rule_set);
}

//load custom ruleset file
List<Ruleset4Sbox> customRulesets = [];
if (_config.routingBasicItem.enableRoutingAdvanced)
{
var routing = ConfigHandler.GetDefaultRouting(_config);
if (!Utils.IsNullOrEmpty(routing.customRulesetPath4Singbox))
{
var result = Utils.LoadResource(routing.customRulesetPath4Singbox);
if (!Utils.IsNullOrEmpty(result))
{
customRulesets = (JsonUtils.Deserialize<List<Ruleset4Sbox>>(result) ?? [])
.Where(t => t.tag != null)
.Where(t => t.type != null)
.Where(t => t.format != null)
.ToList();
}
}
}

//Add ruleset srs
singboxConfig.route.rule_set = [];
foreach (var item in new HashSet<string>(ruleSets))
{
singboxConfig.route.rule_set.Add(new()
var customRuleset = customRulesets.FirstOrDefault(t => t.tag != null && t.tag.Equals(item));
if (customRuleset != null)
{
type = "remote",
format = "binary",
tag = item,
url = string.Format(Global.SingboxRulesetUrl, item.StartsWith(geosite) ? geosite : geoip, item),
download_detour = Global.ProxyTag
});
singboxConfig.route.rule_set.Add(customRuleset);
}
else
{
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;
Expand Down
7 changes: 1 addition & 6 deletions v2rayN/v2rayN/Models/ConfigItems.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,16 +182,11 @@ public class SpeedTestItem
[Serializable]
public class RoutingBasicItem
{
/// <summary>
/// 域名解析策略
/// </summary>
public string domainStrategy { get; set; }

public string domainStrategy4Singbox { get; set; }

public string domainMatcher { get; set; }
public string routingIndexId { get; set; }
public bool enableRoutingAdvanced { get; set; }
public bool enableRoutingAdvanced { get; set; }
}

[Serializable]
Expand Down
1 change: 1 addition & 0 deletions v2rayN/v2rayN/Models/RoutingItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class RoutingItem
public bool enabled { get; set; } = true;
public bool locked { get; set; }
public string customIcon { get; set; }
public string customRulesetPath4Singbox { get; set; }
public string domainStrategy { get; set; }
public string domainStrategy4Singbox { get; set; }
public int sort { get; set; }
Expand Down
2 changes: 2 additions & 0 deletions v2rayN/v2rayN/Models/SingboxConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,9 @@ public class Ruleset4Sbox
public string? tag { get; set; }
public string? type { get; set; }
public string? format { get; set; }
public string? path { get; set; }
public string? url { get; set; }
public string? download_detour { get; set; }
public string? update_interval { get; set; }
}
}

0 comments on commit 124cbfa

Please sign in to comment.