Skip to content

Commit

Permalink
Support setting output style for proxy groups in Clash configs (#734)
Browse files Browse the repository at this point in the history
  • Loading branch information
tindy2013 committed Apr 3, 2024
1 parent cb15d56 commit 4864a6b
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 9 deletions.
3 changes: 2 additions & 1 deletion base/pref.example.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion base/pref.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions base/pref.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down
30 changes: 24 additions & 6 deletions src/generator/config/subexport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/generator/config/subexport.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/handler/interfaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions src/handler/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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
);

Expand Down Expand Up @@ -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"))
{
Expand Down
2 changes: 1 addition & 1 deletion src/handler/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 4864a6b

Please sign in to comment.