feat: convert public ip to json (#4332)

* 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.

* refactor: update API URL and set default values for public IP, country, and country code

The commit refactors the code by updating the API URL to remove a trailing slash. Additionally, it sets default values for the variables `publicip`, `country`, and `countrycode` when the file `${tmpdir}/publicip.txt` is not found.

* 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>

* 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.

* 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.

* refactor(info_messages): Remove Mailgun alert

This commit refactors the `info_messages.sh` module by removing the Mailgun (email) alert functionality. The code changes remove the corresponding section that displays the Mailgun alert status in the info message script. This change simplifies and streamlines the code by eliminating unused functionality.

---------

Co-authored-by: AI Assistant <assistant@example.com>
This commit is contained in:
Daniel Gibbs 2023-10-08 20:08:44 +01:00 committed by GitHub
parent e0b77391bc
commit 08a8da26ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 17 deletions

View File

@ -2400,25 +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
publicip="$(curl --connect-timeout 10 -s https://api.ipify.org 2> /dev/null)"
# Public IP address
# Cache public IP address for 24 hours
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 externalip.txt
# if curl passes add publicip to publicip.json
if [ "${exitcode}" == "0" ]; then
echo "${publicip}" > "${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
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
else
publicip="$(cat "${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

View File

@ -506,7 +506,6 @@ fn_info_message_script() {
# Email alert: off
# Gotify alert: off
# IFTTT alert: off
# Mailgun (email) alert: off
# Pushbullet alert: off
# Pushover alert: off
# Rocketchat alert: off
@ -550,8 +549,6 @@ fn_info_message_script() {
echo -e "${lightblue}Gotify alert:\t${default}${gotifyalert}"
# IFTTT alert
echo -e "${lightblue}IFTTT alert:\t${default}${iftttalert}"
# Mailgun alert
echo -e "${lightblue}Mailgun (email) alert:\t${default}${mailgunalert}"
# Pushbullet alert
echo -e "${lightblue}Pushbullet alert:\t${default}${pushbulletalert}"
# Pushover alert