From d755383e394e8754cebd6f7cb2dbf205b9a3c686 Mon Sep 17 00:00:00 2001 From: Dreamacro <8615343+Dreamacro@users.noreply.github.com> Date: Sun, 4 Jul 2021 20:32:59 +0800 Subject: [PATCH] Chore: move provider interface to constant --- adapter/outboundgroup/common.go | 2 +- adapter/outboundgroup/fallback.go | 2 +- adapter/outboundgroup/loadbalance.go | 2 +- adapter/outboundgroup/parser.go | 11 +-- adapter/outboundgroup/relay.go | 2 +- adapter/outboundgroup/selector.go | 2 +- adapter/outboundgroup/urltest.go | 2 +- adapter/provider/fetcher.go | 11 +-- adapter/provider/parser.go | 5 +- adapter/provider/provider.go | 54 ++------------ adapter/provider/vehicle.go | 38 ++-------- config/config.go | 11 +-- constant/provider/interface.go | 105 +++++++++++++++++++++++++++ hub/executor/executor.go | 2 +- hub/route/provider.go | 2 +- tunnel/tunnel.go | 2 +- 16 files changed, 148 insertions(+), 105 deletions(-) create mode 100644 constant/provider/interface.go diff --git a/adapter/outboundgroup/common.go b/adapter/outboundgroup/common.go index 31b8cb6b..758c7801 100644 --- a/adapter/outboundgroup/common.go +++ b/adapter/outboundgroup/common.go @@ -3,8 +3,8 @@ package outboundgroup import ( "time" - "github.com/Dreamacro/clash/adapter/provider" C "github.com/Dreamacro/clash/constant" + "github.com/Dreamacro/clash/constant/provider" ) const ( diff --git a/adapter/outboundgroup/fallback.go b/adapter/outboundgroup/fallback.go index 8b38f09c..da01d4de 100644 --- a/adapter/outboundgroup/fallback.go +++ b/adapter/outboundgroup/fallback.go @@ -5,9 +5,9 @@ import ( "encoding/json" "github.com/Dreamacro/clash/adapter/outbound" - "github.com/Dreamacro/clash/adapter/provider" "github.com/Dreamacro/clash/common/singledo" C "github.com/Dreamacro/clash/constant" + "github.com/Dreamacro/clash/constant/provider" ) type Fallback struct { diff --git a/adapter/outboundgroup/loadbalance.go b/adapter/outboundgroup/loadbalance.go index 7856a07d..e529b53b 100644 --- a/adapter/outboundgroup/loadbalance.go +++ b/adapter/outboundgroup/loadbalance.go @@ -8,10 +8,10 @@ import ( "net" "github.com/Dreamacro/clash/adapter/outbound" - "github.com/Dreamacro/clash/adapter/provider" "github.com/Dreamacro/clash/common/murmur3" "github.com/Dreamacro/clash/common/singledo" C "github.com/Dreamacro/clash/constant" + "github.com/Dreamacro/clash/constant/provider" "golang.org/x/net/publicsuffix" ) diff --git a/adapter/outboundgroup/parser.go b/adapter/outboundgroup/parser.go index d11040a5..cf97579f 100644 --- a/adapter/outboundgroup/parser.go +++ b/adapter/outboundgroup/parser.go @@ -7,6 +7,7 @@ import ( "github.com/Dreamacro/clash/adapter/provider" "github.com/Dreamacro/clash/common/structure" C "github.com/Dreamacro/clash/constant" + types "github.com/Dreamacro/clash/constant/provider" ) var ( @@ -28,7 +29,7 @@ type GroupCommonOption struct { DisableUDP bool `group:"disable-udp,omitempty"` } -func ParseProxyGroup(config map[string]interface{}, proxyMap map[string]C.Proxy, providersMap map[string]provider.ProxyProvider) (C.ProxyAdapter, error) { +func ParseProxyGroup(config map[string]interface{}, proxyMap map[string]C.Proxy, providersMap map[string]types.ProxyProvider) (C.ProxyAdapter, error) { decoder := structure.NewDecoder(structure.Option{TagName: "group", WeaklyTypedInput: true}) groupOption := &GroupCommonOption{ @@ -44,7 +45,7 @@ func ParseProxyGroup(config map[string]interface{}, proxyMap map[string]C.Proxy, groupName := groupOption.Name - providers := []provider.ProxyProvider{} + providers := []types.ProxyProvider{} if len(groupOption.Proxies) == 0 && len(groupOption.Use) == 0 { return nil, errMissProxy @@ -138,15 +139,15 @@ func getProxies(mapping map[string]C.Proxy, list []string) ([]C.Proxy, error) { return ps, nil } -func getProviders(mapping map[string]provider.ProxyProvider, list []string) ([]provider.ProxyProvider, error) { - var ps []provider.ProxyProvider +func getProviders(mapping map[string]types.ProxyProvider, list []string) ([]types.ProxyProvider, error) { + var ps []types.ProxyProvider for _, name := range list { p, ok := mapping[name] if !ok { return nil, fmt.Errorf("'%s' not found", name) } - if p.VehicleType() == provider.Compatible { + if p.VehicleType() == types.Compatible { return nil, fmt.Errorf("proxy group %s can't contains in `use`", name) } ps = append(ps, p) diff --git a/adapter/outboundgroup/relay.go b/adapter/outboundgroup/relay.go index e4a897b8..8d8128ba 100644 --- a/adapter/outboundgroup/relay.go +++ b/adapter/outboundgroup/relay.go @@ -7,10 +7,10 @@ import ( "fmt" "github.com/Dreamacro/clash/adapter/outbound" - "github.com/Dreamacro/clash/adapter/provider" "github.com/Dreamacro/clash/common/singledo" "github.com/Dreamacro/clash/component/dialer" C "github.com/Dreamacro/clash/constant" + "github.com/Dreamacro/clash/constant/provider" ) type Relay struct { diff --git a/adapter/outboundgroup/selector.go b/adapter/outboundgroup/selector.go index c28b6f8c..12453927 100644 --- a/adapter/outboundgroup/selector.go +++ b/adapter/outboundgroup/selector.go @@ -6,9 +6,9 @@ import ( "errors" "github.com/Dreamacro/clash/adapter/outbound" - "github.com/Dreamacro/clash/adapter/provider" "github.com/Dreamacro/clash/common/singledo" C "github.com/Dreamacro/clash/constant" + "github.com/Dreamacro/clash/constant/provider" ) type Selector struct { diff --git a/adapter/outboundgroup/urltest.go b/adapter/outboundgroup/urltest.go index 0f59d4be..2a11cd23 100644 --- a/adapter/outboundgroup/urltest.go +++ b/adapter/outboundgroup/urltest.go @@ -6,9 +6,9 @@ import ( "time" "github.com/Dreamacro/clash/adapter/outbound" - "github.com/Dreamacro/clash/adapter/provider" "github.com/Dreamacro/clash/common/singledo" C "github.com/Dreamacro/clash/constant" + "github.com/Dreamacro/clash/constant/provider" ) type urlTestOption func(*URLTest) diff --git a/adapter/provider/fetcher.go b/adapter/provider/fetcher.go index 5ef3f25c..777e3aa5 100644 --- a/adapter/provider/fetcher.go +++ b/adapter/provider/fetcher.go @@ -8,6 +8,7 @@ import ( "path/filepath" "time" + types "github.com/Dreamacro/clash/constant/provider" "github.com/Dreamacro/clash/log" ) @@ -20,7 +21,7 @@ type parser = func([]byte) (interface{}, error) type fetcher struct { name string - vehicle Vehicle + vehicle types.Vehicle updatedAt *time.Time ticker *time.Ticker done chan struct{} @@ -33,7 +34,7 @@ func (f *fetcher) Name() string { return f.name } -func (f *fetcher) VehicleType() VehicleType { +func (f *fetcher) VehicleType() types.VehicleType { return f.vehicle.Type() } @@ -76,7 +77,7 @@ func (f *fetcher) Initial() (interface{}, error) { isLocal = false } - if f.vehicle.Type() != File && !isLocal { + if f.vehicle.Type() != types.File && !isLocal { if err := safeWrite(f.vehicle.Path(), buf); err != nil { return nil, err } @@ -110,7 +111,7 @@ func (f *fetcher) Update() (interface{}, bool, error) { return nil, false, err } - if f.vehicle.Type() != File { + if f.vehicle.Type() != types.File { if err := safeWrite(f.vehicle.Path(), buf); err != nil { return nil, false, err } @@ -167,7 +168,7 @@ func safeWrite(path string, buf []byte) error { return ioutil.WriteFile(path, buf, fileMode) } -func newFetcher(name string, interval time.Duration, vehicle Vehicle, parser parser, onUpdate func(interface{})) *fetcher { +func newFetcher(name string, interval time.Duration, vehicle types.Vehicle, parser parser, onUpdate func(interface{})) *fetcher { var ticker *time.Ticker if interval != 0 { ticker = time.NewTicker(interval) diff --git a/adapter/provider/parser.go b/adapter/provider/parser.go index aa5d7ae2..f326d4cc 100644 --- a/adapter/provider/parser.go +++ b/adapter/provider/parser.go @@ -7,6 +7,7 @@ import ( "github.com/Dreamacro/clash/common/structure" C "github.com/Dreamacro/clash/constant" + types "github.com/Dreamacro/clash/constant/provider" ) var ( @@ -28,7 +29,7 @@ type proxyProviderSchema struct { HealthCheck healthCheckSchema `provider:"health-check,omitempty"` } -func ParseProxyProvider(name string, mapping map[string]interface{}) (ProxyProvider, error) { +func ParseProxyProvider(name string, mapping map[string]interface{}) (types.ProxyProvider, error) { decoder := structure.NewDecoder(structure.Option{TagName: "provider", WeaklyTypedInput: true}) schema := &proxyProviderSchema{ @@ -48,7 +49,7 @@ func ParseProxyProvider(name string, mapping map[string]interface{}) (ProxyProvi path := C.Path.Resolve(schema.Path) - var vehicle Vehicle + var vehicle types.Vehicle switch schema.Type { case "file": vehicle = NewFileVehicle(path) diff --git a/adapter/provider/provider.go b/adapter/provider/provider.go index f87fe2a1..b6e1ac75 100644 --- a/adapter/provider/provider.go +++ b/adapter/provider/provider.go @@ -9,6 +9,7 @@ import ( "github.com/Dreamacro/clash/adapter" C "github.com/Dreamacro/clash/constant" + types "github.com/Dreamacro/clash/constant/provider" "gopkg.in/yaml.v2" ) @@ -17,45 +18,6 @@ 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 - Update() error -} - -// 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() -} - type ProxySchema struct { Proxies []map[string]interface{} `yaml:"proxies"` } @@ -107,8 +69,8 @@ func (pp *proxySetProvider) Initial() error { return nil } -func (pp *proxySetProvider) Type() ProviderType { - return Proxy +func (pp *proxySetProvider) Type() types.ProviderType { + return types.Proxy } func (pp *proxySetProvider) Proxies() []C.Proxy { @@ -160,7 +122,7 @@ func stopProxyProvider(pd *ProxySetProvider) { pd.fetcher.Destroy() } -func NewProxySetProvider(name string, interval time.Duration, vehicle Vehicle, hc *HealthCheck) *ProxySetProvider { +func NewProxySetProvider(name string, interval time.Duration, vehicle types.Vehicle, hc *HealthCheck) *ProxySetProvider { if hc.auto() { go hc.process() } @@ -219,12 +181,12 @@ func (cp *compatibleProvider) Initial() error { return nil } -func (cp *compatibleProvider) VehicleType() VehicleType { - return Compatible +func (cp *compatibleProvider) VehicleType() types.VehicleType { + return types.Compatible } -func (cp *compatibleProvider) Type() ProviderType { - return Proxy +func (cp *compatibleProvider) Type() types.ProviderType { + return types.Proxy } func (cp *compatibleProvider) Proxies() []C.Proxy { diff --git a/adapter/provider/vehicle.go b/adapter/provider/vehicle.go index 13d898fc..ea6a835a 100644 --- a/adapter/provider/vehicle.go +++ b/adapter/provider/vehicle.go @@ -8,43 +8,15 @@ import ( "time" "github.com/Dreamacro/clash/component/dialer" + types "github.com/Dreamacro/clash/constant/provider" ) -// Vehicle Type -const ( - File VehicleType = iota - HTTP - Compatible -) - -// VehicleType defined -type VehicleType int - -func (v VehicleType) String() string { - switch v { - case File: - return "File" - case HTTP: - return "HTTP" - case Compatible: - return "Compatible" - default: - return "Unknown" - } -} - -type Vehicle interface { - Read() ([]byte, error) - Path() string - Type() VehicleType -} - type FileVehicle struct { path string } -func (f *FileVehicle) Type() VehicleType { - return File +func (f *FileVehicle) Type() types.VehicleType { + return types.File } func (f *FileVehicle) Path() string { @@ -64,8 +36,8 @@ type HTTPVehicle struct { path string } -func (h *HTTPVehicle) Type() VehicleType { - return HTTP +func (h *HTTPVehicle) Type() types.VehicleType { + return types.HTTP } func (h *HTTPVehicle) Path() string { diff --git a/config/config.go b/config/config.go index a256d282..a9628192 100644 --- a/config/config.go +++ b/config/config.go @@ -16,6 +16,7 @@ import ( "github.com/Dreamacro/clash/component/fakeip" "github.com/Dreamacro/clash/component/trie" C "github.com/Dreamacro/clash/constant" + providerTypes "github.com/Dreamacro/clash/constant/provider" "github.com/Dreamacro/clash/dns" "github.com/Dreamacro/clash/log" R "github.com/Dreamacro/clash/rule" @@ -93,7 +94,7 @@ type Config struct { Rules []C.Rule Users []auth.AuthUser Proxies map[string]C.Proxy - Providers map[string]provider.ProxyProvider + Providers map[string]providerTypes.ProxyProvider } type RawDNS struct { @@ -267,9 +268,9 @@ func parseGeneral(cfg *RawConfig) (*General, error) { }, nil } -func parseProxies(cfg *RawConfig) (proxies map[string]C.Proxy, providersMap map[string]provider.ProxyProvider, err error) { +func parseProxies(cfg *RawConfig) (proxies map[string]C.Proxy, providersMap map[string]providerTypes.ProxyProvider, err error) { proxies = make(map[string]C.Proxy) - providersMap = make(map[string]provider.ProxyProvider) + providersMap = make(map[string]providerTypes.ProxyProvider) proxyList := []string{} proxiesConfig := cfg.Proxy groupsConfig := cfg.ProxyGroup @@ -345,7 +346,7 @@ func parseProxies(cfg *RawConfig) (proxies map[string]C.Proxy, providersMap map[ // initial compatible provider for _, pd := range providersMap { - if pd.VehicleType() != provider.Compatible { + if pd.VehicleType() != providerTypes.Compatible { continue } @@ -367,7 +368,7 @@ func parseProxies(cfg *RawConfig) (proxies map[string]C.Proxy, providersMap map[ &outboundgroup.GroupCommonOption{ Name: "GLOBAL", }, - []provider.ProxyProvider{pd}, + []providerTypes.ProxyProvider{pd}, ) proxies["GLOBAL"] = adapter.NewProxy(global) return proxies, providersMap, nil diff --git a/constant/provider/interface.go b/constant/provider/interface.go new file mode 100644 index 00000000..d3483cdc --- /dev/null +++ b/constant/provider/interface.go @@ -0,0 +1,105 @@ +package provider + +import ( + "github.com/Dreamacro/clash/constant" +) + +// Vehicle Type +const ( + File VehicleType = iota + HTTP + Compatible +) + +// VehicleType defined +type VehicleType int + +func (v VehicleType) String() string { + switch v { + case File: + return "File" + case HTTP: + return "HTTP" + case Compatible: + return "Compatible" + default: + return "Unknown" + } +} + +type Vehicle interface { + Read() ([]byte, error) + Path() string + Type() VehicleType +} + +// 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 + Update() error +} + +// ProxyProvider interface +type ProxyProvider interface { + Provider + Proxies() []constant.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() []constant.Proxy + HealthCheck() +} + +// Rule Type +const ( + Domain RuleType = iota + IPCIDR + Classical +) + +// RuleType defined +type RuleType int + +func (rt RuleType) String() string { + switch rt { + case Domain: + return "Domain" + case IPCIDR: + return "IPCIDR" + case Classical: + return "Classical" + default: + return "Unknown" + } +} + +// RuleProvider interface +type RuleProvider interface { + Provider + Behavior() RuleType + Match(*constant.Metadata) bool + ShouldResolveIP() bool + AsRule(adaptor string) constant.Rule +} diff --git a/hub/executor/executor.go b/hub/executor/executor.go index 81920221..321efe25 100644 --- a/hub/executor/executor.go +++ b/hub/executor/executor.go @@ -8,7 +8,6 @@ import ( "github.com/Dreamacro/clash/adapter" "github.com/Dreamacro/clash/adapter/outboundgroup" - "github.com/Dreamacro/clash/adapter/provider" "github.com/Dreamacro/clash/component/auth" "github.com/Dreamacro/clash/component/dialer" "github.com/Dreamacro/clash/component/profile" @@ -17,6 +16,7 @@ import ( "github.com/Dreamacro/clash/component/trie" "github.com/Dreamacro/clash/config" C "github.com/Dreamacro/clash/constant" + "github.com/Dreamacro/clash/constant/provider" "github.com/Dreamacro/clash/dns" P "github.com/Dreamacro/clash/listener" authStore "github.com/Dreamacro/clash/listener/auth" diff --git a/hub/route/provider.go b/hub/route/provider.go index f373d511..0b599445 100644 --- a/hub/route/provider.go +++ b/hub/route/provider.go @@ -4,7 +4,7 @@ import ( "context" "net/http" - "github.com/Dreamacro/clash/adapter/provider" + "github.com/Dreamacro/clash/constant/provider" "github.com/Dreamacro/clash/tunnel" "github.com/go-chi/chi/v5" diff --git a/tunnel/tunnel.go b/tunnel/tunnel.go index a40b697d..0ec36d3f 100644 --- a/tunnel/tunnel.go +++ b/tunnel/tunnel.go @@ -8,10 +8,10 @@ import ( "time" "github.com/Dreamacro/clash/adapter/inbound" - "github.com/Dreamacro/clash/adapter/provider" "github.com/Dreamacro/clash/component/nat" "github.com/Dreamacro/clash/component/resolver" C "github.com/Dreamacro/clash/constant" + "github.com/Dreamacro/clash/constant/provider" "github.com/Dreamacro/clash/context" "github.com/Dreamacro/clash/log" "github.com/Dreamacro/clash/tunnel/statistic"