mirror of
https://gitclone.com/github.com/MetaCubeX/Clash.Meta
synced 2024-11-15 13:41:23 +08:00
61 lines
1.6 KiB
Go
61 lines
1.6 KiB
Go
package provider
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/Dreamacro/clash/common/structure"
|
|
C "github.com/Dreamacro/clash/constant"
|
|
)
|
|
|
|
var (
|
|
errVehicleType = errors.New("unsupport vehicle type")
|
|
)
|
|
|
|
type healthCheckSchema struct {
|
|
Enable bool `provider:"enable"`
|
|
URL string `provider:"url"`
|
|
Interval int `provider:"interval"`
|
|
}
|
|
|
|
type proxyProviderSchema struct {
|
|
Type string `provider:"type"`
|
|
Path string `provider:"path"`
|
|
URL string `provider:"url,omitempty"`
|
|
Interval int `provider:"interval,omitempty"`
|
|
HealthCheck healthCheckSchema `provider:"health-check,omitempty"`
|
|
}
|
|
|
|
func ParseProxyProvider(name string, mapping map[string]interface{}) (ProxyProvider, error) {
|
|
decoder := structure.NewDecoder(structure.Option{TagName: "provider", WeaklyTypedInput: true})
|
|
|
|
schema := &proxyProviderSchema{}
|
|
if err := decoder.Decode(mapping, schema); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var healthCheckOption *HealthCheckOption
|
|
if schema.HealthCheck.Enable {
|
|
healthCheckOption = &HealthCheckOption{
|
|
URL: schema.HealthCheck.URL,
|
|
Interval: uint(schema.HealthCheck.Interval),
|
|
}
|
|
}
|
|
|
|
path := C.Path.Reslove(schema.Path)
|
|
|
|
var vehicle Vehicle
|
|
switch schema.Type {
|
|
case "file":
|
|
vehicle = NewFileVehicle(path)
|
|
case "http":
|
|
vehicle = NewHTTPVehicle(schema.URL, path)
|
|
default:
|
|
return nil, fmt.Errorf("%w: %s", errVehicleType, schema.Type)
|
|
}
|
|
|
|
interval := time.Duration(uint(schema.Interval)) * time.Second
|
|
return NewProxySetProvider(name, interval, vehicle, healthCheckOption), nil
|
|
}
|