def list_institution_tunnels_geojson(request, code=None): institution = get_object_for_view_or_404(Institution, request, code=code) tunnels = list_objects_for_view(Tunnel, request, endpoint1__host__autonomous_system__institution=institution).filter(endpoint2__host__autonomous_system__institution=institution).select_related( 'endpoint1__host__autonomous_system', 'endpoint2__host__autonomous_system', ) return JsonResponse(geojson_from_tunnels(tunnels))
def show_institution(request, code=None): institution = get_object_for_view_or_404(Institution, request, code=code) autonomous_systems = list_objects_for_view(AutonomousSystem, request, institution=institution) ipv4_subnets = list_objects_for_view(IPv4Subnet, request, institution=institution) tunnels = list_objects_for_view(Tunnel, request, Q(endpoint1__host__autonomous_system__institution=institution) | Q(endpoint2__host__autonomous_system__institution=institution)).select_related( 'endpoint1__host__autonomous_system', 'endpoint2__host__autonomous_system', 'endpoint1__host__autonomous_system__institution', 'endpoint2__host__autonomous_system__institution', ) show_map = list_objects_for_view(AutonomousSystem, request, institution=institution).exclude(location_lat__isnull=True).exclude(location_lng__isnull=True).exists() for tunnel in tunnels: for i, endpoint in enumerate([tunnel.endpoint1, tunnel.endpoint2]): endpoint.autonomous_system.show_link = endpoint.autonomous_system.can_view(request.user) return render(request, 'institutions_details.html', { 'header_active': 'institutions', 'institution': institution, 'autonomous_systems': autonomous_systems, 'ipv4_subnets': ipv4_subnets, 'tunnels': tunnels, 'can_edit': institution.can_edit(request.user), 'show_map': show_map, })
def show_autonomous_system_geojson(request, as_number=None): autonomous_system = get_object_for_view_or_404( AutonomousSystem, request, select_related=['institution'], as_number=as_number) return JsonResponse(geojson_from_autonomous_systems([autonomous_system]))
def show_autonomous_system_web(request, as_number=None): autonomous_system = get_object_for_view_or_404( AutonomousSystem, request, select_related=['institution'], as_number=as_number) hosts = list_objects_for_view(Host, request, autonomous_system=autonomous_system) tunnels = list_objects_for_view( Tunnel, request, Q(endpoint1__host__autonomous_system__as_number=as_number) | Q(endpoint2__host__autonomous_system__as_number=as_number) ).select_related( 'endpoint1__host__autonomous_system', 'endpoint2__host__autonomous_system', ) for tunnel in tunnels: if tunnel.endpoint1.autonomous_system.as_number == int(as_number): tunnel.peer_endpoint = tunnel.endpoint2 else: tunnel.peer_endpoint = tunnel.endpoint1 return render( request, 'autonomous_systems_details.html', { 'header_active': 'autonomous_systems', 'autonomous_system': autonomous_system, 'hosts': hosts, 'tunnels': tunnels, 'can_edit': autonomous_system.can_edit(request.user), })
def show_institution(request, code=None): institution = get_object_for_view_or_404(Institution, request, code=code) autonomous_systems = list_objects_for_view(AutonomousSystem, request, institution=institution) ipv4_subnets = list_objects_for_view(IPv4Subnet, request, institution=institution) tunnels = list_objects_for_view(Tunnel, request, Q(endpoint1__host__autonomous_system__institution=institution) | Q(endpoint2__host__autonomous_system__institution=institution)).select_related( 'endpoint1__host__autonomous_system', 'endpoint2__host__autonomous_system', 'endpoint1__host__autonomous_system__institution', 'endpoint2__host__autonomous_system__institution', ) show_map = list_objects_for_view(AutonomousSystem, request, institution=institution).exclude(location_lat__isnull=True).exclude(location_lng__isnull=True).exists() for tunnel in tunnels: for i, endpoint in enumerate([tunnel.endpoint1, tunnel.endpoint2]): endpoint.autonomous_system.show_link = endpoint.autonomous_system.can_view(request.user) return render(request, 'institutions_details.html', { 'header_active': 'institutions', 'institution': institution, 'autonomous_systems': autonomous_systems, 'ipv4_subnets': ipv4_subnets, 'tunnels': tunnels, 'can_edit': institution.can_edit(request.user), 'show_map': show_map, })
def show_tunnel_web(request, as_number1=None, as_number2=None): # FIXME: with_subclasses breaks select_related. tunnel = get_object_for_view_or_404( Tunnel, request, with_subclasses=True, endpoint1__host__autonomous_system__as_number=as_number1, endpoint2__host__autonomous_system__as_number=as_number2) if tunnel.supports_config_generation() and tunnel.is_config_complete(): for i, endpoint in enumerate( [tunnel.real_endpoint1, tunnel.real_endpoint2]): endpoint.config_generation_url = tunnel.get_config_generation_url( i + 1) for i, endpoint in enumerate( [tunnel.real_endpoint1, tunnel.real_endpoint2]): endpoint.autonomous_system.show_link = endpoint.autonomous_system.can_view( request.user) return render( request, 'tunnels_details.html', { 'header_active': 'tunnels', 'tunnel': tunnel, 'endpoints': [tunnel.real_endpoint1, tunnel.real_endpoint2], 'can_edit': tunnel.can_edit(request.user), })
def list_institution_tunnels_geojson(request, code=None): institution = get_object_for_view_or_404(Institution, request, code=code) tunnels = list_objects_for_view(Tunnel, request, endpoint1__host__autonomous_system__institution=institution).filter(endpoint2__host__autonomous_system__institution=institution).select_related( 'endpoint1__host__autonomous_system', 'endpoint2__host__autonomous_system', ) return JsonResponse(geojson_from_tunnels(tunnels))
def retrieve(self, request, pk=None, format=None): host = get_object_for_view_or_404(Host, request, fqdn=pk) fastd_tunnels = list_objects_for_view(FastdTunnel, request, Q(endpoint1__host=host) | Q(endpoint2__host=host)).select_related( 'endpoint1', 'endpoint2', 'endpoint1__fastdtunnelendpoint', 'endpoint2__fastdtunnelendpoint', 'endpoint1__fastdtunnelendpoint__host', 'endpoint2__fastdtunnelendpoint__host', 'endpoint1__fastdtunnelendpoint__tunnel1', 'endpoint2__fastdtunnelendpoint__tunnel1', 'endpoint1__fastdtunnelendpoint__tunnel2', 'endpoint2__fastdtunnelendpoint__tunnel2', ) serialized_fastd_tunnels = [self.serialize_tunnel(FastdTunnelSerializer, FastdTunnelEndpointSerializer, t, host) for t in fastd_tunnels] vtun_tunnels = list_objects_for_view(VtunTunnel, request, Q(endpoint1__host=host) | Q(endpoint2__host=host)).select_related( 'endpoint1', 'endpoint2', 'endpoint1__vtuntunnelendpoint', 'endpoint2__vtuntunnelendpoint', 'endpoint1__vtuntunnelendpoint__host', 'endpoint2__vtuntunnelendpoint__host', 'endpoint1__vtuntunnelendpoint__tunnel1', 'endpoint2__vtuntunnelendpoint__tunnel1', 'endpoint1__vtuntunnelendpoint__tunnel2', 'endpoint2__vtuntunnelendpoint__tunnel2', ) serialized_vtun_tunnels = [self.serialize_tunnel(VtunTunnelSerializer, VtunTunnelEndpointSerializer, t, host) for t in vtun_tunnels] peerings = list_objects_for_view(Peering, request, Q(tunnelpeering__tunnel__endpoint1__host=host) | Q(tunnelpeering__tunnel__endpoint2__host=host), with_subclasses=True) serialized_peerings = [self.serialize_peering(p, host) for p in peerings] institutions = [(p.host1.institution, p.host2.institution) for p in peerings] institutions = set([c for t in institutions for c in t]) ipv4_subnets = list_objects_for_view(IPv4Subnet, request, institution__in=institutions).select_related('institution') ipv4_networks = defaultdict(list) for subnet in ipv4_subnets: ipv4_networks[subnet.institution.code].append(subnet.network) serialized_ipv4_subnets = {i: [str(n) for n in reduce_networks(ipv4_networks[i])] for i in ipv4_networks} return Response({ 'lana': { 'subnets': { 'ipv4': serialized_ipv4_subnets, }, 'peerings': serialized_peerings, 'tunnels': { 'fastd': serialized_fastd_tunnels, 'vtun': serialized_vtun_tunnels, } }, })
def show_host(request, fqdn): host = get_object_for_view_or_404(Host, request, select_related=[ 'autonomous_system', 'autonomous_system__institution', ], fqdn=fqdn) return render(request, 'hosts_details.html', { 'header_active': 'autonomous_systems', 'host': host, 'can_edit': host.autonomous_system.can_edit(request.user), })
def show_tunnel_geojson(request, as_number1=None, as_number2=None): tunnel = get_object_for_view_or_404( Tunnel, request, select_related=[ 'endpoint1__host__autonomous_system', 'endpoint2__host__autonomous_system', ], endpoint1__host__autonomous_system__as_number=as_number1, endpoint2__host__autonomous_system__as_number=as_number2) return JsonResponse(geojson_from_tunnels([tunnel]))
def show_peering(request, as_number1=None, as_number2=None): # FIXME: with_subclasses breaks select_related. peering = get_object_for_view_or_404(Peering, request, with_subclasses=True, tunnelpeering__tunnel__endpoint1__host__autonomous_system__as_number=as_number1, tunnelpeering__tunnel__endpoint2__host__autonomous_system__as_number=as_number2) for i, host in enumerate([peering.host1, peering.host2]): host.autonomous_system.show_link = host.autonomous_system.can_view(request.user) return render(request, 'peerings_details.html', { 'header_active': 'tunnels', 'peering': peering, 'hosts': [peering.host1, peering.host2], 'can_edit': peering.can_edit(request.user), })
def show_ipv4(request, network): try: subnet = get_object_for_view_or_404(IPv4Subnet, request, select_related=['institution'], network=network) except ValidationError: raise Http404 hosts = list_objects_for_view(Host, request, Q(internal_ipv4__net_contained_or_equal=subnet.network) | Q(tunnel_ipv4__net_contained_or_equal=subnet.network)) return render(request, 'ipv4_details.html', { 'header_active': 'ipv4', 'subnet': subnet, 'hosts': hosts, 'can_edit': subnet.can_edit(request.user), })
def show_host(request, fqdn): host = get_object_for_view_or_404(Host, request, select_related=[ 'autonomous_system', 'autonomous_system__institution', ], fqdn=fqdn) return render( request, 'hosts_details.html', { 'header_active': 'autonomous_systems', 'host': host, 'can_edit': host.autonomous_system.can_edit(request.user), })
def generate_fastd(request, as_number1, as_number2, endpoint_number): tunnel = get_object_for_view_or_404( Tunnel, request, with_subclasses=True, endpoint1__host__autonomous_system__as_number=as_number1, endpoint2__host__autonomous_system__as_number=as_number2) if not isinstance(tunnel, FastdTunnel) or not tunnel.is_config_complete(): raise Http404 config = None if request.method == "POST": form = FastdGeneratorForm(data=request.POST) if form.is_valid(): local_endpoint = tunnel.real_endpoint1 remote_endpoint = tunnel.real_endpoint2 if int(endpoint_number) == 2: local_endpoint, remote_endpoint = remote_endpoint, local_endpoint config = render_to_string( 'fastd.conf', { 'tunnel': tunnel, 'tunnel_name': form.cleaned_data['tunnel_name'], 'local_endpoint': local_endpoint, 'remote_endpoint': remote_endpoint, 'remote_host': remote_endpoint.host.external_hostname or remote_endpoint.host.external_ipv4, }) else: form = FastdGeneratorForm() form.helper = FormHelper() form.helper.form_class = 'form-horizontal' form.helper.label_class = 'col-xs-4 col-md-3 col-lg-2' form.helper.field_class = 'col-xs-8 col-md-6 col-lg-5 col-xl-4' form.helper.html5_required = True form.helper.add_input(Submit("submit", "Generate")) return render(request, 'generate_fastd.html', { 'tunnel': tunnel, 'form': form, 'config': config, })
def show_tunnel_web(request, as_number1=None, as_number2=None): # FIXME: with_subclasses breaks select_related. tunnel = get_object_for_view_or_404(Tunnel, request, with_subclasses=True, endpoint1__host__autonomous_system__as_number=as_number1, endpoint2__host__autonomous_system__as_number=as_number2) if tunnel.supports_config_generation() and tunnel.is_config_complete(): for i, endpoint in enumerate([tunnel.real_endpoint1, tunnel.real_endpoint2]): endpoint.config_generation_url = tunnel.get_config_generation_url(i + 1) for i, endpoint in enumerate([tunnel.real_endpoint1, tunnel.real_endpoint2]): endpoint.autonomous_system.show_link = endpoint.autonomous_system.can_view(request.user) return render(request, 'tunnels_details.html', { 'header_active': 'tunnels', 'tunnel': tunnel, 'endpoints': [tunnel.real_endpoint1, tunnel.real_endpoint2], 'can_edit': tunnel.can_edit(request.user), })
def show_ipv4(request, network): try: subnet = get_object_for_view_or_404(IPv4Subnet, request, select_related=['institution'], network=network) except ValidationError: raise Http404 hosts = list_objects_for_view( Host, request, Q(internal_ipv4__net_contained_or_equal=subnet.network) | Q(tunnel_ipv4__net_contained_or_equal=subnet.network)) return render( request, 'ipv4_details.html', { 'header_active': 'ipv4', 'subnet': subnet, 'hosts': hosts, 'can_edit': subnet.can_edit(request.user), })
def show_autonomous_system_web(request, as_number=None): autonomous_system = get_object_for_view_or_404(AutonomousSystem, request, select_related=['institution'], as_number=as_number) hosts = list_objects_for_view(Host, request, autonomous_system=autonomous_system) tunnels = list_objects_for_view(Tunnel, request, Q(endpoint1__host__autonomous_system__as_number=as_number) | Q(endpoint2__host__autonomous_system__as_number=as_number)).select_related( 'endpoint1__host__autonomous_system', 'endpoint2__host__autonomous_system', ) for tunnel in tunnels: if tunnel.endpoint1.autonomous_system.as_number == int(as_number): tunnel.peer_endpoint = tunnel.endpoint2 else: tunnel.peer_endpoint = tunnel.endpoint1 return render(request, 'autonomous_systems_details.html', { 'header_active': 'autonomous_systems', 'autonomous_system': autonomous_system, 'hosts': hosts, 'tunnels': tunnels, 'can_edit': autonomous_system.can_edit(request.user), })
def show_peering(request, as_number1=None, as_number2=None): # FIXME: with_subclasses breaks select_related. peering = get_object_for_view_or_404( Peering, request, with_subclasses=True, tunnelpeering__tunnel__endpoint1__host__autonomous_system__as_number= as_number1, tunnelpeering__tunnel__endpoint2__host__autonomous_system__as_number= as_number2) for i, host in enumerate([peering.host1, peering.host2]): host.autonomous_system.show_link = host.autonomous_system.can_view( request.user) return render( request, 'peerings_details.html', { 'header_active': 'tunnels', 'peering': peering, 'hosts': [peering.host1, peering.host2], 'can_edit': peering.can_edit(request.user), })
def show_autonomous_system_geojson(request, as_number=None): autonomous_system = get_object_for_view_or_404(AutonomousSystem, request, select_related=['institution'], as_number=as_number) return JsonResponse(geojson_from_autonomous_systems([autonomous_system]))
def list_institution_autonomous_systems_geojson(request, code=None): institution = get_object_for_view_or_404(Institution, request, code=code) autonomous_systems = list_objects_for_view(AutonomousSystem, request, institution=institution).select_related('institution') return JsonResponse(geojson_from_autonomous_systems(autonomous_systems))
def retrieve(self, request, pk=None, format=None): host = get_object_for_view_or_404(Host, request, fqdn=pk) fastd_tunnels = list_objects_for_view(FastdTunnel, request, Q(endpoint1__host=host) | Q(endpoint2__host=host)).select_related( 'endpoint1', 'endpoint2', 'endpoint1__fastdtunnelendpoint', 'endpoint2__fastdtunnelendpoint', 'endpoint1__fastdtunnelendpoint__host', 'endpoint2__fastdtunnelendpoint__host', 'endpoint1__fastdtunnelendpoint__tunnel1', 'endpoint2__fastdtunnelendpoint__tunnel1', 'endpoint1__fastdtunnelendpoint__tunnel2', 'endpoint2__fastdtunnelendpoint__tunnel2', ) serialized_fastd_tunnels = [self.serialize_tunnel(FastdTunnelSerializer, FastdTunnelEndpointSerializer, t, host) for t in fastd_tunnels] vtun_tunnels = list_objects_for_view(VtunTunnel, request, Q(endpoint1__host=host) | Q(endpoint2__host=host)).select_related( 'endpoint1', 'endpoint2', 'endpoint1__vtuntunnelendpoint', 'endpoint2__vtuntunnelendpoint', 'endpoint1__vtuntunnelendpoint__host', 'endpoint2__vtuntunnelendpoint__host', 'endpoint1__vtuntunnelendpoint__tunnel1', 'endpoint2__vtuntunnelendpoint__tunnel1', 'endpoint1__vtuntunnelendpoint__tunnel2', 'endpoint2__vtuntunnelendpoint__tunnel2', ) serialized_vtun_tunnels = [self.serialize_tunnel(VtunTunnelSerializer, VtunTunnelEndpointSerializer, t, host) for t in vtun_tunnels] wireguard_tunnels = list_objects_for_view(WireGuardTunnel, request, Q(endpoint1__host=host) | Q(endpoint2__host=host)).select_related( 'endpoint1', 'endpoint2', 'endpoint1__wireguardtunnelendpoint', 'endpoint2__wireguardtunnelendpoint', 'endpoint1__wireguardtunnelendpoint__host', 'endpoint2__wireguardtunnelendpoint__host', 'endpoint1__wireguardtunnelendpoint__tunnel1', 'endpoint2__wireguardtunnelendpoint__tunnel1', 'endpoint1__wireguardtunnelendpoint__tunnel2', 'endpoint2__wireguardtunnelendpoint__tunnel2', ) serialized_wireguard_tunnels = [self.serialize_tunnel(WireGuardTunnelSerializer, WireGuardTunnelEndpointSerializer, t, host) for t in wireguard_tunnels] peerings = list_objects_for_view(Peering, request, Q(tunnelpeering__tunnel__endpoint1__host=host) | Q(tunnelpeering__tunnel__endpoint2__host=host), with_subclasses=True) serialized_peerings = [self.serialize_peering(p, host) for p in peerings] institutions = [(p.host1.institution, p.host2.institution) for p in peerings] institutions = set([c for t in institutions for c in t]) ipv4_subnets = list_objects_for_view(IPv4Subnet, request, institution__in=institutions).select_related('institution') ipv4_networks = defaultdict(list) for subnet in ipv4_subnets: ipv4_networks[subnet.institution.code].append(subnet.network) serialized_ipv4_subnets = {i: [str(n) for n in reduce_networks(ipv4_networks[i])] for i in ipv4_networks} return Response({ 'lana': { 'subnets': { 'ipv4': serialized_ipv4_subnets, }, 'peerings': serialized_peerings, 'tunnels': { 'fastd': serialized_fastd_tunnels, 'vtun': serialized_vtun_tunnels, 'wireguard': serialized_wireguard_tunnels, } }, })
def list_institution_autonomous_systems_geojson(request, code=None): institution = get_object_for_view_or_404(Institution, request, code=code) autonomous_systems = list_objects_for_view(AutonomousSystem, request, institution=institution).select_related('institution') return JsonResponse(geojson_from_autonomous_systems(autonomous_systems))
def list_tunnel_autonomous_systems_geojson(request, as_number1=None, as_number2=None): tunnel = get_object_for_view_or_404(Tunnel, request, select_related=[ 'endpoint1__host__autonomous_system', 'endpoint2__host__autonomous_system', ], endpoint1__host__autonomous_system__as_number=as_number1, endpoint2__host__autonomous_system__as_number=as_number2) return JsonResponse(geojson_from_autonomous_systems([tunnel.endpoint1.host.autonomous_system, tunnel.endpoint2.host.autonomous_system]))