diff --git a/machines/serializers.py b/machines/serializers.py index 6adc22cc..34716cd8 100644 --- a/machines/serializers.py +++ b/machines/serializers.py @@ -206,3 +206,28 @@ class ServiceServersSerializer(serializers.ModelSerializer): def get_regen_status(self, obj): return obj.need_regen() +class OuverturePortsSerializer(serializers.Serializer): + ipv4 = serializers.SerializerMethodField() + ipv6 = serializers.SerializerMethodField() + + def get_ipv4(): + return {i.ipv4.ipv4: + { + "tcp_in":[j.tcp_ports_in() for j in i.port_lists.all()], + "tcp_out":[j.tcp_ports_out()for j in i.port_lists.all()], + "udp_in":[j.udp_ports_in() for j in i.port_lists.all()], + "udp_out":[j.udp_ports_out() for j in i.port_lists.all()], + } + for i in Interface.objects.all() if i.ipv4 + } + + def get_ipv6(): + return {i.ipv6: + { + "tcp_in":[j.tcp_ports_in() for j in i.port_lists.all()], + "tcp_out":[j.tcp_ports_out()for j in i.port_lists.all()], + "udp_in":[j.udp_ports_in() for j in i.port_lists.all()], + "udp_out":[j.udp_ports_out() for j in i.port_lists.all()], + } + for i in Interface.objects.all() if i.ipv6 + } diff --git a/machines/urls.py b/machines/urls.py index 62576a4e..e0ff1e77 100644 --- a/machines/urls.py +++ b/machines/urls.py @@ -92,6 +92,7 @@ urlpatterns = [ url(r'^rest/text/$', views.text, name='text'), url(r'^rest/zones/$', views.zones, name='zones'), url(r'^rest/service_servers/$', views.service_servers, name='service-servers'), + url(r'^rest/ouverture_ports/$', views.ouverture_ports, name='ouverture-ports'), url(r'index_portlist/$', views.index_portlist, name='index-portlist'), url(r'^edit_portlist/(?P[0-9]+)$', views.edit_portlist, name='edit-portlist'), url(r'^del_portlist/(?P[0-9]+)$', views.del_portlist, name='del-portlist'), diff --git a/machines/views.py b/machines/views.py index 4be86c03..b286db75 100644 --- a/machines/views.py +++ b/machines/views.py @@ -41,7 +41,7 @@ from django.contrib.auth import authenticate, login from django.views.decorators.csrf import csrf_exempt from rest_framework.renderers import JSONRenderer -from machines.serializers import FullInterfaceSerializer, InterfaceSerializer, TypeSerializer, DomainSerializer, TextSerializer, MxSerializer, ExtensionSerializer, ServiceServersSerializer, NsSerializer +from machines.serializers import FullInterfaceSerializer, InterfaceSerializer, TypeSerializer, DomainSerializer, TextSerializer, MxSerializer, ExtensionSerializer, ServiceServersSerializer, NsSerializer, OuverturePortsSerializer from reversion import revisions as reversion from reversion.models import Version @@ -1108,6 +1108,30 @@ def service_servers(request): @csrf_exempt @login_required @permission_required('serveur') +def ouverture_ports(request): + r = {'ipv4':{}, 'ipv6':{}} + for i in Interface.objects.all(): + if not i.may_have_port_open(): + continue + if i.ipv4: + r['ipv4'][i.ipv4.ipv4] = {"tcp_in":[],"tcp_out":[],"udp_in":[],"udp_out":[]} + if i.ipv6: + r['ipv6'][i.ipv6] = {"tcp_in":[],"tcp_out":[],"udp_in":[],"udp_out":[]} + for j in i.port_lists.all(): + if i.ipv4: + r['ipv4'][i.ipv4.ipv4]["tcp_in"].extend(j.tcp_ports_in()) + r['ipv4'][i.ipv4.ipv4]["tcp_out"].extend(j.tcp_ports_out()) + r['ipv4'][i.ipv4.ipv4]["udp_in"].extend(j.udp_ports_in()) + r['ipv4'][i.ipv4.ipv4]["udp_out"].extend(j.udp_ports_out()) + if i.ipv6: + r['ipv6'][i.ipv6]["tcp_in"].extend(j.tcp_ports_in()) + r['ipv6'][i.ipv6]["tcp_out"].extend(j.tcp_ports_out()) + r['ipv6'][i.ipv6]["udp_in"].extend(j.udp_ports_in()) + r['ipv6'][i.ipv6]["udp_out"].extend(j.udp_ports_out()) + return JSONResponse(r) +@csrf_exempt +@login_required +@permission_required('serveur') def regen_achieved(request): obj = Service_link.objects.filter(service__in=Service.objects.filter(service_type=request.POST['service']), server__in=Interface.objects.filter(domain__in=Domain.objects.filter(name=request.POST['server']))) if obj: