From f438a7dd507861250f1ad08538333821c4e86fe7 Mon Sep 17 00:00:00 2001 From: Nabi Date: Tue, 10 Mar 2026 13:24:45 +0330 Subject: [PATCH] feat #3910: enhance IP detection method to prioritize local interface and improve reliability --- x-ui.sh | 86 ++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 57 insertions(+), 29 deletions(-) diff --git a/x-ui.sh b/x-ui.sh index 2e555b25..239e8df5 100644 --- a/x-ui.sh +++ b/x-ui.sh @@ -50,6 +50,59 @@ is_domain() { [[ "$1" =~ ^([A-Za-z0-9](-*[A-Za-z0-9])*\.)+(xn--[a-z0-9]{2,}|[A-Za-z]{2,})$ ]] && return 0 || return 1 } +# Detect server IP - tries local interface first, then external services +# Usage: get_server_ip [interactive] +# - No argument: returns IP silently (for scripts) +# - "interactive": shows IP and allows user to correct it +# Sets global variable: DETECTED_SERVER_IP +get_server_ip() { + local interactive="${1:-}" + local ip="" + + # Method 1: Try to get IP from network interface (avoids proxy issues) + if command -v ip >/dev/null 2>&1; then + ip=$(ip -4 addr show scope global 2>/dev/null | grep -oP 'inet \K[\d.]+' | head -n1) + fi + + # Method 2: Fallback to external services (multiple for reliability) + if [ -z "$ip" ]; then + local URL_lists=( + "https://api.ipify.org" + "https://api4.ipify.org" + "https://ipv4.icanhazip.com" + "https://v4.api.ipinfo.io/ip" + "https://ipv4.myexternalip.com/raw" + "https://4.ident.me" + "https://check-host.net/ip" + ) + for url in "${URL_lists[@]}"; do + local response=$(curl -s -w "\n%{http_code}" --max-time 3 "${url}" 2>/dev/null) + local http_code=$(echo "$response" | tail -n1) + local ip_result=$(echo "$response" | head -n-1 | tr -d '[:space:]') + if [[ "${http_code}" == "200" && -n "${ip_result}" ]]; then + ip="${ip_result}" + break + fi + done + fi + + # Interactive mode: show IP and allow user to change + if [ "$interactive" = "interactive" ]; then + if [ -n "$ip" ]; then + echo -e "${green}[INF] Server IP detected: ${ip}${plain}" >&2 + else + echo -e "${yellow}Could not detect server IP automatically.${plain}" >&2 + fi + read -rp "Press Enter to use this IP, or type a different IP: " user_input /dev/null) - local http_code=$(echo "$response" | tail -n1) - local ip_result=$(echo "$response" | head -n-1 | tr -d '[:space:]') - if [[ "${http_code}" == "200" && -n "${ip_result}" ]]; then - server_ip="${ip_result}" - break - fi - done + local server_ip=$(get_server_ip) local existing_webBasePath=$(${xui_folder}/x-ui setting -show true | grep -Eo 'webBasePath: .+' | awk '{print $2}') local existing_port=$(${xui_folder}/x-ui setting -show true | grep -Eo 'port: .+' | awk '{print $2}')