Skip to content

Commit

Permalink
refactor: improve retrieval and handling of public IP address
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dgibbs64 committed Oct 7, 2023
1 parent e0b7739 commit 913622c
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions lgsm/modules/info_game.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2409,16 +2409,25 @@ if [ -f "${tmpdir}/publicip.txt" ]; then
fi

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=$?
# if curl passes add publicip to externalip.txt
# if curl passes add publicip to publicip.txt
if [ "${exitcode}" == "0" ]; then
echo "${publicip}" > "${tmpdir}/publicip.txt"
else
echo "Unable to get external IP address"
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
publicip="$(cat "${tmpdir}/publicip.txt")"
publicip="unknown"
country="unknown"
countrycode="unknown"
fi

# Alert IP address
Expand Down

0 comments on commit 913622c

Please sign in to comment.