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

This commit is contained in:
Tindy X committed 2024-04-03 18:07:12 +08:00
1 parent cb15d568c0
commit 4864a6b13c
8 files changed
+36 -9

No files matched your search

+2 -1
View File
@@ -109,13 +109,14 @@ filter_deprecated_nodes=false
append_sub_userinfo=true append_sub_userinfo=true
clash_use_new_field_name=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 ;Supported styles: block, flow, compact
;Block: - name: name1 Flow: - {name: name1, key: value} Compact: [{name: name1, key: value},{name: name2, key: value}] ;Block: - name: name1 Flow: - {name: name1, key: value} Compact: [{name: name1, key: value},{name: name2, key: value}]
; key: value - {name: name2, key: value} ; key: value - {name: name2, key: value}
; - name: name2 ; - name: name2
; key: value ; key: value
clash_proxies_style=flow 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 ;add Clash mode to sing-box rules, and add a GLOBAL group to end of outbounds
singbox_add_clash_modes=true singbox_add_clash_modes=true
+2 -1
View File
@@ -135,13 +135,14 @@ filter_deprecated_nodes = false
append_sub_userinfo = true append_sub_userinfo = true
clash_use_new_field_name = 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 # Supported styles: block, flow, compact
# Block: - name: name1 Flow: - {name: name1, key: value} Compact: [{name: name1, key: value},{name: name2, key: value}] # Block: - name: name1 Flow: - {name: name1, key: value} Compact: [{name: name1, key: value},{name: name2, key: value}]
# key: value - {name: name2, key: value} # key: value - {name: name2, key: value}
# - name: name2 # - name: name2
# key: value # key: value
clash_proxies_style = "flow" 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 # add Clash mode to sing-box rules, and add a GLOBAL group to end of outbounds
singbox_add_clash_modes = true singbox_add_clash_modes = true
+1
View File
@@ -50,6 +50,7 @@ node_pref:
append_sub_userinfo: true append_sub_userinfo: true
clash_use_new_field_name: true clash_use_new_field_name: true
clash_proxies_style: flow clash_proxies_style: flow
clash_proxy_groups_style: block
singbox_add_clash_modes: true singbox_add_clash_modes: true
rename_node: rename_node:
# - {match: "\\(?((x|X)?(\\d+)(\\.?\\d+)?)((\\s?倍率?)|(x|X))\\)?", replace: "$1x"} # - {match: "\\(?((x|X)?(\\d+)(\\.?\\d+)?)((\\s?倍率?)|(x|X))\\)?", replace: "$1x"}
+24 -6
View File
@@ -230,19 +230,31 @@ void proxyToClash(std::vector<Proxy> &nodes, YAML::Node &yamlnode, const ProxyGr
std::vector<Proxy> nodelist; std::vector<Proxy> nodelist;
string_array remarks_list; string_array remarks_list;
/// proxies style /// 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)) switch(hash_(ext.clash_proxies_style))
{ {
case "block"_hash: case "block"_hash:
block = true; proxy_block = true;
break; break;
default: default:
case "flow"_hash: case "flow"_hash:
break; break;
case "compact"_hash: case "compact"_hash:
compact = true; proxy_compact = true;
break; 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) 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 // sees in https://dreamacro.github.io/clash/configuration/outbound.html#snell
if(udp && x.Type != ProxyType::Snell) if(udp && x.Type != ProxyType::Snell)
singleproxy["udp"] = true; singleproxy["udp"] = true;
if(block) if(proxy_block)
singleproxy.SetStyle(YAML::EmitterStyle::Block); singleproxy.SetStyle(YAML::EmitterStyle::Block);
else else
singleproxy.SetStyle(YAML::EmitterStyle::Flow); singleproxy.SetStyle(YAML::EmitterStyle::Flow);
@@ -483,7 +495,7 @@ void proxyToClash(std::vector<Proxy> &nodes, YAML::Node &yamlnode, const ProxyGr
nodelist.emplace_back(x); nodelist.emplace_back(x);
} }
if(compact) if(proxy_compact)
proxies.SetStyle(YAML::EmitterStyle::Flow); proxies.SetStyle(YAML::EmitterStyle::Flow);
if(ext.nodelist) if(ext.nodelist)
@@ -545,7 +557,10 @@ void proxyToClash(std::vector<Proxy> &nodes, YAML::Node &yamlnode, const ProxyGr
} }
if(!filtered_nodelist.empty()) if(!filtered_nodelist.empty())
singlegroup["proxies"] = filtered_nodelist; 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; bool replace_flag = false;
for(auto && original_group : original_groups) 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); original_groups.push_back(singlegroup);
} }
if(group_compact)
original_groups.SetStyle(YAML::EmitterStyle::Flow);
if(ext.clash_new_field_name) if(ext.clash_new_field_name)
yamlnode["proxy-groups"] = original_groups; yamlnode["proxy-groups"] = original_groups;
else else
+1
View File
@@ -39,6 +39,7 @@ struct extra_settings
bool clash_classical_ruleset = false; bool clash_classical_ruleset = false;
std::string sort_script; std::string sort_script;
std::string clash_proxies_style = "flow"; std::string clash_proxies_style = "flow";
std::string clash_proxy_groups_style = "flow";
bool authorized = false; bool authorized = false;
extra_settings() = default; extra_settings() = default;
+2
View File
@@ -415,6 +415,7 @@ std::string subconverter(RESPONSE_CALLBACK_ARGS)
argExpandRulesets.define(true); argExpandRulesets.define(true);
ext.clash_proxies_style = global.clashProxiesStyle; ext.clash_proxies_style = global.clashProxiesStyle;
ext.clash_proxy_groups_style = global.clashProxyGroupsStyle;
/// read preference from argument, assign global var if not in argument /// read preference from argument, assign global var if not in argument
ext.tfo.define(argTFO).define(global.TFOFlag); ext.tfo.define(argTFO).define(global.TFOFlag);
@@ -1073,6 +1074,7 @@ std::string surgeConfToClash(RESPONSE_CALLBACK_ARGS)
ext.skip_cert_verify = global.skipCertVerify; ext.skip_cert_verify = global.skipCertVerify;
ext.tls13 = global.TLS13Flag; ext.tls13 = global.TLS13Flag;
ext.clash_proxies_style = global.clashProxiesStyle; ext.clash_proxies_style = global.clashProxiesStyle;
ext.clash_proxy_groups_style = global.clashProxyGroupsStyle;
ProxyGroupConfigs dummy_groups; ProxyGroupConfigs dummy_groups;
proxyToClash(nodes, clash, dummy_groups, false, ext); proxyToClash(nodes, clash, dummy_groups, false, ext);
+3
View File
@@ -377,6 +377,7 @@ void readYAMLConf(YAML::Node &node)
section["append_sub_userinfo"] >> global.appendUserinfo; section["append_sub_userinfo"] >> global.appendUserinfo;
section["clash_use_new_field_name"] >> global.clashUseNewField; section["clash_use_new_field_name"] >> global.clashUseNewField;
section["clash_proxies_style"] >> global.clashProxiesStyle; section["clash_proxies_style"] >> global.clashProxiesStyle;
section["clash_proxy_groups_style"] >> global.clashProxyGroupsStyle;
section["singbox_add_clash_modes"] >> global.singBoxAddClashModes; section["singbox_add_clash_modes"] >> global.singBoxAddClashModes;
} }
@@ -638,6 +639,7 @@ void readTOMLConf(toml::value &root)
"append_sub_userinfo", global.appendUserinfo, "append_sub_userinfo", global.appendUserinfo,
"clash_use_new_field_name", global.clashUseNewField, "clash_use_new_field_name", global.clashUseNewField,
"clash_proxies_style", global.clashProxiesStyle, "clash_proxies_style", global.clashProxiesStyle,
"clash_proxy_groups_style", global.clashProxyGroupsStyle,
"singbox_add_clash_modes", global.singBoxAddClashModes "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("append_sub_userinfo", global.appendUserinfo);
ini.get_bool_if_exist("clash_use_new_field_name", global.clashUseNewField); 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_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); ini.get_bool_if_exist("singbox_add_clash_modes", global.singBoxAddClashModes);
if(ini.item_prefix_exist("rename_node")) if(ini.item_prefix_exist("rename_node"))
{ {
+1 -1
View File
@@ -48,7 +48,7 @@ struct Settings
tribool UDPFlag, TFOFlag, skipCertVerify, TLS13Flag, enableInsert; tribool UDPFlag, TFOFlag, skipCertVerify, TLS13Flag, enableInsert;
bool enableSort = false, updateStrict = false; bool enableSort = false, updateStrict = false;
bool clashUseNewField = false, singBoxAddClashModes = true; bool clashUseNewField = false, singBoxAddClashModes = true;
std::string clashProxiesStyle = "flow"; std::string clashProxiesStyle = "flow", clashProxyGroupsStyle = "block";
std::string proxyConfig, proxyRuleset, proxySubscription; std::string proxyConfig, proxyRuleset, proxySubscription;
int updateInterval = 0; int updateInterval = 0;
std::string sortScript, filterScript; std::string sortScript, filterScript;