From 01aa58c5d7947e37bc3f0a927c2ed809873122a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Benencia?= Date: Wed, 1 Aug 2012 18:05:04 -0300 Subject: New scheme. Starting port to python 3. It's now or never! --- .gitignore | 1 + core.py | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++ data.py | 38 +++++++++++++++++++++++++++++++++ doc/pywhoisd.dia | Bin 0 -> 3510 bytes examples/networks.xml | 13 ++++++++++++ main.py | 51 -------------------------------------------- networks.xml | 13 ------------ pywhoisd.conf | 4 ++-- pywhoisd.dia | Bin 3510 -> 0 bytes pywhoisd.py | 53 ++++++++++++++++++++++++++++++++++++++++++++++ whoiscore.py | 56 ------------------------------------------------- whoisdata.py | 38 --------------------------------- 12 files changed, 164 insertions(+), 160 deletions(-) create mode 100644 .gitignore create mode 100644 core.py create mode 100644 data.py create mode 100644 doc/pywhoisd.dia create mode 100644 examples/networks.xml delete mode 100755 main.py delete mode 100644 networks.xml delete mode 100644 pywhoisd.dia create mode 100755 pywhoisd.py delete mode 100644 whoiscore.py delete mode 100644 whoisdata.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..72723e5 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*pyc diff --git a/core.py b/core.py new file mode 100644 index 0000000..5ff69bc --- /dev/null +++ b/core.py @@ -0,0 +1,57 @@ +from ipcalc import IP, Network +import socketserver + +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(self): + 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(socketserver.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): + pass + +class WhoisWebServer(socketserver.TCPServer): + def __init__(self, config): + self.host = config.get('Servers', 'web_host') + self.port = config.get('Servers', 'web_host') diff --git a/data.py b/data.py new file mode 100644 index 0000000..200d3ba --- /dev/null +++ b/data.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) diff --git a/doc/pywhoisd.dia b/doc/pywhoisd.dia new file mode 100644 index 0000000..d108724 Binary files /dev/null and b/doc/pywhoisd.dia differ diff --git a/examples/networks.xml b/examples/networks.xml new file mode 100644 index 0000000..641071d --- /dev/null +++ b/examples/networks.xml @@ -0,0 +1,13 @@ + + + + one.network.example + 10.10.0.0/24 + Admin Istrator <admin@one.network.example> + + + two.network.example + 10.11.0.0/24 + Admin Ister <admin@two.network.example> + + \ No newline at end of file diff --git a/main.py b/main.py deleted file mode 100755 index 9205304..0000000 --- a/main.py +++ /dev/null @@ -1,51 +0,0 @@ -#!/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 deleted file mode 100644 index 641071d..0000000 --- a/networks.xml +++ /dev/null @@ -1,13 +0,0 @@ - - - - one.network.example - 10.10.0.0/24 - Admin Istrator <admin@one.network.example> - - - two.network.example - 10.11.0.0/24 - Admin Ister <admin@two.network.example> - - \ No newline at end of file diff --git a/pywhoisd.conf b/pywhoisd.conf index 346ddc4..b7d0662 100644 --- a/pywhoisd.conf +++ b/pywhoisd.conf @@ -5,8 +5,8 @@ classic = yes # Only makes sense when classic server is enabled -web_host = localhost -web_port = 4343 +classic_host = localhost +classic_port = 4343 # Run a web whois server? web = yes diff --git a/pywhoisd.dia b/pywhoisd.dia deleted file mode 100644 index d108724..0000000 Binary files a/pywhoisd.dia and /dev/null differ diff --git a/pywhoisd.py b/pywhoisd.py new file mode 100755 index 0000000..7832cae --- /dev/null +++ b/pywhoisd.py @@ -0,0 +1,53 @@ +#!/usr/bin/python3 +import configparser +import whoiscore + +class PyWhoisD(): + """Main class. It reads the configuration options and starts the server""" + + def __init__(self): + self.config = configparser.ConfigParser() + 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['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/whoiscore.py b/whoiscore.py deleted file mode 100644 index 6053508..0000000 --- a/whoiscore.py +++ /dev/null @@ -1,56 +0,0 @@ -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 deleted file mode 100644 index 200d3ba..0000000 --- a/whoisdata.py +++ /dev/null @@ -1,38 +0,0 @@ -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