Fix example preference file.
Fix error when template include path is not set.
Fix crash when adding SSID group to Loon configurations.
Clean up codes.
This commit is contained in:
Tindy X 2021-10-04 23:48:51 +08:00
parent 94696a5da5
commit a653e3a874
No known key found for this signature in database
GPG Key ID: C6AD413169968D58
8 changed files with 44 additions and 59 deletions

View File

@ -215,7 +215,7 @@ custom_proxy_group=!!import:snippets/groups.txt
[template]
;The file scope limit of 'include' statement inside the templates.
template_path=templates
template_path=
;The following settings will be added to the "global" scope of the template variables
;Value of 'clash.http_port' can be accessed with 'global.clash.http_port' in the template.

View File

@ -216,7 +216,7 @@ update_ruleset_on_request = false
import = "snippets/rulesets.toml"
[template]
template_path = "templates"
template_path = ""
[[template.globals]]
key = "clash.http_port"

View File

@ -99,7 +99,7 @@ proxy_groups:
- {import: snippets/groups.txt}
template:
template_path: "templates"
template_path: ""
globals:
- {key: clash.http_port, value: 7890}
- {key: clash.socks_port, value: 7891}

View File

@ -758,10 +758,7 @@ std::string proxyToSurge(std::vector<Proxy> &nodes, const std::string &base_conf
args.emplace_back("-G");
args.emplace_back(std::move(protoparam));
}
proxy += std::accumulate(std::next(args.begin()), args.end(), args[0], [](std::string a, std::string b)
{
return std::move(a) + "\", args=\"" + std::move(b);
});
proxy += join(args, "\", args=\"");
proxy += "\", local-port=" + std::to_string(local_port);
if(isIPv4(hostname) || isIPv6(hostname))
proxy += ", addresses=" + hostname;
@ -843,10 +840,7 @@ std::string proxyToSurge(std::vector<Proxy> &nodes, const std::string &base_conf
[[fallthrough]];
case ProxyGroupType::SSID:
proxy = x.TypeStr() + ",default=" + x.Proxies[0] + ",";
proxy += std::accumulate(x.Proxies.begin() + 2, x.Proxies.end(), x.Proxies[1], [](std::string a, std::string b)
{
return std::move(a) + "," + std::move(b);
});
proxy += join(x.Proxies.begin() + 1, x.Proxies.end(), ",");
ini.Set("{NONAME}", x.Name + " = " + proxy); //insert order
continue;
default:
@ -873,10 +867,7 @@ std::string proxyToSurge(std::vector<Proxy> &nodes, const std::string &base_conf
}
proxy = x.TypeStr() + ",";
proxy += std::accumulate(std::next(filtered_nodelist.cbegin()), filtered_nodelist.cend(), filtered_nodelist[0], [](std::string a, std::string b)
{
return std::move(a) + "," + std::move(b);
});
proxy += join(filtered_nodelist, ",");
if(x.Type == ProxyGroupType::URLTest || x.Type == ProxyGroupType::Fallback)
{
proxy += ",url=" + x.Url + ",interval=" + std::to_string(x.Interval);
@ -1068,10 +1059,7 @@ std::string proxyToQuan(std::vector<Proxy> &nodes, const std::string &base_conf,
std::string allLinks;
ini.GetAll("SERVER", "{NONAME}", allnodes);
if(allnodes.size())
allLinks = std::accumulate(std::next(allnodes.begin()), allnodes.end(), allnodes[0], [](std::string a, std::string b)
{
return std::move(a) + "\n" + std::move(b);
});
allLinks = join(allnodes, "\n");
return base64Encode(allLinks);
}
return ini.ToString();
@ -1267,10 +1255,7 @@ void proxyToQuan(std::vector<Proxy> &nodes, INIReader &ini, std::vector<ruleset_
if(filtered_nodelist.size() < 2) // force groups with 1 node to be static
type = "static";
proxies = std::accumulate(std::next(filtered_nodelist.begin()), filtered_nodelist.end(), filtered_nodelist[0], [](std::string a, std::string b)
{
return std::move(a) + "\n" + std::move(b);
});
proxies = join(filtered_nodelist, "\n");
singlegroup = x.Name + " : " + type;
if(type == "static")
@ -1308,10 +1293,7 @@ std::string proxyToQuanX(std::vector<Proxy> &nodes, const std::string &base_conf
std::string allLinks;
ini.GetAll("server_local", "{NONAME}", allnodes);
if(allnodes.size())
allLinks = std::accumulate(std::next(allnodes.begin()), allnodes.end(), allnodes[0], [](std::string a, std::string b)
{
return std::move(a) + "\n" + std::move(b);
});
allLinks = join(allnodes, "\n");
return allLinks;
}
return ini.ToString();
@ -1514,10 +1496,7 @@ void proxyToQuanX(std::vector<Proxy> &nodes, INIReader &ini, std::vector<ruleset
}
}
proxies = std::accumulate(std::next(filtered_nodelist.begin()), filtered_nodelist.end(), filtered_nodelist[0], [](std::string a, std::string b)
{
return std::move(a) + ", " + std::move(b);
});
proxies = join(filtered_nodelist, ", ");
singlegroup = type + "=" + x.Name + ", " + proxies;
ini.Set("{NONAME}", singlegroup);
@ -1829,10 +1808,7 @@ void proxyToMellow(std::vector<Proxy> &nodes, INIReader &ini, std::vector<rulese
proxy += y + ":";
proxy = proxy.substr(0, proxy.size() - 1);
*/
proxy += std::accumulate(std::next(filtered_nodelist.begin()), filtered_nodelist.end(), filtered_nodelist[0], [](std::string a, std::string b)
{
return std::move(a) + ":" + std::move(b);
});
proxy += join(filtered_nodelist, ":");
proxy += ", latency, interval=300, timeout=6"; //use hard-coded values for now
ini.Set("{NONAME}", proxy); //insert order
@ -1977,10 +1953,7 @@ std::string proxyToLoon(std::vector<Proxy> &nodes, const std::string &base_conf,
if(x.Proxies.size() < 2)
continue;
proxy = x.TypeStr() + ",default=" + x.Proxies[0] + ",";
proxy += std::accumulate(x.Proxies.begin() + 2, x.Proxies.end(), vArray[1], [](std::string a, std::string b)
{
return std::move(a) + "," + std::move(b);
});
proxy += join(x.Proxies.begin() + 1, x.Proxies.end(), ",");
ini.Set("{NONAME}", x.Name + " = " + proxy); //insert order
continue;
default:
@ -1998,10 +1971,7 @@ std::string proxyToLoon(std::vector<Proxy> &nodes, const std::string &base_conf,
for(std::string &y : filtered_nodelist)
proxy += "," + y;
*/
proxy += std::accumulate(std::next(filtered_nodelist.cbegin()), filtered_nodelist.cend(), filtered_nodelist[0], [](std::string a, std::string b)
{
return std::move(a) + "," + std::move(b);
});
proxy += join(filtered_nodelist, ",");
if(x.Type == ProxyGroupType::URLTest || x.Type == ProxyGroupType::Fallback)
proxy += ",url=" + x.Url + ",interval=" + std::to_string(x.Interval);

View File

@ -42,7 +42,8 @@ int render_template(const std::string &content, const template_args &vars, std::
std::string absolute_scope;
try
{
absolute_scope = std::filesystem::canonical(include_scope).string();
if(!include_scope.empty())
absolute_scope = std::filesystem::canonical(include_scope).string();
}
catch(std::exception &e)
{
@ -193,7 +194,7 @@ int render_template(const std::string &content, const template_args &vars, std::
{
throw inja::FileError(e.what());
}
if(!include_scope.empty() && !startsWith(absolute_path, absolute_scope))
if(!absolute_scope.empty() && !startsWith(absolute_path, absolute_scope))
throw inja::FileError("access denied when trying to include '" + template_name + "': out of scope");
return env.parse(fileGet(template_name, true));
});

View File

@ -924,15 +924,6 @@ void find_if_exist(const toml::value &v, const toml::key &k, T& target, U&&... a
if constexpr (sizeof...(args) > 0) find_if_exist(v, std::forward<U>(args)...);
}
std::string join(const string_array &arr, const std::string &delimiter = "|")
{
if(arr.size() == 0)
return "";
if(arr.size() == 1)
return arr[0];
return std::accumulate(arr.begin() + 1, arr.end(), arr[0], [&](const std::string &a, const std::string &b) { return a + delimiter + b; });
}
void operate_toml_kv_table(const std::vector<toml::table> &arr, const toml::key &key_name, const toml::key &value_name, std::function<void (const toml::value&, const toml::value&)> binary_op)
{
for(const toml::table &table : arr)
@ -948,8 +939,8 @@ void readTOMLConf(toml::value &root)
string_array default_url, insert_url;
find_if_exist(section_common, "default_url", default_url, "insert_url", insert_url);
gDefaultUrls = join(default_url);
gInsertUrls = join(insert_url);
gDefaultUrls = join(default_url, "|");
gInsertUrls = join(insert_url, "|");
bool filter = false;
find_if_exist(section_common,

View File

@ -1,7 +1,8 @@
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <sstream>
#include <string>
#include <vector>
#include <stdlib.h>
#include <time.h>
@ -394,3 +395,12 @@ int to_int(const std::string &str, int def_value)
*/
return std::atoi(str.data());
}
std::string join(const string_array &arr, const std::string &delimiter)
{
if(arr.size() == 0)
return "";
if(arr.size() == 1)
return arr[0];
return std::accumulate(arr.begin() + 1, arr.end(), arr[0], [&](const std::string &a, const std::string &b) {return a + delimiter + b; });
}

View File

@ -1,6 +1,7 @@
#ifndef STRING_H_INCLUDED
#define STRING_H_INCLUDED
#include <numeric>
#include <string>
#include <sstream>
#include <vector>
@ -12,6 +13,18 @@ using string_map = std::map<std::string, std::string>;
using string_pair_array = std::vector<std::pair<std::string, std::string>>;
std::vector<std::string> split(const std::string &s, const std::string &seperator);
std::string join(const string_array &arr, const std::string &delimiter);
template <typename InputIt>
std::string join(InputIt first, InputIt last, const std::string &delimiter)
{
if(first == last)
return "";
if(std::next(first) == last)
return *first;
return std::accumulate(std::next(first), last, *first, [&](const std::string &a, const std::string &b) {return a + delimiter + b; });
}
std::string getUrlArg(const std::string &url, const std::string &request);
std::string replaceAllDistinct(std::string str, const std::string &old_value, const std::string &new_value);
std::string trimOf(const std::string& str, char target, bool before = true, bool after = true);