From b2e6d07fa61b5dec2baacbe8a4836620f309b6a9 Mon Sep 17 00:00:00 2001 From: Daniel Gibbs Date: Sun, 8 Oct 2023 17:49:44 +0100 Subject: [PATCH] feat: cache and retrieve public IP address This commit adds functionality to cache the public IP address for 24 hours. If the cached IP address is older than 24 hours or doesn't exist, a new request is made to retrieve the public IP address. The retrieved IP address is then stored in a file for future use. Additionally, if there are any errors during the retrieval process, appropriate warning messages are logged and default values are set for the IP address, country, and country code. --- lgsm/modules/info_game.sh | 33 ++++++++++++++------------------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/lgsm/modules/info_game.sh b/lgsm/modules/info_game.sh index 709995ba4..a16bee3bd 100644 --- a/lgsm/modules/info_game.sh +++ b/lgsm/modules/info_game.sh @@ -2400,34 +2400,29 @@ elif [ "${engine}" == "unreal2" ]; then fn_info_game_unreal2 fi -# External IP address -# Cache external IP address for 24 hours -if [ -f "${tmpdir}/publicip.txt" ]; then - if [ "$(find "${tmpdir}/publicip.txt" -mmin +1440)" ]; then - rm -f "${tmpdir:?}/publicip.txt" - fi -fi - -if [ ! -f "${tmpdir}/publicip.txt" ]; then +# Public IP address +# Cache public IP address for 24 hours +if [ ! -f "${tmpdir}/publicip.txt" ] || [ "$(find "${tmpdir}/publicip.txt" -mmin +1440)" ]; then apiurl="http://ip-api.com/json" - curl -s "${apiurl}" > "${tmpdir}/publicip.txt" + publicipresponse=$(curl -s "${apiurl}") exitcode=$? # if curl passes add publicip to publicip.txt if [ "${exitcode}" == "0" ]; then - echo "${publicip}" > "${tmpdir}/publicip.txt" + fn_script_pass "Getting public IP address" + echo "${publicipresponse}" > "${tmpdir}/publicip.txt" + publicip="$(jq -r '.query' "${tmpdir}/publicip.txt")" + country="$(jq -r '.country' "${tmpdir}/publicip.txt")" + countrycode="$(jq -r '.countryCode' "${tmpdir}/publicip.txt")" else - echo "Unable to get external IP address" + fn_script_log_warn "Unable to get public IP address" + publicip="NOT SET" + country="NOT SET" + countrycode="NOT SET" fi -fi - -if [ -f "${tmpdir}/publicip.txt" ]; then +else publicip="$(jq -r '.query' "${tmpdir}/publicip.txt")" country="$(jq -r '.country' "${tmpdir}/publicip.txt")" countrycode="$(jq -r '.countryCode' "${tmpdir}/publicip.txt")" -else - publicip="NOT SET" - country="NOT SET" - countrycode="NOT SET" fi # Alert IP address