From 6c094aceaea7d1da138a1630da751da5898ac1c7 Mon Sep 17 00:00:00 2001 From: Andrey Petelin Date: Tue, 9 Sep 2025 11:15:29 +0500 Subject: [PATCH] fix: Ensure unique values when patching local source ruleset --- .../files/usr/lib/sing_box_config_manager.sh | 29 +++++++++++++++++-- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/podkop/files/usr/lib/sing_box_config_manager.sh b/podkop/files/usr/lib/sing_box_config_manager.sh index a959034..8c9a0c1 100644 --- a/podkop/files/usr/lib/sing_box_config_manager.sh +++ b/podkop/files/usr/lib/sing_box_config_manager.sh @@ -1227,12 +1227,28 @@ sing_box_cm_configure_clash_api() { }' } +####################################### +# Create a local source ruleset JSON file for sing-box. +# Arguments: +# filepath: path to the JSON file to create +# Example: +# sing_box_cm_create_local_source_ruleset "/tmp/sing-box/ruleset.json" +####################################### sing_box_cm_create_local_source_ruleset() { local filepath="$1" jq -n '{version: 3, rules: []}' > "$filepath" } +####################################### +# Patch a local source ruleset JSON file for sing-box by adding unique! values to a given key. +# Arguments: +# filepath: path to the JSON file to patch +# key: the ruleset key to update (e.g., "ip_cidr") +# value: a JSON array of values to add to the key +# Example: +# sing_box_cm_patch_local_source_ruleset_rules "/tmp/sing-box/ruleset.json" "ip_cidr" '["1.1.1.1","2.2.2.2"]' +####################################### sing_box_cm_patch_local_source_ruleset_rules() { local filepath="$1" local key="$2" @@ -1244,9 +1260,16 @@ sing_box_cm_patch_local_source_ruleset_rules() { content="$(cat "$filepath")" echo "$content" | jq \ - --arg key "$key" \ - --argjson value "$value" \ - '.rules += [{($key): $value}]' > "$filepath" + --arg key "$key" \ + --argjson value "$value" ' + ([.rules[]?[$key][]] | unique) as $existing + | ($value - $existing) as $value + | if ($value | length) > 0 then + .rules += [{($key): $value}] + else + . + end + ' > "$filepath" } #######################################