Clash.Meta/adapters/provider/provider.go

262 lines
4.9 KiB
Go
Raw Normal View History

2019-12-08 12:17:24 +08:00
package provider
import (
"encoding/json"
2019-12-08 12:17:24 +08:00
"errors"
"fmt"
2020-04-26 22:38:15 +08:00
"runtime"
2019-12-08 12:17:24 +08:00
"time"
"github.com/Dreamacro/clash/adapters/outbound"
C "github.com/Dreamacro/clash/constant"
"gopkg.in/yaml.v2"
)
const (
ReservedName = "default"
)
// 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
2020-04-26 22:38:15 +08:00
Update() error
2019-12-08 12:17:24 +08:00
}
// ProxyProvider interface
type ProxyProvider interface {
Provider
Proxies() []C.Proxy
// ProxiesWithTouch is used to inform the provider that the proxy is actually being used while getting the list of proxies.
// Commonly used in Dial and DialUDP
ProxiesWithTouch() []C.Proxy
HealthCheck()
2019-12-08 12:17:24 +08:00
}
type ProxySchema struct {
Proxies []map[string]interface{} `yaml:"proxies"`
}
2020-04-26 22:38:15 +08:00
// for auto gc
2019-12-08 12:17:24 +08:00
type ProxySetProvider struct {
2020-04-26 22:38:15 +08:00
*proxySetProvider
}
type proxySetProvider struct {
*fetcher
proxies []C.Proxy
healthCheck *HealthCheck
2019-12-08 12:17:24 +08:00
}
2020-04-26 22:38: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,
})
}
2020-04-26 22:38:15 +08:00
func (pp *proxySetProvider) Name() string {
2019-12-08 12:17:24 +08:00
return pp.name
}
2020-04-26 22:38:15 +08:00
func (pp *proxySetProvider) HealthCheck() {
pp.healthCheck.check()
}
2020-04-26 22:38:15 +08:00
func (pp *proxySetProvider) Update() error {
elm, same, err := pp.fetcher.Update()
if err == nil && !same {
pp.onUpdate(elm)
2019-12-08 12:17:24 +08:00
}
2020-04-26 22:38:15 +08:00
return err
2019-12-08 12:17:24 +08:00
}
2020-04-26 22:38:15 +08:00
func (pp *proxySetProvider) Initial() error {
elm, err := pp.fetcher.Initial()
2019-12-08 12:17:24 +08:00
if err != nil {
return err
}
2020-04-26 22:38:15 +08:00
pp.onUpdate(elm)
2019-12-08 12:17:24 +08:00
return nil
}
2020-04-26 22:38:15 +08:00
func (pp *proxySetProvider) Type() ProviderType {
2019-12-08 12:17:24 +08:00
return Proxy
}
2020-04-26 22:38:15 +08:00
func (pp *proxySetProvider) Proxies() []C.Proxy {
2019-12-08 12:17:24 +08:00
return pp.proxies
}
func (pp *proxySetProvider) ProxiesWithTouch() []C.Proxy {
pp.healthCheck.touch()
return pp.Proxies()
}
2020-04-26 22:38:15 +08:00
func proxiesParse(buf []byte) (interface{}, error) {
2019-12-08 12:17:24 +08:00
schema := &ProxySchema{}
if err := yaml.Unmarshal(buf, schema); err != nil {
return nil, err
}
if schema.Proxies == nil {
2020-08-25 22:19:59 +08:00
return nil, errors.New("file must have a `proxies` field")
2019-12-08 12:17:24 +08:00
}
proxies := []C.Proxy{}
for idx, mapping := range schema.Proxies {
proxy, err := outbound.ParseProxy(mapping)
if err != nil {
2020-08-25 22:19:59 +08:00
return nil, fmt.Errorf("proxy %d error: %w", idx, err)
2019-12-08 12:17:24 +08:00
}
proxies = append(proxies, proxy)
}
2019-12-13 00:29:24 +08:00
if len(proxies) == 0 {
2020-08-25 22:19:59 +08:00
return nil, errors.New("file doesn't have any valid proxy")
2019-12-13 00:29:24 +08:00
}
2019-12-08 12:17:24 +08:00
return proxies, nil
}
2020-04-26 22:38:15 +08:00
func (pp *proxySetProvider) setProxies(proxies []C.Proxy) {
2019-12-08 12:17:24 +08:00
pp.proxies = proxies
pp.healthCheck.setProxy(proxies)
if pp.healthCheck.auto() {
go pp.healthCheck.check()
}
2019-12-08 12:17:24 +08:00
}
2020-04-26 22:38:15 +08:00
func stopProxyProvider(pd *ProxySetProvider) {
pd.healthCheck.close()
pd.fetcher.Destroy()
}
2019-12-08 12:17:24 +08:00
2020-04-26 22:38:15 +08:00
func NewProxySetProvider(name string, interval time.Duration, vehicle Vehicle, hc *HealthCheck) *ProxySetProvider {
if hc.auto() {
go hc.process()
}
2020-04-26 22:38:15 +08:00
pd := &proxySetProvider{
proxies: []C.Proxy{},
healthCheck: hc,
2019-12-08 12:17:24 +08:00
}
2020-04-26 22:38:15 +08:00
onUpdate := func(elm interface{}) {
ret := elm.([]C.Proxy)
pd.setProxies(ret)
}
fetcher := newFetcher(name, interval, vehicle, proxiesParse, onUpdate)
pd.fetcher = fetcher
wrapper := &ProxySetProvider{pd}
runtime.SetFinalizer(wrapper, stopProxyProvider)
return wrapper
2019-12-08 12:17:24 +08:00
}
2020-04-26 22:38:15 +08:00
// for auto gc
2020-01-11 21:02:55 +08:00
type CompatibleProvider struct {
2020-04-26 22:38:15 +08:00
*compatibleProvider
}
type compatibleProvider struct {
2019-12-08 12:17:24 +08:00
name string
healthCheck *HealthCheck
2019-12-08 12:17:24 +08:00
proxies []C.Proxy
}
2020-04-26 22:38:15 +08:00
func (cp *compatibleProvider) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"name": cp.Name(),
"type": cp.Type().String(),
"vehicleType": cp.VehicleType().String(),
"proxies": cp.Proxies(),
})
}
2020-04-26 22:38:15 +08:00
func (cp *compatibleProvider) Name() string {
2019-12-08 12:17:24 +08:00
return cp.name
}
2020-04-26 22:38:15 +08:00
func (cp *compatibleProvider) HealthCheck() {
cp.healthCheck.check()
}
2020-04-26 22:38:15 +08:00
func (cp *compatibleProvider) Update() error {
2019-12-13 00:29:24 +08:00
return nil
}
2020-04-26 22:38:15 +08:00
func (cp *compatibleProvider) Initial() error {
2019-12-08 12:17:24 +08:00
return nil
}
2020-04-26 22:38:15 +08:00
func (cp *compatibleProvider) VehicleType() VehicleType {
2019-12-08 12:17:24 +08:00
return Compatible
}
2020-04-26 22:38:15 +08:00
func (cp *compatibleProvider) Type() ProviderType {
2019-12-08 12:17:24 +08:00
return Proxy
}
2020-04-26 22:38:15 +08:00
func (cp *compatibleProvider) Proxies() []C.Proxy {
2019-12-08 12:17:24 +08:00
return cp.proxies
}
func (cp *compatibleProvider) ProxiesWithTouch() []C.Proxy {
cp.healthCheck.touch()
return cp.Proxies()
}
2020-04-26 22:38:15 +08:00
func stopCompatibleProvider(pd *CompatibleProvider) {
pd.healthCheck.close()
}
2020-01-11 21:02:55 +08:00
func NewCompatibleProvider(name string, proxies []C.Proxy, hc *HealthCheck) (*CompatibleProvider, error) {
2019-12-08 12:17:24 +08:00
if len(proxies) == 0 {
return nil, errors.New("Provider need one proxy at least")
}
if hc.auto() {
go hc.process()
2019-12-08 12:17:24 +08:00
}
2020-04-26 22:38:15 +08:00
pd := &compatibleProvider{
2019-12-08 12:17:24 +08:00
name: name,
proxies: proxies,
healthCheck: hc,
2020-04-26 22:38:15 +08:00
}
wrapper := &CompatibleProvider{pd}
runtime.SetFinalizer(wrapper, stopCompatibleProvider)
return wrapper, nil
2019-12-08 12:17:24 +08:00
}