1
0
mirror of https://github.com/nitezs/sub2clash.git synced 2024-12-23 14:44:42 -05:00
sub2clash/common/rule.go

52 lines
1.2 KiB
Go
Raw Normal View History

package common
2023-09-12 06:40:24 -04:00
import (
"fmt"
2023-09-13 01:47:22 -04:00
"strings"
"github.com/nitezs/sub2clash/model"
2023-09-12 06:40:24 -04:00
)
2023-09-13 01:47:22 -04:00
func PrependRuleProvider(
sub *model.Subscription, providerName string, group string, provider model.RuleProvider,
) {
if sub.RuleProviders == nil {
sub.RuleProviders = make(map[string]model.RuleProvider)
2023-09-12 06:40:24 -04:00
}
2023-09-13 01:47:22 -04:00
sub.RuleProviders[providerName] = provider
PrependRules(
sub,
fmt.Sprintf("RULE-SET,%s,%s", providerName, group),
)
2023-09-12 06:40:24 -04:00
}
2023-09-13 01:47:22 -04:00
func AppenddRuleProvider(
sub *model.Subscription, providerName string, group string, provider model.RuleProvider,
2023-09-12 06:40:24 -04:00
) {
if sub.RuleProviders == nil {
sub.RuleProviders = make(map[string]model.RuleProvider)
}
sub.RuleProviders[providerName] = provider
2023-09-13 01:47:22 -04:00
AppendRules(sub, fmt.Sprintf("RULE-SET,%s,%s", providerName, group))
2023-09-12 06:40:24 -04:00
}
2023-09-13 01:47:22 -04:00
func PrependRules(sub *model.Subscription, rules ...string) {
if sub.Rules == nil {
sub.Rules = make([]string, 0)
}
2023-09-12 06:40:24 -04:00
sub.Rules = append(rules, sub.Rules...)
}
2023-09-13 01:47:22 -04:00
func AppendRules(sub *model.Subscription, rules ...string) {
if sub.Rules == nil {
sub.Rules = make([]string, 0)
}
matchRule := sub.Rules[len(sub.Rules)-1]
if strings.Contains(matchRule, "MATCH") {
sub.Rules = append(sub.Rules[:len(sub.Rules)-1], rules...)
sub.Rules = append(sub.Rules, matchRule)
return
}
sub.Rules = append(sub.Rules, rules...)
}