mirror of
https://github.com/nitezs/sub2clash.git
synced 2024-12-24 12:54:41 -05:00
55 lines
1.1 KiB
Go
55 lines
1.1 KiB
Go
|
package utils
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"gopkg.in/yaml.v3"
|
||
|
"io"
|
||
|
"sub/model"
|
||
|
)
|
||
|
|
||
|
func AddRulesByUrl(sub *model.Subscription, url string, proxy string) {
|
||
|
get, err := GetWithRetry(url)
|
||
|
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...)
|
||
|
}
|