Skip to content

Commit 124cbfa

Browse files
committed
Add custom sing-box rule-set support
1 parent 8d21f9b commit 124cbfa

15 files changed

+512
-410
lines changed

v2rayN/v2rayN/Common/Utils.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,14 @@ public static string GetEmbedText(string res)
5757
/// 取得存储资源
5858
/// </summary>
5959
/// <returns></returns>
60-
public static string? LoadResource(string res)
60+
public static string? LoadResource(string? res)
6161
{
6262
try
6363
{
64-
if (!File.Exists(res)) return null;
64+
if (!File.Exists(res))
65+
{
66+
return null;
67+
}
6568
return File.ReadAllText(res);
6669
}
6770
catch (Exception ex)

v2rayN/v2rayN/Handler/CoreConfigSingbox.cs

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -895,18 +895,45 @@ private int ConvertGeo2Ruleset(SingboxConfig singboxConfig)
895895
ruleSets.AddRange(dnsRule.rule_set);
896896
}
897897

898+
//load custom ruleset file
899+
List<Ruleset4Sbox> customRulesets = [];
900+
if (_config.routingBasicItem.enableRoutingAdvanced)
901+
{
902+
var routing = ConfigHandler.GetDefaultRouting(_config);
903+
if (!Utils.IsNullOrEmpty(routing.customRulesetPath4Singbox))
904+
{
905+
var result = Utils.LoadResource(routing.customRulesetPath4Singbox);
906+
if (!Utils.IsNullOrEmpty(result))
907+
{
908+
customRulesets = (JsonUtils.Deserialize<List<Ruleset4Sbox>>(result) ?? [])
909+
.Where(t => t.tag != null)
910+
.Where(t => t.type != null)
911+
.Where(t => t.format != null)
912+
.ToList();
913+
}
914+
}
915+
}
916+
898917
//Add ruleset srs
899918
singboxConfig.route.rule_set = [];
900919
foreach (var item in new HashSet<string>(ruleSets))
901920
{
902-
singboxConfig.route.rule_set.Add(new()
921+
var customRuleset = customRulesets.FirstOrDefault(t => t.tag != null && t.tag.Equals(item));
922+
if (customRuleset != null)
903923
{
904-
type = "remote",
905-
format = "binary",
906-
tag = item,
907-
url = string.Format(Global.SingboxRulesetUrl, item.StartsWith(geosite) ? geosite : geoip, item),
908-
download_detour = Global.ProxyTag
909-
});
924+
singboxConfig.route.rule_set.Add(customRuleset);
925+
}
926+
else
927+
{
928+
singboxConfig.route.rule_set.Add(new()
929+
{
930+
type = "remote",
931+
format = "binary",
932+
tag = item,
933+
url = string.Format(Global.SingboxRulesetUrl, item.StartsWith(geosite) ? geosite : geoip, item),
934+
download_detour = Global.ProxyTag
935+
});
936+
}
910937
}
911938

912939
return 0;

v2rayN/v2rayN/Models/ConfigItems.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -182,16 +182,11 @@ public class SpeedTestItem
182182
[Serializable]
183183
public class RoutingBasicItem
184184
{
185-
/// <summary>
186-
/// 域名解析策略
187-
/// </summary>
188185
public string domainStrategy { get; set; }
189-
190186
public string domainStrategy4Singbox { get; set; }
191-
192187
public string domainMatcher { get; set; }
193188
public string routingIndexId { get; set; }
194-
public bool enableRoutingAdvanced { get; set; }
189+
public bool enableRoutingAdvanced { get; set; }
195190
}
196191

197192
[Serializable]

v2rayN/v2rayN/Models/RoutingItem.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class RoutingItem
1515
public bool enabled { get; set; } = true;
1616
public bool locked { get; set; }
1717
public string customIcon { get; set; }
18+
public string customRulesetPath4Singbox { get; set; }
1819
public string domainStrategy { get; set; }
1920
public string domainStrategy4Singbox { get; set; }
2021
public int sort { get; set; }

v2rayN/v2rayN/Models/SingboxConfig.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,9 @@ public class Ruleset4Sbox
238238
public string? tag { get; set; }
239239
public string? type { get; set; }
240240
public string? format { get; set; }
241+
public string? path { get; set; }
241242
public string? url { get; set; }
242243
public string? download_detour { get; set; }
244+
public string? update_interval { get; set; }
243245
}
244246
}

0 commit comments

Comments
 (0)