forked from fossabot/clash
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
218 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
package provider | ||
|
||
import ( | ||
"bytes" | ||
"crypto/md5" | ||
"io/ioutil" | ||
"os" | ||
"time" | ||
|
||
"github.com/Dreamacro/clash/log" | ||
) | ||
|
||
var ( | ||
fileMode os.FileMode = 0666 | ||
) | ||
|
||
type parser = func([]byte) (interface{}, error) | ||
|
||
type fetcher struct { | ||
name string | ||
vehicle Vehicle | ||
updatedAt *time.Time | ||
ticker *time.Ticker | ||
hash [16]byte | ||
parser parser | ||
onUpdate func(interface{}) | ||
} | ||
|
||
func (f *fetcher) Name() string { | ||
return f.name | ||
} | ||
|
||
func (f *fetcher) VehicleType() VehicleType { | ||
return f.vehicle.Type() | ||
} | ||
|
||
func (f *fetcher) Initial() (interface{}, error) { | ||
var buf []byte | ||
var err error | ||
var isLocal bool | ||
if stat, err := os.Stat(f.vehicle.Path()); err == nil { | ||
buf, err = ioutil.ReadFile(f.vehicle.Path()) | ||
modTime := stat.ModTime() | ||
f.updatedAt = &modTime | ||
isLocal = true | ||
} 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 | ||
} | ||
} | ||
|
||
if err := ioutil.WriteFile(f.vehicle.Path(), buf, fileMode); err != nil { | ||
return nil, err | ||
} | ||
|
||
f.hash = md5.Sum(buf) | ||
|
||
// pull proxies automatically | ||
if f.ticker != nil { | ||
go f.pullLoop() | ||
} | ||
|
||
return proxies, 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 | ||
return nil, true, nil | ||
} | ||
|
||
proxies, err := f.parser(buf) | ||
if err != nil { | ||
return nil, false, err | ||
} | ||
|
||
if err := ioutil.WriteFile(f.vehicle.Path(), buf, fileMode); err != nil { | ||
return nil, false, err | ||
} | ||
|
||
f.updatedAt = &now | ||
f.hash = hash | ||
|
||
return proxies, false, nil | ||
} | ||
|
||
func (f *fetcher) Destroy() error { | ||
if f.ticker != nil { | ||
f.ticker.Stop() | ||
} | ||
return nil | ||
} | ||
|
||
func (f *fetcher) pullLoop() { | ||
for range f.ticker.C { | ||
elm, same, err := f.Update() | ||
if err != nil { | ||
log.Warnln("[Provider] %s pull error: %s", f.Name(), err.Error()) | ||
continue | ||
} | ||
|
||
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) | ||
} | ||
} | ||
} | ||
|
||
func newFetcher(name string, interval time.Duration, vehicle Vehicle, parser parser, onUpdate func(interface{})) *fetcher { | ||
var ticker *time.Ticker | ||
if interval != 0 { | ||
ticker = time.NewTicker(interval) | ||
} | ||
|
||
return &fetcher{ | ||
name: name, | ||
ticker: ticker, | ||
vehicle: vehicle, | ||
parser: parser, | ||
onUpdate: onUpdate, | ||
} | ||
} |
Oops, something went wrong.