From 3b58b04d63539b1a09355f0d1382343e2906eacf Mon Sep 17 00:00:00 2001 From: Tindy X <49061470+tindy2013@users.noreply.github.com> Date: Mon, 29 Jun 2020 16:16:49 +0800 Subject: [PATCH] Fix error in generated Clash scripts Fix missing classical providers when exporting Clash rule-providers. Fix not correctly process some rulesets when generating Clash scripts. Fix typo in pref.ini. --- base/pref.ini | 4 ++-- src/templates.cpp | 23 +++++++++++++++++------ 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/base/pref.ini b/base/pref.ini index a981762..9aece67 100644 --- a/base/pref.ini +++ b/base/pref.ini @@ -123,8 +123,8 @@ managed_config_prefix=http://127.0.0.1:25500 ;Managed config update interval in seconds, determine how long the config will be updated. config_update_interval=86400 -;If config_update_struct is set to true, Surge will require a force update after the interval. -config_update_struct=false +;If config_update_strict is set to true, Surge will require a force update after the interval. +config_update_strict=false ;Device ID to be written to rewrite scripts for some version of Quantumult X quanx_device_id= diff --git a/src/templates.cpp b/src/templates.cpp index 34b5c5e..37e472d 100644 --- a/src/templates.cpp +++ b/src/templates.cpp @@ -185,15 +185,16 @@ int render_template(const std::string &content, const template_args &vars, std:: } const std::string clash_script_template = R"(def main(ctx, md): + host = md["host"] {% for rule in rules %} {% if rule.set == "true" %}{% include "group_template" %}{% endif %} {% if not rule.keyword == "" %}{% include "keyword_template" %}{% endif %} {% endfor %} {% if exists("geoips") %} geoips = { {{ geoips }} } - ip = md.dst_ip + ip = md["dst_ip"] if ip == "": - ip = ctx.resolve_ip(md.host) + ip = ctx.resolve_ip(host) if ip == "": ctx.log('[Script] dns lookup error use {{ match_group }}') return "{{ match_group }}" @@ -213,7 +214,7 @@ const std::string clash_script_group_template = R"({% if rule.has_domain == "fal const std::string clash_script_keyword_template = R"( keywords = [{{ rule.keyword }}] for keyword in keywords: - if keyword in md.host: + if keyword in host: ctx.log('[Script] matched {{ rule.group }} DOMAIN-KEYWORD rule') return "{{ rule.group }}")"; @@ -323,10 +324,10 @@ int renderClashScript(YAML::Node &base_rule, std::vector &rules while(getline(strStrm, strLine, delimiter)) { lineSize = strLine.size(); - if(lineSize) + if(lineSize && strLine[lineSize - 1] == '\r') //remove line break { - strLine = regTrim(strLine); - lineSize = strLine.size(); + strLine.erase(lineSize - 1); + lineSize--; } if(!lineSize || strLine[0] == ';' || strLine[0] == '#' || (lineSize >= 2 && strLine[0] == '/' && strLine[1] == '/')) //empty lines and comments are ignored continue; @@ -401,6 +402,16 @@ int renderClashScript(YAML::Node &base_rule, std::vector &rules 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(!group_has_domain && !group_has_ipcidr) + { + base_rule["rule-providers"][name + "_" + x + "_classical"]["type"] = "http"; + base_rule["rule-providers"][name + "_" + x + "_classical"]["behavior"] = "classical"; + if(url[0] == '*') + base_rule["rule-providers"][name + "_" + x + "_classical"]["url"] = url.substr(1); + else + 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(script) {