mirror of
https://github.com/itdoginfo/podkop.git
synced 2025-12-06 03:26:51 +03:00
refactor: fetch remote JSON to temp files and parse ip_cidr into subnets; remove download_to_stream
This commit is contained in:
@@ -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"
|
||||
}
|
||||
|
||||
|
||||
@@ -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"
|
||||
|
||||
Reference in New Issue
Block a user