-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcassandradns.py
56 lines (47 loc) · 1.98 KB
/
cassandradns.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from zope.interface import implements
from twisted.python import failure, log
from twisted.internet import interfaces, reactor, defer
from twisted.names import common, dns, resolve
from twisted.names.server import DNSServerFactory
from dnstypeconstants import *
import cassandranames
import sys
import pprint
class CassandraNamesResolver(common.ResolverBase):
implements(interfaces.IResolver)
def __init__(self):
self.names = cassandranames.CassandraNames()
common.ResolverBase.__init__(self)
def _lookup(self, name, cls, type, timeout):
log.msg("Looking up type %s records for hostname: %s" % (type, name))
all_types = self.names.lookup(name, type)
results = []
authority = []
additional = []
if len(all_types) > 0:
log.msg("Got results.")
else:
log.msg("No results.")
for type, records in all_types.items():
for data, metadata in records.items():
if type == A:
payload = dns.Record_A(data)
elif type == CNAME:
# TODO: Add proper CNAME support that sends corresponding "A" records.
payload = dns.Record_CNAME(data)
elif type == MX:
payload = dns.Record_MX(metadata["preference"], data)
elif type == NS:
payload = dns.Record_NS(data)
header = dns.RRHeader(name, type=type, payload=payload, ttl=metadata["ttl"], auth=True)
results.append(header)
return defer.succeed((results, authority, additional))
#return defer.fail(failure.Failure(dns.DomainError(name)))
authorities = [CassandraNamesResolver()]
factory = DNSServerFactory(authorities, verbose=True)
protocol = dns.DNSDatagramProtocol(factory)
if __name__ == '__main__':
log.startLogging(sys.stdout)
reactor.listenUDP(1053, protocol)
reactor.listenTCP(1053, factory)
reactor.run()