From e34add21d210bf3ab374045089cceacb7528c2ad Mon Sep 17 00:00:00 2001 From: Tindy X <49061470+tindy2013@users.noreply.github.com> Date: Sun, 31 Jul 2022 01:05:20 +0800 Subject: [PATCH] Enhancements Fix load-balance in Surge configuration not being generated correctly. Add persistent and evaluate-before-use options to Surge proxy-groups. --- src/config/binding.h | 6 ++++++ src/config/proxygroup.h | 2 ++ src/generator/config/ruleconvert.cpp | 10 +++++----- src/generator/config/subexport.cpp | 14 +++++++++----- 4 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/config/binding.h b/src/config/binding.h index 347ef29..90cfc64 100644 --- a/src/config/binding.h +++ b/src/config/binding.h @@ -32,6 +32,8 @@ namespace toml conf.Tolerance = toml::find_or(v, "tolerance", 0); if(v.contains("lazy")) conf.Lazy = toml::find_or(v, "lazy", false); + if(v.contains("evaluate-before-use")) + conf.EvaluateBeforeUse = toml::find_or(v, "evaluate-before-use", conf.EvaluateBeforeUse.get()); break; case "load-balance"_hash: conf.Type = ProxyGroupType::LoadBalance; @@ -46,11 +48,15 @@ namespace toml conf.Strategy = BalanceStrategy::RoundRobin; break; } + if(v.contains("persistent")) + conf.Persistent = toml::find_or(v, "persistent", conf.Persistent.get()); break; case "fallback"_hash: conf.Type = ProxyGroupType::Fallback; conf.Url = toml::find(v, "url"); conf.Interval = toml::find(v, "interval"); + if(v.contains("evaluate-before-use")) + conf.EvaluateBeforeUse = toml::find_or(v, "evaluate-before-use", conf.EvaluateBeforeUse.get()); break; case "relay"_hash: conf.Type = ProxyGroupType::Relay; diff --git a/src/config/proxygroup.h b/src/config/proxygroup.h index 85baa36..4300e29 100644 --- a/src/config/proxygroup.h +++ b/src/config/proxygroup.h @@ -32,6 +32,8 @@ struct ProxyGroupConfig BalanceStrategy Strategy = BalanceStrategy::ConsistentHashing; Boolean Lazy; Boolean DisableUdp; + Boolean Persistent; + Boolean EvaluateBeforeUse; String TypeStr() const { diff --git a/src/generator/config/ruleconvert.cpp b/src/generator/config/ruleconvert.cpp index 98a2aab..b84692d 100644 --- a/src/generator/config/ruleconvert.cpp +++ b/src/generator/config/ruleconvert.cpp @@ -100,11 +100,11 @@ void rulesetToClash(YAML::Node &base_rule, std::vector &ruleset_ std::string rule_group, retrieved_rules, strLine; std::stringstream strStrm; const std::string field_name = new_field_name ? "rules" : "Rule"; - YAML::Node Rules; + YAML::Node rules; size_t total_rules = 0; if(!overwrite_original_rules && base_rule[field_name].IsDefined()) - Rules = base_rule[field_name]; + rules = base_rule[field_name]; for(RulesetContent &x : ruleset_content_array) { @@ -154,16 +154,16 @@ void rulesetToClash(YAML::Node &base_rule, std::vector &ruleset_ if(count_least(strLine, ',', 3)) strLine = regReplace(strLine, "^(.*?,.*?)(,.*)(,.*)$", "$1$3$2"); allRules.emplace_back(std::move(strLine)); - //Rules.push_back(strLine); + //rules.push_back(strLine); } } for(std::string &x : allRules) { - Rules.push_back(x); + rules.push_back(x); } - base_rule[field_name] = Rules; + base_rule[field_name] = rules; } std::string rulesetToClashStr(YAML::Node &base_rule, std::vector &ruleset_content_array, bool overwrite_original_rules, bool new_field_name) diff --git a/src/generator/config/subexport.cpp b/src/generator/config/subexport.cpp index 27576f1..db42b10 100644 --- a/src/generator/config/subexport.cpp +++ b/src/generator/config/subexport.cpp @@ -580,6 +580,8 @@ std::string proxyToClash(std::vector &nodes, const std::string &base_conf std::string output_content = rulesetToClashStr(yamlnode, ruleset_content_array, ext.overwrite_original_rules, ext.clash_new_field_name); output_content.insert(0, YAML::Dump(yamlnode)); + //rulesetToClash(yamlnode, ruleset_content_array, ext.overwrite_original_rules, ext.clash_new_field_name); + //std::string output_content = YAML::Dump(yamlnode); return output_content; } @@ -790,9 +792,9 @@ std::string proxyToSurge(std::vector &nodes, const std::string &base_conf case ProxyGroupType::Fallback: break; case ProxyGroupType::LoadBalance: - if(surge_ver < 1) + if(surge_ver < 1 && surge_ver != -3) continue; - [[fallthrough]]; + break; case ProxyGroupType::SSID: proxy = x.TypeStr() + ",default=" + x.Proxies[0] + ","; proxy += join(x.Proxies.begin() + 1, x.Proxies.end(), ","); @@ -823,16 +825,18 @@ std::string proxyToSurge(std::vector &nodes, const std::string &base_conf proxy = x.TypeStr() + ","; proxy += join(filtered_nodelist, ","); - if(x.Type == ProxyGroupType::URLTest || x.Type == ProxyGroupType::Fallback) + if(x.Type == ProxyGroupType::URLTest || x.Type == ProxyGroupType::Fallback || x.Type == ProxyGroupType::LoadBalance) { proxy += ",url=" + x.Url + ",interval=" + std::to_string(x.Interval); if(x.Tolerance > 0) proxy += ",tolerance=" + std::to_string(x.Tolerance); if(x.Timeout > 0) proxy += ",timeout=" + std::to_string(x.Timeout); + if(!x.Persistent.is_undef()) + proxy += ",persistent=" + x.Persistent.get_str(); + if(!x.EvaluateBeforeUse.is_undef()) + proxy += ",evaluate-before-use=" + x.EvaluateBeforeUse.get_str(); } - else if(x.Type == ProxyGroupType::LoadBalance) - proxy += ",url=" + x.Url; ini.Set("{NONAME}", x.Name + " = " + proxy); //insert order }