From f168fb7e3153c908d739079d63ef5441250b92d4 Mon Sep 17 00:00:00 2001 From: Andrey Petelin Date: Wed, 29 Oct 2025 21:52:44 +0500 Subject: [PATCH] refactor: fetch remote JSON to temp files and parse ip_cidr into subnets; remove download_to_stream --- podkop/files/usr/bin/podkop | 14 ++++++++------ podkop/files/usr/lib/helpers.sh | 19 ------------------- 2 files changed, 8 insertions(+), 25 deletions(-) diff --git a/podkop/files/usr/bin/podkop b/podkop/files/usr/bin/podkop index 6534f6a..68577bf 100755 --- a/podkop/files/usr/bin/podkop +++ b/podkop/files/usr/bin/podkop @@ -1337,19 +1337,21 @@ import_domains_or_subnets_from_remote_file() { import_subnets_from_remote_json_file() { local url="$1" - local tmpfile subnets http_proxy_address - tmpfile="$(mktemp)" + local json_tmpfile subnets_tmpfile subnets http_proxy_address + json_tmpfile="$(mktemp)" + subnets_tmpfile="$(mktemp)" http_proxy_address="$(get_service_proxy_address)" - download_to_stream "$url" "$http_proxy_address" | jq -r '.rules[].ip_cidr[]?' > "$tmpfile" + download_to_file "$url" "$json_tmpfile" "$http_proxy_address" - if [ $? -ne 0 ] || [ ! -s "$tmpfile" ]; then + if [ $? -ne 0 ] || [ ! -s "$json_tmpfile" ]; then log "Download $url list failed" "error" return 1 fi - subnets="$(parse_domain_or_subnet_file_to_comma_string "$tmpfile" "subnets")" - rm -f "$tmpfile" + jq -r '.rules[].ip_cidr[]' "$json_tmpfile" > "$subnets_tmpfile" + subnets="$(parse_domain_or_subnet_file_to_comma_string "$subnets_tmpfile" "subnets")" + rm -f "$json_tmpfile" "$subnets_tmpfile" nft_add_set_elements "$NFT_TABLE_NAME" "$NFT_COMMON_SET_NAME" "$subnets" } diff --git a/podkop/files/usr/lib/helpers.sh b/podkop/files/usr/lib/helpers.sh index 5801fec..8957711 100644 --- a/podkop/files/usr/lib/helpers.sh +++ b/podkop/files/usr/lib/helpers.sh @@ -268,25 +268,6 @@ migration_rename_config_key() { fi } -# Download URL content directly -download_to_stream() { - local url="$1" - local http_proxy_address="$2" - local retries="${3:-3}" - local wait="${4:-2}" - - for attempt in $(seq 1 "$retries"); do - if [ -n "$http_proxy_address" ]; then - http_proxy="http://$http_proxy_address" https_proxy="http://$http_proxy_address" wget -qO- "$url" | sed 's/\r$//' && break - else - wget -qO- "$url" | sed 's/\r$//' && break - fi - - log "Attempt $attempt/$retries to download $url failed" "warn" - sleep "$wait" - done -} - # Download URL to file download_to_file() { local url="$1"