mirror of
https://github.com/asdlokj1qpi233/subconverter.git
synced 2026-11-04 10:42:04 +00:00
Optimize codes
This commit is contained in:
2 files changed
+46
-18
No files matched your search
+44
-16
@@ -20,6 +20,8 @@
|
|||||||
#include "templates.h"
|
#include "templates.h"
|
||||||
#include "upload.h"
|
#include "upload.h"
|
||||||
|
|
||||||
|
#define MAX_EXTCONF_RULESET_COUNT 30
|
||||||
|
|
||||||
//common settings
|
//common settings
|
||||||
std::string pref_path = "pref.ini", def_ext_config;
|
std::string pref_path = "pref.ini", def_ext_config;
|
||||||
string_array def_exclude_remarks, def_include_remarks, rulesets, stream_rules, time_rules;
|
string_array def_exclude_remarks, def_include_remarks, rulesets, stream_rules, time_rules;
|
||||||
@@ -358,15 +360,23 @@ void readGroup(YAML::Node node, string_array &dest, bool scope_limit = true)
|
|||||||
object["timeout"] >> timeout;
|
object["timeout"] >> timeout;
|
||||||
for(j = 0; j < object["rule"].size(); j++)
|
for(j = 0; j < object["rule"].size(); j++)
|
||||||
tempArray.emplace_back(safe_as<std::string>(object["rule"][j]));
|
tempArray.emplace_back(safe_as<std::string>(object["rule"][j]));
|
||||||
if(type != "select" && type != "ssid")
|
switch(hash_(type))
|
||||||
{
|
{
|
||||||
|
case "select"_hash:
|
||||||
|
if(tempArray.size() < 3)
|
||||||
|
continue;
|
||||||
|
break;
|
||||||
|
case "ssid"_hash:
|
||||||
|
if(tempArray.size() < 4)
|
||||||
|
continue;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
if(tempArray.size() < 3)
|
||||||
|
continue;
|
||||||
tempArray.emplace_back(url);
|
tempArray.emplace_back(url);
|
||||||
tempArray.emplace_back(interval + "," + timeout + "," + tolerance);
|
tempArray.emplace_back(interval + "," + timeout + "," + tolerance);
|
||||||
}
|
}
|
||||||
|
|
||||||
if((type == "select" && tempArray.size() < 3) || (type == "ssid" && tempArray.size() < 4) || (type != "select" && type != "ssid" && tempArray.size() < 5))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
strLine = std::accumulate(std::next(tempArray.begin()), tempArray.end(), tempArray[0], [](std::string a, std::string b) -> std::string
|
strLine = std::accumulate(std::next(tempArray.begin()), tempArray.end(), tempArray[0], [](std::string a, std::string b) -> std::string
|
||||||
{
|
{
|
||||||
return std::move(a) + "`" + std::move(b);
|
return std::move(a) + "`" + std::move(b);
|
||||||
@@ -972,7 +982,15 @@ int loadExternalYAML(YAML::Node &node, ExternalConfig &ext)
|
|||||||
readGroup(section["custom_proxy_group"], ext.custom_proxy_group, api_mode);
|
readGroup(section["custom_proxy_group"], ext.custom_proxy_group, api_mode);
|
||||||
|
|
||||||
if(section["surge_ruleset"].size())
|
if(section["surge_ruleset"].size())
|
||||||
|
{
|
||||||
readRuleset(section["surge_ruleset"], ext.surge_ruleset, api_mode);
|
readRuleset(section["surge_ruleset"], ext.surge_ruleset, api_mode);
|
||||||
|
if(ext.surge_ruleset.size() > MAX_EXTCONF_RULESET_COUNT)
|
||||||
|
{
|
||||||
|
writeLog(0, "Ruleset count in external config has exceeded limit.", LOG_LEVEL_WARNING);
|
||||||
|
eraseElements(ext.surge_ruleset);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if(section["rename_node"].size())
|
if(section["rename_node"].size())
|
||||||
readRegexMatch(section["rename_node"], "@", ext.rename, api_mode);
|
readRegexMatch(section["rename_node"], "@", ext.rename, api_mode);
|
||||||
@@ -1031,6 +1049,12 @@ int loadExternalConfig(std::string &path, ExternalConfig &ext)
|
|||||||
{
|
{
|
||||||
ini.GetAll("surge_ruleset", ext.surge_ruleset);
|
ini.GetAll("surge_ruleset", ext.surge_ruleset);
|
||||||
importItems(ext.surge_ruleset, api_mode);
|
importItems(ext.surge_ruleset, api_mode);
|
||||||
|
if(ext.surge_ruleset.size() > MAX_EXTCONF_RULESET_COUNT)
|
||||||
|
{
|
||||||
|
writeLog(0, "Ruleset count in external config has exceeded limit. ", LOG_LEVEL_WARNING);
|
||||||
|
eraseElements(ext.surge_ruleset);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ini.GetIfExist("clash_rule_base", ext.clash_rule_base);
|
ini.GetIfExist("clash_rule_base", ext.clash_rule_base);
|
||||||
@@ -1206,7 +1230,7 @@ std::string subconverter(RESPONSE_CALLBACK_ARGS)
|
|||||||
ext.remove_emoji = remove_old_emoji;
|
ext.remove_emoji = remove_old_emoji;
|
||||||
}
|
}
|
||||||
ext.append_proxy_type = append_type.get(append_proxy_type);
|
ext.append_proxy_type = append_type.get(append_proxy_type);
|
||||||
if(target == "clash" || target == "clashr")
|
if((target == "clash" || target == "clashr") && clash_script.is_undef())
|
||||||
expand.define(true);
|
expand.define(true);
|
||||||
|
|
||||||
/// read preference from argument
|
/// read preference from argument
|
||||||
@@ -1248,7 +1272,8 @@ std::string subconverter(RESPONSE_CALLBACK_ARGS)
|
|||||||
//then load external configuration
|
//then load external configuration
|
||||||
ExternalConfig extconf;
|
ExternalConfig extconf;
|
||||||
extconf.tpl_args = &tpl_args;
|
extconf.tpl_args = &tpl_args;
|
||||||
loadExternalConfig(config, extconf);
|
if(loadExternalConfig(config, extconf) == 0)
|
||||||
|
{
|
||||||
if(!ext.nodelist)
|
if(!ext.nodelist)
|
||||||
{
|
{
|
||||||
checkExternalBase(extconf.clash_rule_base, ext_clash_base);
|
checkExternalBase(extconf.clash_rule_base, ext_clash_base);
|
||||||
@@ -1270,6 +1295,11 @@ std::string subconverter(RESPONSE_CALLBACK_ARGS)
|
|||||||
extra_group = extconf.custom_proxy_group;
|
extra_group = extconf.custom_proxy_group;
|
||||||
//load custom rules
|
//load custom rules
|
||||||
ext.overwrite_original_rules = extconf.overwrite_original_rules;
|
ext.overwrite_original_rules = extconf.overwrite_original_rules;
|
||||||
|
if(extconf.include.size())
|
||||||
|
include_remarks = extconf.include;
|
||||||
|
if(extconf.exclude.size())
|
||||||
|
exclude_remarks = extconf.exclude;
|
||||||
|
}
|
||||||
if(extconf.surge_ruleset.size() && !ext.nodelist)
|
if(extconf.surge_ruleset.size() && !ext.nodelist)
|
||||||
{
|
{
|
||||||
extra_ruleset = extconf.surge_ruleset;
|
extra_ruleset = extconf.surge_ruleset;
|
||||||
@@ -1285,10 +1315,6 @@ std::string subconverter(RESPONSE_CALLBACK_ARGS)
|
|||||||
rca = ruleset_content_array;
|
rca = ruleset_content_array;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(extconf.include.size())
|
|
||||||
include_remarks = extconf.include;
|
|
||||||
if(extconf.exclude.size())
|
|
||||||
exclude_remarks = extconf.exclude;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -1855,20 +1881,22 @@ std::string surgeConfToClash(RESPONSE_CALLBACK_ARGS)
|
|||||||
if(!dummy_str_array.size())
|
if(!dummy_str_array.size())
|
||||||
continue;
|
continue;
|
||||||
content = trim(dummy_str_array[0]);
|
content = trim(dummy_str_array[0]);
|
||||||
if(content == "direct")
|
switch(hash_(content))
|
||||||
{
|
{
|
||||||
|
case "direct"_hash:
|
||||||
singlegroup["name"] = name;
|
singlegroup["name"] = name;
|
||||||
singlegroup["type"] = "select";
|
singlegroup["type"] = "select";
|
||||||
singlegroup["proxies"].push_back("DIRECT");
|
singlegroup["proxies"].push_back("DIRECT");
|
||||||
}
|
break;
|
||||||
else if(content == "reject")
|
case "reject"_hash:
|
||||||
{
|
case "reject-tinygif"_hash:
|
||||||
singlegroup["name"] = name;
|
singlegroup["name"] = name;
|
||||||
singlegroup["type"] = "select";
|
singlegroup["type"] = "select";
|
||||||
singlegroup["proxies"].push_back("REJECT");
|
singlegroup["proxies"].push_back("REJECT");
|
||||||
}
|
break;
|
||||||
else
|
default:
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
clash[proxygroup_name].push_back(singlegroup);
|
clash[proxygroup_name].push_back(singlegroup);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+2
-2
@@ -47,7 +47,7 @@ static int writer(char *data, size_t size, size_t nmemb, std::string *writerData
|
|||||||
|
|
||||||
static int size_checker(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow)
|
static int size_checker(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow)
|
||||||
{
|
{
|
||||||
if(dltotal > 1048576.0)
|
if(dltotal > 524288.0)
|
||||||
return 1;
|
return 1;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -63,7 +63,7 @@ static inline void curl_set_common_options(CURL *curl_handle, const char *url)
|
|||||||
curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYHOST, 0L);
|
curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYHOST, 0L);
|
||||||
curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT, 15L);
|
curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT, 15L);
|
||||||
curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, user_agent_str.data());
|
curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, user_agent_str.data());
|
||||||
curl_easy_setopt(curl_handle, CURLOPT_MAXFILESIZE, 1048576L);
|
curl_easy_setopt(curl_handle, CURLOPT_MAXFILESIZE, 524288L);
|
||||||
curl_easy_setopt(curl_handle, CURLOPT_PROGRESSFUNCTION, size_checker);
|
curl_easy_setopt(curl_handle, CURLOPT_PROGRESSFUNCTION, size_checker);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user