Fix cache system not working properly

Clean up codes.
This commit is contained in:
Tindy X
2021-10-10 21:32:54 +08:00
parent b972dfaa56
commit b4201b24f7
18 changed files with 1661 additions and 1667 deletions

View File

@@ -3,6 +3,7 @@
#include <toml.hpp>
#include "../handler/settings.h"
#include "crontask.h"
#include "proxygroup.h"
#include "regmatch.h"
@@ -171,29 +172,6 @@ namespace toml
namespace INIBinding
{
void parseGroupTimes(const std::string &src, int *interval, int *tolerance, int *timeout)
{
std::vector<int*> ptrs;
ptrs.push_back(interval);
ptrs.push_back(timeout);
ptrs.push_back(tolerance);
string_size bpos = 0, epos = src.find(",");
for(int *x : ptrs)
{
if(x != NULL)
*x = to_int(src.substr(bpos, epos - bpos), 0);
if(epos != src.npos)
{
bpos = epos + 1;
epos = src.find(",", bpos);
}
else
return;
}
return;
}
template<class T> struct from
{};

View File

@@ -3,6 +3,7 @@
#include <iostream>
#include <algorithm>
#include "../../handler/settings.h"
#include "../../handler/webget.h"
#include "../../parser/config/proxy.h"
#include "../../parser/infoparser.h"
@@ -17,10 +18,7 @@
#include "nodemanip.h"
#include "subexport.h"
std::string override_conf_port;
bool ss_libev, ssr_libev;
extern int gCacheSubscription;
extern bool gScriptCleanContext;
extern Settings global;
int explodeConf(const std::string &filepath, std::vector<Proxy> &nodes)
{
@@ -39,7 +37,7 @@ int addNodes(std::string link, std::vector<Proxy> &allNodes, int groupID, parse_
string_array &include_remarks = *parse_set.include_remarks;
RegexMatchConfigs &stream_rules = *parse_set.stream_rules;
RegexMatchConfigs &time_rules = *parse_set.time_rules;
string_icase_map &request_headers = *parse_set.request_header;
string_icase_map *request_headers = parse_set.request_header;
bool &authorized = parse_set.authorized;
ConfType linkType = ConfType::Unknow;
@@ -88,7 +86,7 @@ int addNodes(std::string link, std::vector<Proxy> &allNodes, int groupID, parse_
}
}
}
}, gScriptCleanContext);
}, global.scriptCleanContext);
/*
duk_context *ctx = duktape_init();
defer(duk_destroy_heap(ctx);)
@@ -142,7 +140,7 @@ int addNodes(std::string link, std::vector<Proxy> &allNodes, int groupID, parse_
writeLog(LOG_TYPE_INFO, "Downloading subscription data...");
if(startsWith(link, "surge:///install-config")) //surge config link
link = urlDecode(getUrlArg(link, "url"));
strSub = webGet(link, proxy, gCacheSubscription, &extra_headers, &request_headers);
strSub = webGet(link, proxy, global.cacheSubscription, &extra_headers, request_headers);
/*
if(strSub.size() == 0)
{

View File

@@ -1,13 +1,12 @@
#include <string>
#include "../../handler/settings.h"
#include "../../utils/logger.h"
#include "../../utils/network.h"
#include "../../utils/regexp.h"
#include "../../utils/string.h"
#include "subexport.h"
extern size_t gMaxAllowedRules;
/// rule type lists
#define basic_types "DOMAIN", "DOMAIN-SUFFIX", "DOMAIN-KEYWORD", "IP-CIDR", "SRC-IP-CIDR", "GEOIP", "MATCH", "FINAL"
string_array ClashRuleTypes = {basic_types, "IP-CIDR6", "SRC-PORT", "DST-PORT", "PROCESS-NAME"};
@@ -95,7 +94,7 @@ std::string convertRuleset(const std::string &content, int type)
}
void rulesetToClash(YAML::Node &base_rule, std::vector<ruleset_content> &ruleset_content_array, bool overwrite_original_rules, bool new_field_name)
void rulesetToClash(YAML::Node &base_rule, std::vector<RulesetContent> &ruleset_content_array, bool overwrite_original_rules, bool new_field_name)
{
string_array allRules;
std::string rule_group, retrieved_rules, strLine;
@@ -107,9 +106,9 @@ void rulesetToClash(YAML::Node &base_rule, std::vector<ruleset_content> &ruleset
if(!overwrite_original_rules && base_rule[field_name].IsDefined())
Rules = base_rule[field_name];
for(ruleset_content &x : ruleset_content_array)
for(RulesetContent &x : ruleset_content_array)
{
if(gMaxAllowedRules && total_rules > gMaxAllowedRules)
if(global.maxAllowedRules && total_rules > global.maxAllowedRules)
break;
rule_group = x.rule_group;
retrieved_rules = x.rule_content.get();
@@ -138,7 +137,7 @@ void rulesetToClash(YAML::Node &base_rule, std::vector<ruleset_content> &ruleset
std::string::size_type lineSize;
while(getline(strStrm, strLine, delimiter))
{
if(gMaxAllowedRules && total_rules > gMaxAllowedRules)
if(global.maxAllowedRules && total_rules > global.maxAllowedRules)
break;
strLine = trimWhitespace(strLine, true, true); //remove whitespaces
lineSize = strLine.size();
@@ -167,7 +166,7 @@ void rulesetToClash(YAML::Node &base_rule, std::vector<ruleset_content> &ruleset
base_rule[field_name] = Rules;
}
std::string rulesetToClashStr(YAML::Node &base_rule, std::vector<ruleset_content> &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)
{
std::string rule_group, retrieved_rules, strLine;
std::stringstream strStrm;
@@ -182,9 +181,9 @@ std::string rulesetToClashStr(YAML::Node &base_rule, std::vector<ruleset_content
}
base_rule.remove(field_name);
for(ruleset_content &x : ruleset_content_array)
for(RulesetContent &x : ruleset_content_array)
{
if(gMaxAllowedRules && total_rules > gMaxAllowedRules)
if(global.maxAllowedRules && total_rules > global.maxAllowedRules)
break;
rule_group = x.rule_group;
retrieved_rules = x.rule_content.get();
@@ -213,7 +212,7 @@ std::string rulesetToClashStr(YAML::Node &base_rule, std::vector<ruleset_content
std::string::size_type lineSize;
while(getline(strStrm, strLine, delimiter))
{
if(gMaxAllowedRules && total_rules > gMaxAllowedRules)
if(global.maxAllowedRules && total_rules > global.maxAllowedRules)
break;
strLine = trimWhitespace(strLine, true, true); //remove whitespaces
lineSize = strLine.size();
@@ -236,7 +235,7 @@ std::string rulesetToClashStr(YAML::Node &base_rule, std::vector<ruleset_content
return output_content;
}
void rulesetToSurge(INIReader &base_rule, std::vector<ruleset_content> &ruleset_content_array, int surge_ver, bool overwrite_original_rules, std::string remote_path_prefix)
void rulesetToSurge(INIReader &base_rule, std::vector<RulesetContent> &ruleset_content_array, int surge_ver, bool overwrite_original_rules, std::string remote_path_prefix)
{
string_array allRules;
std::string rule_group, rule_path, rule_path_typed, retrieved_rules, strLine;
@@ -274,9 +273,9 @@ void rulesetToSurge(INIReader &base_rule, std::vector<ruleset_content> &ruleset_
const std::string rule_match_regex = "^(.*?,.*?)(,.*)(,.*)$";
for(ruleset_content &x : ruleset_content_array)
for(RulesetContent &x : ruleset_content_array)
{
if(gMaxAllowedRules && total_rules > gMaxAllowedRules)
if(global.maxAllowedRules && total_rules > global.maxAllowedRules)
break;
rule_group = x.rule_group;
rule_path = x.rule_path;
@@ -387,7 +386,7 @@ void rulesetToSurge(INIReader &base_rule, std::vector<ruleset_content> &ruleset_
std::string::size_type lineSize;
while(getline(strStrm, strLine, delimiter))
{
if(gMaxAllowedRules && total_rules > gMaxAllowedRules)
if(global.maxAllowedRules && total_rules > global.maxAllowedRules)
break;
strLine = trimWhitespace(strLine, true, true);
lineSize = strLine.size();

View File

@@ -18,7 +18,7 @@ enum ruleset_type
RULESET_CLASH_CLASSICAL
};
struct ruleset_content
struct RulesetContent
{
std::string rule_group;
std::string rule_path;
@@ -29,8 +29,8 @@ struct ruleset_content
};
std::string convertRuleset(const std::string &content, int type);
void rulesetToClash(YAML::Node &base_rule, std::vector<ruleset_content> &ruleset_content_array, bool overwrite_original_rules, bool new_field_name);
std::string rulesetToClashStr(YAML::Node &base_rule, std::vector<ruleset_content> &ruleset_content_array, bool overwrite_original_rules, bool new_field_name);
void rulesetToSurge(INIReader &base_rule, std::vector<ruleset_content> &ruleset_content_array, int surge_ver, bool overwrite_original_rules, std::string remote_path_prefix);
void rulesetToClash(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);
void rulesetToSurge(INIReader &base_rule, std::vector<RulesetContent> &ruleset_content_array, int surge_ver, bool overwrite_original_rules, std::string remote_path_prefix);
#endif // RULECONVERT_H_INCLUDED

View File

@@ -11,6 +11,7 @@
#include "../../config/regmatch.h"
#include "../../generator/config/subexport.h"
#include "../../generator/template/templates.h"
#include "../../handler/settings.h"
#include "../../parser/config/proxy.h"
#include "../../script/script_quickjs.h"
#include "../../utils/bitwise.h"
@@ -26,7 +27,6 @@
#include "nodemanip.h"
#include "ruleconvert.h"
extern bool gAPIMode, gSurgeResolveHostname, gScriptCleanContext;
extern string_array ss_ciphers, ssr_ciphers;
const string_array clashr_protocols = {"origin", "auth_sha1_v4", "auth_aes128_md5", "auth_aes128_sha1", "auth_chain_a", "auth_chain_b"};
@@ -89,7 +89,7 @@ void nodeRename(Proxy &node, const RegexMatchConfigs &rename_array, extra_settin
{
script_print_stack(ctx);
}
}, gScriptCleanContext);
}, global.scriptCleanContext);
continue;
}
if(applyMatcher(x.Match, real_rule, node) && real_rule.size())
@@ -142,7 +142,7 @@ std::string addEmoji(const Proxy &node, const RegexMatchConfigs &emoji_array, ex
{
script_print_stack(ctx);
}
}, gScriptCleanContext);
}, global.scriptCleanContext);
if(!result.empty())
return result;
continue;
@@ -175,28 +175,6 @@ void processRemark(std::string &oldremark, std::string &newremark, string_array
oldremark = newremark;
}
void parseGroupTimes(const std::string &src, int *interval, int *tolerance, int *timeout)
{
std::vector<int*> ptrs;
ptrs.push_back(interval);
ptrs.push_back(timeout);
ptrs.push_back(tolerance);
string_size bpos = 0, epos = src.find(",");
for(int *x : ptrs)
{
if(x != NULL)
*x = to_int(src.substr(bpos, epos - bpos), 0);
if(epos != src.npos)
{
bpos = epos + 1;
epos = src.find(",", bpos);
}
else
return;
}
return;
}
void groupGenerate(const std::string &rule, std::vector<Proxy> &nodelist, string_array &filtered_nodelist, bool add_direct, extra_settings &ext)
{
std::string real_rule;
@@ -219,7 +197,7 @@ void groupGenerate(const std::string &rule, std::vector<Proxy> &nodelist, string
{
script_print_stack(ctx);
}
}, gScriptCleanContext);
}, global.scriptCleanContext);
}
else
{
@@ -273,7 +251,7 @@ void preprocessNodes(std::vector<Proxy> &nodes, extra_settings &ext)
{
script_print_stack(ctx);
}
}, gScriptCleanContext);
}, global.scriptCleanContext);
}
if(failed) std::stable_sort(nodes.begin(), nodes.end(), [](const Proxy &a, const Proxy &b)
{
@@ -592,7 +570,7 @@ void proxyToClash(std::vector<Proxy> &nodes, YAML::Node &yamlnode, const ProxyGr
yamlnode["Proxy Group"] = original_groups;
}
std::string proxyToClash(std::vector<Proxy> &nodes, const std::string &base_conf, std::vector<ruleset_content> &ruleset_content_array, const ProxyGroupConfigs &extra_proxy_group, bool clashR, extra_settings &ext)
std::string proxyToClash(std::vector<Proxy> &nodes, const std::string &base_conf, std::vector<RulesetContent> &ruleset_content_array, const ProxyGroupConfigs &extra_proxy_group, bool clashR, extra_settings &ext)
{
YAML::Node yamlnode;
@@ -640,7 +618,7 @@ std::string proxyToClash(std::vector<Proxy> &nodes, const std::string &base_conf
return output_content;
}
std::string proxyToSurge(std::vector<Proxy> &nodes, const std::string &base_conf, std::vector<ruleset_content> &ruleset_content_array, const ProxyGroupConfigs &extra_proxy_group, int surge_ver, extra_settings &ext)
std::string proxyToSurge(std::vector<Proxy> &nodes, const std::string &base_conf, std::vector<RulesetContent> &ruleset_content_array, const ProxyGroupConfigs &extra_proxy_group, int surge_ver, extra_settings &ext)
{
INIReader ini;
std::string proxy;
@@ -762,7 +740,7 @@ std::string proxyToSurge(std::vector<Proxy> &nodes, const std::string &base_conf
proxy += "\", local-port=" + std::to_string(local_port);
if(isIPv4(hostname) || isIPv6(hostname))
proxy += ", addresses=" + hostname;
else if(gSurgeResolveHostname)
else if(global.surgeResolveHostname)
proxy += ", addresses=" + hostnameToIPAddr(hostname);
local_port++;
break;
@@ -1041,7 +1019,7 @@ std::string proxyToSSSub(std::string base_conf, std::vector<Proxy> &nodes, extra
return output_content;
}
std::string proxyToQuan(std::vector<Proxy> &nodes, const std::string &base_conf, std::vector<ruleset_content> &ruleset_content_array, const ProxyGroupConfigs &extra_proxy_group, extra_settings &ext)
std::string proxyToQuan(std::vector<Proxy> &nodes, const std::string &base_conf, std::vector<RulesetContent> &ruleset_content_array, const ProxyGroupConfigs &extra_proxy_group, extra_settings &ext)
{
INIReader ini;
ini.store_any_line = true;
@@ -1065,7 +1043,7 @@ std::string proxyToQuan(std::vector<Proxy> &nodes, const std::string &base_conf,
return ini.ToString();
}
void proxyToQuan(std::vector<Proxy> &nodes, INIReader &ini, std::vector<ruleset_content> &ruleset_content_array, const ProxyGroupConfigs &extra_proxy_group, extra_settings &ext)
void proxyToQuan(std::vector<Proxy> &nodes, INIReader &ini, std::vector<RulesetContent> &ruleset_content_array, const ProxyGroupConfigs &extra_proxy_group, extra_settings &ext)
{
std::string type, proxyStr;
tribool scv;
@@ -1268,7 +1246,7 @@ void proxyToQuan(std::vector<Proxy> &nodes, INIReader &ini, std::vector<ruleset_
rulesetToSurge(ini, ruleset_content_array, -2, ext.overwrite_original_rules, std::string());
}
std::string proxyToQuanX(std::vector<Proxy> &nodes, const std::string &base_conf, std::vector<ruleset_content> &ruleset_content_array, const ProxyGroupConfigs &extra_proxy_group, extra_settings &ext)
std::string proxyToQuanX(std::vector<Proxy> &nodes, const std::string &base_conf, std::vector<RulesetContent> &ruleset_content_array, const ProxyGroupConfigs &extra_proxy_group, extra_settings &ext)
{
INIReader ini;
ini.store_any_line = true;
@@ -1299,7 +1277,7 @@ std::string proxyToQuanX(std::vector<Proxy> &nodes, const std::string &base_conf
return ini.ToString();
}
void proxyToQuanX(std::vector<Proxy> &nodes, INIReader &ini, std::vector<ruleset_content> &ruleset_content_array, const ProxyGroupConfigs &extra_proxy_group, extra_settings &ext)
void proxyToQuanX(std::vector<Proxy> &nodes, INIReader &ini, std::vector<RulesetContent> &ruleset_content_array, const ProxyGroupConfigs &extra_proxy_group, extra_settings &ext)
{
std::string type;
std::string remark, hostname, port, method;
@@ -1661,7 +1639,7 @@ std::string proxyToSSD(std::vector<Proxy> &nodes, std::string &group, std::strin
return "ssd://" + base64Encode(sb.GetString());
}
std::string proxyToMellow(std::vector<Proxy> &nodes, const std::string &base_conf, std::vector<ruleset_content> &ruleset_content_array, const ProxyGroupConfigs &extra_proxy_group, extra_settings &ext)
std::string proxyToMellow(std::vector<Proxy> &nodes, const std::string &base_conf, std::vector<RulesetContent> &ruleset_content_array, const ProxyGroupConfigs &extra_proxy_group, extra_settings &ext)
{
INIReader ini;
ini.store_any_line = true;
@@ -1676,7 +1654,7 @@ std::string proxyToMellow(std::vector<Proxy> &nodes, const std::string &base_con
return ini.ToString();
}
void proxyToMellow(std::vector<Proxy> &nodes, INIReader &ini, std::vector<ruleset_content> &ruleset_content_array, const ProxyGroupConfigs &extra_proxy_group, extra_settings &ext)
void proxyToMellow(std::vector<Proxy> &nodes, INIReader &ini, std::vector<RulesetContent> &ruleset_content_array, const ProxyGroupConfigs &extra_proxy_group, extra_settings &ext)
{
std::string proxy;
std::string type, remark, hostname, port, username, password, method;
@@ -1818,7 +1796,7 @@ void proxyToMellow(std::vector<Proxy> &nodes, INIReader &ini, std::vector<rulese
rulesetToSurge(ini, ruleset_content_array, 0, ext.overwrite_original_rules, std::string());
}
std::string proxyToLoon(std::vector<Proxy> &nodes, const std::string &base_conf, std::vector<ruleset_content> &ruleset_content_array, const ProxyGroupConfigs &extra_proxy_group, extra_settings &ext)
std::string proxyToLoon(std::vector<Proxy> &nodes, const std::string &base_conf, std::vector<RulesetContent> &ruleset_content_array, const ProxyGroupConfigs &extra_proxy_group, extra_settings &ext)
{
rapidjson::Document json;
INIReader ini;

View File

@@ -50,22 +50,22 @@ struct extra_settings
}
};
void rulesetToClash(YAML::Node &base_rule, std::vector<ruleset_content> &ruleset_content_array, bool overwrite_original_rules, bool new_field_name);
void rulesetToSurge(INIReader &base_rule, std::vector<ruleset_content> &ruleset_content_array, int surge_ver, bool overwrite_original_rules, std::string remote_path_prefix);
void rulesetToClash(YAML::Node &base_rule, std::vector<RulesetContent> &ruleset_content_array, bool overwrite_original_rules, bool new_field_name);
void rulesetToSurge(INIReader &base_rule, std::vector<RulesetContent> &ruleset_content_array, int surge_ver, bool overwrite_original_rules, std::string remote_path_prefix);
void preprocessNodes(std::vector<Proxy> &nodes, extra_settings &ext);
std::string proxyToClash(std::vector<Proxy> &nodes, const std::string &base_conf, std::vector<ruleset_content> &ruleset_content_array, const ProxyGroupConfigs &extra_proxy_group, bool clashR, extra_settings &ext);
std::string proxyToClash(std::vector<Proxy> &nodes, const std::string &base_conf, std::vector<RulesetContent> &ruleset_content_array, const ProxyGroupConfigs &extra_proxy_group, bool clashR, extra_settings &ext);
void proxyToClash(std::vector<Proxy> &nodes, YAML::Node &yamlnode, const ProxyGroupConfigs &extra_proxy_group, bool clashR, extra_settings &ext);
std::string proxyToSurge(std::vector<Proxy> &nodes, const std::string &base_conf, std::vector<ruleset_content> &ruleset_content_array, const ProxyGroupConfigs &extra_proxy_group, int surge_ver, extra_settings &ext);
std::string proxyToMellow(std::vector<Proxy> &nodes, const std::string &base_conf, std::vector<ruleset_content> &ruleset_content_array, const ProxyGroupConfigs &extra_proxy_group, extra_settings &ext);
void proxyToMellow(std::vector<Proxy> &nodes, INIReader &ini, std::vector<ruleset_content> &ruleset_content_array, const ProxyGroupConfigs &extra_proxy_group, extra_settings &ext);
std::string proxyToLoon(std::vector<Proxy> &nodes, const std::string &base_conf, std::vector<ruleset_content> &ruleset_content_array, const ProxyGroupConfigs &extra_proxy_group, extra_settings &ext);
std::string proxyToSurge(std::vector<Proxy> &nodes, const std::string &base_conf, std::vector<RulesetContent> &ruleset_content_array, const ProxyGroupConfigs &extra_proxy_group, int surge_ver, extra_settings &ext);
std::string proxyToMellow(std::vector<Proxy> &nodes, const std::string &base_conf, std::vector<RulesetContent> &ruleset_content_array, const ProxyGroupConfigs &extra_proxy_group, extra_settings &ext);
void proxyToMellow(std::vector<Proxy> &nodes, INIReader &ini, std::vector<RulesetContent> &ruleset_content_array, const ProxyGroupConfigs &extra_proxy_group, extra_settings &ext);
std::string proxyToLoon(std::vector<Proxy> &nodes, const std::string &base_conf, std::vector<RulesetContent> &ruleset_content_array, const ProxyGroupConfigs &extra_proxy_group, extra_settings &ext);
std::string proxyToSSSub(std::string base_conf, std::vector<Proxy> &nodes, extra_settings &ext);
std::string proxyToSingle(std::vector<Proxy> &nodes, int types, extra_settings &ext);
std::string proxyToQuanX(std::vector<Proxy> &nodes, const std::string &base_conf, std::vector<ruleset_content> &ruleset_content_array, const ProxyGroupConfigs &extra_proxy_group, extra_settings &ext);
void proxyToQuanX(std::vector<Proxy> &nodes, INIReader &ini, std::vector<ruleset_content> &ruleset_content_array, const ProxyGroupConfigs &extra_proxy_group, extra_settings &ext);
std::string proxyToQuan(std::vector<Proxy> &nodes, const std::string &base_conf, std::vector<ruleset_content> &ruleset_content_array, const ProxyGroupConfigs &extra_proxy_group, extra_settings &ext);
void proxyToQuan(std::vector<Proxy> &nodes, INIReader &ini, std::vector<ruleset_content> &ruleset_content_array, const ProxyGroupConfigs &extra_proxy_group, extra_settings &ext);
std::string proxyToQuanX(std::vector<Proxy> &nodes, const std::string &base_conf, std::vector<RulesetContent> &ruleset_content_array, const ProxyGroupConfigs &extra_proxy_group, extra_settings &ext);
void proxyToQuanX(std::vector<Proxy> &nodes, INIReader &ini, std::vector<RulesetContent> &ruleset_content_array, const ProxyGroupConfigs &extra_proxy_group, extra_settings &ext);
std::string proxyToQuan(std::vector<Proxy> &nodes, const std::string &base_conf, std::vector<RulesetContent> &ruleset_content_array, const ProxyGroupConfigs &extra_proxy_group, extra_settings &ext);
void proxyToQuan(std::vector<Proxy> &nodes, INIReader &ini, std::vector<RulesetContent> &ruleset_content_array, const ProxyGroupConfigs &extra_proxy_group, extra_settings &ext);
std::string proxyToSSD(std::vector<Proxy> &nodes, std::string &group, std::string &userinfo, extra_settings &ext);
#endif // SUBEXPORT_H_INCLUDED

View File

@@ -5,16 +5,15 @@
#include <inja.hpp>
#include <nlohmann/json.hpp>
#include "../../utils/yamlcpp_extra.h"
#include "../../utils/urlencode.h"
#include "../../utils/regexp.h"
#include "../../handler/interfaces.h"
#include "../../handler/settings.h"
#include "../../utils/logger.h"
#include "../../utils/network.h"
#include "../../utils/regexp.h"
#include "../../utils/urlencode.h"
#include "../../utils/yamlcpp_extra.h"
#include "templates.h"
extern std::string gManagedConfigPrefix;
namespace inja
{
void convert_dot_to_json_pointer(nonstd::string_view dot, std::string& out)
@@ -139,7 +138,7 @@ int render_template(const std::string &content, const template_args &vars, std::
});
env.add_callback("getLink", 1, [](inja::Arguments &args)
{
return gManagedConfigPrefix + args.at(0)->get<std::string>();
return global.managedConfigPrefix + args.at(0)->get<std::string>();
});
env.add_callback("startsWith", 2, [](inja::Arguments &args)
{
@@ -265,7 +264,7 @@ std::string findFileName(const std::string &path)
return path.substr(pos + 1, pos2 - pos - 1);
}
int renderClashScript(YAML::Node &base_rule, std::vector<ruleset_content> &ruleset_content_array, std::string remote_path_prefix, bool script, bool overwrite_original_rules, bool clash_classical_ruleset)
int renderClashScript(YAML::Node &base_rule, std::vector<RulesetContent> &ruleset_content_array, std::string remote_path_prefix, bool script, bool overwrite_original_rules, bool clash_classical_ruleset)
{
nlohmann::json data;
std::string match_group, geoips, retrieved_rules;
@@ -281,7 +280,7 @@ int renderClashScript(YAML::Node &base_rule, std::vector<ruleset_content> &rules
if(!overwrite_original_rules && base_rule["rules"].IsDefined())
rules = safe_as<string_array>(base_rule["rules"]);
for(ruleset_content &x : ruleset_content_array)
for(RulesetContent &x : ruleset_content_array)
{
rule_group = x.rule_group;
rule_path = x.rule_path;

View File

@@ -16,6 +16,6 @@ struct template_args
};
int render_template(const std::string &content, const template_args &vars, std::string &output, const std::string &include_scope = "templates");
int renderClashScript(YAML::Node &base_rule, std::vector<ruleset_content> &ruleset_content_array, std::string remote_path_prefix, bool script, bool overwrite_original_rules, bool clash_classic_ruleset);
int renderClashScript(YAML::Node &base_rule, std::vector<RulesetContent> &ruleset_content_array, std::string remote_path_prefix, bool script, bool overwrite_original_rules, bool clash_classic_ruleset);
#endif // TEMPLATES_H_INCLUDED

File diff suppressed because it is too large Load Diff

View File

@@ -9,7 +9,9 @@
#include "../generator/config/subexport.h"
#include "../server/webserver.h"
void refreshRulesets(RulesetConfigs &ruleset_list, std::vector<ruleset_content> &rca);
std::string parseProxy(const std::string &source);
void refreshRulesets(RulesetConfigs &ruleset_list, std::vector<RulesetContent> &rca);
void readConf();
int simpleGenerator();
std::string convertRuleset(const std::string &content, int type);

View File

@@ -1,63 +1,61 @@
#include <future>
#include <thread>
#include "../handler/settings.h"
#include "../utils/network.h"
#include "webget.h"
#include "multithread.h"
//#include "vfs.h"
#include "../utils/network.h"
//safety lock for multi-thread
std::mutex on_emoji, on_rename, on_stream, on_time;
extern RegexMatchConfigs gEmojis, gRenames;
extern RegexMatchConfigs gStreamNodeRules, gTimeNodeRules;
RegexMatchConfigs safe_get_emojis()
{
guarded_mutex guard(on_emoji);
return gEmojis;
return global.emojis;
}
RegexMatchConfigs safe_get_renames()
{
guarded_mutex guard(on_rename);
return gRenames;
return global.renames;
}
RegexMatchConfigs safe_get_streams()
{
guarded_mutex guard(on_stream);
return gStreamNodeRules;
return global.streamNodeRules;
}
RegexMatchConfigs safe_get_times()
{
guarded_mutex guard(on_time);
return gTimeNodeRules;
return global.timeNodeRules;
}
void safe_set_emojis(RegexMatchConfigs data)
{
guarded_mutex guard(on_emoji);
gEmojis.swap(data);
global.emojis.swap(data);
}
void safe_set_renames(RegexMatchConfigs data)
{
guarded_mutex guard(on_rename);
gRenames.swap(data);
global.renames.swap(data);
}
void safe_set_streams(RegexMatchConfigs data)
{
guarded_mutex guard(on_stream);
gStreamNodeRules.swap(data);
global.streamNodeRules.swap(data);
}
void safe_set_times(RegexMatchConfigs data)
{
guarded_mutex guard(on_time);
gTimeNodeRules.swap(data);
global.timeNodeRules.swap(data);
}
std::shared_future<std::string> fetchFileAsync(const std::string &path, const std::string &proxy, int cache_ttl, bool async)

1322
src/handler/settings.cpp Normal file

File diff suppressed because it is too large Load Diff

102
src/handler/settings.h Normal file
View File

@@ -0,0 +1,102 @@
#ifndef SETTINGS_H_INCLUDED
#define SETTINGS_H_INCLUDED
#include <string>
#include "../config/crontask.h"
#include "../config/regmatch.h"
#include "../config/proxygroup.h"
#include "../config/ruleset.h"
#include "../generator/config/ruleconvert.h"
#include "../generator/template/templates.h"
#include "../utils/string.h"
#include "../utils/stl_extra.h"
#include "../utils/tribool.h"
struct Settings
{
//common settings
std::string prefPath = "pref.ini", defaultExtConfig;
string_array excludeRemarks, includeRemarks;
RulesetConfigs customRulesets;
RegexMatchConfigs streamNodeRules, timeNodeRules;
std::vector<RulesetContent> rulesetsContent;
std::string listenAddress = "127.0.0.1", defaultUrls, insertUrls, managedConfigPrefix;
int listenPort = 25500, maxPendingConns = 10, maxConcurThreads = 4;
bool prependInsert = true, skipFailedLinks = false;
bool APIMode = true, writeManagedConfig = false, enableRuleGen = true, updateRulesetOnRequest = false, overwriteOriginalRules = true;
bool printDbgInfo = false, CFWChildProcess = false, appendUserinfo = true, asyncFetchRuleset = false, surgeResolveHostname = true;
std::string accessToken, basePath = "base";
std::string custom_group;
int logLevel;
long maxAllowedDownloadSize;
string_map aliases;
//global variables for template
std::string templatePath = "templates";
string_map templateVars;
//generator settings
bool generatorMode = false;
std::string generateProfiles;
//preferences
RegexMatchConfigs renames, emojis;
bool addEmoji = false, removeEmoji = false, appendType = false, filterDeprecated = true;
tribool UDPFlag, TFOFlag, skipCertVerify, TLS13Flag, enableInsert;
bool enableSort = false, updateStrict = false;
bool clashUseNewField = false;
std::string clashProxiesStyle = "flow";
std::string proxyConfig, proxyRuleset, proxySubscription;
int updateInterval = 0;
std::string sortScript, filterScript;
std::string clashBase;
ProxyGroupConfigs customProxyGroups;
std::string surgeBase, surfboardBase, mellowBase, quanBase, quanXBase, loonBase, SSSubBase;
std::string surgeSSRPath, quanXDevID;
//cache system
bool serveCacheOnFetchFail = false;
int cacheSubscription = 60, cacheConfig = 300, cacheRuleset = 21600;
//limits
size_t maxAllowedRulesets = 64, maxAllowedRules = 32768;
bool scriptCleanContext = false;
//cron system
bool enableCron = false;
CronTaskConfigs cronTasks;
};
struct ExternalConfig
{
ProxyGroupConfigs custom_proxy_group;
RulesetConfigs surge_ruleset;
std::string clash_rule_base;
std::string surge_rule_base;
std::string surfboard_rule_base;
std::string mellow_rule_base;
std::string quan_rule_base;
std::string quanx_rule_base;
std::string loon_rule_base;
std::string sssub_rule_base;
RegexMatchConfigs rename;
RegexMatchConfigs emoji;
string_array include;
string_array exclude;
template_args *tpl_args = nullptr;
bool overwrite_original_rules = false;
bool enable_rule_generator = true;
tribool add_emoji;
tribool remove_old_emoji;
};
extern Settings global;
int importItems(string_array &target, bool scope_limit = true);
int loadExternalConfig(std::string &path, ExternalConfig &ext);
void parseGroupTimes(const std::string &src, int *interval, int *tolerance, int *timeout);
#endif // SETTINGS_H_INCLUDED

View File

@@ -7,6 +7,7 @@
#include <curl/curl.h>
#include "../handler/settings.h"
#include "../utils/base64/base64.h"
#include "../utils/defer.h"
#include "../utils/file_extra.h"
@@ -21,9 +22,6 @@
#endif // _stat
#endif // _WIN32
extern bool gPrintDbgInfo, gServeCacheOnFetchFail;
extern int gLogLevel;
/*
using guarded_mutex = std::lock_guard<std::mutex>;
std::mutex cache_rw_lock;
@@ -150,7 +148,7 @@ static int size_checker(void *clientp, curl_off_t dltotal, curl_off_t dlnow, cur
static inline void curl_set_common_options(CURL *curl_handle, const char *url, curl_progress_data *data)
{
curl_easy_setopt(curl_handle, CURLOPT_URL, url);
curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, gLogLevel == LOG_LEVEL_VERBOSE ? 1L : 0L);
curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, global.logLevel == LOG_LEVEL_VERBOSE ? 1L : 0L);
curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 0L);
curl_easy_setopt(curl_handle, CURLOPT_NOSIGNAL, 1L);
curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1L);
@@ -355,7 +353,7 @@ std::string webGet(const std::string &url, const std::string &proxy, unsigned in
writeLog(0, "CACHE NOT EXIST: '" + url + "', creating new cache.");
//content = curlGet(url, proxy, response_headers, return_code); // try to fetch data
curlGet(argument, fetch_res);
if(return_code == CURLE_OK) // success, save new cache
if(return_code == 200) // success, save new cache
{
//guarded_mutex guard(cache_rw_lock);
cache_rw_lock.writeLock();
@@ -366,7 +364,7 @@ std::string webGet(const std::string &url, const std::string &proxy, unsigned in
}
else
{
if(fileExist(path) && gServeCacheOnFetchFail) // failed, check if cache exist
if(fileExist(path) && global.serveCacheOnFetchFail) // failed, check if cache exist
{
writeLog(0, "Fetch failed. Serving cached content."); // cache exist, serving cache
//guarded_mutex guard(cache_rw_lock);

View File

@@ -9,6 +9,7 @@
#include "config/ruleset.h"
#include "handler/interfaces.h"
#include "handler/webget.h"
#include "handler/settings.h"
#include "script/cron.h"
#include "server/socket.h"
#include "server/webserver.h"
@@ -23,13 +24,6 @@
//#include "vfs.h"
extern std::string gPrefPath, gAccessToken, gListenAddress, gGenerateProfiles, gManagedConfigPrefix;
extern bool gAPIMode, gGeneratorMode, gCFWChildProcess, gUpdateRulesetOnRequest;
extern int gListenPort, gMaxConcurThreads, gMaxPendingConns;
extern RulesetConfigs gCustomRulesets;
extern std::vector<ruleset_content> gRulesetContent;
extern bool gEnableCron;
WebServer webServer;
#ifndef _WIN32
@@ -70,22 +64,22 @@ void chkArg(int argc, char *argv[])
{
if(strcmp(argv[i], "-cfw") == 0)
{
gCFWChildProcess = true;
gUpdateRulesetOnRequest = true;
global.CFWChildProcess = true;
global.updateRulesetOnRequest = true;
}
else if(strcmp(argv[i], "-f") == 0 || strcmp(argv[i], "--file") == 0)
{
if(i < argc - 1)
gPrefPath.assign(argv[++i]);
global.prefPath.assign(argv[++i]);
}
else if(strcmp(argv[i], "-g") == 0 || strcmp(argv[i], "--gen") == 0)
{
gGeneratorMode = true;
global.generatorMode = true;
}
else if(strcmp(argv[i], "--artifact") == 0)
{
if(i < argc - 1)
gGenerateProfiles.assign(argv[++i]);
global.generateProfiles.assign(argv[++i]);
}
else if(strcmp(argv[i], "-l") == 0 || strcmp(argv[i], "--log") == 0)
{
@@ -115,7 +109,7 @@ void signal_handler(int sig)
void cron_tick_caller()
{
if(gEnableCron)
if(global.enableCron)
cron_tick();
}
@@ -126,26 +120,26 @@ int main(int argc, char *argv[])
setcd(prgpath); //first switch to program directory
#endif // _DEBUG
if(fileExist("pref.toml"))
gPrefPath = "pref.toml";
global.prefPath = "pref.toml";
else if(fileExist("pref.yml"))
gPrefPath = "pref.yml";
global.prefPath = "pref.yml";
else if(!fileExist("pref.ini"))
{
if(fileExist("pref.example.toml"))
{
fileCopy("pref.example.toml", "pref.toml");
gPrefPath = "pref.toml";
global.prefPath = "pref.toml";
}
if(fileExist("pref.example.yml"))
else if(fileExist("pref.example.yml"))
{
fileCopy("pref.example.yml", "pref.yml");
gPrefPath = "pref.yml";
global.prefPath = "pref.yml";
}
else if(fileExist("pref.example.ini"))
fileCopy("pref.example.ini", "pref.ini");
}
chkArg(argc, argv);
setcd(gPrefPath); //then switch to pref directory
setcd(global.prefPath); //then switch to pref directory
writeLog(0, "SubConverter " VERSION " starting up..", LOG_LEVEL_INFO);
#ifdef _WIN32
WSADATA wsaData;
@@ -169,17 +163,18 @@ int main(int argc, char *argv[])
SetConsoleTitle("SubConverter " VERSION);
readConf();
if(!gUpdateRulesetOnRequest)
refreshRulesets(gCustomRulesets, gRulesetContent);
//vfs::vfs_read("vfs.ini");
if(!global.updateRulesetOnRequest)
refreshRulesets(global.customRulesets, global.rulesetsContent);
std::string env_api_mode = getEnv("API_MODE"), env_managed_prefix = getEnv("MANAGED_PREFIX"), env_token = getEnv("API_TOKEN");
gAPIMode = tribool().parse(toLower(env_api_mode)).get(gAPIMode);
global.APIMode = tribool().parse(toLower(env_api_mode)).get(global.APIMode);
if(env_managed_prefix.size())
gManagedConfigPrefix = env_managed_prefix;
global.managedConfigPrefix = env_managed_prefix;
if(env_token.size())
gAccessToken = env_token;
global.accessToken = env_token;
if(gGeneratorMode)
if(global.generatorMode)
return simpleGenerator();
/*
@@ -196,42 +191,42 @@ int main(int argc, char *argv[])
webServer.append_response("GET", "/refreshrules", "text/plain", [](RESPONSE_CALLBACK_ARGS) -> std::string
{
if(gAccessToken.size())
if(global.accessToken.size())
{
std::string token = getUrlArg(request.argument, "token");
if(token != gAccessToken)
if(token != global.accessToken)
{
response.status_code = 403;
return "Forbidden\n";
}
}
refreshRulesets(gCustomRulesets, gRulesetContent);
refreshRulesets(global.customRulesets, global.rulesetsContent);
return "done\n";
});
webServer.append_response("GET", "/readconf", "text/plain", [](RESPONSE_CALLBACK_ARGS) -> std::string
{
if(gAccessToken.size())
if(global.accessToken.size())
{
std::string token = getUrlArg(request.argument, "token");
if(token != gAccessToken)
if(token != global.accessToken)
{
response.status_code = 403;
return "Forbidden\n";
}
}
readConf();
if(!gUpdateRulesetOnRequest)
refreshRulesets(gCustomRulesets, gRulesetContent);
if(!global.updateRulesetOnRequest)
refreshRulesets(global.customRulesets, global.rulesetsContent);
return "done\n";
});
webServer.append_response("POST", "/updateconf", "text/plain", [](RESPONSE_CALLBACK_ARGS) -> std::string
{
if(gAccessToken.size())
if(global.accessToken.size())
{
std::string token = getUrlArg(request.argument, "token");
if(token != gAccessToken)
if(token != global.accessToken)
{
response.status_code = 403;
return "Forbidden\n";
@@ -239,9 +234,9 @@ int main(int argc, char *argv[])
}
std::string type = getUrlArg(request.argument, "type");
if(type == "form")
fileWrite(gPrefPath, getFormData(request.postdata), true);
fileWrite(global.prefPath, getFormData(request.postdata), true);
else if(type == "direct")
fileWrite(gPrefPath, request.postdata, true);
fileWrite(global.prefPath, request.postdata, true);
else
{
response.status_code = 501;
@@ -249,14 +244,14 @@ int main(int argc, char *argv[])
}
readConf();
if(!gUpdateRulesetOnRequest)
refreshRulesets(gCustomRulesets, gRulesetContent);
if(!global.updateRulesetOnRequest)
refreshRulesets(global.customRulesets, global.rulesetsContent);
return "done\n";
});
webServer.append_response("GET", "/flushcache", "text/plain", [](RESPONSE_CALLBACK_ARGS) -> std::string
{
if(getUrlArg(request.argument, "token") != gAccessToken)
if(getUrlArg(request.argument, "token") != global.accessToken)
{
response.status_code = 403;
return "Forbidden";
@@ -283,7 +278,7 @@ int main(int argc, char *argv[])
webServer.append_response("GET", "/convert", "text/plain;charset=utf-8", getConvertedRuleset);
if(!gAPIMode)
if(!global.APIMode)
{
webServer.append_response("GET", "/get", "text/plain;charset=utf-8", [](RESPONSE_CALLBACK_ARGS) -> std::string
{
@@ -303,10 +298,10 @@ int main(int argc, char *argv[])
std::string env_port = getEnv("PORT");
if(env_port.size())
gListenPort = to_int(env_port, gListenPort);
listener_args args = {gListenAddress, gListenPort, gMaxPendingConns, gMaxConcurThreads, cron_tick_caller, 200};
global.listenPort = to_int(env_port, global.listenPort);
listener_args args = {global.listenAddress, global.listenPort, global.maxPendingConns, global.maxConcurThreads, cron_tick_caller, 200};
//std::cout<<"Serving HTTP @ http://"<<listen_address<<":"<<listen_port<<std::endl;
writeLog(0, "Startup completed. Serving HTTP @ http://" + gListenAddress + ":" + std::to_string(gListenPort), LOG_LEVEL_INFO);
writeLog(0, "Startup completed. Serving HTTP @ http://" + global.listenAddress + ":" + std::to_string(global.listenPort), LOG_LEVEL_INFO);
webServer.start_web_server_multi(&args);
#ifdef _WIN32

View File

@@ -5,29 +5,15 @@
#include "../config/crontask.h"
#include "../handler/interfaces.h"
#include "../handler/multithread.h"
#include "../handler/settings.h"
#include "../server/webserver.h"
#include "../utils/logger.h"
#include "../utils/rapidjson_extra.h"
#include "../utils/system.h"
#include "script_quickjs.h"
extern bool gEnableCron;
extern CronTaskConfigs gCronTasks;
extern std::string gProxyConfig, gAccessToken;
extern int gCacheConfig;
libcron::Cron cron;
static std::string parseProxy(const std::string &source)
{
std::string proxy = source;
if(source == "SYSTEM")
proxy = getSystemProxy();
else if(source == "NONE")
proxy = "";
return proxy;
}
struct script_info
{
std::string name;
@@ -49,7 +35,7 @@ int timeout_checker(JSRuntime *rt, void *opaque)
void refresh_schedule()
{
cron.clear_schedules();
for(const CronTaskConfig &x : gCronTasks)
for(const CronTaskConfig &x : global.cronTasks)
{
cron.add_schedule(x.Name, x.CronExp, [=](auto &)
{
@@ -60,8 +46,8 @@ void refresh_schedule()
script_runtime_init(runtime);
script_context_init(context);
defer(script_cleanup(context);)
std::string proxy = parseProxy(gProxyConfig);
std::string script = fetchFile(x.Path, proxy, gCacheConfig);
std::string proxy = parseProxy(global.proxyConfig);
std::string script = fetchFile(x.Path, proxy, global.cacheConfig);
if(script.empty())
{
writeLog(0, "Script '" + x.Name + "' run failed: file is empty or not exist!", LOG_LEVEL_WARNING);
@@ -92,7 +78,7 @@ std::string list_cron_schedule(RESPONSE_CALLBACK_ARGS)
rapidjson::StringBuffer sb;
rapidjson::Writer<rapidjson::StringBuffer> writer(sb);
writer.StartObject();
if(token != gAccessToken)
if(token != global.accessToken)
{
response.status_code = 403;
writer.Key("code");
@@ -106,7 +92,7 @@ std::string list_cron_schedule(RESPONSE_CALLBACK_ARGS)
writer.Int(200);
writer.Key("tasks");
writer.StartArray();
for(const CronTaskConfig &x : gCronTasks)
for(const CronTaskConfig &x : global.cronTasks)
{
writer.StartObject();
writer.Key("name");

View File

@@ -9,14 +9,12 @@
#include "../handler/multithread.h"
#include "../handler/webget.h"
#include "../handler/settings.h"
#include "../parser/config/proxy.h"
#include "../utils/map_extra.h"
#include "../utils/system.h"
#include "script_quickjs.h"
extern int gCacheConfig;
extern std::string gProxyConfig;
std::string parseProxy(const std::string &source);
static const std::string qjs_require_module {R"(import * as std from 'std'
@@ -381,7 +379,7 @@ static qjs_fetch_Response qjs_fetch(qjs_fetch_Request request)
std::string getGeoIP(const std::string &address, const std::string &proxy)
{
return fetchFile("https://api.ip.sb/geoip/" + address, parseProxy(proxy), gCacheConfig);
return fetchFile("https://api.ip.sb/geoip/" + address, parseProxy(proxy), global.cacheConfig);
}
void script_runtime_init(qjs::Runtime &runtime)

View File

@@ -5,11 +5,9 @@
#include <sys/types.h>
#include <unistd.h>
#include "../handler/settings.h"
#include "logger.h"
extern bool gPrintDbgInfo;
int gLogLevel = LOG_LEVEL_INFO;
std::string getTime(int type)
{
time_t lt;
@@ -38,7 +36,7 @@ std::string getTime(int type)
void writeLog(int type, const std::string &content, int level)
{
if(level > gLogLevel)
if(level > global.logLevel)
return;
const char *levels[] = {"[FATL]", "[ERRO]", "[WARN]", "[INFO]", "[DEBG]", "[VERB]"};
std::cerr<<getTime(2)<<" ["<<getpid()<<" "<<std::this_thread::get_id()<<"]"<<levels[level % 6];