Created
August 30, 2013 01:14
-
-
Save klizhentas/6385318 to your computer and use it in GitHub Desktop.
DNS server configurable via http
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# http://notmysock.org/blog/hacks/a-twisted-dns-story.html | |
# http://blog.inneoin.org/2009/11/i-used-twisted-to-create-dns-server.html | |
# twistd -y dns.py | |
import socket | |
from twisted.internet import reactor | |
from twisted.names import dns | |
from twisted.names import client, server | |
from twisted.web.server import Site | |
import klein | |
from klein import route | |
class Hosts(object): | |
values = {} | |
@route('/', methods=['POST']) | |
def set_a_record(request): | |
host = request.args['host'][0].strip() | |
ip = request.args['ip'][0].strip() | |
Hosts.values[host] = ip | |
return 'Change accepted' | |
class DNSServerFactory(server.DNSServerFactory): | |
def gotResolverResponse(self, (ans, auth, add), protocol, message, address): | |
qname = message.queries[0].name.name.lower().strip() | |
ip = Hosts.values.get(qname) | |
if ip is not None: | |
for answer in ans: | |
if answer.type != dns.A: | |
continue | |
if qname not in answer.name.name: | |
continue | |
answer.payload.address = socket.inet_aton(ip) | |
answer.payload.ttl = 3600 | |
args = (self, (ans, auth, add), protocol, message, address) | |
return server.DNSServerFactory.gotResolverResponse(*args) | |
verbosity = 0 | |
resolver = client.Resolver(servers=[('8.8.8.8', 53)]) | |
factory = DNSServerFactory(clients=[resolver], verbose=verbosity) | |
protocol = dns.DNSDatagramProtocol(factory) | |
factory.noisy = protocol.noisy = verbosity | |
reactor.listenUDP(53, protocol, interface="10.20.76.46") | |
reactor.listenTCP(53, factory, interface="10.20.76.46") | |
reactor.listenTCP(8232, Site(klein.resource()), interface="127.0.0.1") | |
reactor.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment