1
0
mirror of https://github.com/nitezs/sub2clash.git synced 2024-12-23 22:04:41 -05:00
sub2clash/utils/rule.go

55 lines
1.1 KiB
Go
Raw Normal View History

2023-09-12 06:40:24 -04:00
package utils
import (
"fmt"
"gopkg.in/yaml.v3"
"io"
2023-09-12 12:46:17 -04:00
"sub2clash/model"
2023-09-12 06:40:24 -04:00
)
func AddRulesByUrl(sub *model.Subscription, url string, proxy string) {
2023-09-12 12:46:17 -04:00
get, err := Get(url)
2023-09-12 06:40:24 -04:00
if err != nil {
fmt.Println(err)
return
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
fmt.Println(err)
}
}(get.Body)
bytes, err := io.ReadAll(get.Body)
if err != nil {
fmt.Println(err)
return
}
var payload model.Payload
err = yaml.Unmarshal(bytes, &payload)
if err != nil {
fmt.Println(err)
return
}
for i := range payload.Rules {
payload.Rules[i] = payload.Rules[i] + "," + proxy
}
AddRules(sub, payload.Rules...)
}
func AddRuleProvider(
sub *model.Subscription, providerName string, proxy string, provider model.RuleProvider,
) {
if sub.RuleProviders == nil {
sub.RuleProviders = make(map[string]model.RuleProvider)
}
sub.RuleProviders[providerName] = provider
AddRules(
sub,
fmt.Sprintf("RULE-SET,%s,%s", providerName, proxy),
)
}
func AddRules(sub *model.Subscription, rules ...string) {
sub.Rules = append(rules, sub.Rules...)
}