summaryrefslogtreecommitdiff
path: root/core.py
blob: 1ac7e4f02788662b38924ac0a7abf743e9a297f3 (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import socketserver
import ipaddr

class Daemon():
    def __init__(data):
        self.data = data    

    def query(q):
        if self.is_ip(q):
            self.search_ip(q)
            
        elif self.is_domain(q):
            self.search_domain(q)
            
        else:
            # Try to find a person with the given query
            person = search_person(q)
            if person:
                return person
            else:
                return self.show_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 ipaddr.IPAddress(ip) in ipaddr.IPNetwork(block):
                   result['name'] = network.name
                   for key in network.data:
                       result[key] = network.data[key]

                   return result
                   
        result['error'] = "Red no encontrada"
        return result

    # TODO
    def search_domain(self, domain):
        pass

    def search_person(self, query):
        pass

    # TODO
    def is_ip(self, query):
        pass

    # TODO
    def is_domain(self, query):
        pass

    # TODO
    def show_help():
        pass    

class WhoisHandler(socketserver.BaseRequestHandler):

    def handle(self):
        daemon = self.server.daemon
        data = self.request.recv(100)
        cur_thread = threading.current_thread()
        response = daemon.get_header() + "\n" + daemon.query(data) + "\n" + daemon.get_footer()
        
        self.request.sendall(response)
    
class ClassicServer(socketserver.ThreadingTCPServer):
    def __init__(self, config, daemon):
        host = config['Servers']['classic_host']
        port = config['Servers']['classic_port']
        self.daemon = daemon

        ThreadingTCPServer.__init__(self, (host, port), WhoisHandler)

    def start(self):
        pass        

class WebServer(socketserver.ThreadingTCPServer):
    def __init__(self, config, daemon):
        self.host = config['Servers']['web_host']
        self.port = config['Servers']['web_port']
        self.daemon = daemon

        ThreadingTCPServer.__init__(self, (host, port), WhoisHandler)
nihil fit ex nihilo