From bbdeb65291ff6b2f4e55fb2056928e2a23621bd7 Mon Sep 17 00:00:00 2001 From: mhsanaei Date: Sun, 6 Jul 2025 11:22:36 +0200 Subject: [PATCH] new alternative to get public IP address --- install.sh | 5 ++++- web/service/server.go | 34 ++++++++++++++++++++++++++++++---- x-ui.sh | 10 ++++++++-- 3 files changed, 42 insertions(+), 7 deletions(-) diff --git a/install.sh b/install.sh index 8fb23f3b..30a0f688 100644 --- a/install.sh +++ b/install.sh @@ -85,7 +85,10 @@ config_after_install() { local existing_hasDefaultCredential=$(/usr/local/x-ui/x-ui setting -show true | grep -Eo 'hasDefaultCredential: .+' | awk '{print $2}') local existing_webBasePath=$(/usr/local/x-ui/x-ui setting -show true | grep -Eo 'webBasePath: .+' | awk '{print $2}') local existing_port=$(/usr/local/x-ui/x-ui setting -show true | grep -Eo 'port: .+' | awk '{print $2}') - local server_ip=$(curl -s https://api.ipify.org) + local server_ip=$(curl -s --max-time 3 https://api.ipify.org) + if [ -z "$server_ip" ]; then + server_ip=$(curl -s --max-time 3 https://4.ident.me) + fi if [[ ${#existing_webBasePath} -lt 4 ]]; then if [[ "$existing_hasDefaultCredential" == "true" ]]; then diff --git a/web/service/server.go b/web/service/server.go index 1e29990f..8e0a8096 100644 --- a/web/service/server.go +++ b/web/service/server.go @@ -94,21 +94,34 @@ type ServerService struct { inboundService InboundService cachedIPv4 string cachedIPv6 string + noIPv6 bool } func getPublicIP(url string) string { - resp, err := http.Get(url) + client := &http.Client{ + Timeout: 3 * time.Second, + } + + resp, err := client.Get(url) if err != nil { return "N/A" } defer resp.Body.Close() + // Don't retry if access is blocked or region-restricted + if resp.StatusCode == http.StatusForbidden || resp.StatusCode == http.StatusUnavailableForLegalReasons { + return "N/A" + } + if resp.StatusCode != http.StatusOK { + return "N/A" + } + ip, err := io.ReadAll(resp.Body) if err != nil { return "N/A" } - ipString := string(ip) + ipString := strings.TrimSpace(string(ip)) if ipString == "" { return "N/A" } @@ -221,10 +234,23 @@ func (s *ServerService) GetStatus(lastStatus *Status) *Status { } // IP fetching with caching - if s.cachedIPv4 == "" || s.cachedIPv6 == "" { + if s.cachedIPv4 == "" { s.cachedIPv4 = getPublicIP("https://api.ipify.org") - s.cachedIPv6 = getPublicIP("https://api6.ipify.org") + if s.cachedIPv4 == "N/A" { + s.cachedIPv4 = getPublicIP("https://4.ident.me") + } } + + if s.cachedIPv6 == "" && !s.noIPv6 { + s.cachedIPv6 = getPublicIP("https://api6.ipify.org") + if s.cachedIPv6 == "N/A" { + s.cachedIPv6 = getPublicIP("https://6.ident.me") + if s.cachedIPv6 == "N/A" { + s.noIPv6 = true + } + } + } + status.PublicIP.IPv4 = s.cachedIPv4 status.PublicIP.IPv6 = s.cachedIPv6 diff --git a/x-ui.sh b/x-ui.sh index 84e1b612..1ce85f91 100644 --- a/x-ui.sh +++ b/x-ui.sh @@ -249,7 +249,10 @@ check_config() { local existing_webBasePath=$(echo "$info" | grep -Eo 'webBasePath: .+' | awk '{print $2}') local existing_port=$(echo "$info" | grep -Eo 'port: .+' | awk '{print $2}') local existing_cert=$(/usr/local/x-ui/x-ui setting -getCert true | grep -Eo 'cert: .+' | awk '{print $2}') - local server_ip=$(curl -s https://api.ipify.org) + local server_ip=$(curl -s --max-time 3 https://api.ipify.org) + if [ -z "$server_ip" ]; then + server_ip=$(curl -s --max-time 3 https://4.ident.me) + fi if [[ -n "$existing_cert" ]]; then local domain=$(basename "$(dirname "$existing_cert")") @@ -1630,7 +1633,10 @@ remove_iplimit() { } SSH_port_forwarding() { - local server_ip=$(curl -s https://api.ipify.org) + local server_ip=$(curl -s --max-time 3 https://api.ipify.org) + if [ -z "$server_ip" ]; then + server_ip=$(curl -s --max-time 3 https://4.ident.me) + fi local existing_webBasePath=$(/usr/local/x-ui/x-ui setting -show true | grep -Eo 'webBasePath: .+' | awk '{print $2}') local existing_port=$(/usr/local/x-ui/x-ui setting -show true | grep -Eo 'port: .+' | awk '{print $2}') local existing_listenIP=$(/usr/local/x-ui/x-ui setting -getListen true | grep -Eo 'listenIP: .+' | awk '{print $2}')