Merge branch 'provider' into Alpha

This commit is contained in:
Skyxim 2022-07-11 22:17:56 +08:00
commit 23b2f3b971
7 changed files with 71 additions and 285 deletions

View file

@ -3,6 +3,7 @@ package provider
import ( import (
"errors" "errors"
"fmt" "fmt"
"github.com/Dreamacro/clash/component/resource"
"time" "time"
"github.com/Dreamacro/clash/common/structure" "github.com/Dreamacro/clash/common/structure"
@ -51,9 +52,9 @@ func ParseProxyProvider(name string, mapping map[string]any) (types.ProxyProvide
var vehicle types.Vehicle var vehicle types.Vehicle
switch schema.Type { switch schema.Type {
case "file": case "file":
vehicle = NewFileVehicle(path) vehicle = resource.NewFileVehicle(path)
case "http": case "http":
vehicle = NewHTTPVehicle(schema.URL, path) 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

@ -5,6 +5,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"github.com/Dreamacro/clash/common/convert" "github.com/Dreamacro/clash/common/convert"
"github.com/Dreamacro/clash/component/resource"
"github.com/dlclark/regexp2" "github.com/dlclark/regexp2"
"math" "math"
"runtime" "runtime"
@ -31,7 +32,7 @@ type ProxySetProvider struct {
} }
type proxySetProvider struct { type proxySetProvider struct {
*fetcher[[]C.Proxy] *resource.Fetcher[[]C.Proxy]
proxies []C.Proxy proxies []C.Proxy
healthCheck *HealthCheck healthCheck *HealthCheck
version uint version uint
@ -43,7 +44,7 @@ func (pp *proxySetProvider) MarshalJSON() ([]byte, error) {
"type": pp.Type().String(), "type": pp.Type().String(),
"vehicleType": pp.VehicleType().String(), "vehicleType": pp.VehicleType().String(),
"proxies": pp.Proxies(), "proxies": pp.Proxies(),
"updatedAt": pp.updatedAt, "updatedAt": pp.UpdatedAt,
}) })
} }
@ -52,7 +53,7 @@ func (pp *proxySetProvider) Version() uint {
} }
func (pp *proxySetProvider) Name() string { func (pp *proxySetProvider) Name() string {
return pp.name return pp.Fetcher.Name()
} }
func (pp *proxySetProvider) HealthCheck() { func (pp *proxySetProvider) HealthCheck() {
@ -60,19 +61,19 @@ func (pp *proxySetProvider) HealthCheck() {
} }
func (pp *proxySetProvider) Update() error { func (pp *proxySetProvider) Update() error {
elm, same, err := pp.fetcher.Update() elm, same, err := pp.Fetcher.Update()
if err == nil && !same { if err == nil && !same {
pp.onUpdate(elm) pp.OnUpdate(elm)
} }
return err return err
} }
func (pp *proxySetProvider) Initial() error { func (pp *proxySetProvider) Initial() error {
elm, err := pp.fetcher.Initial() elm, err := pp.Fetcher.Initial()
if err != nil { if err != nil {
return err return err
} }
pp.onUpdate(elm) pp.OnUpdate(elm)
return nil return nil
} }
@ -98,7 +99,7 @@ func (pp *proxySetProvider) setProxies(proxies []C.Proxy) {
func stopProxyProvider(pd *ProxySetProvider) { func stopProxyProvider(pd *ProxySetProvider) {
pd.healthCheck.close() pd.healthCheck.close()
_ = pd.fetcher.Destroy() _ = pd.Fetcher.Destroy()
} }
func NewProxySetProvider(name string, interval time.Duration, filter string, vehicle types.Vehicle, hc *HealthCheck) (*ProxySetProvider, error) { func NewProxySetProvider(name string, interval time.Duration, filter string, vehicle types.Vehicle, hc *HealthCheck) (*ProxySetProvider, error) {
@ -116,8 +117,8 @@ func NewProxySetProvider(name string, interval time.Duration, filter string, veh
healthCheck: hc, healthCheck: hc,
} }
fetcher := newFetcher[[]C.Proxy](name, interval, vehicle, proxiesParseAndFilter(filter, filterReg), proxiesOnUpdate(pd)) fetcher := resource.NewFetcher[[]C.Proxy](name, interval, vehicle, proxiesParseAndFilter(filter, filterReg), proxiesOnUpdate(pd))
pd.fetcher = fetcher pd.Fetcher = fetcher
wrapper := &ProxySetProvider{pd} wrapper := &ProxySetProvider{pd}
runtime.SetFinalizer(wrapper, stopProxyProvider) runtime.SetFinalizer(wrapper, stopProxyProvider)
@ -216,7 +217,7 @@ func proxiesOnUpdate(pd *proxySetProvider) func([]C.Proxy) {
} }
} }
func proxiesParseAndFilter(filter string, filterReg *regexp2.Regexp) parser[[]C.Proxy] { func proxiesParseAndFilter(filter string, filterReg *regexp2.Regexp) resource.Parser[[]C.Proxy] {
return func(buf []byte) ([]C.Proxy, error) { return func(buf []byte) ([]C.Proxy, error) {
schema := &ProxySchema{} schema := &ProxySchema{}

View file

@ -1,4 +1,4 @@
package provider package resource
import ( import (
"bytes" "bytes"
@ -16,29 +16,30 @@ var (
dirMode os.FileMode = 0o755 dirMode os.FileMode = 0o755
) )
type parser[V any] func([]byte) (V, error) type Parser[V any] func([]byte) (V, error)
type fetcher[V any] struct { type Fetcher[V any] struct {
resourceType string
name string name string
vehicle types.Vehicle vehicle types.Vehicle
updatedAt *time.Time UpdatedAt *time.Time
ticker *time.Ticker ticker *time.Ticker
done chan struct{} done chan struct{}
hash [16]byte hash [16]byte
parser parser[V] parser Parser[V]
interval time.Duration interval time.Duration
onUpdate func(V) OnUpdate func(V)
} }
func (f *fetcher[V]) Name() string { func (f *Fetcher[V]) Name() string {
return f.name return f.name
} }
func (f *fetcher[V]) VehicleType() types.VehicleType { func (f *Fetcher[V]) VehicleType() types.VehicleType {
return f.vehicle.Type() return f.vehicle.Type()
} }
func (f *fetcher[V]) Initial() (V, error) { func (f *Fetcher[V]) Initial() (V, error) {
var ( var (
buf []byte buf []byte
err error err error
@ -49,7 +50,7 @@ func (f *fetcher[V]) Initial() (V, error) {
if stat, fErr := os.Stat(f.vehicle.Path()); fErr == nil { if stat, fErr := os.Stat(f.vehicle.Path()); fErr == nil {
buf, err = os.ReadFile(f.vehicle.Path()) buf, err = os.ReadFile(f.vehicle.Path())
modTime := stat.ModTime() modTime := stat.ModTime()
f.updatedAt = &modTime f.UpdatedAt = &modTime
isLocal = true isLocal = true
if f.interval != 0 && modTime.Add(f.interval).Before(time.Now()) { if f.interval != 0 && modTime.Add(f.interval).Before(time.Now()) {
log.Infoln("[Provider] %s not updated for a long time, force refresh", f.Name()) log.Infoln("[Provider] %s not updated for a long time, force refresh", f.Name())
@ -63,11 +64,11 @@ func (f *fetcher[V]) Initial() (V, error) {
return getZero[V](), err return getZero[V](), err
} }
var proxies V var contents V
if forceUpdate { if forceUpdate {
var forceBuf []byte var forceBuf []byte
if forceBuf, err = f.vehicle.Read(); err == nil { if forceBuf, err = f.vehicle.Read(); err == nil {
if proxies, err = f.parser(forceBuf); err == nil { if contents, err = f.parser(forceBuf); err == nil {
isLocal = false isLocal = false
buf = forceBuf buf = forceBuf
} }
@ -75,7 +76,7 @@ func (f *fetcher[V]) Initial() (V, error) {
} }
if err != nil || !forceUpdate { if err != nil || !forceUpdate {
proxies, err = f.parser(buf) contents, err = f.parser(buf)
} }
if err != nil { if err != nil {
@ -89,7 +90,7 @@ func (f *fetcher[V]) Initial() (V, error) {
return getZero[V](), err return getZero[V](), err
} }
proxies, err = f.parser(buf) contents, err = f.parser(buf)
if err != nil { if err != nil {
return getZero[V](), err return getZero[V](), err
} }
@ -105,15 +106,15 @@ func (f *fetcher[V]) Initial() (V, error) {
f.hash = md5.Sum(buf) f.hash = md5.Sum(buf)
// pull proxies automatically // pull contents automatically
if f.ticker != nil { if f.ticker != nil {
go f.pullLoop() go f.pullLoop()
} }
return proxies, nil return contents, nil
} }
func (f *fetcher[V]) Update() (V, bool, error) { func (f *Fetcher[V]) Update() (V, bool, error) {
buf, err := f.vehicle.Read() buf, err := f.vehicle.Read()
if err != nil { if err != nil {
return getZero[V](), false, err return getZero[V](), false, err
@ -122,12 +123,12 @@ func (f *fetcher[V]) Update() (V, bool, error) {
now := time.Now() now := time.Now()
hash := md5.Sum(buf) hash := md5.Sum(buf)
if bytes.Equal(f.hash[:], hash[:]) { if bytes.Equal(f.hash[:], hash[:]) {
f.updatedAt = &now f.UpdatedAt = &now
os.Chtimes(f.vehicle.Path(), now, now) _ = os.Chtimes(f.vehicle.Path(), now, now)
return getZero[V](), true, nil return getZero[V](), true, nil
} }
proxies, err := f.parser(buf) contents, err := f.parser(buf)
if err != nil { if err != nil {
return getZero[V](), false, err return getZero[V](), false, err
} }
@ -138,20 +139,20 @@ func (f *fetcher[V]) Update() (V, bool, error) {
} }
} }
f.updatedAt = &now f.UpdatedAt = &now
f.hash = hash f.hash = hash
return proxies, false, nil return contents, false, nil
} }
func (f *fetcher[V]) Destroy() error { func (f *Fetcher[V]) Destroy() error {
if f.ticker != nil { if f.ticker != nil {
f.done <- struct{}{} f.done <- struct{}{}
} }
return nil return nil
} }
func (f *fetcher[V]) pullLoop() { func (f *Fetcher[V]) pullLoop() {
for { for {
select { select {
case <-f.ticker.C: case <-f.ticker.C:
@ -162,13 +163,13 @@ func (f *fetcher[V]) pullLoop() {
} }
if same { if same {
log.Debugln("[Provider] %s's proxies doesn't change", f.Name()) log.Debugln("[Provider] %s's content doesn't change", f.Name())
continue continue
} }
log.Infoln("[Provider] %s's proxies update", f.Name()) log.Infoln("[Provider] %s's content update", f.Name())
if f.onUpdate != nil { if f.OnUpdate != nil {
f.onUpdate(elm) f.OnUpdate(elm)
} }
case <-f.done: case <-f.done:
f.ticker.Stop() f.ticker.Stop()
@ -189,19 +190,19 @@ func safeWrite(path string, buf []byte) error {
return os.WriteFile(path, buf, fileMode) return os.WriteFile(path, buf, fileMode)
} }
func newFetcher[V any](name string, interval time.Duration, vehicle types.Vehicle, parser parser[V], onUpdate func(V)) *fetcher[V] { func NewFetcher[V any](name string, interval time.Duration, vehicle types.Vehicle, parser Parser[V], onUpdate func(V)) *Fetcher[V] {
var ticker *time.Ticker var ticker *time.Ticker
if interval != 0 { if interval != 0 {
ticker = time.NewTicker(interval) ticker = time.NewTicker(interval)
} }
return &fetcher[V]{ return &Fetcher[V]{
name: name, name: name,
ticker: ticker, ticker: ticker,
vehicle: vehicle, vehicle: vehicle,
parser: parser, parser: parser,
done: make(chan struct{}, 1), done: make(chan struct{}, 1),
onUpdate: onUpdate, OnUpdate: onUpdate,
interval: interval, interval: interval,
} }
} }

View file

@ -1,4 +1,4 @@
package provider package resource
import ( import (
"context" "context"

View file

@ -1,219 +0,0 @@
package provider
import (
"bytes"
"crypto/md5"
P "github.com/Dreamacro/clash/constant/provider"
"github.com/Dreamacro/clash/log"
"io/ioutil"
"os"
"path/filepath"
"time"
)
var (
fileMode os.FileMode = 0666
dirMode os.FileMode = 0755
)
type parser = func([]byte) (interface{}, error)
type fetcher struct {
name string
vehicle P.Vehicle
updatedAt *time.Time
ticker *time.Ticker
done chan struct{}
hash [16]byte
parser parser
onUpdate func(interface{}) error
interval time.Duration
}
func (f *fetcher) Name() string {
return f.name
}
func (f *fetcher) VehicleType() P.VehicleType {
return f.vehicle.Type()
}
func (f *fetcher) Initial() (interface{}, error) {
var (
buf []byte
hasLocal bool
err error
forceUpdate bool
)
defer func() {
if f.ticker != nil {
go f.pullLoop()
}
}()
if stat, fErr := os.Stat(f.vehicle.Path()); fErr == nil {
buf, err = ioutil.ReadFile(f.vehicle.Path())
modTime := stat.ModTime()
f.updatedAt = &modTime
hasLocal = true
if f.interval != 0 && modTime.Add(f.interval).Before(time.Now()) {
forceUpdate = true
log.Infoln("[Provider] %s not updated for a long time, force refresh", f.Name())
}
} else {
buf, err = f.vehicle.Read()
}
if err != nil {
return nil, err
}
var rules interface{}
if forceUpdate {
var forceBuf []byte
if forceBuf, err = f.vehicle.Read(); err == nil {
if rules, err = f.parser(forceBuf); err == nil {
hasLocal = false
buf = forceBuf
}
}
}
if err != nil || !forceUpdate {
rules, err = f.parser(buf)
}
if err != nil {
if !hasLocal {
return nil, err
}
buf, err = f.vehicle.Read()
if err != nil {
return nil, err
}
rules, err = f.parser(buf)
if err != nil {
return nil, err
}
hasLocal = false
}
if f.vehicle.Type() != P.File && !hasLocal {
if err := safeWrite(f.vehicle.Path(), buf); err != nil {
return nil, err
}
}
f.hash = md5.Sum(buf)
return rules, nil
}
func (f *fetcher) Update() (interface{}, bool, error) {
buf, err := f.vehicle.Read()
if err != nil {
return nil, false, err
}
now := time.Now()
hash := md5.Sum(buf)
if bytes.Equal(f.hash[:], hash[:]) {
f.updatedAt = &now
os.Chtimes(f.vehicle.Path(), now, now)
return nil, true, nil
}
rules, err := f.parser(buf)
if err != nil {
return nil, false, err
}
if f.vehicle.Type() != P.File {
if err := safeWrite(f.vehicle.Path(), buf); err != nil {
return nil, false, err
}
}
f.updatedAt = &now
f.hash = hash
return rules, false, nil
}
func (f *fetcher) Destroy() error {
if f.ticker != nil {
f.done <- struct{}{}
}
return nil
}
func newFetcher(name string, interval time.Duration, vehicle P.Vehicle, parser parser, onUpdate func(interface{}) error) *fetcher {
var ticker *time.Ticker
if interval != 0 {
ticker = time.NewTicker(interval)
}
return &fetcher{
name: name,
ticker: ticker,
vehicle: vehicle,
parser: parser,
done: make(chan struct{}, 1),
onUpdate: onUpdate,
interval: interval,
}
}
func safeWrite(path string, buf []byte) error {
dir := filepath.Dir(path)
if _, err := os.Stat(dir); os.IsNotExist(err) {
if err := os.MkdirAll(dir, dirMode); err != nil {
return err
}
}
return ioutil.WriteFile(path, buf, fileMode)
}
func (f *fetcher) pullLoop() {
for {
select {
case <-f.ticker.C:
same, err := f.update()
if same || err != nil {
continue
}
case <-f.done:
f.ticker.Stop()
return
}
}
}
func (f *fetcher) update() (same bool, err error) {
elm, same, err := f.Update()
if err != nil {
log.Warnln("[Provider] %s pull error: %s", f.Name(), err.Error())
return
}
if same {
log.Debugln("[Provider] %s's rules doesn't change", f.Name())
return
}
log.Infoln("[Provider] %s's rules update", f.Name())
if f.onUpdate != nil {
err := f.onUpdate(elm)
if err != nil {
log.Infoln("[Provider] %s update failed", f.Name())
}
}
return
}

View file

@ -2,8 +2,8 @@ package provider
import ( import (
"fmt" "fmt"
"github.com/Dreamacro/clash/adapter/provider"
"github.com/Dreamacro/clash/common/structure" "github.com/Dreamacro/clash/common/structure"
"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" "time"
@ -40,9 +40,9 @@ func ParseRuleProvider(name string, mapping map[string]interface{}, parse func(t
var vehicle P.Vehicle var vehicle P.Vehicle
switch schema.Type { switch schema.Type {
case "file": case "file":
vehicle = provider.NewFileVehicle(path) vehicle = resource.NewFileVehicle(path)
case "http": case "http":
vehicle = provider.NewHTTPVehicle(schema.URL, path) 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)
} }

View file

@ -2,6 +2,7 @@ package provider
import ( import (
"encoding/json" "encoding/json"
"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"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
@ -14,7 +15,7 @@ var (
) )
type ruleSetProvider struct { type ruleSetProvider struct {
*fetcher *resource.Fetcher[any]
behavior P.RuleType behavior P.RuleType
strategy ruleStrategy strategy ruleStrategy
} }
@ -54,18 +55,20 @@ func (rp *ruleSetProvider) Type() P.ProviderType {
} }
func (rp *ruleSetProvider) Initial() error { func (rp *ruleSetProvider) Initial() error {
elm, err := rp.fetcher.Initial() elm, err := rp.Fetcher.Initial()
if err != nil { if err != nil {
return err return err
} }
return rp.fetcher.onUpdate(elm) rp.OnUpdate(elm)
return nil
} }
func (rp *ruleSetProvider) Update() error { func (rp *ruleSetProvider) Update() error {
elm, same, err := rp.fetcher.Update() elm, same, err := rp.Fetcher.Update()
if err == nil && !same { if err == nil && !same {
return rp.fetcher.onUpdate(elm) rp.OnUpdate(elm)
return nil
} }
return err return err
@ -94,7 +97,7 @@ func (rp *ruleSetProvider) MarshalJSON() ([]byte, error) {
"name": rp.Name(), "name": rp.Name(),
"ruleCount": rp.strategy.Count(), "ruleCount": rp.strategy.Count(),
"type": rp.Type().String(), "type": rp.Type().String(),
"updatedAt": rp.updatedAt, "updatedAt": rp.UpdatedAt,
"vehicleType": rp.VehicleType().String(), "vehicleType": rp.VehicleType().String(),
}) })
} }
@ -105,21 +108,20 @@ func NewRuleSetProvider(name string, behavior P.RuleType, interval time.Duration
behavior: behavior, behavior: behavior,
} }
onUpdate := func(elm interface{}) error { onUpdate := func(elm interface{}) {
rulesRaw := elm.([]string) rulesRaw := elm.([]string)
rp.strategy.OnUpdate(rulesRaw) rp.strategy.OnUpdate(rulesRaw)
return nil
} }
fetcher := newFetcher(name, interval, vehicle, rulesParse, onUpdate) fetcher := resource.NewFetcher(name, interval, vehicle, rulesParse, onUpdate)
rp.fetcher = fetcher rp.Fetcher = fetcher
rp.strategy = newStrategy(behavior, parse) rp.strategy = newStrategy(behavior, parse)
wrapper := &RuleSetProvider{ wrapper := &RuleSetProvider{
rp, rp,
} }
final := func(provider *RuleSetProvider) { rp.fetcher.Destroy() } final := func(provider *RuleSetProvider) { _ = rp.Fetcher.Destroy() }
runtime.SetFinalizer(wrapper, final) runtime.SetFinalizer(wrapper, final)
return wrapper return wrapper
} }
@ -140,7 +142,7 @@ func newStrategy(behavior P.RuleType, parse func(tp, payload, target string, par
} }
} }
func rulesParse(buf []byte) (interface{}, error) { func rulesParse(buf []byte) (any, error) {
rulePayload := RulePayload{} rulePayload := RulePayload{}
err := yaml.Unmarshal(buf, &rulePayload) err := yaml.Unmarshal(buf, &rulePayload)
if err != nil { if err != nil {