Fix negative rules

This commit is contained in:
DHR60 2026-05-03 15:08:56 +08:00
parent 1760a92507
commit 9714f70ed5

View file

@ -329,6 +329,46 @@ public partial class CoreConfigSingboxService
if (item.Ip?.Count > 0)
{
var countIp = 0;
var negativeIpList = item.Ip.Where(it => it.StartsWith('!')).ToList();
if (negativeIpList.Count > 0)
{
var positiveIpList = item.Ip.Except(negativeIpList).ToList();
var positiveRule = rule2;
positiveRule = JsonUtils.DeepCopy(rule2);
positiveRule.outbound = null;
positiveRule.action = null;
foreach (var it in positiveIpList)
{
if (ParseV2Address(it, positiveRule))
{
countIp++;
}
}
var negativeRule = new Rule4Sbox();
foreach (var it in negativeIpList)
{
// Remove first '!' and trim spaces
var ip = it[1..].Trim();
if (ParseV2Address(ip, negativeRule))
{
countIp++;
}
}
negativeRule.invert = true;
rule2 = new Rule4Sbox()
{
outbound = rule2.outbound,
action = rule2.action,
type = "logical",
mode = "or",
rules = [
positiveRule,
negativeRule
]
};
}
else
{
foreach (var it in item.Ip)
{
if (ParseV2Address(it, rule2))
@ -336,6 +376,7 @@ public partial class CoreConfigSingboxService
countIp++;
}
}
}
if (countIp > 0)
{
rules.Add(rule2);
@ -459,16 +500,6 @@ public partial class CoreConfigSingboxService
rule.geoip ??= [];
rule.geoip?.Add(address[Global.GeoIPPrefix.Length..]);
}
else if (address.Equals($"{Global.GeoIPPrefix}!private"))
{
rule.ip_is_private = false;
}
else if (address.StartsWith($"{Global.GeoIPPrefix}!"))
{
rule.geoip ??= [];
rule.geoip?.Add(address.Substring($"{Global.GeoIPPrefix}!".Length));
rule.invert = true;
}
else
{
rule.ip_cidr ??= [];