From 913622c02aed86de19c78107fc7aba5b3f049e4d Mon Sep 17 00:00:00 2001 From: Daniel Gibbs Date: Sun, 8 Oct 2023 00:20:42 +0100 Subject: [PATCH] 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. --- lgsm/modules/info_game.sh | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/lgsm/modules/info_game.sh b/lgsm/modules/info_game.sh index 77251784c..3e143678e 100644 --- a/lgsm/modules/info_game.sh +++ b/lgsm/modules/info_game.sh @@ -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