Added country flags

Added Country codes next to public IP
This commit is contained in:
Dispatcharr 2025-03-06 18:58:55 -06:00
parent e305f1cba0
commit fb9a3ca65b
2 changed files with 60 additions and 13 deletions

View file

@ -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",
})

View file

@ -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 }) => {
</ListItemButton>
</ListItem>
</List>
<TextField
size="small"
fullWidth
label="Public IP"
value={public_ip || ''}
disabled
sx={{ p: 1 }}
/>
{open && (
<Box sx={{ display: 'flex', flexDirection: 'column', gap: 1, mt: 1 }}>
{/* Public IP + optional flag */}
<Box sx={{ display: 'flex', alignItems: 'center', gap: 1 }}>
<TextField
size="small"
label="Public IP"
value={public_ip || ''}
disabled
variant="outlined"
sx={{ flex: 1 }}
/>
{/* If we have a country code, show a small flag */}
{country_code && (
<img
src={`https://flagcdn.com/16x12/${country_code.toLowerCase()}.png`}
alt={country_name || country_code}
title={country_name || country_code}
style={{ border: '1px solid #ccc', borderRadius: 2 }}
/>
)}
</Box>
</Box>
)}
</Box>
)}
</Drawer>