2022-01-22 14:10:45 +00:00
|
|
|
package provider
|
2021-12-02 14:56:17 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
P "github.com/Dreamacro/clash/constant/provider"
|
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
"runtime"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-12-07 12:49:39 +00:00
|
|
|
ruleProviders = map[string]P.RuleProvider{}
|
2021-12-02 14:56:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ruleSetProvider struct {
|
|
|
|
*fetcher
|
2022-03-26 10:34:15 +00:00
|
|
|
behavior P.RuleType
|
|
|
|
strategy ruleStrategy
|
2021-12-02 14:56:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type RuleSetProvider struct {
|
|
|
|
*ruleSetProvider
|
|
|
|
}
|
|
|
|
|
|
|
|
type RulePayload struct {
|
|
|
|
/**
|
|
|
|
key: Domain or IP Cidr
|
|
|
|
value: Rule type or is empty
|
|
|
|
*/
|
2022-05-02 16:53:22 +00:00
|
|
|
Rules []string `yaml:"payload"`
|
|
|
|
Rules2 []string `yaml:"rules"`
|
2021-12-02 14:56:17 +00:00
|
|
|
}
|
|
|
|
|
2022-03-26 10:34:15 +00:00
|
|
|
type ruleStrategy interface {
|
|
|
|
Match(metadata *C.Metadata) bool
|
|
|
|
Count() int
|
|
|
|
ShouldResolveIP() bool
|
|
|
|
OnUpdate(rules []string)
|
|
|
|
}
|
|
|
|
|
2021-12-07 12:49:39 +00:00
|
|
|
func RuleProviders() map[string]P.RuleProvider {
|
2021-12-02 14:56:17 +00:00
|
|
|
return ruleProviders
|
|
|
|
}
|
|
|
|
|
2021-12-07 12:49:39 +00:00
|
|
|
func SetRuleProvider(ruleProvider P.RuleProvider) {
|
2021-12-02 14:56:17 +00:00
|
|
|
if ruleProvider != nil {
|
2021-12-07 12:49:39 +00:00
|
|
|
ruleProviders[(ruleProvider).Name()] = ruleProvider
|
2021-12-02 14:56:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rp *ruleSetProvider) Type() P.ProviderType {
|
|
|
|
return P.Rule
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rp *ruleSetProvider) Initial() error {
|
|
|
|
elm, err := rp.fetcher.Initial()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return rp.fetcher.onUpdate(elm)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rp *ruleSetProvider) Update() error {
|
|
|
|
elm, same, err := rp.fetcher.Update()
|
|
|
|
if err == nil && !same {
|
|
|
|
return rp.fetcher.onUpdate(elm)
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rp *ruleSetProvider) Behavior() P.RuleType {
|
|
|
|
return rp.behavior
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rp *ruleSetProvider) Match(metadata *C.Metadata) bool {
|
2022-03-26 10:34:15 +00:00
|
|
|
return rp.strategy != nil && rp.strategy.Match(metadata)
|
2021-12-02 14:56:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rp *ruleSetProvider) ShouldResolveIP() bool {
|
2022-03-26 10:34:15 +00:00
|
|
|
return rp.strategy.ShouldResolveIP()
|
2021-12-02 14:56:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rp *ruleSetProvider) AsRule(adaptor string) C.Rule {
|
|
|
|
panic("implement me")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rp *ruleSetProvider) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(
|
|
|
|
map[string]interface{}{
|
|
|
|
"behavior": rp.behavior.String(),
|
|
|
|
"name": rp.Name(),
|
2022-03-26 10:34:15 +00:00
|
|
|
"ruleCount": rp.strategy.Count(),
|
2021-12-02 14:56:17 +00:00
|
|
|
"type": rp.Type().String(),
|
|
|
|
"updatedAt": rp.updatedAt,
|
|
|
|
"vehicleType": rp.VehicleType().String(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRuleSetProvider(name string, behavior P.RuleType, interval time.Duration, vehicle P.Vehicle) P.RuleProvider {
|
|
|
|
rp := &ruleSetProvider{
|
|
|
|
behavior: behavior,
|
|
|
|
}
|
|
|
|
|
|
|
|
onUpdate := func(elm interface{}) error {
|
|
|
|
rulesRaw := elm.([]string)
|
2022-03-26 10:34:15 +00:00
|
|
|
rp.strategy.OnUpdate(rulesRaw)
|
2021-12-02 14:56:17 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
fetcher := newFetcher(name, interval, vehicle, rulesParse, onUpdate)
|
|
|
|
rp.fetcher = fetcher
|
2022-03-26 10:34:15 +00:00
|
|
|
rp.strategy = newStrategy(behavior)
|
|
|
|
|
2021-12-02 14:56:17 +00:00
|
|
|
wrapper := &RuleSetProvider{
|
|
|
|
rp,
|
|
|
|
}
|
|
|
|
|
|
|
|
runtime.SetFinalizer(wrapper, rp.fetcher.Destroy())
|
|
|
|
return wrapper
|
|
|
|
}
|
|
|
|
|
2022-03-26 10:34:15 +00:00
|
|
|
func newStrategy(behavior P.RuleType) ruleStrategy {
|
2021-12-02 14:56:17 +00:00
|
|
|
switch behavior {
|
|
|
|
case P.Domain:
|
2022-03-26 10:34:15 +00:00
|
|
|
strategy := NewDomainStrategy()
|
|
|
|
return strategy
|
2021-12-02 14:56:17 +00:00
|
|
|
case P.IPCIDR:
|
2022-03-26 10:34:15 +00:00
|
|
|
strategy := NewIPCidrStrategy()
|
|
|
|
return strategy
|
2021-12-02 14:56:17 +00:00
|
|
|
case P.Classical:
|
2022-03-26 10:34:15 +00:00
|
|
|
strategy := NewClassicalStrategy()
|
|
|
|
return strategy
|
2021-12-02 14:56:17 +00:00
|
|
|
default:
|
2022-03-26 10:34:15 +00:00
|
|
|
return nil
|
2021-12-02 14:56:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-26 10:34:15 +00:00
|
|
|
func rulesParse(buf []byte) (interface{}, error) {
|
|
|
|
rulePayload := RulePayload{}
|
|
|
|
err := yaml.Unmarshal(buf, &rulePayload)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-12-02 14:56:17 +00:00
|
|
|
}
|
|
|
|
|
2022-05-02 16:53:22 +00:00
|
|
|
return append(rulePayload.Rules, rulePayload.Rules2...), nil
|
2021-12-02 14:56:17 +00:00
|
|
|
}
|