Skip to content

Commit

Permalink
Add Server Model
Browse files Browse the repository at this point in the history
* Server page for showing IPv4 and IPv6 connectivity
* Management command `server_update`:
  Update DNS values of the pkg-fallout servers
  • Loading branch information
dbaio committed Oct 3, 2020
1 parent 3ca5b99 commit a712216
Show file tree
Hide file tree
Showing 10 changed files with 200 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,6 @@ Execution for keeping the database always updated:
30 10 * * * /portsfallout/scripts/cron-scrapy.sh today
30 18 * * * /portsfallout/scripts/cron-scrapy.sh today

# Update DNS values of the pkg-fallout servers
45 3 * * * python manage.py server_update

8 changes: 8 additions & 0 deletions changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
Changelog
=========

Unreleased
----------

* Add Server page for showing IPv4 and IPv6 connectivity
* Add management command `server_update`:
Update DNS values of the pkg-fallout servers


Version 1.4.0
-------------

Expand Down
91 changes: 91 additions & 0 deletions ports/management/commands/server_update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# Copyright (c) 2020 Danilo G. Baio <dbaio@bsd.com.br>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

from django.core.management.base import BaseCommand, CommandError
from ports.models import Server, Fallout

from django.utils import timezone as dtz

import parser
import dns.resolver


class Command(BaseCommand):
help = 'Update DNS values of the pkg-fallout servers'

def add_arguments(self, parser):

parser.add_argument('period',
nargs='?',
type=int,
help='Query entries from the last X days (default: 90)',)

def handle(self, *args, **options):

if not options['period']:
period = 90
else:
period = options['period']

period_date = dtz.make_aware(dtz.datetime.today() - dtz.timedelta(days=period))
Servers = Fallout.objects.filter(date__gte=period_date).values('server').distinct().order_by('server')

for srv in Servers:
if srv['server']:
self.stdout.write(f"{srv['server']}")

try:
dns_v4 = dns.resolver.query(srv['server'], 'A')
except:
dns_v4 = False

try:
dns_v6 = dns.resolver.query(srv['server'], 'AAAA')
except:
dns_v6 = False

if dns_v4:
self.stdout.write(self.style.SUCCESS(' Has IPv4 address'))
else:
self.stdout.write(self.style.ERROR(' IPv4 address not found'))

if dns_v6:
self.stdout.write(self.style.SUCCESS(' Has IPv6 address'))
else:
self.stdout.write(self.style.ERROR(' IPv6 address not found'))


try:
db_srv = Server.objects.get(name=srv['server'])
except:
db_srv = None

if db_srv:
if db_srv.v4 != bool(dns_v4) or db_srv.v6 != bool(dns_v6):
db_srv.v4 = bool(dns_v4)
db_srv.v6 = bool(dns_v6)
db_srv.save()
else:
db_srv = Server.objects.get_or_create(name=srv['server'],
v4 = bool(dns_v4),
v6 = bool(dns_v6))[0]
22 changes: 22 additions & 0 deletions ports/migrations/0002_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 3.1 on 2020-10-03 01:41

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('ports', '0001_initial'),
]

operations = [
migrations.CreateModel(
name='Server',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=48, unique=True)),
('v4', models.BooleanField(default=False)),
('v6', models.BooleanField(default=False)),
],
),
]
8 changes: 8 additions & 0 deletions ports/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,11 @@ def __str__(self):
# head-arm64-default | net/findomain
return self.env + " | " + self.port.origin


class Server(models.Model):
name = models.CharField(max_length=48, unique=True)
v4 = models.BooleanField(default=False)
v6 = models.BooleanField(default=False)

def __str__(self):
return self.name
3 changes: 3 additions & 0 deletions ports/templates/ports/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
<li class="nav-item {{ navbar_api }}">
<a class="nav-link" href="/api/"">Api</a>
</li>
<li class="nav-item {{ navbar_server }}">
<a class="nav-link" href="{% url 'ports:server' %}"">Servers</a>
</li>
<li class="nav-item {{ navbar_about }}">
<a class="nav-link" href="{% url 'ports:about' %}">About</a>
</li>
Expand Down
51 changes: 51 additions & 0 deletions ports/templates/ports/server_list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{% extends "ports/base.html" %}
{% block title %}Server list{% endblock %}
{% load bootstrap_pagination %}
{% block body_block %}

<br />

<h3>Server List</h3>

<table class="table table-sm table-striped">
<thead>
<tr>
<th scope="col">server</th>
<th scope="col">connectivity</th>
</tr>
</thead>
<tbody>
{% for server in server_list %}
<tr>
<td><small><a href="http://{{ server.name }}">{{ server.name }}</a></small></td>
<td><small>
{% if server.v4 %}
<span class="badge badge-success">IPv4</span>
{% else %}
<span class="badge badge-danger">IPv4</span>
{% endif %}

{% if server.v6 %}
<span class="badge badge-success">IPv6</span>
{% else %}
<span class="badge badge-danger">IPv6</span>
{% endif %}
</small>
</td>
</tr>
{% endfor %}
</tbody>
</table>

<div class="centerstage">
{% bootstrap_paginate page_obj range=10 show_prev_next="true" show_first_last="true" centered="true" extra_pagination_classes="justify-content-center" %}
</div>

<div class="row">
<div class="col-md-12">
<br />
<p>Some servers have only IPv6 connectivity.</p>
</div>
</div>

{% endblock %}
1 change: 1 addition & 0 deletions ports/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
path('fallout/<int:pk>/', views.FalloutDetailView.as_view(), name='fdetail'),
path('port', views.PortListView.as_view(), name='list'),
path('port/<int:pk>/', views.PortDetailView.as_view(), name='detail'),
path('server', views.ServerListView.as_view(), name='server'),
path('about', views.about, name='about'),
path('api/', include(router.urls)),
]
14 changes: 12 additions & 2 deletions ports/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from django.views.generic import View, TemplateView, ListView, DetailView
from django.db.models import Count, Q
from django.db.models.functions import TruncDay
from ports.models import Port, Category, Fallout
from ports.models import Port, Category, Fallout, Server
from ports.serializers import CategorySerializer, PortSerializer, FalloutSerializer
from ports.utils import IsRegex
from rest_framework import filters, viewsets
Expand Down Expand Up @@ -174,6 +174,17 @@ def get_context_data(self, **kwargs):
return context


class ServerListView(ListView):
paginate_by = 50
model = Server
ordering = ['name']

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['navbar_server'] = 'active'
return context


def about(request):
context_dict = {'navbar_about':'active'}
return render(request, 'ports/about.html', context_dict)
Expand Down Expand Up @@ -207,4 +218,3 @@ class FalloutViewSet(viewsets.ReadOnlyModelViewSet):
filter_backends = (filters.SearchFilter,)
queryset = Fallout.objects.all().order_by('-date')
serializer_class = FalloutSerializer

1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ requests
scrapy
djangorestframework
python-dateutil
dnspython

0 comments on commit a712216

Please sign in to comment.