clash/rule/parser.go

97 lines
2.8 KiB
Go
Raw Normal View History

2020-06-05 09:43:50 +00:00
package rules
import (
"fmt"
2021-12-02 14:56:17 +00:00
"github.com/Dreamacro/clash/adapter/provider"
"github.com/Dreamacro/clash/common/structure"
2020-06-05 09:43:50 +00:00
C "github.com/Dreamacro/clash/constant"
2021-12-02 14:56:17 +00:00
P "github.com/Dreamacro/clash/constant/provider"
"time"
2020-06-05 09:43:50 +00:00
)
func ParseRule(tp, payload, target string, params []string) (C.Rule, error) {
var (
parseErr error
parsed C.Rule
)
2021-11-17 08:03:47 +00:00
ruleExtra := &C.RuleExtra{
Network: findNetwork(params),
SourceIPs: findSourceIPs(params),
}
2020-06-05 09:43:50 +00:00
switch tp {
case "DOMAIN":
2021-11-17 08:03:47 +00:00
parsed = NewDomain(payload, target, ruleExtra)
2020-06-05 09:43:50 +00:00
case "DOMAIN-SUFFIX":
2021-11-17 08:03:47 +00:00
parsed = NewDomainSuffix(payload, target, ruleExtra)
2020-06-05 09:43:50 +00:00
case "DOMAIN-KEYWORD":
2021-11-17 08:03:47 +00:00
parsed = NewDomainKeyword(payload, target, ruleExtra)
case "GEOSITE":
parsed, parseErr = NewGEOSITE(payload, target, ruleExtra)
2020-06-05 09:43:50 +00:00
case "GEOIP":
noResolve := HasNoResolve(params)
2021-11-17 08:03:47 +00:00
parsed, parseErr = NewGEOIP(payload, target, noResolve, ruleExtra)
2020-06-05 09:43:50 +00:00
case "IP-CIDR", "IP-CIDR6":
noResolve := HasNoResolve(params)
2021-11-17 08:03:47 +00:00
parsed, parseErr = NewIPCIDR(payload, target, ruleExtra, WithIPCIDRNoResolve(noResolve))
2020-06-05 09:43:50 +00:00
case "SRC-IP-CIDR":
2021-11-17 08:03:47 +00:00
parsed, parseErr = NewIPCIDR(payload, target, ruleExtra, WithIPCIDRSourceIP(true), WithIPCIDRNoResolve(true))
2020-06-05 09:43:50 +00:00
case "SRC-PORT":
2021-11-17 08:03:47 +00:00
parsed, parseErr = NewPort(payload, target, true, ruleExtra)
2020-06-05 09:43:50 +00:00
case "DST-PORT":
2021-11-17 08:03:47 +00:00
parsed, parseErr = NewPort(payload, target, false, ruleExtra)
2020-07-19 05:17:05 +00:00
case "PROCESS-NAME":
2021-11-17 08:03:47 +00:00
parsed, parseErr = NewProcess(payload, target, ruleExtra)
2020-06-05 09:43:50 +00:00
case "MATCH":
2021-11-17 08:03:47 +00:00
parsed = NewMatch(target, ruleExtra)
2021-12-02 14:56:17 +00:00
case "RULE-SET":
2021-12-02 15:32:30 +00:00
parsed, parseErr = NewRuleSet(payload, target, ruleExtra)
2020-06-05 09:43:50 +00:00
default:
parseErr = fmt.Errorf("unsupported rule type %s", tp)
}
return parsed, parseErr
}
2021-12-02 14:56:17 +00:00
type ruleProviderSchema struct {
Type string `provider:"type"`
Behavior string `provider:"behavior"`
Path string `provider:"path"`
URL string `provider:"url,omitempty"`
Interval int `provider:"interval,omitempty"`
}
func ParseRuleProvider(name string, mapping map[string]interface{}) (P.RuleProvider, error) {
schema := &ruleProviderSchema{}
decoder := structure.NewDecoder(structure.Option{TagName: "provider", WeaklyTypedInput: true})
if err := decoder.Decode(mapping, schema); err != nil {
return nil, err
}
var behavior P.RuleType
switch schema.Behavior {
case "domain":
behavior = P.Domain
case "ipcidr":
behavior = P.IPCIDR
case "classical":
behavior = P.Classical
default:
return nil, fmt.Errorf("unsupported behavior type: %s", schema.Behavior)
}
path := C.Path.Resolve(schema.Path)
var vehicle P.Vehicle
switch schema.Type {
case "file":
vehicle = provider.NewFileVehicle(path)
case "http":
vehicle = provider.NewHTTPVehicle(schema.URL, path)
default:
return nil, fmt.Errorf("unsupported vehicle type: %s", schema.Type)
}
return NewRuleSetProvider(name, behavior, time.Duration(uint(schema.Interval))*time.Second, vehicle), nil
}