-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #215 from Security-Tools-Alliance/improve-check-ex…
…ternal-ip feat: enhance IP retrieval with caching
- Loading branch information
Showing
1 changed file
with
20 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,29 @@ | ||
from . import settings | ||
import requests | ||
from django.core.cache import cache | ||
import logging | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
def version(request): | ||
return {"RENGINE_CURRENT_VERSION": settings.RENGINE_CURRENT_VERSION} | ||
|
||
def misc(request): | ||
externalIp = requests.get('https://checkip.amazonaws.com').text.strip() | ||
# Attempt to retrieve the external IP address from the cache | ||
external_ip = cache.get('external_ip') | ||
|
||
if external_ip is None: | ||
try: | ||
# If the IP address is not in the cache, make the request | ||
external_ip = requests.get('https://checkip.amazonaws.com').text.strip() | ||
# Cache the IP address for 1 hour (3600 seconds) | ||
cache.set('external_ip', external_ip, timeout=3600) | ||
except requests.RequestException as e: | ||
# Handle the exception if the request fails | ||
external_ip = 'Unable to retrieve IP' # Default value in case of error | ||
# You can also log the error if necessary | ||
logger.error(f"Error retrieving external IP: {e}") | ||
|
||
return { | ||
'external_ip': externalIp | ||
'external_ip': external_ip | ||
} |