sing-box/route/rule/rule_set.go

72 lines
1.9 KiB
Go
Raw Permalink Normal View History

2024-10-21 15:38:34 +00:00
package rule
2023-12-01 05:24:12 +00:00
import (
"context"
"github.com/sagernet/sing-box/adapter"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/option"
2024-06-07 07:55:21 +00:00
"github.com/sagernet/sing/common"
2023-12-01 05:24:12 +00:00
E "github.com/sagernet/sing/common/exceptions"
"github.com/sagernet/sing/common/logger"
2024-06-07 07:55:21 +00:00
"go4.org/netipx"
2023-12-01 05:24:12 +00:00
)
2024-11-10 04:11:21 +00:00
func NewRuleSet(ctx context.Context, logger logger.ContextLogger, options option.RuleSet) (adapter.RuleSet, error) {
2023-12-01 05:24:12 +00:00
switch options.Type {
case C.RuleSetTypeInline, C.RuleSetTypeLocal, "":
2024-11-10 04:11:21 +00:00
return NewLocalRuleSet(ctx, logger, options)
2023-12-01 05:24:12 +00:00
case C.RuleSetTypeRemote:
2024-11-10 04:11:21 +00:00
return NewRemoteRuleSet(ctx, logger, options), nil
2023-12-01 05:24:12 +00:00
default:
2024-06-25 16:45:10 +00:00
return nil, E.New("unknown rule-set type: ", options.Type)
2023-12-01 05:24:12 +00:00
}
}
2024-06-07 07:55:21 +00:00
func extractIPSetFromRule(rawRule adapter.HeadlessRule) []*netipx.IPSet {
switch rule := rawRule.(type) {
case *DefaultHeadlessRule:
return common.FlatMap(rule.destinationIPCIDRItems, func(rawItem RuleItem) []*netipx.IPSet {
switch item := rawItem.(type) {
case *IPCIDRItem:
return []*netipx.IPSet{item.ipSet}
default:
return nil
}
})
case *LogicalHeadlessRule:
return common.FlatMap(rule.rules, extractIPSetFromRule)
default:
panic("unexpected rule type")
}
}
2024-10-21 15:38:34 +00:00
func hasHeadlessRule(rules []option.HeadlessRule, cond func(rule option.DefaultHeadlessRule) bool) bool {
for _, rule := range rules {
switch rule.Type {
case C.RuleTypeDefault:
if cond(rule.DefaultOptions) {
return true
}
case C.RuleTypeLogical:
if hasHeadlessRule(rule.LogicalOptions.Rules, cond) {
return true
}
}
}
return false
}
func isProcessHeadlessRule(rule option.DefaultHeadlessRule) bool {
return len(rule.ProcessName) > 0 || len(rule.ProcessPath) > 0 || len(rule.PackageName) > 0
}
func isWIFIHeadlessRule(rule option.DefaultHeadlessRule) bool {
return len(rule.WIFISSID) > 0 || len(rule.WIFIBSSID) > 0
}
func isIPCIDRHeadlessRule(rule option.DefaultHeadlessRule) bool {
return len(rule.IPCIDR) > 0 || rule.IPSet != nil
}