+#!/usr/bin/python3
+
+import asyncio
+import hashlib
+import os.path
+
+import dns.message
+
+import sqlite3
+con = sqlite3.connect("tokens.sqlite")
+
+with con:
+ con.execute("CREATE TABLE IF NOT EXISTS token (token CHAR[32], ip VARCHAR[40])")
+
+
+class DnsServerProtocol:
+ def connection_made(self, transport):
+ self.transport = transport
+
+ def datagram_received(self, data, addr):
+ # with open(os.path.join('packets', hashlib.sha224(data).hexdigest()), 'wb') as fd:
+ # fd.write(data)
+ message = dns.message.from_wire(data)
+ for question in message.question:
+ qname = str(question).split('.')[0]
+
+ with con:
+ con.execute("INSERT INTO token (token, ip) VALUES (?, ?)", (qname, addr[0]))
+
+
+ response = dns.message.make_response(message)
+ self.transport.sendto(response.to_wire(), addr)
+
+loop = asyncio.get_event_loop()
+print("Starting UDP server")
+# One protocol instance will be created to serve all client requests
+listen = loop.create_datagram_endpoint(
+ DnsServerProtocol, local_addr=('fda3:c723:abcd:efe1:5054:ff:fec8:148e', 53))
+transport, protocol = loop.run_until_complete(listen)
+listen = loop.create_datagram_endpoint(
+ DnsServerProtocol, local_addr=('10.100.96.17', 53))
+transport, protocol = loop.run_until_complete(listen)
+
+
+try:
+ loop.run_forever()
+except KeyboardInterrupt:
+ pass
+
+transport.close()
+loop.close()