2022-01-22 14:10:45 +00:00
|
|
|
package provider
|
2021-12-02 14:56:17 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
P "github.com/Dreamacro/clash/constant/provider"
|
|
|
|
)
|
|
|
|
|
|
|
|
type RuleSet struct {
|
|
|
|
ruleProviderName string
|
|
|
|
adapter string
|
2021-12-07 12:49:39 +00:00
|
|
|
ruleProvider P.RuleProvider
|
2021-12-02 15:32:30 +00:00
|
|
|
ruleExtra *C.RuleExtra
|
2021-12-02 14:56:17 +00: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 {
|
2021-12-06 13:44:43 +00:00
|
|
|
return rs.getProviders().ShouldResolveIP()
|
2021-12-02 14:56:17 +00:00
|
|
|
}
|
|
|
|
func (rs *RuleSet) getProviders() P.RuleProvider {
|
|
|
|
if rs.ruleProvider == nil {
|
|
|
|
rp := RuleProviders()[rs.ruleProviderName]
|
|
|
|
rs.ruleProvider = rp
|
|
|
|
}
|
|
|
|
|
2021-12-07 12:49:39 +00:00
|
|
|
return rs.ruleProvider
|
2021-12-02 14:56:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rs *RuleSet) RuleExtra() *C.RuleExtra {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-02 15:32:30 +00:00
|
|
|
func NewRuleSet(ruleProviderName string, adapter string, ruleExtra *C.RuleExtra) (*RuleSet, error) {
|
2021-12-02 14:56:17 +00:00
|
|
|
rp, ok := RuleProviders()[ruleProviderName]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("rule set %s not found", ruleProviderName)
|
|
|
|
}
|
|
|
|
return &RuleSet{
|
|
|
|
ruleProviderName: ruleProviderName,
|
|
|
|
adapter: adapter,
|
|
|
|
ruleProvider: rp,
|
2021-12-02 15:32:30 +00:00
|
|
|
ruleExtra: ruleExtra,
|
2021-12-02 14:56:17 +00:00
|
|
|
}, nil
|
|
|
|
}
|