More validation, started search window

This commit is contained in:
Sebastian Lohff 2017-03-27 12:18:02 +02:00
parent eb722f950b
commit 33de8402b7
6 changed files with 68 additions and 2 deletions

View File

@ -8,7 +8,17 @@
<div class="panel panel-default">
<div class="panel-heading">Welcome!</div>
<div class="panel-body">
<p>
Here you can manage your darknet resources!
</p>
<p>
<form class="form navbar-form" method="post" action="{% url "whoisdb:search" %}">
{% csrf_token %}
<input class="textinput form-control" type="text" name="q" placeholder="Search in DB">
<button class="btn btn-primary">Search</button>
</form>
</p>
</div>
</div>
</div>

View File

@ -179,7 +179,6 @@ class InetNumForm(MntFormMixin, WhoisObjectFormMixin, forms.ModelForm):
prefix = cleaned_data['prefix']
parent = cleaned_data['parent_range']
if parent:
parentNet = parent.getNetwork()
if cleaned_data['protocol'] != parent.protocol:
@ -189,6 +188,7 @@ class InetNumForm(MntFormMixin, WhoisObjectFormMixin, forms.ModelForm):
if prefix.network_address not in parentNet or prefix.prefixlen < parentNet.prefixlen:
raise forms.ValidationError("Prefix must be inside parent network range")
# check if parent block has net that overlaps with us
for otherNet in parent.inetnum_set.all():
if self.instance and self.instance.pk == otherNet.pk:
@ -197,6 +197,12 @@ class InetNumForm(MntFormMixin, WhoisObjectFormMixin, forms.ModelForm):
if otherNet.getNetwork().overlaps(prefix):
raise forms.ValidationError("The given prefix overlaps with network %s" % otherNet.handle)
# check if subnets to this subnet are (still) in current network
if not self._create:
for subnet in self.instance.inetnum_set.all():
if subnet.getNetwork().network_address not in self.instance.getNetwork():
raise forms.ValidationError("Subnet %s with %s is not in block anymore" % (subnet, subnet.getNetwork()))
self.instance.address = str(prefix.network_address)
self.instance.netmask = prefix.prefixlen
@ -272,6 +278,18 @@ class ASBlockForm(MntFormMixin, WhoisObjectFormMixin, forms.ModelForm):
asBegin <= block.asBegin <= asEnd or asBegin <= block.asEnd <= asEnd:
raise forms.ValidationError("Block overlaps with block %s" % block.handle)
if not self._create:
# check if subblocks are still in range
for subblock in self.instance.asblock_set.all():
if not (asBegin <= subblock.asBegin <= asEnd and asBegin <= subblock.asEnd <= asEnd):
raise forms.ValidationError("Subblock %s (%s - %s) is not contained in this block anymore" % (subblock, subblock.asBegin, subblock.asEnd))
# check if asnumbers are still in range
for asnumber in self.instance.asnumber_set.all():
if not (asBegin <= asnumber.number <= asEnd):
raise forms.ValidationError("AS %s (%s) is not contained in this block anymore" % (asnumber, asnumber.number))
return cleaned_data

View File

@ -1,6 +1,7 @@
import whoisdb.models
import domains.models
import ipaddress
def _addFields(fields, obj, fieldNames):
for fieldName in fieldNames:
@ -25,7 +26,7 @@ def getWhoisObjectFields(obj, owner):
fields.append(("AS Range", "%s - %s" % (obj.asBegin, obj.asEnd)))
_addFields(fields, obj, ["description", "parent_block", "mnt_by", "mnt_lower", "admin_c"])
elif c == whoisdb.models.ASNumber:
_addFields(fields, obj, ["name", "number", "description", "volatile", "mnt_by", "mnt_lower", "admin_c"])
_addFields(fields, obj, ["name", "number", "description", "asblock", "volatile", "mnt_by", "mnt_lower", "admin_c"])
elif c == whoisdb.models.InetNum:
_addFields(fields, obj, ["name"])
fields.append(("Address CIDR", obj.prefix()))
@ -45,3 +46,23 @@ def getWhoisObjectFields(obj, owner):
def guessWhoisObject(self, handle):
# is it a normal handle?
pass
def findInDatabase(rawValue):
# is this an ip address?
rawValue = rawValue.strip()
value = None
isIp = False
try:
value = ipaddress.ip_network(rawValue, strict=False)
isIp = true
except ValueError:
try:
value = ipaddress.ip_address(rawValue)
except ValueError:
pass
# try:
# - ipnetwork
# - ipaddress
# - asnumber (either asXXXXXX or just number XXXXXX)
# - handle name

View File

@ -179,6 +179,9 @@ class ASBlock(MntdObject):
mnt_lower = models.ManyToManyField(Maintainer, related_name='lower_asblock_set', blank=True)
def contains(self, block):
return self.asBegin <= block.asBegin <= self.asEnd and self.asBegin <= block.asEnd <= self.asEnd
def getResource(self):
return "%s - %s" % (self.asBegin, self.asEnd)

View File

@ -4,6 +4,10 @@ from . import views as whoisdb_views
urlpatterns = [
url(r'^$', whoisdb_views.dbDashboard, name='dashboard'),
url(r'^search/$', whoisdb_views.searchObject, name='search'),
url(r'^create/$', whoisdb_views.createObjectOverview, name='createObjectOverview'),
url(r'^handle/(?P<handle>[A-Z0-9-]+)/$', whoisdb_views.showHandle, name='showhandle'),
url(r'^handle/(?P<handle>[A-Z0-9-]+)/$', whoisdb_views.showHandle, name='handle-detail'),

View File

@ -11,6 +11,7 @@ from formtools.wizard.views import SessionWizardView
from .models import Maintainer, Contact, InetNum, ASBlock, ASNumber
from .forms import MntForm, MntInitialForm, ContactForm, ContactInitialForm, InetNumForm, ASBlockForm, ASNumberForm
from .generic import DeleteCheckView, MntGenericMixin
from .helpers import findInDatabase
@login_required
@ -80,6 +81,15 @@ def showHandle(request, handle):
return render(request, "whoisdb/handle_show.html", {"object": obj})
def searchObject(request):
if request.method == "POST":
name = request.POST.get("q", None)
if name:
#print(name)
findInDatabase(name)
return render(request, "whoisdb/search.html", {})
class MaintainerCreate(LoginRequiredMixin, CreateView):
template_name = "whoisdb/obj_create.html"
form_class = MntForm