-
Notifications
You must be signed in to change notification settings - Fork 1
/
dns_cache.py
95 lines (82 loc) · 3.12 KB
/
dns_cache.py
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
86
87
88
89
90
91
92
93
94
95
import time
import threading
import socketserver
import socket
import logging
from dns_packet import DNSPacket
logging.basicConfig(level=logging.WARNING)
cache = {}
forwarder_addr = None
class ThreadedUDPRequestHandler(socketserver.BaseRequestHandler):
def handle(self):
global cache
response = None
# Get recv data
data = self.request[0]
# Get socket to answer to client
sock = self.request[1]
cur_thread = threading.current_thread()
request = DNSPacket(data)
record = request.data[2:]
if record in cache:
logging.debug(cache[record])
cache_data = cache[record][0].data
cache_time = cache[record][1]
cache_ttl = cache[record][2]
if time.time() - cache_time <= cache_ttl:
response = DNSPacket(cache_data)
response.add_id(request.HEADER[0])
response.set_ttl(cache_time, cache_ttl, time.time())
sock.sendto(response.data, self.client_address)
logging.warning("Response from cache")
logging.warning(response.QNAME[0].decode("utf-8"))
return
# Have to ask forwarder
try:
f_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
f_sock.sendto(data, forwarder_addr)
response = f_sock.recv(4096)
except socket.timeout:
# We don't have to crash if we have timeout from forwarder
# Just go on
pass
if response:
response_packet = DNSPacket(response)
sock.sendto(response_packet.data, self.client_address)
cache[record] = [response_packet, time.time(), response_packet.get_ttl()]
logging.warning("Response from forwarder")
logging.warning(response_packet.QNAME[0].decode("utf-8"))
logging.debug(cache)
logging.debug(cache[record][0].data)
class ThreadedUDPServer(socketserver.ThreadingMixIn, socketserver.UDPServer):
pass
class DNSCacheServer:
"""
Cache full ANSWERS from forwarder
"""
def __init__(self, ip, port, forwarder_ip):
self.ip = ip
self.port = port
global forwarder_addr
forwarder_addr = (forwarder_ip, 53)
def run_server(self):
server = ThreadedUDPServer((self.ip, self.port), ThreadedUDPRequestHandler)
# Start a thread with the server - that thread will then start one more
# per each request
server_thread = threading.Thread(target=server.serve_forever)
# Exit the server thread when the main thread terminates
server_thread.daemon = True
server_thread.start()
logging.warning("Running dns on {} {}".format(self.ip, self.port))
logging.debug("Server loop running in thread: {}".format(server_thread.name))
try:
while 1:
time.sleep(1)
except KeyboardInterrupt:
pass
finally:
logging.warning("Exiting")
server.shutdown()
if __name__ == '__main__':
s = DNSCacheServer("localhost", 53, "8.8.8.8")
s.run_server()