From ff56bab0eb5ff07ca5b0bd0fcb89afea1e78c1c3 Mon Sep 17 00:00:00 2001 From: Daniel Gibbs Date: Sun, 8 Oct 2023 19:57:21 +0100 Subject: [PATCH] refactor: improve caching and logging of public IP address The code has been refactored to use a JSON file instead of a text file for caching the public IP address. The log messages have also been updated to provide more informative output. --- lgsm/modules/info_game.sh | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/lgsm/modules/info_game.sh b/lgsm/modules/info_game.sh index a16bee3bdb..2d172cdd5a 100644 --- a/lgsm/modules/info_game.sh +++ b/lgsm/modules/info_game.sh @@ -2402,17 +2402,17 @@ fi # Public IP address # Cache public IP address for 24 hours -if [ ! -f "${tmpdir}/publicip.txt" ] || [ "$(find "${tmpdir}/publicip.txt" -mmin +1440)" ]; then +if [ ! -f "${tmpdir}/publicip.json" ] || [ "$(find "${tmpdir}/publicip.json" -mmin +1440)" ]; then apiurl="http://ip-api.com/json" publicipresponse=$(curl -s "${apiurl}") exitcode=$? - # if curl passes add publicip to publicip.txt + # if curl passes add publicip to publicip.json if [ "${exitcode}" == "0" ]; then - 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")" + fn_script_log_pass "Getting public IP address" + echo "${publicipresponse}" > "${tmpdir}/publicip.json" + publicip="$(jq -r '.query' "${tmpdir}/publicip.json")" + country="$(jq -r '.country' "${tmpdir}/publicip.json")" + countrycode="$(jq -r '.countryCode' "${tmpdir}/publicip.json")" else fn_script_log_warn "Unable to get public IP address" publicip="NOT SET" @@ -2420,9 +2420,9 @@ if [ ! -f "${tmpdir}/publicip.txt" ] || [ "$(find "${tmpdir}/publicip.txt" -mmin countrycode="NOT SET" fi else - publicip="$(jq -r '.query' "${tmpdir}/publicip.txt")" - country="$(jq -r '.country' "${tmpdir}/publicip.txt")" - countrycode="$(jq -r '.countryCode' "${tmpdir}/publicip.txt")" + publicip="$(jq -r '.query' "${tmpdir}/publicip.json")" + country="$(jq -r '.country' "${tmpdir}/publicip.json")" + countrycode="$(jq -r '.countryCode' "${tmpdir}/publicip.json")" fi # Alert IP address