Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: optional provider path #624

Merged
merged 2 commits into from
Jun 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions adapter/provider/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type healthCheckSchema struct {

type proxyProviderSchema struct {
Type string `provider:"type"`
Path string `provider:"path"`
Path string `provider:"path,omitempty"`
URL string `provider:"url,omitempty"`
Interval int `provider:"interval,omitempty"`
Filter string `provider:"filter,omitempty"`
Expand Down Expand Up @@ -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)

path := C.Path.Resolve(schema.Path)

var vehicle types.Vehicle
switch schema.Type {
case "file":
path := C.Path.Resolve(schema.Path)
vehicle = resource.NewFileVehicle(path)
case "http":
if !C.Path.IsSafePath(path) {
return nil, fmt.Errorf("%w: %s", errSubPath, path)
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 {
path := C.Path.GetRandomPath("proxies", schema.URL)
vehicle = resource.NewHTTPVehicle(schema.URL, path)
}
vehicle = resource.NewHTTPVehicle(schema.URL, path)
default:
return nil, fmt.Errorf("%w: %s", errVehicleType, schema.Type)
}
Expand Down
8 changes: 8 additions & 0 deletions constant/path.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package constant

import (
"crypto/md5"
"encoding/hex"
"os"
P "path"
"path/filepath"
Expand Down Expand Up @@ -72,6 +74,12 @@ func (p *path) IsSafePath(path string) bool {
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 {
files, err := os.ReadDir(p.homeDir)
if err != nil {
Expand Down
24 changes: 20 additions & 4 deletions rules/provider/parse.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
package provider

import (
"errors"
"fmt"
"time"

"github.com/Dreamacro/clash/common/structure"
"github.com/Dreamacro/clash/component/resource"
C "github.com/Dreamacro/clash/constant"
P "github.com/Dreamacro/clash/constant/provider"
"time"
)

var (
errSubPath = errors.New("path is not subpath of home directory")
)

type ruleProviderSchema struct {
Type string `provider:"type"`
Behavior string `provider:"behavior"`
Path string `provider:"path"`
Path string `provider:"path,omitempty"`
URL string `provider:"url,omitempty"`
Format string `provider:"format,omitempty"`
Interval int `provider:"interval,omitempty"`
Expand Down Expand Up @@ -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)
}

path := C.Path.Resolve(schema.Path)
var vehicle P.Vehicle
switch schema.Type {
case "file":
path := C.Path.Resolve(schema.Path)
vehicle = resource.NewFileVehicle(path)
case "http":
vehicle = resource.NewHTTPVehicle(schema.URL, path)
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 {
path := C.Path.GetRandomPath("rules", schema.URL)
vehicle = resource.NewHTTPVehicle(schema.URL, path)
}

default:
return nil, fmt.Errorf("unsupported vehicle type: %s", schema.Type)
}
Expand Down