From f671230ce8e1059a5391ccc83ee21586c87f4b39 Mon Sep 17 00:00:00 2001 From: Tindy X <49061470+tindy2013@users.noreply.github.com> Date: Wed, 12 Aug 2020 18:33:31 +0800 Subject: [PATCH] Add option to change style of proxies section in Clash subscriptions --- base/pref-new.yml | 1 + base/pref.ini | 8 ++++++++ src/interfaces.cpp | 6 ++++++ src/subexport.cpp | 22 +++++++++++++++++++++- src/subexport.h | 1 + 5 files changed, 37 insertions(+), 1 deletion(-) diff --git a/base/pref-new.yml b/base/pref-new.yml index b3725e1..afb49ee 100644 --- a/base/pref-new.yml +++ b/base/pref-new.yml @@ -47,6 +47,7 @@ node_pref: filter_deprecated_nodes: false append_sub_userinfo: true clash_use_new_field_name: true + clash_proxies_style: flow rename_node: # - {match: "\\(?((x|X)?(\\d+)(\\.?\\d+)?)((\\s?倍率?)|(x|X))\\)?", replace: "$1x"} # - {script: "function rename(node){}"} diff --git a/base/pref.ini b/base/pref.ini index 39b0ed2..c91b24b 100644 --- a/base/pref.ini +++ b/base/pref.ini @@ -103,6 +103,14 @@ filter_deprecated_nodes=false append_sub_userinfo=true clash_use_new_field_name=true +;Generate style of the proxies section of Clash subscriptions. +;Supported styles: block, flow, compact +;Block: - name: name1 Flow: - {name: name1, key: value} Compact: [{name: name1, key: value},{name: name2, key: value}] +; key: value - {name: name2, key: value} +; - name: name2 +; key: value +clash_proxies_style=flow + ;Rename remarks with the following patterns. Supports regular expression. ;Format: Search_Pattern@Replace_Pattern ;rename_node=IPLC@专线 diff --git a/src/interfaces.cpp b/src/interfaces.cpp index 34b655d..4ac2174 100644 --- a/src/interfaces.cpp +++ b/src/interfaces.cpp @@ -54,6 +54,7 @@ bool add_emoji = false, remove_old_emoji = false, append_proxy_type = false, fil tribool udp_flag, tfo_flag, scv_flag, tls13_flag, enable_insert; bool do_sort = false, config_update_strict = false; bool clash_use_new_field_name = false; +std::string clash_proxies_style = "flow"; std::string proxy_config, proxy_ruleset, proxy_subscription; int config_update_interval = 0; std::string sort_script, filter_script; @@ -745,6 +746,7 @@ void readYAMLConf(YAML::Node &node) section["filter_deprecated_nodes"] >> filter_deprecated; section["append_sub_userinfo"] >> append_userinfo; section["clash_use_new_field_name"] >> clash_use_new_field_name; + section["clash_proxies_style"] >> clash_proxies_style; } if(section["rename_node"].IsSequence()) @@ -983,6 +985,7 @@ void readConf() ini.GetBoolIfExist("filter_deprecated_nodes", filter_deprecated); ini.GetBoolIfExist("append_sub_userinfo", append_userinfo); ini.GetBoolIfExist("clash_use_new_field_name", clash_use_new_field_name); + ini.GetIfExist("clash_proxies_style", clash_proxies_style); if(ini.ItemPrefixExist("rename_node")) { ini.GetAll("rename_node", tempArray); @@ -1450,6 +1453,8 @@ std::string subconverter(RESPONSE_CALLBACK_ARGS) if((target == "clash" || target == "clashr") && clash_script.is_undef()) expand.define(true); + ext.clash_proxies_style = clash_proxies_style; + /// read preference from argument, assign global var if not in argument ext.tfo.parse(tfo).parse(tfo_flag); ext.udp.parse(udp).parse(udp_flag); @@ -2072,6 +2077,7 @@ std::string surgeConfToClash(RESPONSE_CALLBACK_ARGS) } extra_settings ext = {true, true, dummy_str_array, dummy_str_array, false, false, false, false, do_sort, filter_deprecated, clash_use_new_field_name, false, "", "", "", udp_flag, tfo_flag, scv_flag, tls13_flag}; + ext.clash_proxies_style = clash_proxies_style; netchToClash(nodes, clash, dummy_str_array, false, ext); diff --git a/src/subexport.cpp b/src/subexport.cpp index 7fc451a..0f774e1 100644 --- a/src/subexport.cpp +++ b/src/subexport.cpp @@ -1149,6 +1149,20 @@ void netchToClash(std::vector &nodes, YAML::Node &yamlnode, const stri std::vector nodelist; bool tlssecure; string_array vArray, remarks_list, filtered_nodelist; + /// proxies style + bool block = false, compact = false; + switch(hash_(ext.clash_proxies_style)) + { + case "block"_hash: + block = true; + break; + default: + case "flow"_hash: + break; + case "compact"_hash: + compact = true; + break; + } for(nodeInfo &x : nodes) { @@ -1341,12 +1355,18 @@ void netchToClash(std::vector &nodes, YAML::Node &yamlnode, const stri if(udp) singleproxy["udp"] = true; - singleproxy.SetStyle(YAML::EmitterStyle::Flow); + if(block) + singleproxy.SetStyle(YAML::EmitterStyle::Block); + else + singleproxy.SetStyle(YAML::EmitterStyle::Flow); proxies.push_back(singleproxy); remarks_list.emplace_back(std::move(remark)); nodelist.emplace_back(x); } + if(compact) + proxies.SetStyle(YAML::EmitterStyle::Flow); + if(ext.nodelist) { YAML::Node provider; diff --git a/src/subexport.h b/src/subexport.h index 58636d3..490a5ec 100644 --- a/src/subexport.h +++ b/src/subexport.h @@ -51,6 +51,7 @@ struct extra_settings tribool tls13 = tribool(); bool clash_classical_ruleset = false; std::string sort_script = ""; + std::string clash_proxies_style = "flow"; }; void rulesetToClash(YAML::Node &base_rule, std::vector &ruleset_content_array, bool overwrite_original_rules, bool new_field_name);