Clash.Meta/adapter/provider/parser.go

128 lines
4.3 KiB
Go
Raw Normal View History

2019-12-08 12:17:24 +08:00
package provider
import (
"encoding"
2019-12-08 12:17:24 +08:00
"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"
"github.com/dlclark/regexp2"
2019-12-08 12:17:24 +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 {
Enable bool `provider:"enable"`
URL string `provider:"url"`
Interval int `provider:"interval"`
2024-01-24 12:45:35 +08:00
TestTimeout int `provider:"timeout,omitempty"`
Lazy bool `provider:"lazy,omitempty"`
ExpectedStatus string `provider:"expected-status,omitempty"`
2019-12-08 12:17:24 +08:00
}
type OverrideProxyNameSchema struct {
// matching expression for regex replacement
Pattern *regexp2.Regexp `provider:"pattern"`
// the new content after regex matching
Target string `provider:"target"`
}
var _ encoding.TextUnmarshaler = (*regexp2.Regexp)(nil) // ensure *regexp2.Regexp can decode direct by structure package
type OverrideSchema struct {
2024-06-16 18:19:04 +08:00
TFO *bool `provider:"tfo,omitempty"`
MPTcp *bool `provider:"mptcp,omitempty"`
2024-02-20 15:15:22 +08:00
UDP *bool `provider:"udp,omitempty"`
2024-06-16 18:19:04 +08:00
UDPOverTCP *bool `provider:"udp-over-tcp,omitempty"`
2024-02-20 15:15:22 +08:00
Up *string `provider:"up,omitempty"`
Down *string `provider:"down,omitempty"`
DialerProxy *string `provider:"dialer-proxy,omitempty"`
SkipCertVerify *bool `provider:"skip-cert-verify,omitempty"`
Interface *string `provider:"interface-name,omitempty"`
RoutingMark *int `provider:"routing-mark,omitempty"`
IPVersion *string `provider:"ip-version,omitempty"`
AdditionalPrefix *string `provider:"additional-prefix,omitempty"`
AdditionalSuffix *string `provider:"additional-suffix,omitempty"`
ProxyName []OverrideProxyNameSchema `provider:"proxy-name,omitempty"`
}
2019-12-08 12:17:24 +08:00
type proxyProviderSchema struct {
Type string `provider:"type"`
Path string `provider:"path,omitempty"`
URL string `provider:"url,omitempty"`
Proxy string `provider:"proxy,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"`
Header map[string][]string `provider:"header,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})
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
}
2024-01-20 11:00:06 +08:00
expectedStatus, err := utils.NewUnsignedRanges[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 {
2023-12-31 07:39:17 +08:00
if schema.HealthCheck.Interval == 0 {
schema.HealthCheck.Interval = 300
}
hcInterval = uint(schema.HealthCheck.Interval)
2019-12-08 12:17:24 +08:00
}
2024-01-24 12:45:35 +08:00
hc := NewHealthCheck([]C.Proxy{}, schema.HealthCheck.URL, uint(schema.HealthCheck.TestTimeout), hcInterval, schema.HealthCheck.Lazy, expectedStatus)
2019-12-08 12:17:24 +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)
vehicle = resource.NewFileVehicle(path)
2019-12-08 12:17:24 +08:00
case "http":
path := C.Path.GetPathByHash("proxies", schema.URL)
2023-06-15 22:45:02 +08:00
if schema.Path != "" {
path = C.Path.Resolve(schema.Path)
2024-09-11 16:10:35 +08:00
if !C.Path.IsSafePath(path) {
2023-06-15 22:45:02 +08:00
return nil, fmt.Errorf("%w: %s", errSubPath, path)
}
}
2024-09-22 13:57:57 +08:00
vehicle = resource.NewHTTPVehicle(schema.URL, path, schema.Proxy, schema.Header, resource.DefaultHttpTimeout)
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
filter := schema.Filter
excludeFilter := schema.ExcludeFilter
excludeType := schema.ExcludeType
dialerProxy := schema.DialerProxy
override := schema.Override
return NewProxySetProvider(name, interval, filter, excludeFilter, excludeType, dialerProxy, override, vehicle, hc)
2019-12-08 12:17:24 +08:00
}