From 9f89d5691939f5b23d37c828252443ddc7ed15a7 Mon Sep 17 00:00:00 2001
From: Raúl Benencia <rbenencia@linti.unlp.edu.ar>
Date: Wed, 27 Jun 2012 21:39:17 -0300
Subject: First commit

---
 main.py       |  51 +++++++++++++++++++++++++++++++++++++++++++++++++++
 networks.xml  |  14 ++++++++++++++
 pywhoisd.conf |  23 +++++++++++++++++++++++
 pywhoisd.dia  | Bin 0 -> 3007 bytes
 whoiscore.py  |  56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 whoisdata.py  |  38 ++++++++++++++++++++++++++++++++++++++
 6 files changed, 182 insertions(+)
 create mode 100755 main.py
 create mode 100644 networks.xml
 create mode 100644 pywhoisd.conf
 create mode 100644 pywhoisd.dia
 create mode 100644 whoiscore.py
 create mode 100644 whoisdata.py

diff --git a/main.py b/main.py
new file mode 100755
index 0000000..9205304
--- /dev/null
+++ b/main.py
@@ -0,0 +1,51 @@
+#!/usr/bin/python
+import ConfigParser
+import whoiscore
+
+class PyWhoisD():
+    def __init__(self):
+        self.config = ConfigParser.RawConfigParser()
+        self.config.read('pywhoisd.conf')
+
+        self.data = None
+        self.daemon = None
+
+    # What kind of storage are we using?
+    def config_data(self):
+        mode = self.config.get('Storage', 'mode')
+
+        if mode == 'xml':
+            self.data = WhoisData.WhoisDataXML(self.config)
+
+    def config_daemon(self):
+        self.config_data()
+        self.daemon = WhoisDaemon(self.data)
+
+    # Returns true if web server is enabled
+    def webserver(self):
+        return self.config.get('Servers', 'web') == 'yes'
+
+    # Returns true if web server is enabled    
+    def classicserver(self):
+        self.config.get('Servers', 'classic') == 'yes':        
+
+    def config_servers(self):
+        if self.classicserver()
+            self.classic_server = whoiscore.WhoisServer(self.config, self.daemon)
+                
+        if self.webserver(self):
+            self.web_server = whoiscore.WhoisWebServer(self.config, self.daemon)
+
+    def start_servers(self):
+        if self.classicserver(): self.classic_server.start()
+        if self.webserver(): self.web_server.start()
+
+    def main(self):
+        self.config_daemon()
+        self.config_servers()
+
+        self.start_servers()
+
+if __name__ == "__main__":
+    pwd = PyWhoisD()
+    pwd.main()
diff --git a/networks.xml b/networks.xml
new file mode 100644
index 0000000..136a4e7
--- /dev/null
+++ b/networks.xml
@@ -0,0 +1,14 @@
+<?xml version="1.0"?>
+<networks>
+  <network name="Medicina">
+    <domain>med.unlp.edu.ar</domain>
+    <ip_block>163.10.9.0/24</ip_block>
+    <admin_contact>Carlos Sabena &lt;csabena@med.unlp.edu.ar&gt;</admin_contact>
+  </network>
+  <network name="CeSPI">
+    <domain>cespi.unlp.edu.ar</domain>
+    <ip_block>163.10.0.0/24</ip_block>    
+    <admin_contact>Soporte &lt;soporte@cespi.unlp.edu.ar&gt;</admin_contact>
+    <phone>221 5787689</phone>
+  </network>
+</networks>
\ No newline at end of file
diff --git a/pywhoisd.conf b/pywhoisd.conf
new file mode 100644
index 0000000..346ddc4
--- /dev/null
+++ b/pywhoisd.conf
@@ -0,0 +1,23 @@
+## pywhoisd configuration file ##
+
+[Servers]
+# Run a (classic) whois server?
+classic = yes
+
+# Only makes sense when classic server is enabled
+web_host = localhost
+web_port = 4343
+
+# Run a web whois server?
+web = yes
+
+# Only makes sense when web server is enabled
+web_host = localhost
+web_port = 8080
+
+[Storage]
+# At the moment only xml mode is available
+mode = xml
+
+# Only makes sense when xml storage mode is enabled
+xml_file = networks.xml
\ No newline at end of file
diff --git a/pywhoisd.dia b/pywhoisd.dia
new file mode 100644
index 0000000..e7496fa
Binary files /dev/null and b/pywhoisd.dia differ
diff --git a/whoiscore.py b/whoiscore.py
new file mode 100644
index 0000000..6053508
--- /dev/null
+++ b/whoiscore.py
@@ -0,0 +1,56 @@
+from ipcalc import IP, Network
+
+class WhoisDaemon():
+    def __init__(data):
+        self.data = data    
+
+    def query(q):
+        if is_ip(q):
+            self.search_ip(q)
+        else:
+            if is_domain(q):
+                self.search_domain(q)
+            else:
+                return self.print_help()
+
+    def search_ip(self, ip):
+        result = {}
+
+        # Iterate over all IP block elements
+        for network in self.data.get_networks():
+           for block in network.ip_blocks:
+               if ip in Network(block):
+                   result['name'] = network.name
+                   for key in network.data:
+                       result[key] = network.data[key]
+
+                   return result
+                   
+        result['error'] = "Red no encontrada"
+        return result
+
+    def search_domain:
+        pass
+
+class ThreadedTCPRequestHandler(SocketServer.BaseRequestHandler):
+
+    def handle(self):
+        data = self.request.recv(100)
+        cur_thread = threading.current_thread()
+        response = self.get_header() + "\n" + self.get_answer(data) + "\n" + self.get_footer()        
+        self.request.sendall(response)
+    
+class WhoisServer(TCPServer):
+    def __init__(self, config):
+        host = config.get('Servers', 'classic_host')
+        port = config.get('Servers', 'classic_port')
+
+        TCPServer.__init__(self, (host, port), ThreadedTCPRequestHandler):
+
+    def start(self):
+        
+
+class WhoisWebServer(TCPServer):
+    def __init__(self, config):
+        self.host = config.get('Servers', 'web_host')
+        self.port = config.get('Servers', 'web_host')
diff --git a/whoisdata.py b/whoisdata.py
new file mode 100644
index 0000000..200d3ba
--- /dev/null
+++ b/whoisdata.py
@@ -0,0 +1,38 @@
+from xml.etree.ElementTree import ElementTree
+
+class WhoisNetwork():
+    def __init__(self, name):
+        self.name = name
+        self.domain = None
+        self.ip_blocks = []
+        self.data = {}
+
+class WhoisData():
+    def __init__(self, config):
+        self.networks = []
+        self.config = config
+
+    def parse_config(self): pass
+    def load_data(self): pass
+
+    def get_networks(self):
+        if self.networks == None:
+            self.load_data()
+
+        return self.networks
+
+class WhoisDataXML(WhoisData):
+    def parse_config():
+        self.data_file = self.config.get('Storage', 'xml_file')
+        
+    def load_data(self):
+        root = ElementTree(file=self.data_file).getroot()
+        for elem in root:
+            network = WhoisNetwork(elem.attrib['name'])
+            for e in elem:
+                if e.tag == 'ip_block':
+                    network.ip_blocks.append(e.text)
+                else:
+                    network.data[e.tag] = e.text
+                    
+            self.networks.append(network)
-- 
cgit v1.2.3