Add max rules limit to 32768

Add getLink to template function.
Change max ruleset limit in external configs to 64.
This commit is contained in:
Tindy X
2020-05-18 01:30:54 +08:00
parent 6219ad03bb
commit a1b7d635ba
3 changed files with 30 additions and 7 deletions

View File

@@ -20,7 +20,7 @@
#include "templates.h"
#include "upload.h"
#define MAX_EXTCONF_RULESET_COUNT 30
#define MAX_EXTCONF_RULESET_COUNT 64
//common settings
std::string pref_path = "pref.ini", def_ext_config;
@@ -2336,12 +2336,7 @@ std::string renderTemplate(RESPONSE_CALLBACK_ARGS)
std::string path = UrlDecode(getUrlArg(argument, "path"));
writeLog(0, "Trying to render template '" + path + "'...", LOG_LEVEL_INFO);
if(path.find(template_path) != 0)
{
*status_code = 403;
return "Out of scope";
}
if(!fileExist(path))
if(path.find(template_path) != 0 || !fileExist(path))
{
*status_code = 404;
return "Not found";

View File

@@ -20,6 +20,8 @@
#include <rapidjson/document.h>
#include <yaml-cpp/yaml.h>
#define MAX_RULES_COUNT 32768
extern bool api_mode;
extern string_array ss_ciphers, ssr_ciphers;
@@ -555,12 +557,15 @@ void rulesetToClash(YAML::Node &base_rule, std::vector<ruleset_content> &ruleset
std::stringstream strStrm;
const std::string field_name = new_field_name ? "rules" : "Rule";
YAML::Node Rules;
size_t total_rules = 0;
if(!overwrite_original_rules && base_rule[field_name].IsDefined())
Rules = base_rule[field_name];
for(ruleset_content &x : ruleset_content_array)
{
if(total_rules > MAX_RULES_COUNT)
break;
rule_group = x.rule_group;
retrived_rules = x.rule_content.get();
if(retrived_rules.empty())
@@ -577,6 +582,7 @@ void rulesetToClash(YAML::Node &base_rule, std::vector<ruleset_content> &ruleset
if(std::count(strLine.begin(), strLine.end(), ',') > 2)
strLine = regReplace(strLine, "^(.*?,.*?)(,.*)(,.*)$", "$1$3$2");
allRules.emplace_back(strLine);
total_rules++;
continue;
}
char delimiter = count(retrived_rules.begin(), retrived_rules.end(), '\n') < 1 ? '\r' : '\n';
@@ -586,6 +592,8 @@ 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)
break;
lineSize = strLine.size();
/*
if(lineSize && strLine[lineSize - 1] == '\r') //remove line break
@@ -636,6 +644,7 @@ std::string rulesetToClashStr(YAML::Node &base_rule, std::vector<ruleset_content
std::stringstream strStrm;
const std::string field_name = new_field_name ? "rules" : "Rule";
std::string output_content = "\n" + field_name + ":\n";
size_t total_rules = 0;
if(!overwrite_original_rules && base_rule[field_name].IsDefined())
{
@@ -646,6 +655,8 @@ std::string rulesetToClashStr(YAML::Node &base_rule, std::vector<ruleset_content
for(ruleset_content &x : ruleset_content_array)
{
if(total_rules > MAX_RULES_COUNT)
break;
rule_group = x.rule_group;
retrived_rules = x.rule_content.get();
if(retrived_rules.empty())
@@ -662,6 +673,7 @@ std::string rulesetToClashStr(YAML::Node &base_rule, std::vector<ruleset_content
if(std::count(strLine.begin(), strLine.end(), ',') > 2)
strLine = regReplace(strLine, "^(.*?,.*?)(,.*)(,.*)$", "$1$3$2");
output_content += " - " + strLine + "\n";
total_rules++;
continue;
}
char delimiter = count(retrived_rules.begin(), retrived_rules.end(), '\n') < 1 ? '\r' : '\n';
@@ -671,6 +683,8 @@ 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)
break;
lineSize = strLine.size();
if(lineSize)
{
@@ -685,6 +699,7 @@ std::string rulesetToClashStr(YAML::Node &base_rule, std::vector<ruleset_content
if(std::count(strLine.begin(), strLine.end(), ',') > 2)
strLine = regReplace(strLine, "^(.*?,.*?)(,.*)(,.*)$", "$1$3$2");
output_content += " - " + strLine + "\n";
total_rules++;
}
}
return output_content;
@@ -695,6 +710,7 @@ void rulesetToSurge(INIReader &base_rule, std::vector<ruleset_content> &ruleset_
string_array allRules;
std::string rule_group, rule_path, retrieved_rules, strLine;
std::stringstream strStrm;
size_t total_rules = 0;
switch(surge_ver) //other version: -3 for Surfboard, -4 for Loon
{
@@ -729,6 +745,8 @@ void rulesetToSurge(INIReader &base_rule, std::vector<ruleset_content> &ruleset_
for(ruleset_content &x : ruleset_content_array)
{
if(total_rules > MAX_RULES_COUNT)
break;
rule_group = x.rule_group;
rule_path = x.rule_path;
if(rule_path.empty())
@@ -751,6 +769,7 @@ void rulesetToSurge(INIReader &base_rule, std::vector<ruleset_content> &ruleset_
}
strLine = replace_all_distinct(strLine, ",,", ",");
allRules.emplace_back(strLine);
total_rules++;
continue;
}
else
@@ -815,6 +834,8 @@ 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)
break;
lineSize = strLine.size();
/*
if(lineSize && strLine[lineSize - 1] == '\r') //remove line break
@@ -875,6 +896,7 @@ void rulesetToSurge(INIReader &base_rule, std::vector<ruleset_content> &ruleset_
strLine = regReplace(strLine, rule_match_regex, "$1$3$2");
}
allRules.emplace_back(strLine);
total_rules++;
}
}
}

View File

@@ -9,6 +9,8 @@
#include "logger.h"
#include "misc.h"
extern std::string managed_config_prefix;
static inline void parse_json_pointer(nlohmann::json &json, const std::string &path, const std::string &value)
{
std::string pointer = "/" + replace_all_distinct(path, ".", "/");
@@ -110,6 +112,10 @@ int render_template(const std::string &content, const template_args &vars, std::
data[nlohmann::json::json_pointer(pointer)] = output_content;
return std::string();
});
m_callbacks.add_callback("getLink", 1, [](inja::Arguments &args)
{
return managed_config_prefix + args.at(0)->get<std::string>();
});
m_callbacks.add_callback("fetch", 1, template_webGet);
m_callbacks.add_callback("parseHostname", 1, parseHostname);
m_parser_config.include_scope_limit = true;