From fb9a3ca65b65dd7169061cdb8ee9ecf92a8e50dd Mon Sep 17 00:00:00 2001 From: Dispatcharr Date: Thu, 6 Mar 2025 18:58:55 -0600 Subject: [PATCH] Added country flags Added Country codes next to public IP --- core/api_views.py | 39 +++++++++++++++++++++++++++--- frontend/src/components/Sidebar.js | 34 +++++++++++++++++++------- 2 files changed, 60 insertions(+), 13 deletions(-) diff --git a/core/api_views.py b/core/api_views.py index 842d650f..eab5f44e 100644 --- a/core/api_views.py +++ b/core/api_views.py @@ -8,6 +8,7 @@ from .serializers import UserAgentSerializer, StreamProfileSerializer, CoreSetti from rest_framework.permissions import IsAuthenticated from rest_framework.decorators import api_view, permission_classes from drf_yasg.utils import swagger_auto_schema +import socket import requests import os @@ -42,14 +43,44 @@ class CoreSettingsViewSet(viewsets.ModelViewSet): @permission_classes([IsAuthenticated]) def environment(request): public_ip = None + local_ip = None + country_code = None + country_name = None + + # 1) Get the public IP try: - response = requests.get("https://api64.ipify.org?format=json") - public_ip = response.json().get("ip") + r = requests.get("https://api64.ipify.org?format=json", timeout=5) + r.raise_for_status() + public_ip = r.json().get("ip") except requests.RequestException as e: - return f"Error: {e}" + public_ip = f"Error: {e}" + + # 2) Get the local IP + try: + s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + # connect to a “public” address so the OS can determine our local interface + s.connect(("8.8.8.8", 80)) + local_ip = s.getsockname()[0] + s.close() + except Exception as e: + local_ip = f"Error: {e}" + + # 3) If we got a valid public_ip, fetch geo info from ipapi.co + if public_ip and "Error" not in public_ip: + try: + geo = requests.get(f"https://ipapi.co/{public_ip}/json/", timeout=5).json() + # ipapi returns fields like country_code, country_name, etc. + country_code = geo.get("country_code", "") # e.g. "US" + country_name = geo.get("country_name", "") # e.g. "United States" + except requests.RequestException as e: + country_code = None + country_name = None return Response({ 'authenticated': True, 'public_ip': public_ip, - 'env_mode': "dev" if os.getenv('DISPATCHARR_ENV', None) == "dev" else "prod", + 'local_ip': local_ip, + 'country_code': country_code, + 'country_name': country_name, + 'env_mode': "dev" if os.getenv('DISPATCHARR_ENV') == "dev" else "prod", }) diff --git a/frontend/src/components/Sidebar.js b/frontend/src/components/Sidebar.js index 55cb2d43..5489311d 100644 --- a/frontend/src/components/Sidebar.js +++ b/frontend/src/components/Sidebar.js @@ -41,7 +41,7 @@ const Sidebar = ({ open, miniDrawerWidth, drawerWidth, toggleDrawer }) => { const location = useLocation(); const { isAuthenticated, logout } = useAuthStore(); const { - environment: { public_ip }, + environment: { public_ip, country_code, country_name }, } = useSettingsStore(); const navigate = useNavigate(); @@ -117,14 +117,30 @@ const Sidebar = ({ open, miniDrawerWidth, drawerWidth, toggleDrawer }) => { - + {open && ( + + {/* Public IP + optional flag */} + + + {/* If we have a country code, show a small flag */} + {country_code && ( + {country_name + )} + + + )} )}