From 52df5d005872936fc5fb72b2bad0dc5834763e17 Mon Sep 17 00:00:00 2001 From: Anthony Date: Thu, 26 Feb 2026 23:28:26 +0800 Subject: [PATCH] Fallback PMA URL to reachable server IP on 8443 --- scripts/pma-gateway/create_pma_gateway.sh | 54 ++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/scripts/pma-gateway/create_pma_gateway.sh b/scripts/pma-gateway/create_pma_gateway.sh index a5ad594..169f6e1 100644 --- a/scripts/pma-gateway/create_pma_gateway.sh +++ b/scripts/pma-gateway/create_pma_gateway.sh @@ -590,8 +590,60 @@ if command -v timeout >/dev/null 2>&1; then fi done + # If no domain host is reachable on 8443, try reachable server IP candidates. + if [[ -z "$SELECTED_URL_HOST" ]]; then + PUBLIC_IP_CANDIDATES=() + SEEN_PUBLIC_IPS="|" + + for ip_candidate in "${PUBLIC_IP:-}" "${JELASTIC_PUBLIC_IP:-}"; do + if [[ "$ip_candidate" =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]] && [[ "$SEEN_PUBLIC_IPS" != *"|$ip_candidate|"* ]]; then + PUBLIC_IP_CANDIDATES+=("$ip_candidate") + SEEN_PUBLIC_IPS="${SEEN_PUBLIC_IPS}${ip_candidate}|" + fi + done + + if command -v ip >/dev/null 2>&1; then + route_ip="$(ip -4 route get 1.1.1.1 2>/dev/null | awk '{for (i=1; i<=NF; i++) if ($i=="src") {print $(i+1); exit}}' || true)" + if [[ "$route_ip" =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]] && [[ "$SEEN_PUBLIC_IPS" != *"|$route_ip|"* ]]; then + PUBLIC_IP_CANDIDATES+=("$route_ip") + SEEN_PUBLIC_IPS="${SEEN_PUBLIC_IPS}${route_ip}|" + fi + fi + + host_ips="$(hostname -I 2>/dev/null || true)" + for ip_candidate in $host_ips; do + if [[ "$ip_candidate" =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]] && [[ "$SEEN_PUBLIC_IPS" != *"|$ip_candidate|"* ]]; then + PUBLIC_IP_CANDIDATES+=("$ip_candidate") + SEEN_PUBLIC_IPS="${SEEN_PUBLIC_IPS}${ip_candidate}|" + fi + done + + if [[ "${#PUBLIC_IP_CANDIDATES[@]}" -eq 0 ]] && command -v dig >/dev/null 2>&1; then + observed_public_ip="$(dig +short myip.opendns.com @resolver1.opendns.com 2>/dev/null | tail -n1 || true)" + if [[ "$observed_public_ip" =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]] && [[ "$SEEN_PUBLIC_IPS" != *"|$observed_public_ip|"* ]]; then + PUBLIC_IP_CANDIDATES+=("$observed_public_ip") + SEEN_PUBLIC_IPS="${SEEN_PUBLIC_IPS}${observed_public_ip}|" + fi + fi + + for ip_candidate in "${PUBLIC_IP_CANDIDATES[@]}"; do + if [[ "$ip_candidate" =~ ^(10\.|127\.|169\.254\.|172\.(1[6-9]|2[0-9]|3[0-1])\.|192\.168\.) ]]; then + continue + fi + + if timeout 4 bash -c "exec 3<>/dev/tcp/$ip_candidate/8443" >/dev/null 2>&1; then + SELECTED_URL_HOST="$ip_candidate" + break + fi + done + fi + if [[ -n "$SELECTED_URL_HOST" ]] && [[ "$SELECTED_URL_HOST" != "$URL_HOST" ]]; then - echo "WARNING: Preferred URL host '$URL_HOST' is not reachable on 8443. Using '$SELECTED_URL_HOST' instead." >&2 + if [[ "$SELECTED_URL_HOST" =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]]; then + echo "WARNING: No domain candidate is reachable on 8443. Using reachable server IP '$SELECTED_URL_HOST' for gateway URL." >&2 + else + echo "WARNING: Preferred URL host '$URL_HOST' is not reachable on 8443. Using '$SELECTED_URL_HOST' instead." >&2 + fi URL_HOST="$SELECTED_URL_HOST" fi fi