]> git.siccegge.de Git - software/wtfismyip.git/blob - nameserver.py
Add fake dns server
[software/wtfismyip.git] / nameserver.py
1 #!/usr/bin/python3
2
3 import asyncio
4 import hashlib
5 import os.path
6
7 import dns.message
8
9 import sqlite3
10 con = sqlite3.connect("tokens.sqlite")
11
12 with con:
13 con.execute("CREATE TABLE IF NOT EXISTS token (token CHAR[32], ip VARCHAR[40])")
14
15
16 class DnsServerProtocol:
17 def connection_made(self, transport):
18 self.transport = transport
19
20 def datagram_received(self, data, addr):
21 # with open(os.path.join('packets', hashlib.sha224(data).hexdigest()), 'wb') as fd:
22 # fd.write(data)
23 message = dns.message.from_wire(data)
24 for question in message.question:
25 qname = str(question).split('.')[0]
26
27 with con:
28 con.execute("INSERT INTO token (token, ip) VALUES (?, ?)", (qname, addr[0]))
29
30
31 response = dns.message.make_response(message)
32 self.transport.sendto(response.to_wire(), addr)
33
34 loop = asyncio.get_event_loop()
35 print("Starting UDP server")
36 # One protocol instance will be created to serve all client requests
37 listen = loop.create_datagram_endpoint(
38 DnsServerProtocol, local_addr=('fda3:c723:abcd:efe1:5054:ff:fec8:148e', 53))
39 transport, protocol = loop.run_until_complete(listen)
40 listen = loop.create_datagram_endpoint(
41 DnsServerProtocol, local_addr=('10.100.96.17', 53))
42 transport, protocol = loop.run_until_complete(listen)
43
44
45 try:
46 loop.run_forever()
47 except KeyboardInterrupt:
48 pass
49
50 transport.close()
51 loop.close()