Fix on previous commit: Added a whois utility. Incorporated user profile
authorLeonidas Poulopoulos <leopoul@noc.grnet.gr>
Wed, 23 Nov 2011 10:20:57 +0000 (12:20 +0200)
committerLeonidas Poulopoulos <leopoul@noc.grnet.gr>
Wed, 23 Nov 2011 10:20:57 +0000 (12:20 +0200)
accounts/__init__.py [new file with mode: 0644]
accounts/models.py [new file with mode: 0644]
accounts/tests.py [new file with mode: 0644]
accounts/views.py [new file with mode: 0644]
peers/__init__.py [new file with mode: 0644]
peers/models.py [new file with mode: 0644]
peers/tests.py [new file with mode: 0644]
peers/views.py [new file with mode: 0644]
utils/whois.py [new file with mode: 0644]

diff --git a/accounts/__init__.py b/accounts/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/accounts/models.py b/accounts/models.py
new file mode 100644 (file)
index 0000000..404da7f
--- /dev/null
@@ -0,0 +1,14 @@
+from django.db import models
+from django.contrib.auth.models import User
+from flowspy.peers.models import *
+
+
+class UserProfile(models.Model):
+    user = models.OneToOneField(User)
+    domain = models.ForeignKey(Peer)
+
+    def get_address_space(self):
+        networks = self.domain.networks.all()
+        if not networks:
+            return False
+        return networks
\ No newline at end of file
diff --git a/accounts/tests.py b/accounts/tests.py
new file mode 100644 (file)
index 0000000..2247054
--- /dev/null
@@ -0,0 +1,23 @@
+"""
+This file demonstrates two different styles of tests (one doctest and one
+unittest). These will both pass when you run "manage.py test".
+
+Replace these with more appropriate tests for your application.
+"""
+
+from django.test import TestCase
+
+class SimpleTest(TestCase):
+    def test_basic_addition(self):
+        """
+        Tests that 1 + 1 always equals 2.
+        """
+        self.failUnlessEqual(1 + 1, 2)
+
+__test__ = {"doctest": """
+Another way to test that 1 + 1 is equal to 2.
+
+>>> 1 + 1 == 2
+True
+"""}
+
diff --git a/accounts/views.py b/accounts/views.py
new file mode 100644 (file)
index 0000000..60f00ef
--- /dev/null
@@ -0,0 +1 @@
+# Create your views here.
diff --git a/peers/__init__.py b/peers/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/peers/models.py b/peers/models.py
new file mode 100644 (file)
index 0000000..8b79659
--- /dev/null
@@ -0,0 +1,38 @@
+from django.db import models
+from utils.whois import *
+
+# Create your models here.
+class PeerRange(models.Model):
+    network = models.CharField(max_length=128)
+    def __unicode__(self):
+        return self.network
+    class Meta:
+        db_table = u'peer_range'
+
+# Create your models here.
+class Peer(models.Model):
+    peer_id = models.IntegerField(primary_key=True)
+    peer_name = models.CharField(max_length=128)
+    peer_as = models.IntegerField()
+    peer_tag = models.CharField(max_length=64)
+    domain_name = models.CharField(max_length=128)
+    networks = models.ManyToManyField(PeerRange, null=True, blank=True)
+    def __unicode__(self):
+        return self.peer_name
+    class Meta:
+        db_table = u'peer'
+        
+    def fill_networks(self):
+        network_range = []
+        peer_as = "AS%s" %self.peer_as
+        network_range = whois(peer_as)
+        if network_range:
+            for network_item in network_range:
+                range, created = PeerRange.objects.get_or_create(network=network_item.compressed)
+                if not range.network in self.networks.all():
+                    self.networks.add(range)
+            self.save()
+                    
+            
+        
+        
diff --git a/peers/tests.py b/peers/tests.py
new file mode 100644 (file)
index 0000000..2247054
--- /dev/null
@@ -0,0 +1,23 @@
+"""
+This file demonstrates two different styles of tests (one doctest and one
+unittest). These will both pass when you run "manage.py test".
+
+Replace these with more appropriate tests for your application.
+"""
+
+from django.test import TestCase
+
+class SimpleTest(TestCase):
+    def test_basic_addition(self):
+        """
+        Tests that 1 + 1 always equals 2.
+        """
+        self.failUnlessEqual(1 + 1, 2)
+
+__test__ = {"doctest": """
+Another way to test that 1 + 1 is equal to 2.
+
+>>> 1 + 1 == 2
+True
+"""}
+
diff --git a/peers/views.py b/peers/views.py
new file mode 100644 (file)
index 0000000..60f00ef
--- /dev/null
@@ -0,0 +1 @@
+# Create your views here.
diff --git a/utils/whois.py b/utils/whois.py
new file mode 100644 (file)
index 0000000..ef71dd2
--- /dev/null
@@ -0,0 +1,43 @@
+import socket
+from ipaddr import *
+import re
+
+RIPEWHOIS = 'whois.ripe.net'
+GRNETWHOIS = 'whois.grnet.gr'
+
+def query(query, hostname, flags):
+    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+    s.connect((hostname, 43))
+    s.send(" -i origin -r -K -T route -T route6 " + query + "\r\n")
+    response = ''
+    while True:
+        d = s.recv(4096)
+        response += d
+        if not d:
+            break
+    s.close()
+    query = response.splitlines()
+    routes4 = []
+    routes6 = []
+    final_routes4 = []
+    final_routes6 = []
+    for line in query:
+        m = re.match(r"(^route6?\:\s+)(?P<subnets>\S+)", line)
+        if m:
+            if IPNetwork(m.group('subnets')).version == 4:
+                routes4.append(IPNetwork(m.group('subnets')))
+            if IPNetwork(m.group('subnets')).version == 6:
+                routes6.append(IPNetwork(m.group('subnets')))
+    final_routes = []
+    if len(routes4):
+        final_routes4 = collapse_address_list(routes4)
+    if len(routes6):
+        final_routes6 = collapse_address_list(routes6)
+    final_routes = final_routes4 + final_routes6
+    return final_routes
+
+def whois(queryas):
+    routes = query(queryas,GRNETWHOIS, None)
+    if not routes:
+        routes = query(queryas,RIPEWHOIS, None)
+    return routes
\ No newline at end of file