mihomo/component/resource/fetcher.go

215 lines
4 KiB
Go
Raw Normal View History

package resource
2020-04-26 22:38:15 +08:00
import (
"bytes"
"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
"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
)
type Parser[V any] func([]byte) (V, error)
type Fetcher[V any] struct {
resourceType string
name string
vehicle types.Vehicle
UpdatedAt *time.Time
ticker *time.Ticker
done chan struct{}
hash [16]byte
parser Parser[V]
interval time.Duration
OnUpdate func(V)
2020-04-26 22:38:15 +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
}
func (f *Fetcher[V]) VehicleType() types.VehicleType {
2020-04-26 22:38:15 +08:00
return f.vehicle.Type()
}
func (f *Fetcher[V]) Initial() (V, error) {
var (
buf []byte
err error
isLocal bool
forceUpdate bool
)
2022-05-08 00:04:16 +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
2020-04-26 22:38:15 +08:00
isLocal = true
if f.interval != 0 && modTime.Add(f.interval).Before(time.Now()) {
log.Warnln("[Provider] %s not updated for %s, force update", f.Name(), time.Now().Sub(modTime))
forceUpdate = true
}
2020-04-26 22:38:15 +08:00
} else {
buf, err = f.vehicle.Read()
}
if err != nil {
2023-08-26 21:19:53 +08:00
return lo.Empty[V](), err
2020-04-26 22:38:15 +08:00
}
var contents V
if forceUpdate {
var forceBuf []byte
if forceBuf, err = f.vehicle.Read(); err == nil {
if contents, err = f.parser(forceBuf); err == nil {
isLocal = false
buf = forceBuf
}
}
}
if err != nil || !forceUpdate {
contents, err = f.parser(buf)
}
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
buf, err = f.vehicle.Read()
if err != nil {
2023-08-26 21:19:53 +08:00
return lo.Empty[V](), err
2020-04-26 22:38:15 +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
}
isLocal = false
2020-04-26 22:38:15 +08:00
}
if f.vehicle.Type() != types.File && !isLocal {
if err := safeWrite(f.vehicle.Path(), buf); err != nil {
2023-08-26 21:19:53 +08:00
return lo.Empty[V](), err
}
2020-04-26 22:38:15 +08:00
}
f.hash = md5.Sum(buf)
// pull contents automatically
if f.ticker != nil {
go f.pullLoop()
}
return contents, nil
2020-04-26 22:38:15 +08:00
}
func (f *Fetcher[V]) Update() (V, bool, error) {
2020-04-26 22:38:15 +08:00
buf, err := f.vehicle.Read()
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
}
now := time.Now()
hash := md5.Sum(buf)
if bytes.Equal(f.hash[:], hash[:]) {
f.UpdatedAt = &now
_ = 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
}
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
}
if f.vehicle.Type() != types.File {
if err := safeWrite(f.vehicle.Path(), buf); err != nil {
2023-08-26 21:19:53 +08:00
return lo.Empty[V](), false, err
}
2020-04-26 22:38:15 +08:00
}
f.UpdatedAt = &now
2020-04-26 22:38:15 +08:00
f.hash = hash
return contents, false, nil
2020-04-26 22:38:15 +08:00
}
func (f *Fetcher[V]) Destroy() error {
2020-04-26 22:38:15 +08:00
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
}
func (f *Fetcher[V]) pullLoop() {
2020-07-13 00:25:54 +08:00
for {
select {
case <-f.ticker.C:
elm, same, err := f.Update()
if err != nil {
2023-01-23 11:14:45 +08:00
log.Errorln("[Provider] %s pull error: %s", f.Name(), err.Error())
2020-07-13 00:25:54 +08:00
continue
}
if same {
log.Debugln("[Provider] %s's content doesn't change", f.Name())
continue
}
log.Infoln("[Provider] %s's content update", f.Name())
if f.OnUpdate != nil {
f.OnUpdate(elm)
}
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
}
func NewFetcher[V any](name string, interval time.Duration, vehicle types.Vehicle, parser Parser[V], onUpdate func(V)) *Fetcher[V] {
2020-04-26 22:38:15 +08:00
var ticker *time.Ticker
if interval != 0 {
ticker = time.NewTicker(interval)
}
return &Fetcher[V]{
2020-04-26 22:38:15 +08:00
name: name,
ticker: ticker,
vehicle: vehicle,
parser: parser,
2020-07-13 00:25:54 +08:00
done: make(chan struct{}, 1),
OnUpdate: onUpdate,
interval: interval,
2020-04-26 22:38:15 +08:00
}
}