2022-01-22 14:10:45 +00:00
|
|
|
package provider
|
|
|
|
|
|
|
|
import (
|
2023-06-15 14:45:02 +00:00
|
|
|
"errors"
|
2022-01-22 14:10:45 +00:00
|
|
|
"fmt"
|
2023-06-15 14:45:02 +00:00
|
|
|
"time"
|
|
|
|
|
2023-11-03 13:01:45 +00:00
|
|
|
"github.com/metacubex/mihomo/common/structure"
|
|
|
|
"github.com/metacubex/mihomo/component/resource"
|
|
|
|
C "github.com/metacubex/mihomo/constant"
|
|
|
|
P "github.com/metacubex/mihomo/constant/provider"
|
2023-06-15 14:45:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
errSubPath = errors.New("path is not subpath of home directory")
|
2022-01-22 14:10:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ruleProviderSchema struct {
|
|
|
|
Type string `provider:"type"`
|
|
|
|
Behavior string `provider:"behavior"`
|
2023-06-15 14:45:02 +00:00
|
|
|
Path string `provider:"path,omitempty"`
|
2022-01-22 14:10:45 +00:00
|
|
|
URL string `provider:"url,omitempty"`
|
2023-04-14 05:51:26 +00:00
|
|
|
Format string `provider:"format,omitempty"`
|
2022-01-22 14:10:45 +00:00
|
|
|
Interval int `provider:"interval,omitempty"`
|
|
|
|
}
|
|
|
|
|
2022-12-04 05:37:14 +00:00
|
|
|
func ParseRuleProvider(name string, mapping map[string]interface{}, parse func(tp, payload, target string, params []string, subRules map[string][]C.Rule) (parsed C.Rule, parseErr error)) (P.RuleProvider, error) {
|
2022-01-22 14:10:45 +00:00
|
|
|
schema := &ruleProviderSchema{}
|
|
|
|
decoder := structure.NewDecoder(structure.Option{TagName: "provider", WeaklyTypedInput: true})
|
|
|
|
if err := decoder.Decode(mapping, schema); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-04-14 05:51:26 +00:00
|
|
|
var behavior P.RuleBehavior
|
2022-01-22 14:10:45 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-04-14 05:51:26 +00:00
|
|
|
var format P.RuleFormat
|
|
|
|
|
|
|
|
switch schema.Format {
|
|
|
|
case "", "yaml":
|
|
|
|
format = P.YamlRule
|
|
|
|
case "text":
|
|
|
|
format = P.TextRule
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unsupported format type: %s", schema.Format)
|
|
|
|
}
|
|
|
|
|
2022-01-22 14:10:45 +00:00
|
|
|
var vehicle P.Vehicle
|
|
|
|
switch schema.Type {
|
|
|
|
case "file":
|
2023-06-15 14:45:02 +00:00
|
|
|
path := C.Path.Resolve(schema.Path)
|
2022-07-11 13:30:34 +00:00
|
|
|
vehicle = resource.NewFileVehicle(path)
|
2022-01-22 14:10:45 +00:00
|
|
|
case "http":
|
2023-06-15 14:45:02 +00:00
|
|
|
if schema.Path != "" {
|
|
|
|
path := C.Path.Resolve(schema.Path)
|
|
|
|
if !C.Path.IsSafePath(path) {
|
|
|
|
return nil, fmt.Errorf("%w: %s", errSubPath, path)
|
|
|
|
}
|
|
|
|
vehicle = resource.NewHTTPVehicle(schema.URL, path)
|
|
|
|
} else {
|
2023-06-19 00:32:11 +00:00
|
|
|
path := C.Path.GetPathByHash("rules", schema.URL)
|
2023-06-15 14:45:02 +00:00
|
|
|
vehicle = resource.NewHTTPVehicle(schema.URL, path)
|
|
|
|
}
|
|
|
|
|
2022-01-22 14:10:45 +00:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unsupported vehicle type: %s", schema.Type)
|
|
|
|
}
|
|
|
|
|
2023-04-14 05:51:26 +00:00
|
|
|
return NewRuleSetProvider(name, behavior, format, time.Duration(uint(schema.Interval))*time.Second, vehicle, parse), nil
|
2022-01-22 14:10:45 +00:00
|
|
|
}
|