mihomo/adapter/provider/provider.go

257 lines
5.2 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"
"github.com/dlclark/regexp2"
"math"
2020-04-26 22:38:15 +08:00
"runtime"
2019-12-08 12:17:24 +08:00
"time"
2021-06-10 14:05:56 +08:00
"github.com/Dreamacro/clash/adapter"
2019-12-08 12:17:24 +08:00
C "github.com/Dreamacro/clash/constant"
types "github.com/Dreamacro/clash/constant/provider"
2019-12-08 12:17:24 +08:00
2022-05-08 00:47:01 +08:00
"gopkg.in/yaml.v3"
2019-12-08 12:17:24 +08:00
)
const (
ReservedName = "default"
)
type ProxySchema struct {
2022-03-16 12:10:13 +08:00
Proxies []map[string]any `yaml:"proxies"`
2019-12-08 12:17:24 +08:00
}
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
2022-04-28 23:06:35 +08:00
version uint
}
2022-04-28 23:06:35 +08:00
func (pp *proxySetProvider) Version() uint {
return pp.version
2019-12-08 12:17:24 +08:00
}
2020-04-26 22:38:15 +08:00
func (pp *proxySetProvider) MarshalJSON() ([]byte, error) {
2022-03-16 12:10:13 +08:00
return json.Marshal(map[string]any{
"name": pp.Name(),
"type": pp.Type().String(),
"vehicleType": pp.VehicleType().String(),
"proxies": pp.Proxies(),
2022-03-30 13:05:46 +08:00
//TODO maybe error because year value overflow
"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)
if pp.healthCheck.auto() {
go pp.healthCheck.process()
}
2019-12-08 12:17:24 +08:00
return nil
}
func (pp *proxySetProvider) Type() types.ProviderType {
return types.Proxy
2019-12-08 12:17:24 +08:00
}
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 (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()
}
2022-04-23 01:31:34 +08:00
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
2021-11-21 17:44:03 +08:00
func NewProxySetProvider(name string, interval time.Duration, filter string, vehicle types.Vehicle, hc *HealthCheck) (*ProxySetProvider, error) {
//filterReg, err := regexp.Compile(filter)
filterReg, err := regexp2.Compile(filter, 0)
if err != nil {
2021-11-21 17:44:03 +08:00
return nil, fmt.Errorf("invalid filter regex: %w", err)
}
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
2022-03-16 12:10:13 +08:00
onUpdate := func(elm any) {
2020-04-26 22:38:15 +08:00
ret := elm.([]C.Proxy)
pd.setProxies(ret)
2022-04-28 23:06:35 +08:00
if pd.version == math.MaxUint {
pd.version = 0
} else {
2022-04-28 23:06:35 +08:00
pd.version++
}
2020-04-26 22:38:15 +08:00
}
2022-03-16 12:10:13 +08:00
proxiesParseAndFilter := func(buf []byte) (any, 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 {
name, ok := mapping["name"]
mat, _ := filterReg.FindStringMatch(name.(string))
if ok && len(filter) > 0 && mat == nil {
continue
}
proxy, err := adapter.ParseProxy(mapping)
if err != nil {
return nil, fmt.Errorf("proxy %d error: %w", idx, err)
}
proxies = append(proxies, proxy)
}
if len(proxies) == 0 {
if len(filter) > 0 {
return nil, errors.New("doesn't match any proxy, please check your filter")
}
return nil, errors.New("file doesn't have any proxy")
}
return proxies, nil
}
fetcher := newFetcher(name, interval, vehicle, proxiesParseAndFilter, onUpdate)
2020-04-26 22:38:15 +08:00
pd.fetcher = fetcher
wrapper := &ProxySetProvider{pd}
runtime.SetFinalizer(wrapper, stopProxyProvider)
2021-11-21 17:44:03 +08:00
return wrapper, nil
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
2022-04-28 23:06:35 +08:00
version uint
}
2022-04-28 23:06:35 +08:00
func (cp *compatibleProvider) Version() uint {
return cp.version
2019-12-08 12:17:24 +08:00
}
2020-04-26 22:38:15 +08:00
func (cp *compatibleProvider) MarshalJSON() ([]byte, error) {
2022-03-16 12:10:13 +08:00
return json.Marshal(map[string]any{
"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 {
if cp.healthCheck.auto() {
go cp.healthCheck.process()
}
2019-12-08 12:17:24 +08:00
return nil
}
func (cp *compatibleProvider) VehicleType() types.VehicleType {
return types.Compatible
2019-12-08 12:17:24 +08:00
}
func (cp *compatibleProvider) Type() types.ProviderType {
return types.Proxy
2019-12-08 12:17:24 +08:00
}
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 {
2021-07-06 00:33:13 +08:00
return nil, errors.New("provider need one proxy at least")
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
}