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