2022-01-22 22:10:45 +08:00
|
|
|
package provider
|
2021-12-02 22:56:17 +08:00
|
|
|
|
|
|
|
import (
|
2023-11-03 21:01:45 +08:00
|
|
|
C "github.com/metacubex/mihomo/constant"
|
|
|
|
P "github.com/metacubex/mihomo/constant/provider"
|
|
|
|
"github.com/metacubex/mihomo/rules/common"
|
2021-12-02 22:56:17 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type RuleSet struct {
|
2022-03-19 22:27:22 +08:00
|
|
|
*common.Base
|
2023-01-20 16:29:08 +08:00
|
|
|
ruleProviderName string
|
|
|
|
adapter string
|
|
|
|
noResolveIP bool
|
|
|
|
shouldFindProcess bool
|
2021-12-02 22:56:17 +08:00
|
|
|
}
|
|
|
|
|
2022-03-16 00:43:08 +08:00
|
|
|
func (rs *RuleSet) ShouldFindProcess() bool {
|
2024-06-17 22:04:51 +08:00
|
|
|
if rs.shouldFindProcess {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if provider, ok := rs.getProvider(); ok {
|
|
|
|
return provider.ShouldFindProcess()
|
|
|
|
}
|
|
|
|
return false
|
2022-03-16 00:43:08 +08:00
|
|
|
}
|
|
|
|
|
2021-12-02 22:56:17 +08:00
|
|
|
func (rs *RuleSet) RuleType() C.RuleType {
|
|
|
|
return C.RuleSet
|
|
|
|
}
|
|
|
|
|
feat: support sub-rule, eg.
rules:
- SUB-RULE,(AND,((NETWORK,TCP),(DOMAIN-KEYWORD,google))),TEST2
- SUB-RULE,(GEOIP,!CN),TEST1
- MATCH,DIRECT
sub-rules:
TEST2:
- MATCH,Proxy
TEST1:
- RULE-SET,Local,DIRECT,no-resolve
- GEOSITE,CN,Domestic
- GEOIP,CN,Domestic
- MATCH,Proxy
2022-09-06 17:30:35 +08:00
|
|
|
func (rs *RuleSet) Match(metadata *C.Metadata) (bool, string) {
|
2024-06-17 22:04:51 +08:00
|
|
|
if provider, ok := rs.getProvider(); ok {
|
|
|
|
return provider.Match(metadata), rs.adapter
|
|
|
|
}
|
|
|
|
return false, ""
|
2021-12-02 22:56:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rs *RuleSet) Adapter() string {
|
|
|
|
return rs.adapter
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rs *RuleSet) Payload() string {
|
2024-06-17 22:04:51 +08:00
|
|
|
if provider, ok := rs.getProvider(); ok {
|
|
|
|
return provider.Name()
|
|
|
|
}
|
|
|
|
return ""
|
2021-12-02 22:56:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rs *RuleSet) ShouldResolveIP() bool {
|
2024-06-17 22:04:51 +08:00
|
|
|
if rs.noResolveIP {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if provider, ok := rs.getProvider(); ok {
|
|
|
|
return provider.ShouldResolveIP()
|
2021-12-02 22:56:17 +08:00
|
|
|
}
|
2024-06-17 22:04:51 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rs *RuleSet) ProviderNames() []string {
|
|
|
|
return []string{rs.ruleProviderName}
|
|
|
|
}
|
2021-12-02 22:56:17 +08:00
|
|
|
|
2024-06-17 22:04:51 +08:00
|
|
|
func (rs *RuleSet) getProvider() (P.RuleProvider, bool) {
|
|
|
|
pp, ok := tunnel.RuleProviders()[rs.ruleProviderName]
|
|
|
|
return pp, ok
|
2021-12-02 22:56:17 +08:00
|
|
|
}
|
|
|
|
|
feat: support sub-rule, eg.
rules:
- SUB-RULE,(AND,((NETWORK,TCP),(DOMAIN-KEYWORD,google))),TEST2
- SUB-RULE,(GEOIP,!CN),TEST1
- MATCH,DIRECT
sub-rules:
TEST2:
- MATCH,Proxy
TEST1:
- RULE-SET,Local,DIRECT,no-resolve
- GEOSITE,CN,Domestic
- GEOIP,CN,Domestic
- MATCH,Proxy
2022-09-06 17:30:35 +08:00
|
|
|
func NewRuleSet(ruleProviderName string, adapter string, noResolveIP bool) (*RuleSet, error) {
|
2024-06-17 22:04:51 +08:00
|
|
|
rs := &RuleSet{
|
2022-03-19 22:27:22 +08:00
|
|
|
Base: &common.Base{},
|
2021-12-02 22:56:17 +08:00
|
|
|
ruleProviderName: ruleProviderName,
|
|
|
|
adapter: adapter,
|
2022-05-18 18:43:44 +08:00
|
|
|
noResolveIP: noResolveIP,
|
2024-06-17 22:04:51 +08:00
|
|
|
}
|
|
|
|
return rs, nil
|
2021-12-02 22:56:17 +08:00
|
|
|
}
|