mihomo/adapter/provider/parser.go
wwqgtxx 72a126e580
Some checks failed
Build / build (map[abi:1 goarch:loong64 goos:linux output:loong64-abi1]) (push) Failing after 7m5s
Build / build (map[abi:2 goarch:loong64 goos:linux output:loong64-abi2]) (push) Failing after 4m10s
Build / build (map[goamd64:v1 goarch:amd64 goos:darwin goversion:1.20 output:amd64-compatible-go120]) (push) Failing after 2m35s
Build / build (map[goamd64:v1 goarch:amd64 goos:darwin goversion:1.22 output:amd64-compatible-go122]) (push) Failing after 2m47s
Build / build (map[goamd64:v1 goarch:amd64 goos:darwin output:amd64-compatible]) (push) Failing after 2m36s
Build / build (map[goamd64:v1 goarch:amd64 goos:freebsd output:amd64-compatible]) (push) Failing after 2m53s
Build / build (map[goamd64:v1 goarch:amd64 goos:linux goversion:1.20 output:amd64-compatible-go120 test:test]) (push) Failing after 4m52s
Build / build (map[goamd64:v1 goarch:amd64 goos:linux output:amd64-compatible test:test]) (push) Failing after 4m37s
Build / build (map[goamd64:v1 goarch:amd64 goos:windows goversion:1.20 output:amd64-compatible-go120]) (push) Failing after 3m48s
Build / build (map[goamd64:v1 goarch:amd64 goos:windows goversion:1.21 output:amd64-compatible-go121]) (push) Failing after 2m52s
Build / build (map[goamd64:v1 goarch:amd64 goos:windows goversion:1.22 output:amd64-compatible-go122]) (push) Failing after 4m7s
Build / build (map[goamd64:v1 goarch:amd64 goos:windows output:amd64-compatible]) (push) Failing after 2m44s
Build / build (map[goamd64:v3 goarch:amd64 goos:darwin goversion:1.20 output:amd64-go120]) (push) Failing after 3m13s
Build / build (map[goamd64:v3 goarch:amd64 goos:darwin goversion:1.22 output:amd64-go122]) (push) Failing after 3m11s
Build / build (map[goamd64:v3 goarch:amd64 goos:darwin output:amd64]) (push) Failing after 3m23s
Build / build (map[goamd64:v3 goarch:amd64 goos:freebsd output:amd64]) (push) Failing after 3m49s
Build / build (map[goamd64:v3 goarch:amd64 goos:linux goversion:1.20 output:amd64-go120]) (push) Failing after 3m8s
Build / build (map[goamd64:v3 goarch:amd64 goos:linux output:amd64]) (push) Failing after 3m55s
Build / build (map[goamd64:v3 goarch:amd64 goos:windows goversion:1.20 output:amd64-go120]) (push) Failing after 3m6s
Build / build (map[goamd64:v3 goarch:amd64 goos:windows goversion:1.21 output:amd64-go121]) (push) Failing after 3m12s
Build / build (map[goamd64:v3 goarch:amd64 goos:windows goversion:1.22 output:amd64-go122]) (push) Failing after 2m52s
Build / build (map[goamd64:v3 goarch:amd64 goos:windows output:amd64]) (push) Failing after 2m2s
Build / build (map[goarch:386 goos:android ndk:i686-linux-android34 output:386]) (push) Failing after 5m34s
Build / build (map[goarch:386 goos:freebsd output:386]) (push) Failing after 1m32s
Build / build (map[goarch:386 goos:linux goversion:1.20 output:386-go120]) (push) Failing after 3m31s
Build / build (map[goarch:386 goos:linux output:386]) (push) Failing after 3m40s
Build / build (map[goarch:386 goos:windows goversion:1.20 output:386-go120]) (push) Failing after 3m44s
Build / build (map[goarch:386 goos:windows goversion:1.21 output:386-go121]) (push) Failing after 3m18s
Build / build (map[goarch:386 goos:windows goversion:1.22 output:386-go122]) (push) Failing after 2m48s
Build / build (map[goarch:386 goos:windows output:386]) (push) Failing after 2m8s
Build / build (map[goarch:amd64 goos:android ndk:x86_64-linux-android34 output:amd64]) (push) Failing after 4m13s
Build / build (map[goarch:arm goarm:5 goos:linux output:armv5]) (push) Failing after 3m1s
Build / build (map[goarch:arm goarm:6 goos:linux output:armv6]) (push) Failing after 1m32s
Build / build (map[goarch:arm goarm:7 goos:linux output:armv7]) (push) Failing after 3m30s
Build / build (map[goarch:arm goarm:7 goos:windows output:armv7]) (push) Failing after 1m22s
Build / build (map[goarch:arm goos:android ndk:armv7a-linux-androideabi34 output:armv7]) (push) Failing after 4m30s
Build / build (map[goarch:arm64 goos:android ndk:aarch64-linux-android34 output:arm64-v8]) (push) Failing after 5m2s
Build / build (map[goarch:arm64 goos:darwin goversion:1.20 output:arm64-go120]) (push) Failing after 53s
Build / build (map[goarch:arm64 goos:darwin goversion:1.22 output:arm64-go122]) (push) Failing after 3m11s
Build / build (map[goarch:arm64 goos:darwin output:arm64]) (push) Failing after 4m1s
Build / build (map[goarch:arm64 goos:freebsd output:arm64]) (push) Failing after 3m11s
Build / build (map[goarch:arm64 goos:linux output:arm64]) (push) Failing after 1m53s
Build / build (map[goarch:arm64 goos:windows output:arm64]) (push) Failing after 29s
Build / build (map[goarch:mips gomips:hardfloat goos:linux output:mips-hardfloat]) (push) Failing after 33s
Build / build (map[goarch:mips gomips:softfloat goos:linux output:mips-softfloat]) (push) Failing after 2m23s
Build / build (map[goarch:mips64 goos:linux output:mips64]) (push) Failing after 3m47s
Build / build (map[goarch:mips64le goos:linux output:mips64le]) (push) Failing after 53s
Build / build (map[goarch:mipsle gomips:hardfloat goos:linux output:mipsle-hardfloat]) (push) Failing after 46s
Build / build (map[goarch:mipsle gomips:softfloat goos:linux output:mipsle-softfloat]) (push) Failing after 1m1s
Build / build (map[goarch:riscv64 goos:linux output:riscv64]) (push) Failing after 3m57s
Build / build (map[goarch:s390x goos:linux output:s390x]) (push) Failing after 2m0s
Trigger CMFA Update / trigger-CMFA-update (push) Failing after 57s
Build / Upload-Prerelease (push) Has been skipped
Build / Upload-Release (push) Has been skipped
Build / Docker (push) Has been skipped
feat: support inline proxy provider
2024-12-25 10:34:16 +08:00

132 lines
4.6 KiB
Go

package provider
import (
"encoding"
"errors"
"fmt"
"time"
"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"
)
var (
errVehicleType = errors.New("unsupport vehicle type")
errSubPath = errors.New("path is not subpath of home directory")
)
type healthCheckSchema struct {
Enable bool `provider:"enable"`
URL string `provider:"url"`
Interval int `provider:"interval"`
TestTimeout int `provider:"timeout,omitempty"`
Lazy bool `provider:"lazy,omitempty"`
ExpectedStatus string `provider:"expected-status,omitempty"`
}
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 {
TFO *bool `provider:"tfo,omitempty"`
MPTcp *bool `provider:"mptcp,omitempty"`
UDP *bool `provider:"udp,omitempty"`
UDPOverTCP *bool `provider:"udp-over-tcp,omitempty"`
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"`
}
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"`
SizeLimit int64 `provider:"size-limit,omitempty"`
Payload []map[string]any `provider:"payload,omitempty"`
HealthCheck healthCheckSchema `provider:"health-check,omitempty"`
Override OverrideSchema `provider:"override,omitempty"`
Header map[string][]string `provider:"header,omitempty"`
}
func ParseProxyProvider(name string, mapping map[string]any) (types.ProxyProvider, error) {
decoder := structure.NewDecoder(structure.Option{TagName: "provider", WeaklyTypedInput: true})
schema := &proxyProviderSchema{
HealthCheck: healthCheckSchema{
Lazy: true,
},
}
if err := decoder.Decode(mapping, schema); err != nil {
return nil, err
}
expectedStatus, err := utils.NewUnsignedRanges[uint16](schema.HealthCheck.ExpectedStatus)
if err != nil {
return nil, err
}
var hcInterval uint
if schema.HealthCheck.Enable {
if schema.HealthCheck.Interval == 0 {
schema.HealthCheck.Interval = 300
}
hcInterval = uint(schema.HealthCheck.Interval)
}
hc := NewHealthCheck([]C.Proxy{}, schema.HealthCheck.URL, uint(schema.HealthCheck.TestTimeout), hcInterval, schema.HealthCheck.Lazy, expectedStatus)
parser, err := NewProxiesParser(schema.Filter, schema.ExcludeFilter, schema.ExcludeType, schema.DialerProxy, schema.Override)
if err != nil {
return nil, err
}
var vehicle types.Vehicle
switch schema.Type {
case "file":
path := C.Path.Resolve(schema.Path)
vehicle = resource.NewFileVehicle(path)
case "http":
path := C.Path.GetPathByHash("proxies", schema.URL)
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, schema.Proxy, schema.Header, resource.DefaultHttpTimeout, schema.SizeLimit)
case "inline":
return NewInlineProvider(name, schema.Payload, parser, hc)
default:
return nil, fmt.Errorf("%w: %s", errVehicleType, schema.Type)
}
interval := time.Duration(uint(schema.Interval)) * time.Second
return NewProxySetProvider(name, interval, parser, vehicle, hc)
}