Enhancements

Fix load-balance in Surge configuration not being generated correctly.
Add persistent and evaluate-before-use options to Surge proxy-groups.
This commit is contained in:
Tindy X 2022-07-31 01:05:20 +08:00
parent f6e77b7706
commit e34add21d2
No known key found for this signature in database
GPG Key ID: C6AD413169968D58
4 changed files with 22 additions and 10 deletions

View File

@ -32,6 +32,8 @@ namespace toml
conf.Tolerance = toml::find_or<Integer>(v, "tolerance", 0); conf.Tolerance = toml::find_or<Integer>(v, "tolerance", 0);
if(v.contains("lazy")) if(v.contains("lazy"))
conf.Lazy = toml::find_or<bool>(v, "lazy", false); conf.Lazy = toml::find_or<bool>(v, "lazy", false);
if(v.contains("evaluate-before-use"))
conf.EvaluateBeforeUse = toml::find_or(v, "evaluate-before-use", conf.EvaluateBeforeUse.get());
break; break;
case "load-balance"_hash: case "load-balance"_hash:
conf.Type = ProxyGroupType::LoadBalance; conf.Type = ProxyGroupType::LoadBalance;
@ -46,11 +48,15 @@ namespace toml
conf.Strategy = BalanceStrategy::RoundRobin; conf.Strategy = BalanceStrategy::RoundRobin;
break; break;
} }
if(v.contains("persistent"))
conf.Persistent = toml::find_or(v, "persistent", conf.Persistent.get());
break; break;
case "fallback"_hash: case "fallback"_hash:
conf.Type = ProxyGroupType::Fallback; conf.Type = ProxyGroupType::Fallback;
conf.Url = toml::find<String>(v, "url"); conf.Url = toml::find<String>(v, "url");
conf.Interval = toml::find<Integer>(v, "interval"); conf.Interval = toml::find<Integer>(v, "interval");
if(v.contains("evaluate-before-use"))
conf.EvaluateBeforeUse = toml::find_or(v, "evaluate-before-use", conf.EvaluateBeforeUse.get());
break; break;
case "relay"_hash: case "relay"_hash:
conf.Type = ProxyGroupType::Relay; conf.Type = ProxyGroupType::Relay;

View File

@ -32,6 +32,8 @@ struct ProxyGroupConfig
BalanceStrategy Strategy = BalanceStrategy::ConsistentHashing; BalanceStrategy Strategy = BalanceStrategy::ConsistentHashing;
Boolean Lazy; Boolean Lazy;
Boolean DisableUdp; Boolean DisableUdp;
Boolean Persistent;
Boolean EvaluateBeforeUse;
String TypeStr() const String TypeStr() const
{ {

View File

@ -100,11 +100,11 @@ void rulesetToClash(YAML::Node &base_rule, std::vector<RulesetContent> &ruleset_
std::string rule_group, retrieved_rules, strLine; std::string rule_group, retrieved_rules, strLine;
std::stringstream strStrm; std::stringstream strStrm;
const std::string field_name = new_field_name ? "rules" : "Rule"; const std::string field_name = new_field_name ? "rules" : "Rule";
YAML::Node Rules; YAML::Node rules;
size_t total_rules = 0; size_t total_rules = 0;
if(!overwrite_original_rules && base_rule[field_name].IsDefined()) 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) for(RulesetContent &x : ruleset_content_array)
{ {
@ -154,16 +154,16 @@ void rulesetToClash(YAML::Node &base_rule, std::vector<RulesetContent> &ruleset_
if(count_least(strLine, ',', 3)) if(count_least(strLine, ',', 3))
strLine = regReplace(strLine, "^(.*?,.*?)(,.*)(,.*)$", "$1$3$2"); strLine = regReplace(strLine, "^(.*?,.*?)(,.*)(,.*)$", "$1$3$2");
allRules.emplace_back(std::move(strLine)); allRules.emplace_back(std::move(strLine));
//Rules.push_back(strLine); //rules.push_back(strLine);
} }
} }
for(std::string &x : allRules) 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<RulesetContent> &ruleset_content_array, bool overwrite_original_rules, bool new_field_name) std::string rulesetToClashStr(YAML::Node &base_rule, std::vector<RulesetContent> &ruleset_content_array, bool overwrite_original_rules, bool new_field_name)

View File

@ -580,6 +580,8 @@ std::string proxyToClash(std::vector<Proxy> &nodes, const std::string &base_conf
std::string output_content = rulesetToClashStr(yamlnode, ruleset_content_array, ext.overwrite_original_rules, ext.clash_new_field_name); 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)); 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; return output_content;
} }
@ -790,9 +792,9 @@ std::string proxyToSurge(std::vector<Proxy> &nodes, const std::string &base_conf
case ProxyGroupType::Fallback: case ProxyGroupType::Fallback:
break; break;
case ProxyGroupType::LoadBalance: case ProxyGroupType::LoadBalance:
if(surge_ver < 1) if(surge_ver < 1 && surge_ver != -3)
continue; continue;
[[fallthrough]]; break;
case ProxyGroupType::SSID: case ProxyGroupType::SSID:
proxy = x.TypeStr() + ",default=" + x.Proxies[0] + ","; proxy = x.TypeStr() + ",default=" + x.Proxies[0] + ",";
proxy += join(x.Proxies.begin() + 1, x.Proxies.end(), ","); proxy += join(x.Proxies.begin() + 1, x.Proxies.end(), ",");
@ -823,16 +825,18 @@ std::string proxyToSurge(std::vector<Proxy> &nodes, const std::string &base_conf
proxy = x.TypeStr() + ","; proxy = x.TypeStr() + ",";
proxy += join(filtered_nodelist, ","); 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); proxy += ",url=" + x.Url + ",interval=" + std::to_string(x.Interval);
if(x.Tolerance > 0) if(x.Tolerance > 0)
proxy += ",tolerance=" + std::to_string(x.Tolerance); proxy += ",tolerance=" + std::to_string(x.Tolerance);
if(x.Timeout > 0) if(x.Timeout > 0)
proxy += ",timeout=" + std::to_string(x.Timeout); 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 ini.Set("{NONAME}", x.Name + " = " + proxy); //insert order
} }