optimize for meta

This commit is contained in:
djoeni 2022-06-16 14:30:12 +07:00
parent 1de0c80e40
commit 41127e9312
3 changed files with 53 additions and 7 deletions

View file

@ -9,7 +9,7 @@ import (
) )
func forEachProviders(rawCfg *config.RawConfig, fun func(index int, total int, key string, provider map[string]any)) { func forEachProviders(rawCfg *config.RawConfig, fun func(index int, total int, key string, provider map[string]any)) {
total := len(rawCfg.ProxyProvider) total := len(rawCfg.ProxyProvider) + len(rawCfg.RuleProvider)
index := 0 index := 0
for k, v := range rawCfg.ProxyProvider { for k, v := range rawCfg.ProxyProvider {
@ -17,10 +17,20 @@ func forEachProviders(rawCfg *config.RawConfig, fun func(index int, total int, k
index++ index++
} }
for k, v := range rawCfg.RuleProvider {
fun(index, total, k, v)
index++
}
} }
func destroyProviders(cfg *config.Config) { func destroyProviders(cfg *config.Config) {
for _, p := range cfg.Providers { for _, p := range cfg.Providers {
_ = p.(io.Closer).Close() _ = p.(io.Closer).Close()
} }
for _, p := range cfg.RuleProviders {
_ = p.(io.Closer).Close()
}
} }

View file

@ -3,14 +3,18 @@
package tunnel package tunnel
import ( import (
"errors"
"fmt" "fmt"
"time" "time"
P "github.com/Dreamacro/clash/adapter/provider" P "github.com/Dreamacro/clash/adapter/provider"
"github.com/Dreamacro/clash/constant/provider" "github.com/Dreamacro/clash/constant/provider"
"github.com/Dreamacro/clash/log"
"github.com/Dreamacro/clash/tunnel" "github.com/Dreamacro/clash/tunnel"
) )
var ErrInvalidType = errors.New("invalid type")
type Provider struct { type Provider struct {
Name string `json:"name"` Name string `json:"name"`
VehicleType string `json:"vehicleType"` VehicleType string `json:"vehicleType"`
@ -19,9 +23,18 @@ type Provider struct {
} }
func QueryProviders() []*Provider { func QueryProviders() []*Provider {
r := tunnel.RuleProviders()
p := tunnel.Providers() p := tunnel.Providers()
providers := make([]provider.Provider, 0, len(p)) providers := make([]provider.Provider, 0, len(r)+len(p))
for _, rule := range r {
if rule.VehicleType() == provider.Compatible {
continue
}
providers = append(providers, rule)
}
for _, proxy := range p { for _, proxy := range p {
if proxy.VehicleType() == provider.Compatible { if proxy.VehicleType() == provider.Compatible {
@ -51,11 +64,29 @@ func QueryProviders() []*Provider {
return result return result
} }
func UpdateProvider(_ string, name string) error { func UpdateProvider(t string, name string) error {
p, ok := tunnel.Providers()[name] err := ErrInvalidType
if !ok {
return fmt.Errorf("%s not found", name) switch t {
case "Rule":
p := tunnel.RuleProviders()[name]
if p == nil {
return fmt.Errorf("%s not found", name)
}
err = p.Update()
case "Proxy":
p := tunnel.Providers()[name]
if p == nil {
return fmt.Errorf("%s not found", name)
}
err = p.Update()
} }
return p.Update() if err != nil {
log.Warnln("Updating provider %s: %s", name, err.Error())
}
return err
} }

View file

@ -17,6 +17,8 @@ data class Proxy(
enum class Type(val group: Boolean) { enum class Type(val group: Boolean) {
Direct(false), Direct(false),
Reject(false), Reject(false),
Compatible(false),
Pass(false),
Shadowsocks(false), Shadowsocks(false),
ShadowsocksR(false), ShadowsocksR(false),
@ -24,7 +26,10 @@ data class Proxy(
Socks5(false), Socks5(false),
Http(false), Http(false),
Vmess(false), Vmess(false),
Vless(false),
Trojan(false), Trojan(false),
Hysteria(false),
Relay(true), Relay(true),
Selector(true), Selector(true),