summaryrefslogtreecommitdiff
path: root/core.py
blob: 5ff69bc3f5240fa23161b73b3133fc7346e2f256 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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')
nihil fit ex nihilo