2022-07-11 21:30:34 +08:00
|
|
|
package resource
|
2020-04-26 22:38:15 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2024-08-27 11:04:42 +08:00
|
|
|
"context"
|
2020-04-26 22:38:15 +08:00
|
|
|
"crypto/md5"
|
|
|
|
"os"
|
2020-06-07 00:35:32 +08:00
|
|
|
"path/filepath"
|
2020-04-26 22:38:15 +08:00
|
|
|
"time"
|
|
|
|
|
2023-11-03 21:01:45 +08:00
|
|
|
types "github.com/metacubex/mihomo/constant/provider"
|
|
|
|
"github.com/metacubex/mihomo/log"
|
2023-08-26 21:19:53 +08:00
|
|
|
|
2024-07-24 14:26:15 +08:00
|
|
|
"github.com/sagernet/fswatch"
|
2023-08-26 21:19:53 +08:00
|
|
|
"github.com/samber/lo"
|
2020-04-26 22:38:15 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
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-07-11 21:30:34 +08:00
|
|
|
type Parser[V any] func([]byte) (V, error)
|
|
|
|
|
|
|
|
type Fetcher[V any] struct {
|
2024-08-27 11:04:42 +08:00
|
|
|
ctx context.Context
|
|
|
|
ctxCancel context.CancelFunc
|
2022-07-11 21:30:34 +08:00
|
|
|
resourceType string
|
|
|
|
name string
|
|
|
|
vehicle types.Vehicle
|
2024-08-27 11:04:42 +08:00
|
|
|
updatedAt time.Time
|
2022-07-11 21:30:34 +08:00
|
|
|
hash [16]byte
|
|
|
|
parser Parser[V]
|
|
|
|
interval time.Duration
|
|
|
|
OnUpdate func(V)
|
2024-07-24 14:26:15 +08:00
|
|
|
watcher *fswatch.Watcher
|
2020-04-26 22:38:15 +08:00
|
|
|
}
|
|
|
|
|
2022-07-11 21:30:34 +08:00
|
|
|
func (f *Fetcher[V]) Name() string {
|
2020-04-26 22:38:15 +08:00
|
|
|
return f.name
|
|
|
|
}
|
|
|
|
|
2022-11-05 02:24:08 +08:00
|
|
|
func (f *Fetcher[V]) Vehicle() types.Vehicle {
|
|
|
|
return f.vehicle
|
|
|
|
}
|
|
|
|
|
2022-07-11 21:30:34 +08:00
|
|
|
func (f *Fetcher[V]) VehicleType() types.VehicleType {
|
2020-04-26 22:38:15 +08:00
|
|
|
return f.vehicle.Type()
|
|
|
|
}
|
|
|
|
|
2024-08-27 11:04:42 +08:00
|
|
|
func (f *Fetcher[V]) UpdatedAt() time.Time {
|
|
|
|
return f.updatedAt
|
|
|
|
}
|
|
|
|
|
2022-07-11 21:30:34 +08:00
|
|
|
func (f *Fetcher[V]) Initial() (V, error) {
|
2020-05-29 21:56:29 +08:00
|
|
|
var (
|
2022-06-18 16:05:09 +08:00
|
|
|
buf []byte
|
|
|
|
err error
|
|
|
|
isLocal bool
|
|
|
|
forceUpdate bool
|
2020-05-29 21:56:29 +08:00
|
|
|
)
|
2022-05-08 00:04:16 +08:00
|
|
|
|
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()
|
2024-08-27 11:04:42 +08:00
|
|
|
f.updatedAt = modTime
|
2020-04-26 22:38:15 +08:00
|
|
|
isLocal = true
|
2022-05-26 21:05:00 +08:00
|
|
|
if f.interval != 0 && modTime.Add(f.interval).Before(time.Now()) {
|
2023-11-17 13:19:24 +08:00
|
|
|
log.Warnln("[Provider] %s not updated for a long time, force refresh", f.Name())
|
2022-06-18 16:05:09 +08:00
|
|
|
forceUpdate = true
|
2022-05-26 21:05:00 +08:00
|
|
|
}
|
2020-04-26 22:38:15 +08:00
|
|
|
} else {
|
2024-08-27 11:04:42 +08:00
|
|
|
buf, err = f.vehicle.Read(f.ctx)
|
|
|
|
f.updatedAt = time.Now()
|
2020-04-26 22:38:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2023-08-26 21:19:53 +08:00
|
|
|
return lo.Empty[V](), err
|
2020-04-26 22:38:15 +08:00
|
|
|
}
|
|
|
|
|
2022-07-11 21:30:34 +08:00
|
|
|
var contents V
|
2022-06-18 16:05:09 +08:00
|
|
|
if forceUpdate {
|
|
|
|
var forceBuf []byte
|
2024-08-27 11:04:42 +08:00
|
|
|
if forceBuf, err = f.vehicle.Read(f.ctx); err == nil {
|
2022-07-11 21:30:34 +08:00
|
|
|
if contents, err = f.parser(forceBuf); err == nil {
|
2022-06-18 16:05:09 +08:00
|
|
|
isLocal = false
|
|
|
|
buf = forceBuf
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil || !forceUpdate {
|
2022-07-11 21:30:34 +08:00
|
|
|
contents, err = f.parser(buf)
|
2022-06-18 16:05:09 +08:00
|
|
|
}
|
|
|
|
|
2020-04-26 22:38:15 +08:00
|
|
|
if err != nil {
|
|
|
|
if !isLocal {
|
2023-08-26 21:19:53 +08:00
|
|
|
return lo.Empty[V](), err
|
2020-04-26 22:38:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// parse local file error, fallback to remote
|
2024-08-27 11:04:42 +08:00
|
|
|
buf, err = f.vehicle.Read(f.ctx)
|
2020-04-26 22:38:15 +08:00
|
|
|
if err != nil {
|
2023-08-26 21:19:53 +08:00
|
|
|
return lo.Empty[V](), err
|
2020-04-26 22:38:15 +08:00
|
|
|
}
|
|
|
|
|
2022-07-11 21:30:34 +08:00
|
|
|
contents, err = f.parser(buf)
|
2020-04-26 22:38:15 +08:00
|
|
|
if err != nil {
|
2023-08-26 21:19:53 +08:00
|
|
|
return lo.Empty[V](), err
|
2020-04-26 22:38:15 +08:00
|
|
|
}
|
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 {
|
2023-08-26 21:19:53 +08:00
|
|
|
return lo.Empty[V](), err
|
2020-07-18 19:32:40 +08:00
|
|
|
}
|
2020-04-26 22:38:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
f.hash = md5.Sum(buf)
|
|
|
|
|
2022-07-11 21:30:34 +08:00
|
|
|
// pull contents automatically
|
2024-07-24 14:26:15 +08:00
|
|
|
if f.vehicle.Type() == types.File {
|
|
|
|
f.watcher, err = fswatch.NewWatcher(fswatch.Options{
|
|
|
|
Path: []string{f.vehicle.Path()},
|
|
|
|
Direct: true,
|
|
|
|
Callback: f.update,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return lo.Empty[V](), err
|
|
|
|
}
|
|
|
|
err = f.watcher.Start()
|
|
|
|
if err != nil {
|
|
|
|
return lo.Empty[V](), err
|
|
|
|
}
|
|
|
|
} else if f.interval > 0 {
|
2022-06-05 16:54:56 +08:00
|
|
|
go f.pullLoop()
|
|
|
|
}
|
|
|
|
|
2022-07-11 21:30:34 +08:00
|
|
|
return contents, nil
|
2020-04-26 22:38:15 +08:00
|
|
|
}
|
|
|
|
|
2022-07-11 21:30:34 +08:00
|
|
|
func (f *Fetcher[V]) Update() (V, bool, error) {
|
2024-08-27 11:04:42 +08:00
|
|
|
buf, err := f.vehicle.Read(f.ctx)
|
2020-04-26 22:38:15 +08:00
|
|
|
if err != nil {
|
2023-08-26 21:19:53 +08:00
|
|
|
return lo.Empty[V](), false, err
|
2020-04-26 22:38:15 +08:00
|
|
|
}
|
2024-08-27 11:04:42 +08:00
|
|
|
return f.SideUpdate(buf)
|
|
|
|
}
|
2020-04-26 22:38:15 +08:00
|
|
|
|
2024-08-27 11:04:42 +08:00
|
|
|
func (f *Fetcher[V]) SideUpdate(buf []byte) (V, bool, error) {
|
2020-04-26 22:38:15 +08:00
|
|
|
now := time.Now()
|
|
|
|
hash := md5.Sum(buf)
|
|
|
|
if bytes.Equal(f.hash[:], hash[:]) {
|
2024-08-27 11:04:42 +08:00
|
|
|
f.updatedAt = now
|
2022-07-11 21:30:34 +08:00
|
|
|
_ = os.Chtimes(f.vehicle.Path(), now, now)
|
2023-08-26 21:19:53 +08:00
|
|
|
return lo.Empty[V](), true, nil
|
2020-04-26 22:38:15 +08:00
|
|
|
}
|
|
|
|
|
2022-07-11 21:30:34 +08:00
|
|
|
contents, err := f.parser(buf)
|
2020-04-26 22:38:15 +08:00
|
|
|
if err != nil {
|
2023-08-26 21:19:53 +08:00
|
|
|
return lo.Empty[V](), false, err
|
2020-04-26 22:38:15 +08:00
|
|
|
}
|
|
|
|
|
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 {
|
2023-08-26 21:19:53 +08:00
|
|
|
return lo.Empty[V](), false, err
|
2021-01-06 14:20:15 +08:00
|
|
|
}
|
2020-04-26 22:38:15 +08:00
|
|
|
}
|
|
|
|
|
2024-08-27 11:04:42 +08:00
|
|
|
f.updatedAt = now
|
2020-04-26 22:38:15 +08:00
|
|
|
f.hash = hash
|
|
|
|
|
2022-07-11 21:30:34 +08:00
|
|
|
return contents, false, nil
|
2020-04-26 22:38:15 +08:00
|
|
|
}
|
|
|
|
|
2024-08-27 11:04:42 +08:00
|
|
|
func (f *Fetcher[V]) Close() error {
|
|
|
|
f.ctxCancel()
|
2024-07-24 14:26:15 +08:00
|
|
|
if f.watcher != nil {
|
|
|
|
_ = f.watcher.Close()
|
|
|
|
}
|
2020-04-26 22:38:15 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-07-11 21:30:34 +08:00
|
|
|
func (f *Fetcher[V]) pullLoop() {
|
2024-08-27 11:04:42 +08:00
|
|
|
initialInterval := f.interval - time.Since(f.updatedAt)
|
2024-01-24 11:33:37 +08:00
|
|
|
if initialInterval > f.interval {
|
|
|
|
initialInterval = f.interval
|
2023-11-17 13:19:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
timer := time.NewTimer(initialInterval)
|
|
|
|
defer timer.Stop()
|
2020-07-13 00:25:54 +08:00
|
|
|
for {
|
|
|
|
select {
|
2023-11-17 13:19:24 +08:00
|
|
|
case <-timer.C:
|
|
|
|
timer.Reset(f.interval)
|
2024-07-24 14:26:15 +08:00
|
|
|
f.update(f.vehicle.Path())
|
2024-08-27 11:04:42 +08:00
|
|
|
case <-f.ctx.Done():
|
2020-07-13 00:25:54 +08:00
|
|
|
return
|
2020-04-26 22:38:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-24 14:26:15 +08:00
|
|
|
func (f *Fetcher[V]) update(path string) {
|
|
|
|
elm, same, err := f.Update()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorln("[Provider] %s pull error: %s", f.Name(), err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if same {
|
|
|
|
log.Debugln("[Provider] %s's content doesn't change", f.Name())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infoln("[Provider] %s's content update", f.Name())
|
|
|
|
if f.OnUpdate != nil {
|
|
|
|
f.OnUpdate(elm)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-07-11 21:30:34 +08:00
|
|
|
func NewFetcher[V any](name string, interval time.Duration, vehicle types.Vehicle, parser Parser[V], onUpdate func(V)) *Fetcher[V] {
|
2024-08-27 11:04:42 +08:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2022-07-11 21:30:34 +08:00
|
|
|
return &Fetcher[V]{
|
2024-08-27 11:04:42 +08:00
|
|
|
ctx: ctx,
|
|
|
|
ctxCancel: cancel,
|
|
|
|
name: name,
|
|
|
|
vehicle: vehicle,
|
|
|
|
parser: parser,
|
|
|
|
OnUpdate: onUpdate,
|
|
|
|
interval: interval,
|
2020-04-26 22:38:15 +08:00
|
|
|
}
|
|
|
|
}
|