check and warn before saving a network access setting that could block current client access

This commit is contained in:
dekzter 2025-06-10 08:46:36 -04:00
parent 789d29c97a
commit 82f35d2aef
5 changed files with 148 additions and 29 deletions

View file

@ -1,5 +1,7 @@
# core/api_views.py
import json
import ipaddress
from rest_framework import viewsets, status
from rest_framework.response import Response
from django.shortcuts import get_object_or_404
@ -9,7 +11,7 @@ from .serializers import (
StreamProfileSerializer,
CoreSettingsSerializer,
)
from rest_framework.decorators import api_view, permission_classes
from rest_framework.decorators import api_view, permission_classes, action
from drf_yasg.utils import swagger_auto_schema
import socket
import requests
@ -18,6 +20,7 @@ from core.tasks import rehash_streams
from apps.accounts.permissions import (
Authenticated,
)
from dispatcharr.utils import get_client_ip
class UserAgentViewSet(viewsets.ModelViewSet):
@ -56,6 +59,23 @@ class CoreSettingsViewSet(viewsets.ModelViewSet):
return response
@action(detail=False, methods=["post"], url_path="check")
def check(self, request, *args, **kwargs):
data = request.data
client_ip = ipaddress.ip_address(get_client_ip(request))
in_network = []
key = data.get("key")
value = json.loads(data.get("value", "{}"))
for key, val in value.items():
cidrs = val.split(",")
for cidr in cidrs:
network = ipaddress.ip_network(cidr)
if client_ip not in network:
in_network.append(cidr)
return Response(in_network, status=status.HTTP_200_OK)
@swagger_auto_schema(
method="get",