Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions base/pref.example.ini
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,11 @@ ruleset=!!import:snippets/rulesets.txt
;Rule with "[]" prefix will be added directly.

;custom_proxy_group=Proxy`select`.*`[]AUTO`[]DIRECT`.*
;custom_proxy_group=ProxyWithMaxFailed`select`.*`!!MAX_FAILED_TIMES=5`[]AUTO`[]DIRECT`.*
;custom_proxy_group=UrlTest`url-test`.*`http://www.gstatic.com/generate_204`300,5,100
;custom_proxy_group=FallBack`fallback`.*`http://www.gstatic.com/generate_204`300,5
;custom_proxy_group=LoadBalance`load-balance`.*`http://www.gstatic.com/generate_204`300,,100
;custom_proxy_group=LoadBalanceSticky`load-balance`.*`!!STRATEGY=sticky-sessions`!!MAX_FAILED_TIMES=5`http://www.gstatic.com/generate_204`300,,100
;custom_proxy_group=SSID`ssid`default_group`celluar=group0,ssid1=group1,ssid2=group2

;custom_proxy_group=g1`select`!!GROUPID=0
Expand Down
2 changes: 2 additions & 0 deletions base/pref.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ rulesets:
proxy_groups:
custom_proxy_group:
# - {name: UrlTest, type: url-test, rule: [".*"], url: http://www.gstatic.com/generate_204, interval: 300, tolerance: 100, timeout: 5}
# - {name: LoadBalanceSticky, type: load-balance, rule: [".*"], url: http://www.gstatic.com/generate_204, interval: 300, timeout: 5, tolerance: 100, max-failed-times: 5, strategy: sticky-sessions}
# - {name: Proxy, type: select, rule: [".*"]}
# - {name: ProxyWithMaxFailed, type: select, rule: [".*"], max-failed-times: 5}
# - {name: group1, type: select, rule: ["!!GROUPID=0"]}
# - {name: v2ray, type: select, rule: ["!!GROUP=V2RayProvider"]}
# - {import: snippets/groups_forcerule.txt}
Expand Down
24 changes: 24 additions & 0 deletions src/config/binding.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ namespace toml
case "round-robin"_hash:
conf.Strategy = BalanceStrategy::RoundRobin;
break;
case "sticky-sessions"_hash:
conf.Strategy = BalanceStrategy::StickySessions;
break;
}
if(v.contains("persistent"))
conf.Persistent = find_or(v, "persistent", conf.Persistent.get());
Expand Down Expand Up @@ -80,6 +83,7 @@ namespace toml
conf.Timeout = find_or(v, "timeout", 5);
conf.Proxies = find_or<StrArray>(v, "rule", {});
conf.UsingProvider = find_or<StrArray>(v, "use", {});
conf.MaxFailedTimes = find_or(v, "max_failed_times", 5);
if(conf.Proxies.empty() && conf.UsingProvider.empty())
throw serialization_error(format_error("Proxy Group must contains at least one of proxy match rule or provider!", v.location(), "here"), v.location());
if(v.contains("disable-udp"))
Expand Down Expand Up @@ -254,6 +258,26 @@ namespace INIBinding
conf.UsingProvider.reserve(conf.UsingProvider.size() + list.size());
std::move(list.begin(), list.end(), std::back_inserter(conf.UsingProvider));
}
else if(startsWith(vArray[i], "!!MAX_FAILED_TIMES="))
{
conf.MaxFailedTimes = to_int(vArray[i].substr(19), 0);
}
else if(startsWith(vArray[i], "!!STRATEGY="))
{
std::string strategy = vArray[i].substr(11);
switch(hash_(strategy))
{
case "consistent-hashing"_hash:
conf.Strategy = BalanceStrategy::ConsistentHashing;
break;
case "round-robin"_hash:
conf.Strategy = BalanceStrategy::RoundRobin;
break;
case "sticky-sessions"_hash:
conf.Strategy = BalanceStrategy::StickySessions;
break;
}
}
else
conf.Proxies.emplace_back(std::move(vArray[i]));
}
Expand Down
5 changes: 4 additions & 1 deletion src/config/proxygroup.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ enum class ProxyGroupType
enum class BalanceStrategy
{
ConsistentHashing,
RoundRobin
RoundRobin,
StickySessions
};

struct ProxyGroupConfig
Expand All @@ -30,6 +31,7 @@ struct ProxyGroupConfig
Integer Interval = 0;
Integer Timeout = 0;
Integer Tolerance = 0;
Integer MaxFailedTimes = 5;
BalanceStrategy Strategy = BalanceStrategy::ConsistentHashing;
Boolean Lazy;
Boolean DisableUdp;
Expand Down Expand Up @@ -57,6 +59,7 @@ struct ProxyGroupConfig
{
case BalanceStrategy::ConsistentHashing: return "consistent-hashing";
case BalanceStrategy::RoundRobin: return "round-robin";
case BalanceStrategy::StickySessions: return "sticky-sessions";
}
return "";
}
Expand Down
2 changes: 2 additions & 0 deletions src/generator/config/subexport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,8 @@ void proxyToClash(std::vector<Proxy> &nodes, YAML::Node &yamlnode, const ProxyGr
if(!x.DisableUdp.is_undef())
singlegroup["disable-udp"] = x.DisableUdp.get();

singlegroup["max-failed-times"] = x.MaxFailedTimes;

for(const auto& y : x.Proxies)
groupGenerate(y, nodelist, filtered_nodelist, true, ext);

Expand Down
7 changes: 7 additions & 0 deletions src/handler/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ void readGroup(YAML::Node node, string_array &dest, bool scope_limit = true)
continue;
}
std::string url = "http://www.gstatic.com/generate_204", interval = "300", tolerance, timeout;
std::string max_failed_times, strategy;
object["name"] >>= name;
object["type"] >>= type;
tempArray.emplace_back(name);
Expand All @@ -190,8 +191,14 @@ void readGroup(YAML::Node node, string_array &dest, bool scope_limit = true)
object["interval"] >>= interval;
object["tolerance"] >>= tolerance;
object["timeout"] >>= timeout;
object["max_failed_times"] >>= max_failed_times;
object["strategy"] >>= strategy;
for(std::size_t j = 0; j < object["rule"].size(); j++)
tempArray.emplace_back(safe_as<std::string>(object["rule"][j]));
if(!max_failed_times.empty())
tempArray.emplace_back("!!MAX_FAILED_TIMES=" + max_failed_times);
if(!strategy.empty())
tempArray.emplace_back("!!STRATEGY=" + strategy);
switch(hash_(type))
{
case "select"_hash:
Expand Down