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.

Co-authored-by: AI Assistant <assistant@example.com>
pull/4332/head
Daniel Gibbs 2 years ago
parent
commit
5029b3cd87
No known key found for this signature in database GPG Key ID: 2076B128385E8C55
  1. 33
      lgsm/modules/info_game.sh

33
lgsm/modules/info_game.sh

@ -2400,34 +2400,29 @@ elif [ "${engine}" == "unreal2" ]; then
fn_info_game_unreal2 fn_info_game_unreal2
fi fi
# External IP address # Public IP address
# Cache external IP address for 24 hours # Cache public IP address for 24 hours
if [ -f "${tmpdir}/publicip.txt" ]; then if [ ! -f "${tmpdir}/publicip.txt" ] || [ "$(find "${tmpdir}/publicip.txt" -mmin +1440)" ]; then
if [ "$(find "${tmpdir}/publicip.txt" -mmin +1440)" ]; then
rm -f "${tmpdir:?}/publicip.txt"
fi
fi
if [ ! -f "${tmpdir}/publicip.txt" ]; then
apiurl="http://ip-api.com/json" apiurl="http://ip-api.com/json"
curl -s "${apiurl}" > "${tmpdir}/publicip.txt" publicipresponse=$(curl -s "${apiurl}")
exitcode=$? exitcode=$?
# if curl passes add publicip to publicip.txt # if curl passes add publicip to publicip.txt
if [ "${exitcode}" == "0" ]; then 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 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
fi else
if [ -f "${tmpdir}/publicip.txt" ]; then
publicip="$(jq -r '.query' "${tmpdir}/publicip.txt")" publicip="$(jq -r '.query' "${tmpdir}/publicip.txt")"
country="$(jq -r '.country' "${tmpdir}/publicip.txt")" country="$(jq -r '.country' "${tmpdir}/publicip.txt")"
countrycode="$(jq -r '.countryCode' "${tmpdir}/publicip.txt")" countrycode="$(jq -r '.countryCode' "${tmpdir}/publicip.txt")"
else
publicip="NOT SET"
country="NOT SET"
countrycode="NOT SET"
fi fi
# Alert IP address # Alert IP address

Loading…
Cancel
Save