diff --git a/scripts/pma-gateway/create_pma_gateway.sh b/scripts/pma-gateway/create_pma_gateway.sh index edca8d1..a5ad594 100644 --- a/scripts/pma-gateway/create_pma_gateway.sh +++ b/scripts/pma-gateway/create_pma_gateway.sh @@ -556,6 +556,46 @@ if [[ "$URL_HOST" =~ ^(.+):[0-9]+$ ]]; then URL_HOST="${BASH_REMATCH[1]}" fi +# Best-effort fallback: if preferred host is not reachable on 8443, +# try other known domain candidates before printing final gateway URL. +if command -v timeout >/dev/null 2>&1; then + URL_HOST_CANDIDATES=() + URL_HOST_SEEN="|" + for candidate in "$URL_HOST" "$ENV_HOST" "${DOMAIN_CANDIDATES[@]}"; do + candidate="${candidate#https://}" + candidate="${candidate#http://}" + candidate="${candidate%%/*}" + if [[ "$candidate" =~ ^(.+):[0-9]+$ ]]; then + candidate="${BASH_REMATCH[1]}" + fi + + if [[ -z "$candidate" ]] || [[ "$URL_HOST_SEEN" == *"|$candidate|"* ]]; then + continue + fi + + # Restrict to simple host/domain tokens for safe /dev/tcp probing. + if [[ ! "$candidate" =~ ^[A-Za-z0-9._-]+$ ]]; then + continue + fi + + URL_HOST_CANDIDATES+=("$candidate") + URL_HOST_SEEN="${URL_HOST_SEEN}${candidate}|" + done + + SELECTED_URL_HOST="" + for candidate in "${URL_HOST_CANDIDATES[@]}"; do + if timeout 4 bash -c "exec 3<>/dev/tcp/$candidate/8443" >/dev/null 2>&1; then + SELECTED_URL_HOST="$candidate" + break + fi + done + + 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 + URL_HOST="$SELECTED_URL_HOST" + fi +fi + echo "INFO: Gateway URL host selected: $URL_HOST" >&2 URL="https://$URL_HOST:8443/access-db-$SLUG.php?token=$token" echo "$URL" \ No newline at end of file