Enhancements

Add loading external custom configuration from URL.
Optimize codes.
This commit is contained in:
Tindy X 2020-01-01 14:30:41 +08:00
parent 09c14e0222
commit 9196387fe6
No known key found for this signature in database
GPG Key ID: C6AD413169968D58
2 changed files with 79 additions and 30 deletions

View File

@ -253,6 +253,38 @@ void readConf()
std::cerr<<"Read preference settings completed."<<std::endl;
}
int loadExternalConfig(std::string &path, string_array &custom_proxy_group, string_array &surge_ruleset, std::string proxy)
{
std::string base_content;
if(fileExist(path))
base_content = fileGet(path, false);
else
base_content = webGet(path, proxy);
INIReader ini;
ini.store_isolated_line = true;
ini.SetIsolatedItemsSection("custom");
if(ini.Parse(base_content) != INIREADER_EXCEPTION_NONE)
{
std::cerr<<"Load external configuration failed. Reason: "<<ini.GetLastError()<<"\n";
return -1;
}
ini.EnterSection("custom");
if(ini.ItemPrefixExist("custom_proxy_group"))
{
eraseElements(custom_proxy_group);
ini.GetAll("custom_proxy_group", custom_proxy_group);
}
if(ini.ItemPrefixExist("surge_ruleset"))
{
eraseElements(surge_ruleset);
ini.GetAll("surge_ruleset", surge_ruleset);
}
return 0;
}
void generateBase()
{
std::string base_content;
@ -291,7 +323,7 @@ std::string subconverter(RESPONSE_CALLBACK_ARGS)
std::string include = UrlDecode(getUrlArg(argument, "include")), exclude = UrlDecode(getUrlArg(argument, "exclude")), sort_flag = getUrlArg(argument, "sort");
std::string base_content, output_content;
string_array extra_group, extra_ruleset, include_remarks, exclude_remarks;
std::string groups = urlsafe_base64_decode(getUrlArg(argument, "groups")), ruleset = urlsafe_base64_decode(getUrlArg(argument, "ruleset"));
std::string groups = urlsafe_base64_decode(getUrlArg(argument, "groups")), ruleset = urlsafe_base64_decode(getUrlArg(argument, "ruleset")), config = UrlDecode(getUrlArg(argument, "config"));
std::vector<ruleset_content> rca;
if(!url.size())
@ -301,6 +333,24 @@ std::string subconverter(RESPONSE_CALLBACK_ARGS)
if(!api_mode || cfw_child_process)
readConf();
std::string proxy;
if(proxy_subscription == "SYSTEM")
proxy = getSystemProxy();
else if(proxy_subscription == "NONE")
proxy = "";
else
proxy = proxy_subscription;
if(config.size())
{
std::cerr<<"External configuration file provided. Loading...\n";
extra_group = clash_extra_group;
extra_ruleset = rulesets;
loadExternalConfig(config, extra_group, extra_ruleset, proxy);
refreshRulesets(extra_ruleset, rca);
}
else
{
if(groups.size())
{
extra_group = split(groups, "@");
@ -330,6 +380,7 @@ std::string subconverter(RESPONSE_CALLBACK_ARGS)
refreshRulesets(rulesets, ruleset_content_array);
rca = ruleset_content_array;
}
}
extra_settings ext;
if(emoji.size())
@ -346,14 +397,6 @@ std::string subconverter(RESPONSE_CALLBACK_ARGS)
else
ext.append_proxy_type = append_proxy_type;
std::string proxy;
if(proxy_subscription == "SYSTEM")
proxy = getSystemProxy();
else if(proxy_subscription == "NONE")
proxy = "";
else
proxy = proxy_subscription;
ext.tfo = tfo.size() ? tfo == "true" : tfo_flag;
ext.udp = udp.size() ? udp == "true" : udp_flag;
ext.sort_flag = sort_flag.size() ? sort_flag == "true" : do_sort;
@ -390,7 +433,7 @@ std::string subconverter(RESPONSE_CALLBACK_ARGS)
if(target == "clash" || target == "clashr")
{
std::cerr<<"Clash"<<((target == "clashr") ? "R" : "")<<std::endl;
if(ruleset.size() || groups.size() || update_ruleset_on_request)
if(rca.size() || extra_group.size() || update_ruleset_on_request)
{
if(fileExist(clash_rule_base))
base_content = fileGet(clash_rule_base, false);
@ -443,7 +486,7 @@ std::string subconverter(RESPONSE_CALLBACK_ARGS)
else if(target == "mellow")
{
std::cerr<<"Mellow"<<std::endl;
if(ruleset.size() || groups.size() || update_ruleset_on_request)
if(rca.size() || extra_group.size() || update_ruleset_on_request)
{
if(fileExist(mellow_rule_base))
base_content = fileGet(mellow_rule_base, false);

View File

@ -67,6 +67,12 @@ static inline bool strFind(std::string str, std::string target)
return str.find(target) != str.npos;
}
template <typename T> static inline void eraseElements(std::vector<T> &target)
{
target.clear();
target.shrink_to_fit();
}
template <typename T> static inline void eraseElements(T &target)
{
T().swap(target);