Browse Source

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.
pull/4332/head
Daniel Gibbs 2 years ago
parent
commit
b2e6d07fa6
No known key found for this signature in database GPG Key ID: 2076B128385E8C55
  1. 29
      lgsm/modules/info_game.sh

29
lgsm/modules/info_game.sh

@ -2400,35 +2400,30 @@ 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"
else
echo "Unable to get external IP address"
fi
fi
if [ -f "${tmpdir}/publicip.txt" ]; 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")"
else
fn_script_log_warn "Unable to get public IP address"
publicip="NOT SET"
country="NOT SET"
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")"
fi
# Alert IP address
if [ "${displayip}" ]; then

Loading…
Cancel
Save