2020-04-26 14:38:15 +00:00
|
|
|
package provider
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/md5"
|
|
|
|
"os"
|
2020-06-06 16:35:32 +00:00
|
|
|
"path/filepath"
|
2020-04-26 14:38:15 +00:00
|
|
|
"time"
|
|
|
|
|
2021-07-04 12:32:59 +00:00
|
|
|
types "github.com/Dreamacro/clash/constant/provider"
|
2020-04-26 14:38:15 +00:00
|
|
|
"github.com/Dreamacro/clash/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-10-10 15:44:09 +00:00
|
|
|
fileMode os.FileMode = 0o666
|
|
|
|
dirMode os.FileMode = 0o755
|
2020-04-26 14:38:15 +00:00
|
|
|
)
|
|
|
|
|
2022-06-05 08:54:56 +00:00
|
|
|
type parser[V any] func([]byte) (V, error)
|
2020-04-26 14:38:15 +00:00
|
|
|
|
2022-06-05 08:54:56 +00:00
|
|
|
type fetcher[V any] struct {
|
2020-04-26 14:38:15 +00:00
|
|
|
name string
|
2021-07-04 12:32:59 +00:00
|
|
|
vehicle types.Vehicle
|
2020-04-26 14:38:15 +00:00
|
|
|
updatedAt *time.Time
|
|
|
|
ticker *time.Ticker
|
2020-07-12 16:25:54 +00:00
|
|
|
done chan struct{}
|
2020-04-26 14:38:15 +00:00
|
|
|
hash [16]byte
|
2022-06-05 08:54:56 +00:00
|
|
|
parser parser[V]
|
2022-05-26 13:05:00 +00:00
|
|
|
interval time.Duration
|
2022-06-05 08:54:56 +00:00
|
|
|
onUpdate func(V)
|
2020-04-26 14:38:15 +00:00
|
|
|
}
|
|
|
|
|
2022-06-05 08:54:56 +00:00
|
|
|
func (f *fetcher[V]) Name() string {
|
2020-04-26 14:38:15 +00:00
|
|
|
return f.name
|
|
|
|
}
|
|
|
|
|
2022-06-05 08:54:56 +00:00
|
|
|
func (f *fetcher[V]) VehicleType() types.VehicleType {
|
2020-04-26 14:38:15 +00:00
|
|
|
return f.vehicle.Type()
|
|
|
|
}
|
|
|
|
|
2022-06-05 08:54:56 +00:00
|
|
|
func (f *fetcher[V]) Initial() (V, error) {
|
2020-05-29 13:56:29 +00:00
|
|
|
var (
|
|
|
|
buf []byte
|
|
|
|
err error
|
|
|
|
isLocal bool
|
|
|
|
)
|
2022-05-07 16:04:16 +00:00
|
|
|
|
2020-05-29 13:56:29 +00:00
|
|
|
if stat, fErr := os.Stat(f.vehicle.Path()); fErr == nil {
|
2021-10-09 12:35:06 +00:00
|
|
|
buf, err = os.ReadFile(f.vehicle.Path())
|
2020-04-26 14:38:15 +00:00
|
|
|
modTime := stat.ModTime()
|
|
|
|
f.updatedAt = &modTime
|
|
|
|
isLocal = true
|
2022-05-26 13:05:00 +00:00
|
|
|
if f.interval != 0 && modTime.Add(f.interval).Before(time.Now()) {
|
|
|
|
defer func() {
|
2022-05-26 15:13:36 +00:00
|
|
|
log.Infoln("[Provider] %s's proxies not updated for a long time, force refresh", f.Name())
|
2022-06-05 08:54:56 +00:00
|
|
|
go f.Update()
|
2022-05-26 13:05:00 +00:00
|
|
|
}()
|
|
|
|
}
|
2020-04-26 14:38:15 +00:00
|
|
|
} else {
|
|
|
|
buf, err = f.vehicle.Read()
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2022-06-05 08:54:56 +00:00
|
|
|
return getZero[V](), err
|
2020-04-26 14:38:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
proxies, err := f.parser(buf)
|
|
|
|
if err != nil {
|
|
|
|
if !isLocal {
|
2022-06-05 08:54:56 +00:00
|
|
|
return getZero[V](), err
|
2020-04-26 14:38:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// parse local file error, fallback to remote
|
|
|
|
buf, err = f.vehicle.Read()
|
|
|
|
if err != nil {
|
2022-06-05 08:54:56 +00:00
|
|
|
return getZero[V](), err
|
2020-04-26 14:38:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
proxies, err = f.parser(buf)
|
|
|
|
if err != nil {
|
2022-06-05 08:54:56 +00:00
|
|
|
return getZero[V](), err
|
2020-04-26 14:38:15 +00:00
|
|
|
}
|
2021-04-19 09:40:38 +00:00
|
|
|
|
|
|
|
isLocal = false
|
2020-04-26 14:38:15 +00:00
|
|
|
}
|
|
|
|
|
2021-07-04 12:32:59 +00:00
|
|
|
if f.vehicle.Type() != types.File && !isLocal {
|
2020-07-18 11:32:40 +00:00
|
|
|
if err := safeWrite(f.vehicle.Path(), buf); err != nil {
|
2022-06-05 08:54:56 +00:00
|
|
|
return getZero[V](), err
|
2020-07-18 11:32:40 +00:00
|
|
|
}
|
2020-04-26 14:38:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
f.hash = md5.Sum(buf)
|
|
|
|
|
2022-06-05 08:54:56 +00:00
|
|
|
// pull proxies automatically
|
|
|
|
if f.ticker != nil {
|
|
|
|
go f.pullLoop()
|
|
|
|
}
|
|
|
|
|
2020-04-26 14:38:15 +00:00
|
|
|
return proxies, nil
|
|
|
|
}
|
|
|
|
|
2022-06-05 08:54:56 +00:00
|
|
|
func (f *fetcher[V]) Update() (V, bool, error) {
|
2020-04-26 14:38:15 +00:00
|
|
|
buf, err := f.vehicle.Read()
|
|
|
|
if err != nil {
|
2022-06-05 08:54:56 +00:00
|
|
|
return getZero[V](), false, err
|
2020-04-26 14:38:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
hash := md5.Sum(buf)
|
|
|
|
if bytes.Equal(f.hash[:], hash[:]) {
|
|
|
|
f.updatedAt = &now
|
2022-01-18 05:32:47 +00:00
|
|
|
os.Chtimes(f.vehicle.Path(), now, now)
|
2022-06-05 08:54:56 +00:00
|
|
|
return getZero[V](), true, nil
|
2020-04-26 14:38:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
proxies, err := f.parser(buf)
|
|
|
|
if err != nil {
|
2022-06-05 08:54:56 +00:00
|
|
|
return getZero[V](), false, err
|
2020-04-26 14:38:15 +00:00
|
|
|
}
|
|
|
|
|
2021-07-04 12:32:59 +00:00
|
|
|
if f.vehicle.Type() != types.File {
|
2021-01-06 06:20:15 +00:00
|
|
|
if err := safeWrite(f.vehicle.Path(), buf); err != nil {
|
2022-06-05 08:54:56 +00:00
|
|
|
return getZero[V](), false, err
|
2021-01-06 06:20:15 +00:00
|
|
|
}
|
2020-04-26 14:38:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
f.updatedAt = &now
|
|
|
|
f.hash = hash
|
|
|
|
|
|
|
|
return proxies, false, nil
|
|
|
|
}
|
|
|
|
|
2022-06-05 08:54:56 +00:00
|
|
|
func (f *fetcher[V]) Destroy() error {
|
2020-04-26 14:38:15 +00:00
|
|
|
if f.ticker != nil {
|
2020-07-12 16:25:54 +00:00
|
|
|
f.done <- struct{}{}
|
2020-04-26 14:38:15 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-06-05 08:54:56 +00:00
|
|
|
func (f *fetcher[V]) pullLoop() {
|
2020-07-12 16:25:54 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-f.ticker.C:
|
2022-06-05 08:54:56 +00:00
|
|
|
elm, same, err := f.Update()
|
|
|
|
if err != nil {
|
|
|
|
log.Warnln("[Provider] %s pull error: %s", f.Name(), err.Error())
|
2020-07-12 16:25:54 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-06-05 08:54:56 +00:00
|
|
|
if same {
|
|
|
|
log.Debugln("[Provider] %s's proxies doesn't change", f.Name())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infoln("[Provider] %s's proxies update", f.Name())
|
|
|
|
if f.onUpdate != nil {
|
|
|
|
f.onUpdate(elm)
|
|
|
|
}
|
2020-07-12 16:25:54 +00:00
|
|
|
case <-f.done:
|
|
|
|
f.ticker.Stop()
|
|
|
|
return
|
2020-04-26 14:38:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-06 16:35:32 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-09 12:35:06 +00:00
|
|
|
return os.WriteFile(path, buf, fileMode)
|
2020-06-06 16:35:32 +00:00
|
|
|
}
|
|
|
|
|
2022-06-05 08:54:56 +00:00
|
|
|
func newFetcher[V any](name string, interval time.Duration, vehicle types.Vehicle, parser parser[V], onUpdate func(V)) *fetcher[V] {
|
2020-04-26 14:38:15 +00:00
|
|
|
var ticker *time.Ticker
|
|
|
|
if interval != 0 {
|
|
|
|
ticker = time.NewTicker(interval)
|
|
|
|
}
|
|
|
|
|
2022-06-05 08:54:56 +00:00
|
|
|
return &fetcher[V]{
|
2020-04-26 14:38:15 +00:00
|
|
|
name: name,
|
|
|
|
ticker: ticker,
|
|
|
|
vehicle: vehicle,
|
|
|
|
parser: parser,
|
2020-07-12 16:25:54 +00:00
|
|
|
done: make(chan struct{}, 1),
|
2020-04-26 14:38:15 +00:00
|
|
|
onUpdate: onUpdate,
|
|
|
|
}
|
|
|
|
}
|
2022-06-05 08:54:56 +00:00
|
|
|
|
|
|
|
func getZero[V any]() V {
|
|
|
|
var result V
|
|
|
|
return result
|
|
|
|
}
|