Skip to content

Instantly share code, notes, and snippets.

@gleicon
Created July 10, 2011 01:13
Show Gist options
  • Save gleicon/1074124 to your computer and use it in GitHub Desktop.
Save gleicon/1074124 to your computer and use it in GitHub Desktop.
Python/Twisted/Redis backed DNS server.
# Python/Twisted/Redis backed DNS server - resolves from NAME to IP addrs
# fallback to google or any other DNS server to resolv domains not present on Redis
# to set a new domain on redis, just issue a SET domain.tld ip_addr
# run with twistd -ny txredns.tac
# gleicon 2011
from twisted.names import dns, server, client, cache
from twisted.application import service, internet
from twisted.internet import defer
from twisted.python import log
import txredisapi
class RedisResolverBackend(client.Resolver):
def __init__(self, redis, servers=None):
self.redis = redis
client.Resolver.__init__(self, servers=servers)
self.ttl = 5
@defer.inlineCallbacks
def _get_ip_addr(self, hostname, timeout):
ip = yield self.redis.get(hostname)
log.msg('redis: %s'% ip)
r = None
if ip:
defer.returnValue([(dns.RRHeader(hostname, dns.A, dns.IN, self.ttl, dns.Record_A(ip, self.ttl)),), (), ()])
else:
i = yield self._lookup(hostname, dns.IN, dns.A, timeout)
defer.returnValue(i)
def lookupAddress(self, name, timeout = None):
return self._get_ip_addr(name, timeout)
def create_application():
rd = txredisapi.lazyRedisConnectionPool()
redisBackend = RedisResolverBackend(rd, servers=[('8.8.8.8', 53)])
application = service.Application("txdnsredis")
srv_collection = service.IServiceCollection(application)
dnsFactory = server.DNSServerFactory(caches=[cache.CacheResolver()], clients=[redisBackend])
internet.TCPServer(53, dnsFactory).setServiceParent(srv_collection)
internet.UDPServer(53, dns.DNSDatagramProtocol(dnsFactory)).setServiceParent(srv_collection)
return application
# .tac app
application = create_application()
@fnando
Copy link

fnando commented Jul 10, 2011

Sweet! :D

@mikeifomin
Copy link

Simple and cool!

@stefanfoulis
Copy link

I'd like to flesh out the code a bit and release it on pypi.
(and later make backends pluggable, e.g for plainfiles and etcd).

@gleicon Is it ok for you if I release it under the BSD license?

@gleicon
Copy link
Author

gleicon commented Mar 25, 2014

I've answered through email but I'll copy here: ok to use, let me know if you need any help or info. Check out the full repo: https://github.com/gleicon/python_dns_servers. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment