refactor: fetch remote JSON to temp files and parse ip_cidr into subnets; remove download_to_stream

This commit is contained in:
Andrey Petelin
2025-10-29 21:52:44 +05:00
parent fe84b3154f
commit f168fb7e31
2 changed files with 8 additions and 25 deletions

View File

@@ -1337,19 +1337,21 @@ import_domains_or_subnets_from_remote_file() {
import_subnets_from_remote_json_file() { import_subnets_from_remote_json_file() {
local url="$1" local url="$1"
local tmpfile subnets http_proxy_address local json_tmpfile subnets_tmpfile subnets http_proxy_address
tmpfile="$(mktemp)" json_tmpfile="$(mktemp)"
subnets_tmpfile="$(mktemp)"
http_proxy_address="$(get_service_proxy_address)" 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" log "Download $url list failed" "error"
return 1 return 1
fi fi
subnets="$(parse_domain_or_subnet_file_to_comma_string "$tmpfile" "subnets")" jq -r '.rules[].ip_cidr[]' "$json_tmpfile" > "$subnets_tmpfile"
rm -f "$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" nft_add_set_elements "$NFT_TABLE_NAME" "$NFT_COMMON_SET_NAME" "$subnets"
} }

View File

@@ -268,25 +268,6 @@ migration_rename_config_key() {
fi 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 URL to file
download_to_file() { download_to_file() {
local url="$1" local url="$1"