2019-12-08 12:17:24 +08:00
|
|
|
package provider
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2023-11-03 21:01:45 +08:00
|
|
|
"github.com/metacubex/mihomo/common/structure"
|
|
|
|
"github.com/metacubex/mihomo/common/utils"
|
|
|
|
"github.com/metacubex/mihomo/component/resource"
|
|
|
|
C "github.com/metacubex/mihomo/constant"
|
|
|
|
types "github.com/metacubex/mihomo/constant/provider"
|
2019-12-08 12:17:24 +08:00
|
|
|
)
|
|
|
|
|
2023-06-06 09:45:05 +08:00
|
|
|
var (
|
|
|
|
errVehicleType = errors.New("unsupport vehicle type")
|
|
|
|
errSubPath = errors.New("path is not subpath of home directory")
|
|
|
|
)
|
2019-12-08 12:17:24 +08:00
|
|
|
|
|
|
|
type healthCheckSchema struct {
|
2023-06-04 14:00:24 +08:00
|
|
|
Enable bool `provider:"enable"`
|
|
|
|
URL string `provider:"url"`
|
|
|
|
Interval int `provider:"interval"`
|
|
|
|
Lazy bool `provider:"lazy,omitempty"`
|
|
|
|
ExpectedStatus string `provider:"expected-status,omitempty"`
|
2019-12-08 12:17:24 +08:00
|
|
|
}
|
|
|
|
|
2023-11-17 00:37:54 +08:00
|
|
|
type OverrideSchema struct {
|
|
|
|
UDP *bool `proxy:"udp,omitempty"`
|
|
|
|
Up *string `proxy:"up,omitempty"`
|
|
|
|
Down *string `proxy:"down,omitempty"`
|
|
|
|
DialerProxy *string `provider:"dialer-proxy,omitempty"`
|
|
|
|
SkipCertVerify *bool `proxy:"skip-cert-verify,omitempty"`
|
|
|
|
}
|
|
|
|
|
2019-12-08 12:17:24 +08:00
|
|
|
type proxyProviderSchema struct {
|
2023-11-17 00:37:54 +08:00
|
|
|
Type string `provider:"type"`
|
|
|
|
Path string `provider:"path,omitempty"`
|
|
|
|
URL string `provider:"url,omitempty"`
|
|
|
|
Interval int `provider:"interval,omitempty"`
|
|
|
|
Filter string `provider:"filter,omitempty"`
|
|
|
|
ExcludeFilter string `provider:"exclude-filter,omitempty"`
|
|
|
|
ExcludeType string `provider:"exclude-type,omitempty"`
|
|
|
|
DialerProxy string `provider:"dialer-proxy,omitempty"`
|
|
|
|
|
|
|
|
HealthCheck healthCheckSchema `provider:"health-check,omitempty"`
|
|
|
|
Override OverrideSchema `provider:"override,omitempty"`
|
2019-12-08 12:17:24 +08:00
|
|
|
}
|
|
|
|
|
2022-03-16 12:10:13 +08:00
|
|
|
func ParseProxyProvider(name string, mapping map[string]any) (types.ProxyProvider, error) {
|
2019-12-08 12:17:24 +08:00
|
|
|
decoder := structure.NewDecoder(structure.Option{TagName: "provider", WeaklyTypedInput: true})
|
|
|
|
|
2020-11-19 00:53:22 +08:00
|
|
|
schema := &proxyProviderSchema{
|
|
|
|
HealthCheck: healthCheckSchema{
|
|
|
|
Lazy: true,
|
|
|
|
},
|
|
|
|
}
|
2019-12-08 12:17:24 +08:00
|
|
|
if err := decoder.Decode(mapping, schema); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-06-04 14:00:24 +08:00
|
|
|
expectedStatus, err := utils.NewIntRanges[uint16](schema.HealthCheck.ExpectedStatus)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-03-24 01:00:21 +08:00
|
|
|
var hcInterval uint
|
2019-12-08 12:17:24 +08:00
|
|
|
if schema.HealthCheck.Enable {
|
2019-12-26 18:41:06 +08:00
|
|
|
hcInterval = uint(schema.HealthCheck.Interval)
|
2019-12-08 12:17:24 +08:00
|
|
|
}
|
2023-06-04 14:00:24 +08:00
|
|
|
hc := NewHealthCheck([]C.Proxy{}, schema.HealthCheck.URL, hcInterval, schema.HealthCheck.Lazy, expectedStatus)
|
2019-12-08 12:17:24 +08:00
|
|
|
|
2021-07-04 20:32:59 +08:00
|
|
|
var vehicle types.Vehicle
|
2019-12-08 12:17:24 +08:00
|
|
|
switch schema.Type {
|
|
|
|
case "file":
|
2023-06-15 22:45:02 +08:00
|
|
|
path := C.Path.Resolve(schema.Path)
|
2022-07-11 21:30:34 +08:00
|
|
|
vehicle = resource.NewFileVehicle(path)
|
2019-12-08 12:17:24 +08:00
|
|
|
case "http":
|
2023-06-15 22:45:02 +08: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 08:32:11 +08:00
|
|
|
path := C.Path.GetPathByHash("proxies", schema.URL)
|
2023-06-15 22:45:02 +08:00
|
|
|
vehicle = resource.NewHTTPVehicle(schema.URL, path)
|
2023-06-06 09:45:05 +08:00
|
|
|
}
|
2019-12-08 12:17:24 +08:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("%w: %s", errVehicleType, schema.Type)
|
|
|
|
}
|
|
|
|
|
|
|
|
interval := time.Duration(uint(schema.Interval)) * time.Second
|
2021-11-20 23:38:49 +08:00
|
|
|
filter := schema.Filter
|
2022-10-30 21:04:33 +08:00
|
|
|
excludeFilter := schema.ExcludeFilter
|
2023-01-07 12:24:28 +08:00
|
|
|
excludeType := schema.ExcludeType
|
2023-04-12 10:39:24 +08:00
|
|
|
dialerProxy := schema.DialerProxy
|
2023-11-17 00:37:54 +08:00
|
|
|
override := schema.Override
|
2023-01-03 21:27:07 +08:00
|
|
|
|
2023-11-17 00:37:54 +08:00
|
|
|
return NewProxySetProvider(name, interval, filter, excludeFilter, excludeType, dialerProxy, override, vehicle, hc)
|
2019-12-08 12:17:24 +08:00
|
|
|
}
|