2022-01-22 22:10:45 +08:00
|
|
|
package provider
|
2021-12-02 22:56:17 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
P "github.com/Dreamacro/clash/constant/provider"
|
2022-06-04 03:25:33 +08:00
|
|
|
"github.com/Dreamacro/clash/rules/common"
|
2021-12-02 22:56:17 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type RuleSet struct {
|
2022-03-19 22:27:22 +08:00
|
|
|
*common.Base
|
2021-12-02 22:56:17 +08:00
|
|
|
ruleProviderName string
|
|
|
|
adapter string
|
2021-12-07 20:49:39 +08:00
|
|
|
ruleProvider P.RuleProvider
|
2022-05-18 18:43:44 +08:00
|
|
|
noResolveIP bool
|
2021-12-02 22:56:17 +08:00
|
|
|
}
|
|
|
|
|
2022-03-16 00:43:08 +08:00
|
|
|
func (rs *RuleSet) ShouldFindProcess() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-12-02 22:56:17 +08:00
|
|
|
func (rs *RuleSet) RuleType() C.RuleType {
|
|
|
|
return C.RuleSet
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rs *RuleSet) Match(metadata *C.Metadata) bool {
|
|
|
|
return rs.getProviders().Match(metadata)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rs *RuleSet) Adapter() string {
|
|
|
|
return rs.adapter
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rs *RuleSet) Payload() string {
|
|
|
|
return rs.getProviders().Name()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rs *RuleSet) ShouldResolveIP() bool {
|
2022-05-18 18:43:44 +08:00
|
|
|
return !rs.noResolveIP && rs.getProviders().ShouldResolveIP()
|
2021-12-02 22:56:17 +08:00
|
|
|
}
|
|
|
|
func (rs *RuleSet) getProviders() P.RuleProvider {
|
|
|
|
if rs.ruleProvider == nil {
|
|
|
|
rp := RuleProviders()[rs.ruleProviderName]
|
|
|
|
rs.ruleProvider = rp
|
|
|
|
}
|
|
|
|
|
2021-12-07 20:49:39 +08:00
|
|
|
return rs.ruleProvider
|
2021-12-02 22:56:17 +08:00
|
|
|
}
|
|
|
|
|
2022-06-10 13:36:09 +08:00
|
|
|
func NewRuleSet(ruleProviderName string, adapter string, noResolveIP bool, parse func(tp, payload, target string, params []string) (parsed C.Rule, parseErr error)) (*RuleSet, error) {
|
2021-12-02 22:56:17 +08:00
|
|
|
rp, ok := RuleProviders()[ruleProviderName]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("rule set %s not found", ruleProviderName)
|
|
|
|
}
|
|
|
|
return &RuleSet{
|
2022-03-19 22:27:22 +08:00
|
|
|
Base: &common.Base{},
|
2021-12-02 22:56:17 +08:00
|
|
|
ruleProviderName: ruleProviderName,
|
|
|
|
adapter: adapter,
|
|
|
|
ruleProvider: rp,
|
2022-05-18 18:43:44 +08:00
|
|
|
noResolveIP: noResolveIP,
|
2021-12-02 22:56:17 +08:00
|
|
|
}, nil
|
|
|
|
}
|