feat: optional provider path (#624)

This commit is contained in:
汐殇 2023-06-15 22:45:02 +08:00 committed by Larvan2
parent c8a3d6edd9
commit a2334430c1
3 changed files with 39 additions and 10 deletions

View file

@ -27,7 +27,7 @@ type healthCheckSchema struct {
type proxyProviderSchema struct { type proxyProviderSchema struct {
Type string `provider:"type"` Type string `provider:"type"`
Path string `provider:"path"` Path string `provider:"path,omitempty"`
URL string `provider:"url,omitempty"` URL string `provider:"url,omitempty"`
Interval int `provider:"interval,omitempty"` Interval int `provider:"interval,omitempty"`
Filter string `provider:"filter,omitempty"` Filter string `provider:"filter,omitempty"`
@ -60,17 +60,22 @@ func ParseProxyProvider(name string, mapping map[string]any) (types.ProxyProvide
} }
hc := NewHealthCheck([]C.Proxy{}, schema.HealthCheck.URL, hcInterval, schema.HealthCheck.Lazy, expectedStatus) hc := NewHealthCheck([]C.Proxy{}, schema.HealthCheck.URL, hcInterval, schema.HealthCheck.Lazy, expectedStatus)
path := C.Path.Resolve(schema.Path)
var vehicle types.Vehicle var vehicle types.Vehicle
switch schema.Type { switch schema.Type {
case "file": case "file":
path := C.Path.Resolve(schema.Path)
vehicle = resource.NewFileVehicle(path) vehicle = resource.NewFileVehicle(path)
case "http": case "http":
if schema.Path != "" {
path := C.Path.Resolve(schema.Path)
if !C.Path.IsSafePath(path) { if !C.Path.IsSafePath(path) {
return nil, fmt.Errorf("%w: %s", errSubPath, path) return nil, fmt.Errorf("%w: %s", errSubPath, path)
} }
vehicle = resource.NewHTTPVehicle(schema.URL, path) vehicle = resource.NewHTTPVehicle(schema.URL, path)
} else {
path := C.Path.GetRandomPath("proxies", schema.URL)
vehicle = resource.NewHTTPVehicle(schema.URL, path)
}
default: default:
return nil, fmt.Errorf("%w: %s", errVehicleType, schema.Type) return nil, fmt.Errorf("%w: %s", errVehicleType, schema.Type)
} }

View file

@ -1,6 +1,8 @@
package constant package constant
import ( import (
"crypto/md5"
"encoding/hex"
"os" "os"
P "path" P "path"
"path/filepath" "path/filepath"
@ -72,6 +74,12 @@ func (p *path) IsSafePath(path string) bool {
return !strings.Contains(rel, "..") return !strings.Contains(rel, "..")
} }
func (p *path) GetRandomPath(prefix, name string) string {
hash := md5.Sum([]byte(name))
filename := hex.EncodeToString(hash[:])
return filepath.Join(p.HomeDir(), prefix, filename)
}
func (p *path) MMDB() string { func (p *path) MMDB() string {
files, err := os.ReadDir(p.homeDir) files, err := os.ReadDir(p.homeDir)
if err != nil { if err != nil {

View file

@ -1,18 +1,24 @@
package provider package provider
import ( import (
"errors"
"fmt" "fmt"
"time"
"github.com/Dreamacro/clash/common/structure" "github.com/Dreamacro/clash/common/structure"
"github.com/Dreamacro/clash/component/resource" "github.com/Dreamacro/clash/component/resource"
C "github.com/Dreamacro/clash/constant" C "github.com/Dreamacro/clash/constant"
P "github.com/Dreamacro/clash/constant/provider" P "github.com/Dreamacro/clash/constant/provider"
"time" )
var (
errSubPath = errors.New("path is not subpath of home directory")
) )
type ruleProviderSchema struct { type ruleProviderSchema struct {
Type string `provider:"type"` Type string `provider:"type"`
Behavior string `provider:"behavior"` Behavior string `provider:"behavior"`
Path string `provider:"path"` Path string `provider:"path,omitempty"`
URL string `provider:"url,omitempty"` URL string `provider:"url,omitempty"`
Format string `provider:"format,omitempty"` Format string `provider:"format,omitempty"`
Interval int `provider:"interval,omitempty"` Interval int `provider:"interval,omitempty"`
@ -48,13 +54,23 @@ func ParseRuleProvider(name string, mapping map[string]interface{}, parse func(t
return nil, fmt.Errorf("unsupported format type: %s", schema.Format) return nil, fmt.Errorf("unsupported format type: %s", schema.Format)
} }
path := C.Path.Resolve(schema.Path)
var vehicle P.Vehicle var vehicle P.Vehicle
switch schema.Type { switch schema.Type {
case "file": case "file":
path := C.Path.Resolve(schema.Path)
vehicle = resource.NewFileVehicle(path) vehicle = resource.NewFileVehicle(path)
case "http": case "http":
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) vehicle = resource.NewHTTPVehicle(schema.URL, path)
} else {
path := C.Path.GetRandomPath("rules", schema.URL)
vehicle = resource.NewHTTPVehicle(schema.URL, path)
}
default: default:
return nil, fmt.Errorf("unsupported vehicle type: %s", schema.Type) return nil, fmt.Errorf("unsupported vehicle type: %s", schema.Type)
} }