@@ -2467,25 +2467,52 @@ fi
2467
2467
# Cache public IP address for 24 hours
2468
2468
if [ ! -f " ${tmpdir} /publicip.json" ] || [ " $( find " ${tmpdir} /publicip.json" -mmin +1440) " ]; then
2469
2469
apiurl=" http://ip-api.com/json"
2470
- publicipresponse=$( curl -s " ${apiurl} " )
2470
+ fn_script_log_info " Querying ${apiurl} for public IP address"
2471
+
2472
+ ipresponse=$( curl -s --max-time 3 " ${apiurl} " ) # Attempt to query ip-api.com with a 3 second timeout
2471
2473
exitcode=$?
2472
- # if curl passes add publicip to publicip.json
2473
- if [ " ${exitcode} " == " 0" ]; then
2474
- fn_script_log_pass " Getting public IP address"
2475
- echo " ${publicipresponse} " > " ${tmpdir} /publicip.json"
2476
- publicip=" $( jq -r ' .query' " ${tmpdir} /publicip.json" ) "
2477
- country=" $( jq -r ' .country' " ${tmpdir} /publicip.json" ) "
2478
- countrycode=" $( jq -r ' .countryCode' " ${tmpdir} /publicip.json" ) "
2474
+
2475
+ # Check if the first request was successfull
2476
+ if [ " ${exitcode} " -eq 0 ]; then
2477
+ fn_script_log_pass " Queried ${apiurl} for public IP address"
2478
+
2479
+ # Parse and reformat the response
2480
+ publicip=" $( echo " ${ipresponse} " | jq -r ' .query' ) "
2481
+ country=" $( echo " ${ipresponse} " | jq -r ' .country' ) "
2482
+ countrycode=" $( echo " ${ipresponse} " | jq -r ' .countryCode' ) "
2483
+ # Construct a universal JSON format
2484
+ echo " {\" ip\" :\" ${publicip} \" ,\" country\" :\" ${country} \" ,\" countryCode\" :\" ${countrycode} \" ,\" apiurl\" :\" ${apiurl} \" }" > " ${tmpdir} /publicip.json"
2479
2485
else
2480
- fn_script_log_warn " Unable to get public IP address"
2481
- publicip=" NOT SET"
2482
- country=" NOT SET"
2483
- countrycode=" NOT SET"
2486
+ # Fallback to myip.wtf if the initial request failed or timed out
2487
+ apiurl=" https://myip.wtf/json"
2488
+ fn_script_log_pass " Querying ${apiurl} for public IP address"
2489
+
2490
+ ipresponse=$( curl -s --max-time 3 " ${apiurl} " ) # Attempt to query myip.wtf with a 3 second timeout as a backup
2491
+ exitcode=$?
2492
+
2493
+ # Check if the backup request was successfull
2494
+ if [ " ${exitcode} " -eq 0 ]; then
2495
+ fn_script_log_pass " Queried ${apiurl} for public IP address"
2496
+
2497
+ # Parse and reformat the response from myip.wtf
2498
+ publicip=" $( echo " ${ipresponse} " | jq -r ' .YourFuckingIPAddress' ) "
2499
+ country=" $( echo " ${ipresponse} " | jq -r ' .YourFuckingCountry' ) "
2500
+ countrycode=" $( echo " ${ipresponse} " | jq -r ' .YourFuckingCountryCode' ) "
2501
+ # Construct a universal JSON format
2502
+ echo " {\" ip\" :\" ${publicip} \" ,\" country\" :\" ${country} \" ,\" countryCode\" :\" ${countrycode} \" ,\" apiurl\" :\" ${apiurl} \" }" > " ${tmpdir} /publicip.json"
2503
+ else
2504
+ fn_script_log_error " Unable to get public IP address"
2505
+ publicip=" NOT SET"
2506
+ country=" NOT SET"
2507
+ countrycode=" NOT SET"
2508
+ fi
2484
2509
fi
2485
2510
else
2486
- publicip=" $( jq -r ' .query' " ${tmpdir} /publicip.json" ) "
2487
- country=" $( jq -r ' .country' " ${tmpdir} /publicip.json" ) "
2488
- countrycode=" $( jq -r ' .countryCode' " ${tmpdir} /publicip.json" ) "
2511
+ # Cached IP is still valid
2512
+ fn_script_log_pass " Using cached IP as public IP address"
2513
+ publicip=" $( jq -r ' .ip' " ${tmpdir} /publicip.json" ) "
2514
+ country=" $( jq -r ' .country' " ${tmpdir} /publicip.json" ) "
2515
+ countrycode=" $( jq -r ' .countryCode' " ${tmpdir} /publicip.json" ) "
2489
2516
fi
2490
2517
2491
2518
# Alert IP address
0 commit comments