refactor: adjust provider loading order, remove meaningless pointers

This commit is contained in:
Skyxim 2022-04-28 08:55:45 +08:00
parent 30eaa8add6
commit 2e74986fe4
3 changed files with 35 additions and 32 deletions

View file

@ -151,7 +151,7 @@ type Config struct {
Users []auth.AuthUser
Proxies map[string]C.Proxy
Providers map[string]providerTypes.ProxyProvider
RuleProviders map[string]*providerTypes.RuleProvider
RuleProviders map[string]providerTypes.RuleProvider
Sniffer *Sniffer
}
@ -523,8 +523,8 @@ func parseProxies(cfg *RawConfig) (proxies map[string]C.Proxy, providersMap map[
return proxies, providersMap, nil
}
func parseRules(cfg *RawConfig, proxies map[string]C.Proxy) ([]C.Rule, map[string]*providerTypes.RuleProvider, error) {
ruleProviders := map[string]*providerTypes.RuleProvider{}
func parseRules(cfg *RawConfig, proxies map[string]C.Proxy) ([]C.Rule, map[string]providerTypes.RuleProvider, error) {
ruleProviders := map[string]providerTypes.RuleProvider{}
log.Infoln("Geodata Loader mode: %s", geodata.LoaderName())
// parse rule provider
for name, mapping := range cfg.RuleProvider {
@ -533,7 +533,7 @@ func parseRules(cfg *RawConfig, proxies map[string]C.Proxy) ([]C.Rule, map[strin
return nil, nil, err
}
ruleProviders[name] = &rp
ruleProviders[name] = rp
RP.SetRuleProvider(rp)
}

View file

@ -82,8 +82,9 @@ func ApplyConfig(cfg *config.Config, force bool) {
updateTun(cfg.Tun, cfg.DNS)
updateExperimental(cfg)
updateHosts(cfg.Hosts)
loadProvider(cfg.RuleProviders, cfg.Providers)
loadProxyProvider(cfg.Providers)
updateProfile(cfg)
loadRuleProvider(cfg.RuleProviders)
log.SetLevel(cfg.General.LogLevel)
}
@ -181,39 +182,41 @@ func updateProxies(proxies map[string]C.Proxy, providers map[string]provider.Pro
tunnel.UpdateProxies(proxies, providers)
}
func updateRules(rules []C.Rule, ruleProviders map[string]*provider.RuleProvider) {
func updateRules(rules []C.Rule, ruleProviders map[string]provider.RuleProvider) {
tunnel.UpdateRules(rules, ruleProviders)
}
func loadProvider(ruleProviders map[string]*provider.RuleProvider, proxyProviders map[string]provider.ProxyProvider) {
load := func(pv provider.Provider) {
if pv.VehicleType() == provider.Compatible {
log.Infoln("Start initial compatible provider %s", pv.Name())
} else {
log.Infoln("Start initial provider %s", (pv).Name())
}
if err := (pv).Initial(); err != nil {
switch pv.Type() {
case provider.Proxy:
{
log.Warnln("initial proxy provider %s error: %v", (pv).Name(), err)
}
case provider.Rule:
{
log.Warnln("initial rule provider %s error: %v", (pv).Name(), err)
}
func loadProvider(pv provider.Provider) {
if pv.VehicleType() == provider.Compatible {
log.Infoln("Start initial compatible provider %s", pv.Name())
} else {
log.Infoln("Start initial provider %s", (pv).Name())
}
if err := (pv).Initial(); err != nil {
switch pv.Type() {
case provider.Proxy:
{
log.Warnln("initial proxy provider %s error: %v", (pv).Name(), err)
}
case provider.Rule:
{
log.Warnln("initial rule provider %s error: %v", (pv).Name(), err)
}
}
}
}
for _, proxyProvider := range proxyProviders {
load(proxyProvider)
}
func loadRuleProvider(ruleProviders map[string]provider.RuleProvider) {
for _, ruleProvider := range ruleProviders {
load(*ruleProvider)
loadProvider(ruleProvider)
}
}
func loadProxyProvider(ruleProviders map[string]provider.ProxyProvider) {
for _, ruleProvider := range ruleProviders {
loadProvider(ruleProvider)
}
}

View file

@ -30,7 +30,7 @@ var (
rules []C.Rule
proxies = make(map[string]C.Proxy)
providers map[string]provider.ProxyProvider
ruleProviders map[string]*provider.RuleProvider
ruleProviders map[string]provider.RuleProvider
configMux sync.RWMutex
// Outbound Rule
@ -62,7 +62,7 @@ func Rules() []C.Rule {
}
// UpdateRules handle update rules
func UpdateRules(newRules []C.Rule, rp map[string]*provider.RuleProvider) {
func UpdateRules(newRules []C.Rule, rp map[string]provider.RuleProvider) {
configMux.Lock()
rules = newRules
ruleProviders = rp
@ -80,7 +80,7 @@ func Providers() map[string]provider.ProxyProvider {
}
// RuleProviders return all loaded rule providers
func RuleProviders() map[string]*provider.RuleProvider {
func RuleProviders() map[string]provider.RuleProvider {
return ruleProviders
}