Browse Source

refactor: improve retrieval and handling of public IP address

The code in `info_game.sh` has been refactored to enhance the process of retrieving and handling the public IP address. The changes include:
- Using the API endpoint `http://ip-api.com/json/` instead of `https://api.ipify.org`
- Storing the retrieved data in `publicip.txt`
- Extracting additional information such as country and country code using `jq`

These improvements aim to provide more accurate and detailed information about the public IP address.
pull/4332/head
Daniel Gibbs 2 years ago
parent
commit
913622c02a
No known key found for this signature in database GPG Key ID: 2076B128385E8C55
  1. 15
      lgsm/modules/info_game.sh

15
lgsm/modules/info_game.sh

@ -2409,16 +2409,25 @@ if [ -f "${tmpdir}/publicip.txt" ]; then
fi fi
if [ ! -f "${tmpdir}/publicip.txt" ]; then if [ ! -f "${tmpdir}/publicip.txt" ]; then
publicip="$(curl --connect-timeout 10 -s https://api.ipify.org 2> /dev/null)" apiurl="http://ip-api.com/json/"
curl -s "${apiurl}" > "${tmpdir}/publicip.txt"
exitcode=$? exitcode=$?
# if curl passes add publicip to externalip.txt # if curl passes add publicip to publicip.txt
if [ "${exitcode}" == "0" ]; then if [ "${exitcode}" == "0" ]; then
echo "${publicip}" > "${tmpdir}/publicip.txt" echo "${publicip}" > "${tmpdir}/publicip.txt"
else else
echo "Unable to get external IP address" echo "Unable to get external IP address"
fi fi
fi
if [ -f "${tmpdir}/publicip.txt" ]; then
publicip="$(jq -r '.query' "${tmpdir}/publicip.txt")"
country="$(jq -r '.country' "${tmpdir}/publicip.txt")"
countrycode="$(jq -r '.countryCode' "${tmpdir}/publicip.txt")"
else else
publicip="$(cat "${tmpdir}/publicip.txt")" publicip="unknown"
country="unknown"
countrycode="unknown"
fi fi
# Alert IP address # Alert IP address

Loading…
Cancel
Save