Fix JSON in curl output
Some checks failed
Deploy DNS Configuration / deploy (push) Has been cancelled

This commit is contained in:
Kirill Kodanev 2025-09-15 21:00:34 +03:00
parent 8b1cef6944
commit edbc320e44

View file

@ -5,29 +5,27 @@ set -euo pipefail
# Конфигурация через переменные окружения # Конфигурация через переменные окружения
# ============================== # ==============================
INPUT_FILE="${DOMAINS_FILE:-domains.txt}" INPUT_FILE="${DOMAINS_FILE:-domains.txt}"
IPSET_CONF="${IPSET_CONF:-/tmp/91-ipset-bbrkn.conf}" IPSET_CONF="${IPSET_CONF:-/tmp/91-ipset-bbrkn.conf}"
RESOLVE_CONF="${RESOLVE_CONF:-/tmp/92-resolve-bbrkn.conf}" RESOLVE_CONF="${RESOLVE_CONF:-/tmp/92-resolve-bbrkn.conf}"
API_URL="${CHROME_SERVER:-http://127.0.0.1:3000}/domains?domain=" API_URL="${CHROME_SERVER:-http://127.0.0.1:3000}/domains?domain="
DNS_SERVER="${DNS_SERVER:-8.8.8.8}" DNS_SERVER="${DNS_SERVER:-8.8.8.8}"
# Debug knobs # Debug knobs
DEBUG="${DEBUG:-0}" DEBUG="${DEBUG:-0}"
DEBUG_LOG="${DEBUG_LOG:-/tmp/generate-configs.$$.debug.log}" DEBUG_LOG="${DEBUG_LOG:-/tmp/generate-configs.debug.log}"
DRY_RUN=false DRY_RUN=false
if [[ "${1:-}" == "--dry-run" ]]; then if [[ "${1:-}" == "--dry-run" ]]; then
DRY_RUN=true DRY_RUN=true
fi fi
# Helpers
log() { printf '%s\n' "$*"; } log() { printf '%s\n' "$*"; }
dbg() { if [ "$DEBUG" != "0" ]; then printf '[DEBUG] %s\n' "$*" | tee -a "$DEBUG_LOG"; fi } dbg() { if [ "$DEBUG" != "0" ]; then printf '[DEBUG] %s\n' "$*" | tee -a "$DEBUG_LOG"; fi }
err() { printf '[ERROR] %s\n' "$*" | tee -a "$DEBUG_LOG" >&2; } err() { printf '[ERROR] %s\n' "$*" | tee -a "$DEBUG_LOG" >&2; }
for cmd in curl jq; do if ! command -v curl >/dev/null 2>&1; then err "curl is required"; exit 2; fi
if ! command -v "$cmd" >/dev/null 2>&1; then err "$cmd is required"; exit 2; fi if ! command -v jq >/dev/null 2>&1; then err "jq is required"; exit 2; fi
done
if [ "$DEBUG" != "0" ]; then : > "$DEBUG_LOG"; dbg "Debugging enabled"; fi if [ "$DEBUG" != "0" ]; then : > "$DEBUG_LOG"; dbg "Debugging enabled"; fi
@ -52,14 +50,6 @@ api_success=0
api_error=0 api_error=0
related_total=0 related_total=0
TEMP_FILES=()
cleanup_tmp() {
for f in "${TEMP_FILES[@]}"; do
[ -f "$f" ] && rm -f "$f"
done
}
trap cleanup_tmp EXIT
normalize_domain() { normalize_domain() {
local raw="$1" local raw="$1"
raw="$(printf '%s' "$raw" | sed -E 's/#.*$//' | awk '{$1=$1};1')" raw="$(printf '%s' "$raw" | sed -E 's/#.*$//' | awk '{$1=$1};1')"
@ -72,40 +62,35 @@ normalize_domain() {
return 0 return 0
} }
query_api() { fetch_json() {
local domain="$1" local domain="$1"
local max_attempts=5 local max_attempts=5
local attempt=0 local attempt=0
local max_sleep=8 local max_sleep=8
local tmpfile body http_code sleep_time local resp http_code json sleep_time preview
while :; do while :; do
attempt=$((attempt+1)) attempt=$((attempt+1))
dbg " -> API attempt #$attempt for $domain" dbg " -> API attempt #$attempt for $domain"
tmpfile=$(mktemp) resp=$(curl -sS --compressed \
curl -sS --compressed \
-m 10 --connect-timeout 5 \ -m 10 --connect-timeout 5 \
--retry 2 --retry-connrefused \ --retry 2 --retry-connrefused \
-H 'Accept: application/json' \ -H 'Accept: application/json' \
-w '%{http_code}' \ -w '%{http_code}' \
-o "$tmpfile" \ "${API_URL}${domain}" 2>>"$DEBUG_LOG") || true
"${API_URL}${domain}" 2>>"$DEBUG_LOG" || true
http_code=$(tail -n1 "$tmpfile") http_code="${resp: -3}" # последние 3 символа
body=$(cat "$tmpfile") json="${resp:0:-3}" # тело без http_code
preview="$(printf '%s' "$body" | tr '\n' ' ' | cut -c1-400)" preview="$(printf '%s' "$json" | tr '\n' ' ' | cut -c1-400)"
dbg " -> HTTP=$http_code, preview=${preview}" dbg " -> HTTP=$http_code, preview=${preview}"
if [ "$http_code" = "200" ] && jq -e . "$tmpfile" >/dev/null 2>&1; then if [ "$http_code" = "200" ] && jq -e . >/dev/null 2>&1 <<<"$json"; then
cat "$tmpfile" # вернём тело JSON напрямую echo "$json"
rm -f "$tmpfile"
return 0 return 0
fi fi
rm -f "$tmpfile"
if [ "$attempt" -ge "$max_attempts" ]; then if [ "$attempt" -ge "$max_attempts" ]; then
ERRORS["$domain"]="http_${http_code}_or_invalid_json" ERRORS["$domain"]="http_${http_code}_or_invalid_json"
dbg " -> Failed after $attempt attempts, preview=${preview}" dbg " -> Failed after $attempt attempts, preview=${preview}"
@ -119,7 +104,6 @@ query_api() {
done done
} }
if [ ! -f "$INPUT_FILE" ]; then if [ ! -f "$INPUT_FILE" ]; then
err "Input file not found: $INPUT_FILE" err "Input file not found: $INPUT_FILE"
exit 3 exit 3
@ -143,28 +127,36 @@ while IFS= read -r line || [ -n "$line" ]; do
normalized_ok=$((normalized_ok+1)) normalized_ok=$((normalized_ok+1))
dbg " -> NORMALIZED: $dom_norm" dbg " -> NORMALIZED: $dom_norm"
tmpjson=$(query_api "$dom_norm" || true) json="$(fetch_json "$dom_norm" || true)"
if [ -z "$tmpjson" ]; then if [ -z "$json" ]; then
dbg " -> No valid response for $dom_norm, skipping." api_error=$((api_error+1))
dbg " -> API returned empty or invalid JSON for $dom_norm"
continue continue
fi fi
if jq -e 'has("error")' "$tmpjson" >/dev/null 2>&1; then if jq -e 'has("error")' <<<"$json" >/dev/null; then
err_msg=$(jq -r '.error' "$tmpjson") err_msg="$(jq -r '.error' <<<"$json")"
dbg " -> API error: $err_msg" dbg " -> API error: $err_msg"
DOM_ROLE["$dom_norm"]="error"
if grep -Eq "ERR_NAME_NOT_RESOLVED|Timeout" <<<"$err_msg"; then
continue
fi
DOM_ROLE["$dom_norm"]="service"
SOURCES["$dom_norm"]="base"
EXPANDED["$dom_norm"]=1
ERRORS["$dom_norm"]="$err_msg" ERRORS["$dom_norm"]="$err_msg"
continue continue
fi fi
# валидный сайт # valid JSON -> site
api_success=$((api_success+1)) api_success=$((api_success+1))
DOM_ROLE["$dom_norm"]="site" DOM_ROLE["$dom_norm"]="site"
SOURCES["$dom_norm"]="base" SOURCES["$dom_norm"]="base"
EXPANDED["$dom_norm"]=1 EXPANDED["$dom_norm"]=1
VALID_SITES["$dom_norm"]=1 VALID_SITES["$dom_norm"]=1
mapfile -t subs < <(jq -r '.relatedDomains[]? // empty' "$tmpjson") mapfile -t subs < <(jq -r '.relatedDomains[]? // empty' <<<"$json")
dbg " -> API returned ${#subs[@]} related domains" dbg " -> API returned ${#subs[@]} related domains"
for s in "${subs[@]}"; do for s in "${subs[@]}"; do
nd="$(normalize_domain "$s" || true)" nd="$(normalize_domain "$s" || true)"