Fixed get public IP address

This commit is contained in:
Mikhail Grigorev 2025-07-07 17:20:21 +05:00
parent b7164805f8
commit 60540644fc
2 changed files with 24 additions and 12 deletions

View file

@ -7,6 +7,7 @@ yellow='\033[0;33m'
plain='\033[0m' plain='\033[0m'
cur_dir=$(pwd) cur_dir=$(pwd)
show_ip_service_lists=("https://api.ipify.org" "https://4.ident.me" "https://ifconfig.io/ip" "https://ipinfo.tw/ip")
# check root # check root
[[ $EUID -ne 0 ]] && echo -e "${red}Fatal error: ${plain} Please run this script with root privilege \n " && exit 1 [[ $EUID -ne 0 ]] && echo -e "${red}Fatal error: ${plain} Please run this script with root privilege \n " && exit 1
@ -85,10 +86,13 @@ config_after_install() {
local existing_hasDefaultCredential=$(/usr/local/x-ui/x-ui setting -show true | grep -Eo 'hasDefaultCredential: .+' | awk '{print $2}') 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_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_port=$(/usr/local/x-ui/x-ui setting -show true | grep -Eo 'port: .+' | awk '{print $2}')
local server_ip=$(curl -s --max-time 3 https://api.ipify.org)
if [ -z "$server_ip" ]; then for ip_service_addr in "${show_ip_service_lists[@]}"; do
server_ip=$(curl -s --max-time 3 https://4.ident.me) local server_ip=$(curl -s --max-time 3 ${ip_service_addr} 2>/dev/null)
if [ -n "${server_ip}" ]; then
break
fi fi
done
if [[ ${#existing_webBasePath} -lt 4 ]]; then if [[ ${#existing_webBasePath} -lt 4 ]]; then
if [[ "$existing_hasDefaultCredential" == "true" ]]; then if [[ "$existing_hasDefaultCredential" == "true" ]]; then

View file

@ -234,22 +234,30 @@ func (s *ServerService) GetStatus(lastStatus *Status) *Status {
} }
// IP fetching with caching // IP fetching with caching
showIp4ServiceLists := []string{"https://api.ipify.org", "https://4.ident.me", "https://ifconfig.io/ip", "https://ipinfo.tw/ip"}
showIp6ServiceLists := []string{"https://api6.ipify.org", "https://6.ident.me"}
if s.cachedIPv4 == "" { if s.cachedIPv4 == "" {
s.cachedIPv4 = getPublicIP("https://api.ipify.org") for _, ip4Service := range showIp4ServiceLists {
if s.cachedIPv4 == "N/A" { s.cachedIPv4 = getPublicIP(ip4Service)
s.cachedIPv4 = getPublicIP("https://4.ident.me") if s.cachedIPv4 != "N/A" {
break
}
} }
} }
if s.cachedIPv6 == "" && !s.noIPv6 { if s.cachedIPv6 == "" && !s.noIPv6 {
s.cachedIPv6 = getPublicIP("https://api6.ipify.org") for _, ip6Service := range showIp6ServiceLists {
if s.cachedIPv6 == "N/A" { s.cachedIPv6 = getPublicIP(ip6Service)
s.cachedIPv6 = getPublicIP("https://6.ident.me") if s.cachedIPv6 != "N/A" {
break
}
}
}
if s.cachedIPv6 == "N/A" { if s.cachedIPv6 == "N/A" {
s.noIPv6 = true s.noIPv6 = true
} }
}
}
status.PublicIP.IPv4 = s.cachedIPv4 status.PublicIP.IPv4 = s.cachedIPv4
status.PublicIP.IPv6 = s.cachedIPv6 status.PublicIP.IPv6 = s.cachedIPv6