From 4864a6b13cd09a1eff28b4d3d4ac0f76379d980d Mon Sep 17 00:00:00 2001 From: Tindy X <49061470+tindy2013@users.noreply.github.com> Date: Wed, 3 Apr 2024 18:06:32 +0800 Subject: [PATCH] Support setting output style for proxy groups in Clash configs (#734) --- base/pref.example.ini | 3 ++- base/pref.example.toml | 3 ++- base/pref.example.yml | 1 + src/generator/config/subexport.cpp | 30 ++++++++++++++++++++++++------ src/generator/config/subexport.h | 1 + src/handler/interfaces.cpp | 2 ++ src/handler/settings.cpp | 3 +++ src/handler/settings.h | 2 +- 8 files changed, 36 insertions(+), 9 deletions(-) diff --git a/base/pref.example.ini b/base/pref.example.ini index fe1688fa8..49fe30167 100644 --- a/base/pref.example.ini +++ b/base/pref.example.ini @@ -109,13 +109,14 @@ filter_deprecated_nodes=false append_sub_userinfo=true clash_use_new_field_name=true -;Generate style of the proxies section of Clash subscriptions. +;Generate style of the proxies and proxy groups 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 +clash_proxy_groups_style=block ;add Clash mode to sing-box rules, and add a GLOBAL group to end of outbounds singbox_add_clash_modes=true diff --git a/base/pref.example.toml b/base/pref.example.toml index 4e0c563d4..f7e4f85b6 100644 --- a/base/pref.example.toml +++ b/base/pref.example.toml @@ -135,13 +135,14 @@ filter_deprecated_nodes = false append_sub_userinfo = true clash_use_new_field_name = true -# Generate style of the proxies section of Clash subscriptions. +# Generate style of the proxies and proxy groups 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" +clash_proxy_groups_style = "block" # add Clash mode to sing-box rules, and add a GLOBAL group to end of outbounds singbox_add_clash_modes = true diff --git a/base/pref.example.yml b/base/pref.example.yml index f532994cc..336a0257a 100644 --- a/base/pref.example.yml +++ b/base/pref.example.yml @@ -50,6 +50,7 @@ node_pref: append_sub_userinfo: true clash_use_new_field_name: true clash_proxies_style: flow + clash_proxy_groups_style: block singbox_add_clash_modes: true rename_node: # - {match: "\\(?((x|X)?(\\d+)(\\.?\\d+)?)((\\s?倍率?)|(x|X))\\)?", replace: "$1x"} diff --git a/src/generator/config/subexport.cpp b/src/generator/config/subexport.cpp index c8eb16a46..1acc79f51 100644 --- a/src/generator/config/subexport.cpp +++ b/src/generator/config/subexport.cpp @@ -230,19 +230,31 @@ void proxyToClash(std::vector &nodes, YAML::Node &yamlnode, const ProxyGr std::vector nodelist; string_array remarks_list; /// proxies style - bool block = false, compact = false; + bool proxy_block = false, proxy_compact = false, group_block = false, group_compact = false; switch(hash_(ext.clash_proxies_style)) { case "block"_hash: - block = true; + proxy_block = true; break; default: case "flow"_hash: break; case "compact"_hash: - compact = true; + proxy_compact = true; break; } + switch(hash_(ext.clash_proxy_groups_style)) + { + case "block"_hash: + group_block = true; + break; + default: + case "flow"_hash: + break; + case "compact"_hash: + group_compact = true; + break; + } for(Proxy &x : nodes) { @@ -474,7 +486,7 @@ void proxyToClash(std::vector &nodes, YAML::Node &yamlnode, const ProxyGr // sees in https://dreamacro.github.io/clash/configuration/outbound.html#snell if(udp && x.Type != ProxyType::Snell) singleproxy["udp"] = true; - if(block) + if(proxy_block) singleproxy.SetStyle(YAML::EmitterStyle::Block); else singleproxy.SetStyle(YAML::EmitterStyle::Flow); @@ -483,7 +495,7 @@ void proxyToClash(std::vector &nodes, YAML::Node &yamlnode, const ProxyGr nodelist.emplace_back(x); } - if(compact) + if(proxy_compact) proxies.SetStyle(YAML::EmitterStyle::Flow); if(ext.nodelist) @@ -545,7 +557,10 @@ void proxyToClash(std::vector &nodes, YAML::Node &yamlnode, const ProxyGr } if(!filtered_nodelist.empty()) singlegroup["proxies"] = filtered_nodelist; - //singlegroup.SetStyle(YAML::EmitterStyle::Flow); + if(group_block) + singlegroup.SetStyle(YAML::EmitterStyle::Block); + else + singlegroup.SetStyle(YAML::EmitterStyle::Flow); bool replace_flag = false; for(auto && original_group : original_groups) @@ -561,6 +576,9 @@ void proxyToClash(std::vector &nodes, YAML::Node &yamlnode, const ProxyGr original_groups.push_back(singlegroup); } + if(group_compact) + original_groups.SetStyle(YAML::EmitterStyle::Flow); + if(ext.clash_new_field_name) yamlnode["proxy-groups"] = original_groups; else diff --git a/src/generator/config/subexport.h b/src/generator/config/subexport.h index 241dc7c47..fd7705585 100644 --- a/src/generator/config/subexport.h +++ b/src/generator/config/subexport.h @@ -39,6 +39,7 @@ struct extra_settings bool clash_classical_ruleset = false; std::string sort_script; std::string clash_proxies_style = "flow"; + std::string clash_proxy_groups_style = "flow"; bool authorized = false; extra_settings() = default; diff --git a/src/handler/interfaces.cpp b/src/handler/interfaces.cpp index 6f138cd4c..fdb119c76 100644 --- a/src/handler/interfaces.cpp +++ b/src/handler/interfaces.cpp @@ -415,6 +415,7 @@ std::string subconverter(RESPONSE_CALLBACK_ARGS) argExpandRulesets.define(true); ext.clash_proxies_style = global.clashProxiesStyle; + ext.clash_proxy_groups_style = global.clashProxyGroupsStyle; /// read preference from argument, assign global var if not in argument ext.tfo.define(argTFO).define(global.TFOFlag); @@ -1073,6 +1074,7 @@ std::string surgeConfToClash(RESPONSE_CALLBACK_ARGS) ext.skip_cert_verify = global.skipCertVerify; ext.tls13 = global.TLS13Flag; ext.clash_proxies_style = global.clashProxiesStyle; + ext.clash_proxy_groups_style = global.clashProxyGroupsStyle; ProxyGroupConfigs dummy_groups; proxyToClash(nodes, clash, dummy_groups, false, ext); diff --git a/src/handler/settings.cpp b/src/handler/settings.cpp index e41400c27..86e399e87 100644 --- a/src/handler/settings.cpp +++ b/src/handler/settings.cpp @@ -377,6 +377,7 @@ void readYAMLConf(YAML::Node &node) section["append_sub_userinfo"] >> global.appendUserinfo; section["clash_use_new_field_name"] >> global.clashUseNewField; section["clash_proxies_style"] >> global.clashProxiesStyle; + section["clash_proxy_groups_style"] >> global.clashProxyGroupsStyle; section["singbox_add_clash_modes"] >> global.singBoxAddClashModes; } @@ -638,6 +639,7 @@ void readTOMLConf(toml::value &root) "append_sub_userinfo", global.appendUserinfo, "clash_use_new_field_name", global.clashUseNewField, "clash_proxies_style", global.clashProxiesStyle, + "clash_proxy_groups_style", global.clashProxyGroupsStyle, "singbox_add_clash_modes", global.singBoxAddClashModes ); @@ -883,6 +885,7 @@ void readConf() ini.get_bool_if_exist("append_sub_userinfo", global.appendUserinfo); ini.get_bool_if_exist("clash_use_new_field_name", global.clashUseNewField); ini.get_if_exist("clash_proxies_style", global.clashProxiesStyle); + ini.get_if_exist("clash_proxy_groups_style", global.clashProxyGroupsStyle); ini.get_bool_if_exist("singbox_add_clash_modes", global.singBoxAddClashModes); if(ini.item_prefix_exist("rename_node")) { diff --git a/src/handler/settings.h b/src/handler/settings.h index 495adab1c..18c79738c 100644 --- a/src/handler/settings.h +++ b/src/handler/settings.h @@ -48,7 +48,7 @@ struct Settings tribool UDPFlag, TFOFlag, skipCertVerify, TLS13Flag, enableInsert; bool enableSort = false, updateStrict = false; bool clashUseNewField = false, singBoxAddClashModes = true; - std::string clashProxiesStyle = "flow"; + std::string clashProxiesStyle = "flow", clashProxyGroupsStyle = "block"; std::string proxyConfig, proxyRuleset, proxySubscription; int updateInterval = 0; std::string sortScript, filterScript;