Enhancements

Fix exporting useless info in Clash HTTP/SOCKS 5 nodes.
Add support for exporting Clash classical rule-provider.
Optimize codes.
Update build script.
This commit is contained in:
Tindy X committed 2020-06-08 19:56:06 +08:00
1 parent d0e4bed68c
commit 64b5d3bdaa
6 files changed
+159 -95

No files matched your search

+45 -19
View File
@@ -11,6 +11,13 @@
extern std::string managed_config_prefix;
template <typename T> T safe_as (const YAML::Node& node)
{
if(node.IsDefined() && !node.IsNull())
return node.as<T>();
return T();
};
static inline void parse_json_pointer(nlohmann::json &json, const std::string &path, const std::string &value)
{
std::string pointer = "/" + replace_all_distinct(path, ".", "/");
@@ -177,12 +184,14 @@ const std::string clash_script_template = R"(def main(ctx, md):
return geoips[key]
return "{{ match_group }}")";
const std::string clash_script_group_template = R"({% if rule.has_domain == "true" %} if ctx.rule_providers["{{ rule.name }}_domain"].match(md):
const std::string clash_script_group_template = R"({% if rule.has_domain == "false" and rule.has_ipcidr == "false" %} if ctx.rule_providers["{{ rule.name }}_classical"].match(md):
ctx.log('[Script] matched {{ rule.group }} rule')
return "{{ rule.group }}"{% else %}{% if rule.has_domain == "true" %} if ctx.rule_providers["{{ rule.name }}_domain"].match(md):
ctx.log('[Script] matched {{ rule.group }} DOMAIN rule')
return "{{ rule.group }}"{% endif %}
{% if rule.has_ipcidr == "true" %} if ctx.rule_providers["{{ rule.name }}_ipcidr"].match(md):
ctx.log('[Script] matched {{ rule.group }} IP rule')
return "{{ rule.group }}"{% endif %})";
return "{{ rule.group }}"{% endif %}{% endif %})";
const std::string clash_script_keyword_template = R"( keywords = [{{ rule.keyword }}]
for keyword in keywords:
@@ -190,7 +199,7 @@ const std::string clash_script_keyword_template = R"( keywords = [{{ rule.keywo
ctx.log('[Script] matched {{ rule.group }} DOMAIN-KEYWORD rule')
return "{{ rule.group }}")";
int renderClashScript(YAML::Node &base_rule, std::vector<ruleset_content> &ruleset_content_array, std::string remote_path_prefix, bool script, bool overwrite_original_rules)
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)
{
nlohmann::json data;
std::string match_group, geoips, retrieved_rules;
@@ -199,10 +208,10 @@ int renderClashScript(YAML::Node &base_rule, std::vector<ruleset_content> &rules
string_array vArray, groups;
string_map keywords, urls, names;
std::map<std::string, bool> has_domain, has_ipcidr;
YAML::Node rules;
string_array rules;
if(!overwrite_original_rules && base_rule["rules"].IsDefined())
rules = base_rule["rules"];
rules = safe_as<string_array>(base_rule["rules"]);
for(ruleset_content &x : ruleset_content_array)
{
@@ -229,7 +238,7 @@ int renderClashScript(YAML::Node &base_rule, std::vector<ruleset_content> &rules
strLine += "," + rule_group;
if(std::count(strLine.begin(), strLine.end(), ',') > 2)
strLine = regReplace(strLine, "^(.*?,.*?)(,.*)(,.*)$", "$1$3$2");
rules.push_back(strLine);
rules.emplace_back(strLine);
continue;
}
else
@@ -241,6 +250,13 @@ int renderClashScript(YAML::Node &base_rule, std::vector<ruleset_content> &rules
rule_name = std::to_string(hash_(rule_path));
names[rule_name] = rule_group;
urls[rule_name] = rule_path;
if(clash_classical_ruleset)
{
groups.emplace_back(rule_name);
if(!script)
rules.emplace_back("RULE-SET," + rule_group + "_" + rule_name + "_classical," + rule_group);
continue;
}
}
else
continue;
@@ -286,7 +302,7 @@ int renderClashScript(YAML::Node &base_rule, std::vector<ruleset_content> &rules
strLine += "," + rule_group;
if(std::count(strLine.begin(), strLine.end(), ',') > 2)
strLine = regReplace(strLine, "^(.*?,.*?)(,.*)(,.*)$", "$1$3$2");
rules.push_back(strLine);
rules.emplace_back(strLine);
}
}
else if(startsWith(strLine, "DOMAIN,") || startsWith(strLine, "DOMAIN-SUFFIX,"))
@@ -295,9 +311,9 @@ int renderClashScript(YAML::Node &base_rule, std::vector<ruleset_content> &rules
has_ipcidr[rule_name] = true;
}
if(has_domain[rule_name] && !script)
rules.push_back("RULE-SET," + rule_group + "_" + rule_name + "_domain," + rule_group);
rules.emplace_back("RULE-SET," + rule_group + "_" + rule_name + "_domain," + rule_group);
if(has_ipcidr[rule_name] && !script)
rules.push_back("RULE-SET," + rule_group + "_" + rule_name + "_ipcidr," + rule_group);
rules.emplace_back("RULE-SET," + rule_group + "_" + rule_name + "_ipcidr," + rule_group);
if(std::find(groups.begin(), groups.end(), rule_name) == groups.end())
groups.emplace_back(rule_name);
}
@@ -308,19 +324,29 @@ int renderClashScript(YAML::Node &base_rule, std::vector<ruleset_content> &rules
std::string json_path = "rules." + std::to_string(index) + ".";
std::string url = urls[x], keyword = keywords[x], name = names[x];
bool group_has_domain = has_domain[x], group_has_ipcidr = has_ipcidr[x];
if(group_has_domain)
if(clash_classical_ruleset)
{
base_rule["rule-providers"][name + "_" + x + "_domain"]["type"] = "http";
base_rule["rule-providers"][name + "_" + x + "_domain"]["behavior"] = "domain";
base_rule["rule-providers"][name + "_" + x + "_domain"]["url"] = remote_path_prefix + "/getruleset?type=3&url=" + urlsafe_base64_encode(url);
base_rule["rule-providers"][name + "_" + x + "_domain"]["path"] = "./rule-providers_" + x + "_domain.yaml";
base_rule["rule-providers"][name + "_" + x + "_classical"]["type"] = "http";
base_rule["rule-providers"][name + "_" + x + "_classical"]["behavior"] = "classical";
base_rule["rule-providers"][name + "_" + x + "_classical"]["url"] = remote_path_prefix + "/getruleset?type=6&url=" + urlsafe_base64_encode(url);
base_rule["rule-providers"][name + "_" + x + "_classical"]["path"] = "./providers/rule-provider_" + x + "_classical.yaml";
}
if(group_has_ipcidr)
else
{
base_rule["rule-providers"][name + "_" + x + "_ipcidr"]["type"] = "http";
base_rule["rule-providers"][name + "_" + x + "_ipcidr"]["behavior"] = "ipcidr";
base_rule["rule-providers"][name + "_" + x + "_ipcidr"]["url"] = remote_path_prefix + "/getruleset?type=4&url=" + urlsafe_base64_encode(url);
base_rule["rule-providers"][name + "_" + x + "_ipcidr"]["path"] = "./rule-providers_" + x + "_ipcidr.yaml";
if(group_has_domain)
{
base_rule["rule-providers"][name + "_" + x + "_domain"]["type"] = "http";
base_rule["rule-providers"][name + "_" + x + "_domain"]["behavior"] = "domain";
base_rule["rule-providers"][name + "_" + x + "_domain"]["url"] = remote_path_prefix + "/getruleset?type=3&url=" + urlsafe_base64_encode(url);
base_rule["rule-providers"][name + "_" + x + "_domain"]["path"] = "./providers/rule-provider_" + x + "_domain.yaml";
}
if(group_has_ipcidr)
{
base_rule["rule-providers"][name + "_" + x + "_ipcidr"]["type"] = "http";
base_rule["rule-providers"][name + "_" + x + "_ipcidr"]["behavior"] = "ipcidr";
base_rule["rule-providers"][name + "_" + x + "_ipcidr"]["url"] = remote_path_prefix + "/getruleset?type=4&url=" + urlsafe_base64_encode(url);
base_rule["rule-providers"][name + "_" + x + "_ipcidr"]["path"] = "./providers/rule-provider_" + x + "_ipcidr.yaml";
}
}
if(script)
{