Compare commits

..

8 Commits

Author SHA1 Message Date
aclist
661329877f
Merge pull request #232 from GaryBlackbourne/fix-deadlock-on-ip-api-server
Some checks failed
Mirror to Codeberg / mirror-to-codeberg (push) Has been cancelled
Fix deadlock on ip api server
2025-10-25 11:30:05 +09:00
Gergely Koloszar
cc73f23c1c fix: suppress ping stderr output 2025-10-24 12:35:56 +00:00
aclist
cbc131028f
chore: capitalize TODO for syntax highlighting 2025-10-23 08:22:20 +09:00
Gergely Koloszar
239a82876e fix: silence ping while checking remote urls 2025-10-22 17:20:56 +02:00
aclist
6aa1ec4201 fix: early abort if local coords are missing 2025-10-17 19:12:59 +02:00
Gergely Koloszar
d6223e54b0 Fix unintended deletion 2025-10-17 19:12:59 +02:00
Gergely Koloszar
83552f833c Add availability checks for local_latlon function 2025-10-17 19:12:59 +02:00
Gergely Koloszar
f0657be289 Add availability checker for remote services based on ping and timeout 2025-10-17 19:12:59 +02:00
2 changed files with 34 additions and 3 deletions

View File

@ -493,20 +493,47 @@ stale_symlinks(){
unlink "$l" unlink "$l"
done done
} }
check_availability() {
if [[ -z $1 ]]; then
return 1
fi
local url=$1
local timeout_sec="3"
if [[ $2 ]]; then
timeout_sec=$2
fi
if ! ping -w "$timeout_sec" "$url" > /dev/null 2>&1; then
logger WARN "Failed to reach $url, service may be down."
return 1
fi
}
local_latlon(){ local_latlon(){
if [[ -z $(command -v dig) ]]; then if [[ -z $(command -v dig) ]]; then
local local_ip=$(curl -Ls "https://ipecho.net/plain") local url_ipecho="https://ipecho.net/plain"
if ! check_availability "ipecho.net"; then
logger WARN "Failed to get external ip address, ipecho.net service may be down."
return 1
fi
local local_ip=$(curl -Ls "$url_ipecho")
else else
# TODO : implement checking remote
local local_ip=$(dig -4 +short myip.opendns.com @resolver1.opendns.com) local local_ip=$(dig -4 +short myip.opendns.com @resolver1.opendns.com)
fi fi
local url="http://ip-api.com/json/$local_ip" local url_ip_api="http://ip-api.com/json/$local_ip"
local res=$(curl -Ls "$url" | jq -r '"\(.lat)\n\(.lon)"') if ! check_availability "ip-api.com"; then
logger WARN "Failed to get local coordinates, ip-api.com service may be down."
return 1
fi
local res=$(curl -Ls "$url_ip_api" | jq -r '"\(.lat)\n\(.lon)"')
if [[ -z "$res" ]]; then if [[ -z "$res" ]]; then
logger WARN "Failed to get local coordinates" logger WARN "Failed to get local coordinates"
return 1 return 1
fi fi
echo "$res" > "$coords_file" echo "$res" > "$coords_file"
} }
lock(){ lock(){
[[ ! -f $lock_file ]] && touch $lock_file [[ ! -f $lock_file ]] && touch $lock_file
local pid=$(cat $lock_file) local pid=$(cat $lock_file)

View File

@ -369,6 +369,10 @@ local_latlon(){
get_dist(){ get_dist(){
shift shift
local given_ip="$1" local given_ip="$1"
if [[ ! -f $_cache_coords ]]; then
printf "Unknown"
return
fi
readarray -t coords < "$_cache_coords" readarray -t coords < "$_cache_coords"
readarray -t n < <(<<< "$given_ip" awk 'BEGIN{RS="."}{$1=$1}1') readarray -t n < <(<<< "$given_ip" awk 'BEGIN{RS="."}{$1=$1}1')