Enhancements

Add support for tolerance option in Clash proxy groups.
Add options for specifying maximum allowed rulesets in external configs and generated rules.
This commit is contained in:
Tindy X committed 2020-05-30 18:09:23 +08:00
1 parent 8eadbcd6ea
commit a61a450fbe
5 files changed
+45 -21

No files matched your search

+21 -5
View File
@@ -611,25 +611,41 @@ public:
}
/**
* @brief Retrieve one boolean item value with the exact same name in the given section.
* @brief Retrieve one number item value with the exact same name in the given section.
*/
int GetIntIfExist(const std::string &section, const std::string &itemName, int &target)
template <typename T> int GetNumberIfExist(const std::string &section, const std::string &itemName, T &target)
{
std::string result;
int retval = GetIfExist(section, itemName, result);
if(retval != INIREADER_EXCEPTION_NONE)
return retval;
if(result.size())
target = to_int(result, target);
target = to_number<T>(result, target);
return INIREADER_EXCEPTION_NONE;
}
/**
* @brief Retrieve one boolean item value with the exact same name in current section.
* @brief Retrieve one number item value with the exact same name in current section.
*/
template <typename T> int GetNumberIfExist(const std::string &itemName, T &target)
{
return current_section.size() ? GetNumberIfExist(current_section, itemName, target) : INIREADER_EXCEPTION_NOTEXIST;
}
/**
* @brief Retrieve one integer item value with the exact same name in the given section.
*/
int GetIntIfExist(const std::string &section, const std::string &itemName, int &target)
{
return GetNumberIfExist<int>(section, itemName, target);
}
/**
* @brief Retrieve one integer item value with the exact same name in current section.
*/
int GetIntIfExist(const std::string &itemName, int &target)
{
return current_section.size() ? GetIntIfExist(current_section, itemName, target) : INIREADER_EXCEPTION_NOTEXIST;
return GetNumberIfExist<int>(itemName, target);
}
/**
+10 -6
View File
@@ -21,8 +21,6 @@
#include "upload.h"
#include "script_duktape.h"
#define MAX_EXTCONF_RULESET_COUNT 64
//common settings
std::string pref_path = "pref.ini", def_ext_config;
string_array def_exclude_remarks, def_include_remarks, rulesets, stream_rules, time_rules;
@@ -71,6 +69,9 @@ INIReader surge_base, mellow_base;
bool serve_cache_on_fetch_fail = false;
int cache_subscription = 60, cache_config = 300, cache_ruleset = 21600;
//limits
size_t max_allowed_rulesets = 64, max_allowed_rules = 32768;
string_array regex_blacklist = {"(.*)*"};
template <typename T> void operator >> (const YAML::Node& node, T& i)
@@ -224,8 +225,7 @@ std::string getRuleset(RESPONSE_CALLBACK_ARGS)
strLine = regReplace(strLine, rule_match_regex, "$1$3");
}
}
output_content.append(strLine + "\n");
output_content += strLine + "\n";
}
if(type_int == 3 && output_content == "payload:\n")
@@ -698,6 +698,8 @@ void readYAMLConf(YAML::Node &node)
node["advanced"]["max_pending_connections"] >> max_pending_connections;
node["advanced"]["max_concurrent_threads"] >> max_concurrent_threads;
node["advanced"]["enable_base_gen"] >> enable_base_gen;
node["advanced"]["max_allowed_rulesets"] >> max_allowed_rulesets;
node["advanced"]["max_allowed_rules"] >> max_allowed_rules;
if(node["advanced"]["enable_cache"].IsDefined())
{
if(safe_as<bool>(node["advanced"]["enable_cache"]))
@@ -933,6 +935,8 @@ void readConf()
ini.GetIntIfExist("max_pending_connections", max_pending_connections);
ini.GetIntIfExist("max_concurrent_threads", max_concurrent_threads);
ini.GetBoolIfExist("enable_base_gen", enable_base_gen);
ini.GetNumberIfExist("max_allowed_rulesets", max_allowed_rulesets);
ini.GetNumberIfExist("max_allowed_rules", max_allowed_rules);
if(ini.ItemExist("enable_cache"))
{
if(ini.GetBool("enable_cache"))
@@ -999,7 +1003,7 @@ int loadExternalYAML(YAML::Node &node, ExternalConfig &ext)
if(section["surge_ruleset"].size())
{
readRuleset(section["surge_ruleset"], ext.surge_ruleset, api_mode);
if(ext.surge_ruleset.size() > MAX_EXTCONF_RULESET_COUNT)
if(max_allowed_rulesets && ext.surge_ruleset.size() > max_allowed_rulesets)
{
writeLog(0, "Ruleset count in external config has exceeded limit.", LOG_LEVEL_WARNING);
eraseElements(ext.surge_ruleset);
@@ -1067,7 +1071,7 @@ int loadExternalConfig(std::string &path, ExternalConfig &ext)
{
ini.GetAll("surge_ruleset", ext.surge_ruleset);
importItems(ext.surge_ruleset, api_mode);
if(ext.surge_ruleset.size() > MAX_EXTCONF_RULESET_COUNT)
if(max_allowed_rulesets && ext.surge_ruleset.size() > max_allowed_rulesets)
{
writeLog(0, "Ruleset count in external config has exceeded limit. ", LOG_LEVEL_WARNING);
eraseElements(ext.surge_ruleset);
+10 -10
View File
@@ -22,10 +22,9 @@
#include <yaml-cpp/yaml.h>
#include <duktape.h>
#define MAX_RULES_COUNT 32768
extern bool api_mode;
extern string_array ss_ciphers, ssr_ciphers;
extern size_t max_allowed_rules;
const string_array clashr_protocols = {"auth_aes128_md5", "auth_aes128_sha1"};
const string_array clashr_obfs = {"plain", "http_simple", "http_post", "tls1.2_ticket_auth"};
@@ -577,7 +576,7 @@ void rulesetToClash(YAML::Node &base_rule, std::vector<ruleset_content> &ruleset
for(ruleset_content &x : ruleset_content_array)
{
if(total_rules > MAX_RULES_COUNT)
if(max_allowed_rules && total_rules > max_allowed_rules)
break;
rule_group = x.rule_group;
retrived_rules = x.rule_content.get();
@@ -605,7 +604,7 @@ void rulesetToClash(YAML::Node &base_rule, std::vector<ruleset_content> &ruleset
std::string::size_type lineSize;
while(getline(strStrm, strLine, delimiter))
{
if(total_rules > MAX_RULES_COUNT)
if(max_allowed_rules && total_rules > max_allowed_rules)
break;
lineSize = strLine.size();
/*
@@ -668,7 +667,7 @@ std::string rulesetToClashStr(YAML::Node &base_rule, std::vector<ruleset_content
for(ruleset_content &x : ruleset_content_array)
{
if(total_rules > MAX_RULES_COUNT)
if(max_allowed_rules && total_rules > max_allowed_rules)
break;
rule_group = x.rule_group;
retrived_rules = x.rule_content.get();
@@ -696,7 +695,7 @@ std::string rulesetToClashStr(YAML::Node &base_rule, std::vector<ruleset_content
std::string::size_type lineSize;
while(getline(strStrm, strLine, delimiter))
{
if(total_rules > MAX_RULES_COUNT)
if(max_allowed_rules && total_rules > max_allowed_rules)
break;
lineSize = strLine.size();
if(lineSize)
@@ -758,7 +757,7 @@ void rulesetToSurge(INIReader &base_rule, std::vector<ruleset_content> &ruleset_
for(ruleset_content &x : ruleset_content_array)
{
if(total_rules > MAX_RULES_COUNT)
if(max_allowed_rules && total_rules > max_allowed_rules)
break;
rule_group = x.rule_group;
rule_path = x.rule_path;
@@ -847,7 +846,7 @@ void rulesetToSurge(INIReader &base_rule, std::vector<ruleset_content> &ruleset_
std::string::size_type lineSize;
while(getline(strStrm, strLine, delimiter))
{
if(total_rules > MAX_RULES_COUNT)
if(max_allowed_rules && total_rules > max_allowed_rules)
break;
lineSize = strLine.size();
/*
@@ -1264,7 +1263,7 @@ void netchToClash(std::vector<nodeInfo> &nodes, YAML::Node &yamlnode, string_arr
singlegroup["name"] = vArray[0];
singlegroup["type"] = vArray[1];
int interval = -1;
int interval = 0, tolerance = 0;
rules_upper_bound = vArray.size();
switch(hash_(vArray[1]))
{
@@ -1278,8 +1277,9 @@ void netchToClash(std::vector<nodeInfo> &nodes, YAML::Node &yamlnode, string_arr
continue;
rules_upper_bound -= 2;
singlegroup["url"] = vArray[rules_upper_bound];
parseGroupTimes(vArray[rules_upper_bound + 1], &interval, NULL, NULL);
parseGroupTimes(vArray[rules_upper_bound + 1], &interval, &tolerance, NULL);
singlegroup["interval"] = interval;
singlegroup["tolerance"] = tolerance;
break;
default:
continue;