2022-01-22 22:10:45 +08:00
|
|
|
package provider
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/Dreamacro/clash/adapter/provider"
|
|
|
|
"github.com/Dreamacro/clash/common/structure"
|
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
P "github.com/Dreamacro/clash/constant/provider"
|
|
|
|
RC "github.com/Dreamacro/clash/rule/common"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseRule(tp, payload, target string, params []string) (C.Rule, error) {
|
|
|
|
var (
|
|
|
|
parseErr error
|
|
|
|
parsed C.Rule
|
|
|
|
)
|
|
|
|
|
|
|
|
switch tp {
|
|
|
|
case "DOMAIN":
|
2022-03-17 23:24:07 +08:00
|
|
|
parsed = RC.NewDomain(payload, target)
|
2022-01-22 22:10:45 +08:00
|
|
|
case "DOMAIN-SUFFIX":
|
2022-03-17 23:24:07 +08:00
|
|
|
parsed = RC.NewDomainSuffix(payload, target)
|
2022-01-22 22:10:45 +08:00
|
|
|
case "DOMAIN-KEYWORD":
|
2022-03-17 23:24:07 +08:00
|
|
|
parsed = RC.NewDomainKeyword(payload, target)
|
2022-01-22 22:10:45 +08:00
|
|
|
case "GEOSITE":
|
2022-03-17 23:24:07 +08:00
|
|
|
parsed, parseErr = RC.NewGEOSITE(payload, target)
|
2022-01-22 22:10:45 +08:00
|
|
|
case "IP-CIDR", "IP-CIDR6":
|
|
|
|
noResolve := RC.HasNoResolve(params)
|
2022-03-17 23:24:07 +08:00
|
|
|
parsed, parseErr = RC.NewIPCIDR(payload, target, RC.WithIPCIDRNoResolve(noResolve))
|
2022-01-22 22:10:45 +08:00
|
|
|
case "SRC-IP-CIDR":
|
2022-03-17 23:24:07 +08:00
|
|
|
parsed, parseErr = RC.NewIPCIDR(payload, target, RC.WithIPCIDRSourceIP(true), RC.WithIPCIDRNoResolve(true))
|
2022-01-22 22:10:45 +08:00
|
|
|
case "SRC-PORT":
|
2022-03-17 23:24:07 +08:00
|
|
|
parsed, parseErr = RC.NewPort(payload, target, true)
|
2022-01-22 22:10:45 +08:00
|
|
|
case "DST-PORT":
|
2022-03-17 23:24:07 +08:00
|
|
|
parsed, parseErr = RC.NewPort(payload, target, false)
|
2022-01-22 22:10:45 +08:00
|
|
|
case "PROCESS-NAME":
|
2022-03-17 23:24:07 +08:00
|
|
|
parsed, parseErr = RC.NewProcess(payload, target, true)
|
2022-03-16 17:54:44 +08:00
|
|
|
case "PROCESS-PATH":
|
2022-03-17 23:24:07 +08:00
|
|
|
parsed, parseErr = RC.NewProcess(payload, target, false)
|
2022-03-15 02:20:19 +08:00
|
|
|
case "GEOIP":
|
|
|
|
noResolve := RC.HasNoResolve(params)
|
2022-03-17 23:24:07 +08:00
|
|
|
parsed, parseErr = RC.NewGEOIP(payload, target, noResolve)
|
2022-01-22 22:10:45 +08:00
|
|
|
default:
|
|
|
|
parseErr = fmt.Errorf("unsupported rule type %s", tp)
|
|
|
|
}
|
2022-03-17 23:24:07 +08:00
|
|
|
ruleExtra := &C.RuleExtra{
|
|
|
|
Network: RC.FindNetwork(params),
|
|
|
|
SourceIPs: RC.FindSourceIPs(params),
|
|
|
|
}
|
|
|
|
parsed.SetRuleExtra(ruleExtra)
|
2022-01-22 22:10:45 +08:00
|
|
|
return parsed, parseErr
|
|
|
|
}
|