diff options
-rwxr-xr-x | main.py | 51 | ||||
-rw-r--r-- | networks.xml | 14 | ||||
-rw-r--r-- | pywhoisd.conf | 23 | ||||
-rw-r--r-- | pywhoisd.dia | bin | 0 -> 3007 bytes | |||
-rw-r--r-- | whoiscore.py | 56 | ||||
-rw-r--r-- | whoisdata.py | 38 |
6 files changed, 182 insertions, 0 deletions
@@ -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 <csabena@med.unlp.edu.ar></admin_contact> + </network> + <network name="CeSPI"> + <domain>cespi.unlp.edu.ar</domain> + <ip_block>163.10.0.0/24</ip_block> + <admin_contact>Soporte <soporte@cespi.unlp.edu.ar></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 Binary files differnew file mode 100644 index 0000000..e7496fa --- /dev/null +++ b/pywhoisd.dia 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) |