mirror of
https://github.com/asdlokj1qpi233/subconverter.git
synced 2025-10-27 20:03:01 +00:00
Enhancement
Add /render interface to directly render a template as output content. Add find as a template function. Add filtering out token as a template variable.
This commit is contained in:
@@ -1042,6 +1042,8 @@ std::string subconverter(RESPONSE_CALLBACK_ARGS)
|
||||
pos = x.find("=");
|
||||
if(pos == x.npos)
|
||||
continue;
|
||||
if(x.substr(0, pos) == "token")
|
||||
continue;
|
||||
req_arg_map[x.substr(0, pos)] = x.substr(pos + 1);
|
||||
}
|
||||
|
||||
@@ -2091,3 +2093,45 @@ int simpleGenerator()
|
||||
writeLog(0, "All artifact generated. Exiting...", LOG_LEVEL_INFO);
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::string renderTemplate(RESPONSE_CALLBACK_ARGS)
|
||||
{
|
||||
std::string path = UrlDecode(getUrlArg(argument, "path"));
|
||||
|
||||
if(path.find(template_path) != 0)
|
||||
{
|
||||
*status_code = 403;
|
||||
return "Out of scope";
|
||||
}
|
||||
if(!fileExist(path))
|
||||
{
|
||||
*status_code = 404;
|
||||
return "Not found";
|
||||
}
|
||||
std::string template_content = fetchFile(path, parseProxy(proxy_config), cache_config);
|
||||
if(template_content.empty())
|
||||
{
|
||||
*status_code = 400;
|
||||
return "File empty or out of scope";
|
||||
}
|
||||
template_args tpl_args;
|
||||
tpl_args.global_vars = global_vars;
|
||||
|
||||
//load request arguments as template variables
|
||||
string_array req_args = split(argument, "&");
|
||||
string_size pos;
|
||||
string_map req_arg_map;
|
||||
for(std::string &x : req_args)
|
||||
{
|
||||
pos = x.find("=");
|
||||
if(pos == x.npos)
|
||||
continue;
|
||||
req_arg_map[x.substr(0, pos)] = x.substr(pos + 1);
|
||||
}
|
||||
tpl_args.request_params = req_arg_map;
|
||||
|
||||
std::string output_content;
|
||||
if(render_template(template_content, tpl_args, output_content, template_path) != 0)
|
||||
*status_code = 400;
|
||||
return output_content;
|
||||
}
|
||||
|
||||
@@ -22,6 +22,8 @@ std::string subconverter(RESPONSE_CALLBACK_ARGS);
|
||||
std::string simpleToClashR(RESPONSE_CALLBACK_ARGS);
|
||||
std::string surgeConfToClash(RESPONSE_CALLBACK_ARGS);
|
||||
|
||||
std::string renderTemplate(RESPONSE_CALLBACK_ARGS);
|
||||
|
||||
std::string template_webGet(inja::Arguments &args);
|
||||
std::string jinja2_webGet(const std::string &url);
|
||||
std::string parseHostname(inja::Arguments &args);
|
||||
|
||||
@@ -283,6 +283,8 @@ int main(int argc, char *argv[])
|
||||
return subconverter(argument + "&target=trojan", postdata, status_code, extra_headers);
|
||||
});
|
||||
|
||||
append_response("GET", "/render", "text/plain", renderTemplate);
|
||||
|
||||
if(!api_mode)
|
||||
{
|
||||
append_response("GET", "/get", "text/plain;charset=utf-8", [](RESPONSE_CALLBACK_ARGS) -> std::string
|
||||
|
||||
@@ -50,6 +50,11 @@ int render_template(const std::string &content, const template_args &vars, std::
|
||||
std::string data = args.at(0)->get<std::string>();
|
||||
return trim(data);
|
||||
});
|
||||
m_callbacks.add_callback("find", 1, [](inja::Arguments &args)
|
||||
{
|
||||
std::string src = args.at(0)->get<std::string>(), target = args.at(1)->get<std::string>();
|
||||
return regFind(src, target);
|
||||
});
|
||||
m_callbacks.add_callback("replace", 3, [](inja::Arguments &args)
|
||||
{
|
||||
std::string src = args.at(0)->get<std::string>(), target = args.at(1)->get<std::string>(), rep = args.at(2)->get<std::string>();
|
||||
|
||||
Reference in New Issue
Block a user