Support setting output style for proxy groups in Clash configs (#734)

This commit is contained in:
Tindy X 2024-04-03 18:06:32 +08:00
parent cb15d568c0
commit 4864a6b13c
No known key found for this signature in database
GPG Key ID: 7FA1E66774759718
8 changed files with 36 additions and 9 deletions

View File

@ -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

View File

@ -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

View File

@ -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"}

View File

@ -230,19 +230,31 @@ void proxyToClash(std::vector<Proxy> &nodes, YAML::Node &yamlnode, const ProxyGr
std::vector<Proxy> 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<Proxy> &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<Proxy> &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<Proxy> &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<Proxy> &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

View File

@ -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;

View File

@ -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);

View File

@ -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"))
{

View File

@ -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;