X-Git-Url: https://git.siccegge.de//index.cgi?p=software%2Fwtfismyip.git;a=blobdiff_plain;f=nameserver.py;fp=nameserver.py;h=e5a0ba1e06af46e5a89f0543f193ae8c4ec81e50;hp=0000000000000000000000000000000000000000;hb=f8147b32ff43ee3280390615a7fa500ff49ed590;hpb=38017d454d44b8945bf5d9758f7029c9c159aca7 diff --git a/nameserver.py b/nameserver.py new file mode 100755 index 0000000..e5a0ba1 --- /dev/null +++ b/nameserver.py @@ -0,0 +1,51 @@ +#!/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()