Improve IP detection: fallback to curl and set missing IPs to None

This commit is contained in:
Whispering Wind
2025-03-08 20:38:25 +03:30
committed by GitHub
parent 7b9263e996
commit 77594a9ec1

View File

@ -75,25 +75,32 @@ load_hysteria2_env() {
load_hysteria2_ips() { load_hysteria2_ips() {
if [ -f "$CONFIG_ENV" ]; then if [ -f "$CONFIG_ENV" ]; then
export $(grep -v '^#' "$CONFIG_ENV" | xargs) export $(grep -v '^#' "$CONFIG_ENV" | xargs)
if [[ -z "$IP4" || -z "$IP6" ]]; then
echo "Warning: IP4 or IP6 is not set in configs.env. Fetching from system..."
default_interface=$(ip route | grep default | awk '{print $5}')
IP4=$(ip addr show "$default_interface" | grep "inet " | awk '{print $2}' | cut -d '/' -f 1 | head -n 1)
IP6=$(ip addr show "$default_interface" | grep "inet6 " | awk '{print $2}' | grep -v "^fe80::" | cut -d '/' -f 1 | head -n 1)
fi
else else
echo "Error: configs.env file not found. Fetching IPs from system..." echo "Error: $CONFIG_ENV file not found. Creating a new one..."
default_interface=$(ip route | grep default | awk '{print $5}')
IP4=$(ip addr show "$default_interface" | grep "inet " | awk '{print $2}' | cut -d '/' -f 1 | head -n 1)
IP6=$(ip addr show "$default_interface" | grep "inet6 " | awk '{print $2}' | grep -v "^fe80::" | cut -d '/' -f 1 | head -n 1)
fi fi
default_interface=$(ip route | grep default | awk '{print $5}')
IP4=$(ip addr show "$default_interface" | grep "inet " | awk '{print $2}' | cut -d '/' -f 1 | head -n 1)
if [[ -z "$IP4" ]]; then
echo "Warning: Could not fetch local IPv4. Trying external service..."
IP4=$(curl -s -4 ip.sb)
fi
[[ -z "$IP4" ]] && IP4="None" # Set to None if still empty
IP6=$(ip addr show "$default_interface" | grep "inet6 " | awk '{print $2}' | grep -v "^fe80::" | cut -d '/' -f 1 | head -n 1)
if [[ -z "$IP6" ]]; then
echo "Warning: Could not fetch local IPv6. Trying external service..."
IP6=$(curl -s -6 ip.sb)
fi
[[ -z "$IP6" ]] && IP6="None" # Set to None if still empty
echo "IP4=$IP4" > "$CONFIG_ENV" echo "IP4=$IP4" > "$CONFIG_ENV"
echo "IP6=$IP6" >> "$CONFIG_ENV" echo "IP6=$IP6" >> "$CONFIG_ENV"
} }
# check_services() { # check_services() {
# # source /etc/hysteria/core/scripts/services_status.sh # # source /etc/hysteria/core/scripts/services_status.sh
# for service in "${services[@]}"; do # for service in "${services[@]}"; do