2019-12-08 12:17:24 +08:00
|
|
|
package provider
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/md5"
|
2019-12-11 17:31:15 +08:00
|
|
|
"encoding/json"
|
2019-12-08 12:17:24 +08:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/Dreamacro/clash/adapters/outbound"
|
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
"github.com/Dreamacro/clash/log"
|
|
|
|
|
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
ReservedName = "default"
|
|
|
|
|
|
|
|
fileMode = 0666
|
|
|
|
)
|
|
|
|
|
|
|
|
// Provider Type
|
|
|
|
const (
|
|
|
|
Proxy ProviderType = iota
|
|
|
|
Rule
|
|
|
|
)
|
|
|
|
|
|
|
|
// ProviderType defined
|
|
|
|
type ProviderType int
|
|
|
|
|
|
|
|
func (pt ProviderType) String() string {
|
|
|
|
switch pt {
|
|
|
|
case Proxy:
|
|
|
|
return "Proxy"
|
|
|
|
case Rule:
|
|
|
|
return "Rule"
|
|
|
|
default:
|
|
|
|
return "Unknown"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Provider interface
|
|
|
|
type Provider interface {
|
|
|
|
Name() string
|
|
|
|
VehicleType() VehicleType
|
|
|
|
Type() ProviderType
|
|
|
|
Initial() error
|
|
|
|
Reload() error
|
|
|
|
Destroy() error
|
|
|
|
}
|
|
|
|
|
|
|
|
// ProxyProvider interface
|
|
|
|
type ProxyProvider interface {
|
|
|
|
Provider
|
|
|
|
Proxies() []C.Proxy
|
2019-12-11 17:31:15 +08:00
|
|
|
HealthCheck()
|
2019-12-13 00:29:24 +08:00
|
|
|
Update() error
|
2019-12-08 12:17:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type ProxySchema struct {
|
|
|
|
Proxies []map[string]interface{} `yaml:"proxies"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type ProxySetProvider struct {
|
2019-12-26 18:41:06 +08:00
|
|
|
name string
|
|
|
|
vehicle Vehicle
|
|
|
|
hash [16]byte
|
|
|
|
proxies []C.Proxy
|
|
|
|
healthCheck *HealthCheck
|
|
|
|
ticker *time.Ticker
|
|
|
|
updatedAt *time.Time
|
2019-12-08 12:17:24 +08:00
|
|
|
}
|
|
|
|
|
2019-12-11 17:31:15 +08:00
|
|
|
func (pp *ProxySetProvider) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(map[string]interface{}{
|
|
|
|
"name": pp.Name(),
|
|
|
|
"type": pp.Type().String(),
|
|
|
|
"vehicleType": pp.VehicleType().String(),
|
|
|
|
"proxies": pp.Proxies(),
|
|
|
|
"updatedAt": pp.updatedAt,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-12-08 12:17:24 +08:00
|
|
|
func (pp *ProxySetProvider) Name() string {
|
|
|
|
return pp.name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pp *ProxySetProvider) Reload() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-11 17:31:15 +08:00
|
|
|
func (pp *ProxySetProvider) HealthCheck() {
|
2019-12-26 18:41:06 +08:00
|
|
|
pp.healthCheck.check()
|
2019-12-11 17:31:15 +08:00
|
|
|
}
|
|
|
|
|
2019-12-13 00:29:24 +08:00
|
|
|
func (pp *ProxySetProvider) Update() error {
|
|
|
|
return pp.pull()
|
|
|
|
}
|
|
|
|
|
2019-12-08 12:17:24 +08:00
|
|
|
func (pp *ProxySetProvider) Destroy() error {
|
2019-12-26 18:41:06 +08:00
|
|
|
pp.healthCheck.close()
|
2019-12-08 12:17:24 +08:00
|
|
|
|
|
|
|
if pp.ticker != nil {
|
|
|
|
pp.ticker.Stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pp *ProxySetProvider) Initial() error {
|
|
|
|
var buf []byte
|
|
|
|
var err error
|
2019-12-13 00:29:24 +08:00
|
|
|
if stat, err := os.Stat(pp.vehicle.Path()); err == nil {
|
2019-12-08 12:17:24 +08:00
|
|
|
buf, err = ioutil.ReadFile(pp.vehicle.Path())
|
2019-12-13 00:29:24 +08:00
|
|
|
modTime := stat.ModTime()
|
|
|
|
pp.updatedAt = &modTime
|
2019-12-08 12:17:24 +08:00
|
|
|
} else {
|
|
|
|
buf, err = pp.vehicle.Read()
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
proxies, err := pp.parse(buf)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ioutil.WriteFile(pp.vehicle.Path(), buf, fileMode); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
pp.hash = md5.Sum(buf)
|
|
|
|
pp.setProxies(proxies)
|
|
|
|
|
|
|
|
// pull proxies automatically
|
|
|
|
if pp.ticker != nil {
|
|
|
|
go pp.pullLoop()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pp *ProxySetProvider) VehicleType() VehicleType {
|
|
|
|
return pp.vehicle.Type()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pp *ProxySetProvider) Type() ProviderType {
|
|
|
|
return Proxy
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pp *ProxySetProvider) Proxies() []C.Proxy {
|
|
|
|
return pp.proxies
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pp *ProxySetProvider) pullLoop() {
|
|
|
|
for range pp.ticker.C {
|
|
|
|
if err := pp.pull(); err != nil {
|
|
|
|
log.Warnln("[Provider] %s pull error: %s", pp.Name(), err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pp *ProxySetProvider) pull() error {
|
|
|
|
buf, err := pp.vehicle.Read()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-13 00:29:24 +08:00
|
|
|
now := time.Now()
|
2019-12-08 12:17:24 +08:00
|
|
|
hash := md5.Sum(buf)
|
|
|
|
if bytes.Equal(pp.hash[:], hash[:]) {
|
|
|
|
log.Debugln("[Provider] %s's proxies doesn't change", pp.Name())
|
2019-12-13 00:29:24 +08:00
|
|
|
pp.updatedAt = &now
|
2019-12-08 12:17:24 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
proxies, err := pp.parse(buf)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Infoln("[Provider] %s's proxies update", pp.Name())
|
|
|
|
|
|
|
|
if err := ioutil.WriteFile(pp.vehicle.Path(), buf, fileMode); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-11 17:31:15 +08:00
|
|
|
pp.updatedAt = &now
|
2019-12-08 12:17:24 +08:00
|
|
|
pp.hash = hash
|
|
|
|
pp.setProxies(proxies)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pp *ProxySetProvider) parse(buf []byte) ([]C.Proxy, error) {
|
|
|
|
schema := &ProxySchema{}
|
|
|
|
|
|
|
|
if err := yaml.Unmarshal(buf, schema); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if schema.Proxies == nil {
|
|
|
|
return nil, errors.New("File must have a `proxies` field")
|
|
|
|
}
|
|
|
|
|
|
|
|
proxies := []C.Proxy{}
|
|
|
|
for idx, mapping := range schema.Proxies {
|
|
|
|
proxy, err := outbound.ParseProxy(mapping)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Proxy %d error: %w", idx, err)
|
|
|
|
}
|
|
|
|
proxies = append(proxies, proxy)
|
|
|
|
}
|
|
|
|
|
2019-12-13 00:29:24 +08:00
|
|
|
if len(proxies) == 0 {
|
|
|
|
return nil, errors.New("File doesn't have any valid proxy")
|
|
|
|
}
|
|
|
|
|
2019-12-08 12:17:24 +08:00
|
|
|
return proxies, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pp *ProxySetProvider) setProxies(proxies []C.Proxy) {
|
|
|
|
pp.proxies = proxies
|
2019-12-26 18:41:06 +08:00
|
|
|
pp.healthCheck.setProxy(proxies)
|
|
|
|
go pp.healthCheck.check()
|
2019-12-08 12:17:24 +08:00
|
|
|
}
|
|
|
|
|
2019-12-26 18:41:06 +08:00
|
|
|
func NewProxySetProvider(name string, interval time.Duration, vehicle Vehicle, hc *HealthCheck) *ProxySetProvider {
|
2019-12-08 12:17:24 +08:00
|
|
|
var ticker *time.Ticker
|
|
|
|
if interval != 0 {
|
|
|
|
ticker = time.NewTicker(interval)
|
|
|
|
}
|
|
|
|
|
2019-12-26 18:41:06 +08:00
|
|
|
if hc.auto() {
|
|
|
|
go hc.process()
|
|
|
|
}
|
|
|
|
|
2019-12-08 12:17:24 +08:00
|
|
|
return &ProxySetProvider{
|
2019-12-26 18:41:06 +08:00
|
|
|
name: name,
|
|
|
|
vehicle: vehicle,
|
|
|
|
proxies: []C.Proxy{},
|
|
|
|
healthCheck: hc,
|
|
|
|
ticker: ticker,
|
2019-12-08 12:17:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type CompatibleProvier struct {
|
|
|
|
name string
|
2019-12-26 18:41:06 +08:00
|
|
|
healthCheck *HealthCheck
|
2019-12-08 12:17:24 +08:00
|
|
|
proxies []C.Proxy
|
|
|
|
}
|
|
|
|
|
2019-12-11 17:31:15 +08:00
|
|
|
func (cp *CompatibleProvier) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(map[string]interface{}{
|
|
|
|
"name": cp.Name(),
|
|
|
|
"type": cp.Type().String(),
|
|
|
|
"vehicleType": cp.VehicleType().String(),
|
|
|
|
"proxies": cp.Proxies(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-12-08 12:17:24 +08:00
|
|
|
func (cp *CompatibleProvier) Name() string {
|
|
|
|
return cp.name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cp *CompatibleProvier) Reload() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cp *CompatibleProvier) Destroy() error {
|
2019-12-26 18:41:06 +08:00
|
|
|
cp.healthCheck.close()
|
2019-12-08 12:17:24 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-11 17:31:15 +08:00
|
|
|
func (cp *CompatibleProvier) HealthCheck() {
|
2019-12-26 18:41:06 +08:00
|
|
|
cp.healthCheck.check()
|
2019-12-11 17:31:15 +08:00
|
|
|
}
|
|
|
|
|
2019-12-13 00:29:24 +08:00
|
|
|
func (cp *CompatibleProvier) Update() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-08 12:17:24 +08:00
|
|
|
func (cp *CompatibleProvier) Initial() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cp *CompatibleProvier) VehicleType() VehicleType {
|
|
|
|
return Compatible
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cp *CompatibleProvier) Type() ProviderType {
|
|
|
|
return Proxy
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cp *CompatibleProvier) Proxies() []C.Proxy {
|
|
|
|
return cp.proxies
|
|
|
|
}
|
|
|
|
|
2019-12-26 18:41:06 +08:00
|
|
|
func NewCompatibleProvier(name string, proxies []C.Proxy, hc *HealthCheck) (*CompatibleProvier, error) {
|
2019-12-08 12:17:24 +08:00
|
|
|
if len(proxies) == 0 {
|
|
|
|
return nil, errors.New("Provider need one proxy at least")
|
|
|
|
}
|
|
|
|
|
2019-12-26 18:41:06 +08:00
|
|
|
if hc.auto() {
|
|
|
|
go hc.process()
|
2019-12-08 12:17:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return &CompatibleProvier{
|
|
|
|
name: name,
|
|
|
|
proxies: proxies,
|
|
|
|
healthCheck: hc,
|
|
|
|
}, nil
|
|
|
|
}
|