Created
January 25, 2012 00:52
-
-
Save tomprince/1673856 to your computer and use it in GitHub Desktop.
This file contains 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
# Copyright (c) Twisted Matrix Laboratories. | |
# See LICENSE for details. | |
from twisted.application.service import ServiceMaker | |
TwistedNames = ServiceMaker( | |
"Twisted DNS Server", | |
"resolve", | |
"A domain name server.", | |
"resolve") |
This file contains 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
# Copyright (c) Twisted Matrix Laboratories. | |
# See LICENSE for details. | |
""" | |
Lookup a name using multiple resolvers. | |
Future Plans: This needs someway to specify which resolver answered | |
the query, or someway to specify (authority|ttl|cache behavior|more?) | |
@author: Jp Calderone | |
""" | |
from twisted.internet import defer, interfaces | |
from twisted.names import dns | |
from zope.interface import implements | |
from twisted.names import common | |
class FailureHandler: | |
def __init__(self, resolver, query, timeout): | |
self.resolver = resolver | |
self.query = query | |
self.timeout = timeout | |
def __call__(self, failure): | |
# AuthoritativeDomainErrors should halt resolution attempts | |
failure.trap(dns.DomainError, defer.TimeoutError, NotImplementedError) | |
return self.resolver(self.query, self.timeout) | |
class ResolverChain(common.ResolverBase): | |
"""Lookup an address using multiple C{IResolver}s""" | |
implements(interfaces.IResolver) | |
def __init__(self, resolvers): | |
common.ResolverBase.__init__(self) | |
self.resolvers = resolvers | |
def _lookup(self, name, cls, type, timeout): | |
q = dns.Query(name, type, cls) | |
if name.endswith("hocat.ca") or name.endswith("hocat.ca."): | |
d = self.resolvers[0].query(q, timeout) | |
else: | |
d = self.resolvers[1].query(q, timeout) | |
return d | |
def lookupAllRecords(self, name, timeout = None): | |
if name.endswith("hocat.ca") or name.endswith("hocat.ca."): | |
d = self.resolvers[0].lookupAllRecords(name, timeout) | |
else: | |
d = self.resolvers[1].lookupAllRecords(name, timeout) | |
return d | |
# Copyright (c) Twisted Matrix Laboratories. | |
# See LICENSE for details. | |
""" | |
Domain Name Server | |
""" | |
import os, traceback | |
from twisted.python import usage | |
from twisted.names import dns | |
from twisted.application import internet, service | |
from twisted.names import server | |
from twisted.names import authority | |
from twisted.names import secondary | |
class Options(usage.Options): | |
optParameters = [ | |
["interface", "i", "", "The interface to which to bind"], | |
["port", "p", "53", "The port on which to listen"], | |
["resolv-conf", None, None, | |
"Override location of resolv.conf (implies --recursive)"], | |
["hosts-file", None, None, "Perform lookups with a hosts file"], | |
] | |
optFlags = [ | |
["cache", "c", "Enable record caching"], | |
["recursive", "r", "Perform recursive lookups"], | |
["verbose", "v", "Log verbosely"], | |
] | |
compData = usage.Completions( | |
optActions={"interface" : usage.CompleteNetInterfaces()} | |
) | |
zones = None | |
zonefiles = None | |
def __init__(self): | |
usage.Options.__init__(self) | |
self['verbose'] = 0 | |
self.bindfiles = [] | |
self.zonefiles = [] | |
self.secondaries = [] | |
def opt_pyzone(self, filename): | |
"""Specify the filename of a Python syntax zone definition""" | |
if not os.path.exists(filename): | |
raise usage.UsageError(filename + ": No such file") | |
self.zonefiles.append(filename) | |
def opt_bindzone(self, filename): | |
"""Specify the filename of a BIND9 syntax zone definition""" | |
if not os.path.exists(filename): | |
raise usage.UsageError(filename + ": No such file") | |
self.bindfiles.append(filename) | |
def opt_secondary(self, ip_domain): | |
"""Act as secondary for the specified domain, performing | |
zone transfers from the specified IP (IP/domain) | |
""" | |
args = ip_domain.split('/', 1) | |
if len(args) != 2: | |
raise usage.UsageError("Argument must be of the form IP/domain") | |
self.secondaries.append((args[0], [args[1]])) | |
def opt_verbose(self): | |
"""Increment verbosity level""" | |
self['verbose'] += 1 | |
def postOptions(self): | |
if self['resolv-conf']: | |
self['recursive'] = True | |
self.svcs = [] | |
self.zones = [] | |
for f in self.zonefiles: | |
try: | |
self.zones.append(authority.PySourceAuthority(f)) | |
except Exception, e: | |
traceback.print_exc() | |
raise usage.UsageError("Invalid syntax in " + f) | |
for f in self.bindfiles: | |
try: | |
self.zones.append(authority.BindAuthority(f)) | |
except Exception, e: | |
traceback.print_exc() | |
raise usage.UsageError("Invalid syntax in " + f) | |
for f in self.secondaries: | |
self.svcs.append(secondary.SecondaryAuthorityService(*f)) | |
self.zones.append(self.svcs[-1].getAuthority()) | |
try: | |
self['port'] = int(self['port']) | |
except ValueError: | |
raise usage.UsageError("Invalid port: %r" % (self['port'],)) | |
def makeService(config): | |
from twisted.names import client, cache, hosts | |
ca, cl = [], [] | |
servers = [('ns.teksavvy.com',53),('ns2.teksavvy.com',53)] | |
servers = [('206.248.182.3',53),('206.248.182.4',53)] | |
resolve_conf = "/tmp/resolve.conf" | |
cl = [ResolverChain([client.Resolver(servers=servers), client.createResolver(resolvconf=resolve_conf)])] | |
f = server.DNSServerFactory(config.zones, ca, cl, config['verbose']) | |
p = dns.DNSDatagramProtocol(f) | |
f.noisy = 0 | |
ret = service.MultiService() | |
for (klass, arg) in [(internet.TCPServer, f), (internet.UDPServer, p)]: | |
s = klass(config['port'], arg, interface=config['interface']) | |
s.setServiceParent(ret) | |
for svc in config.svcs: | |
svc.setServiceParent(ret) | |
return ret |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment