TXT
This commit is contained in:
@@ -9,10 +9,13 @@ from twisted.python import log
|
|||||||
|
|
||||||
domain = sys.argv[1]
|
domain = sys.argv[1]
|
||||||
port = int(sys.argv[2])
|
port = int(sys.argv[2])
|
||||||
ttl = 120
|
ttl = 0 # TODO 120
|
||||||
timeout = 2
|
timeout = 2
|
||||||
|
|
||||||
class DynamicResolver(object):
|
class DynamicResolver(object):
|
||||||
|
def __init__(self):
|
||||||
|
self.zeroconf = Zeroconf(ip_version=4)
|
||||||
|
|
||||||
def _dynamicResponseRequired(self, query):
|
def _dynamicResponseRequired(self, query):
|
||||||
if str(query.name).endswith(domain):
|
if str(query.name).endswith(domain):
|
||||||
return True
|
return True
|
||||||
@@ -31,40 +34,28 @@ class DynamicResolver(object):
|
|||||||
if state_change is ServiceStateChange.Added:
|
if state_change is ServiceStateChange.Added:
|
||||||
services.append(name)
|
services.append(name)
|
||||||
|
|
||||||
zeroconf = Zeroconf(ip_version=4) # TODO: move out?
|
sb = ServiceBrowser(self.zeroconf, localname, [handler])
|
||||||
sb = ServiceBrowser(zeroconf, localname, [handler])
|
|
||||||
print("waiting for", localname) # TODO: remove
|
|
||||||
time.sleep(timeout)
|
time.sleep(timeout)
|
||||||
sb.cancel()
|
sb.cancel()
|
||||||
print("done waiting for", localname) # TODO: remove
|
|
||||||
|
|
||||||
answers = []
|
answers = []
|
||||||
for service in services:
|
for service in services:
|
||||||
answers.append(dns.RRHeader(name=query.name.name, ttl=ttl, type=dns.PTR, payload=dns.Record_PTR(
|
answers.append(dns.RRHeader(name=query.name.name, ttl=ttl, type=dns.PTR, payload=dns.Record_PTR(
|
||||||
name=service[:-6] + domain
|
name=service[:-6] + domain
|
||||||
)))
|
)))
|
||||||
return answers
|
return answers, [], []
|
||||||
|
|
||||||
def txt(localname):
|
def txt(localname):
|
||||||
zeroconf = Zeroconf(ip_version=4) # TODO: move out?
|
info = self.zeroconf.get_service_info(localname, localname)
|
||||||
print("waiting for txt", localname) # TODO: remove
|
|
||||||
info = zeroconf.get_service_info(localname, localname)
|
|
||||||
print("done waiting for txt", localname) # TODO: remove
|
|
||||||
|
|
||||||
if info.text == b"\0":
|
data = [b"%s=%s" % (p, info.properties[p]) for p in info.properties]
|
||||||
answers = []
|
answers = [dns.RRHeader(name=query.name.name, ttl=ttl, type=dns.TXT, payload=dns.Record_TXT(
|
||||||
else:
|
*data
|
||||||
answers = [dns.RRHeader(name=query.name.name, ttl=ttl, type=dns.TXT, payload=dns.Record_TXT(
|
))]
|
||||||
info.text # TODO: something wrong here?
|
return answers, [], []
|
||||||
))]
|
|
||||||
print(answers, type(info.text), info.text)
|
|
||||||
return answers
|
|
||||||
|
|
||||||
def srv(localname):
|
def srv(localname):
|
||||||
zeroconf = Zeroconf(ip_version=4) # TODO: move out?
|
info = self.zeroconf.get_service_info(localname, localname)
|
||||||
print("waiting for srv", localname)
|
|
||||||
info = zeroconf.get_service_info(localname, localname)
|
|
||||||
print("done waiting for srv", localname)
|
|
||||||
|
|
||||||
answers = [dns.RRHeader(name=query.name.name, ttl=ttl, type=dns.SRV, payload=dns.Record_SRV(
|
answers = [dns.RRHeader(name=query.name.name, ttl=ttl, type=dns.SRV, payload=dns.Record_SRV(
|
||||||
info.priority, info.weight, info.port, info.server[:-6] + domain
|
info.priority, info.weight, info.port, info.server[:-6] + domain
|
||||||
@@ -72,7 +63,7 @@ class DynamicResolver(object):
|
|||||||
|
|
||||||
print(answers)
|
print(answers)
|
||||||
|
|
||||||
return answers
|
return answers, [], []
|
||||||
|
|
||||||
def host(localname):
|
def host(localname):
|
||||||
answers = [dns.RRHeader(name=query.name.name, ttl=ttl, type=dns.A, payload=dns.Record_A(
|
answers = [dns.RRHeader(name=query.name.name, ttl=ttl, type=dns.A, payload=dns.Record_A(
|
||||||
@@ -81,32 +72,26 @@ class DynamicResolver(object):
|
|||||||
|
|
||||||
print(answers)
|
print(answers)
|
||||||
|
|
||||||
return answers
|
return answers, [], []
|
||||||
|
|
||||||
d = defer.Deferred()
|
d = defer.Deferred()
|
||||||
|
|
||||||
if query.type == dns.PTR:
|
if query.type == dns.PTR:
|
||||||
d = threads.deferToThread(browse, localname)
|
d = threads.deferToThread(browse, localname)
|
||||||
d.addCallback(lambda a: (a, [], []))
|
|
||||||
return d
|
return d
|
||||||
elif query.type == dns.TXT:
|
elif query.type == dns.TXT:
|
||||||
d = threads.deferToThread(txt, localname)
|
d = threads.deferToThread(txt, localname)
|
||||||
d.addCallback(lambda a: (a, [], []))
|
|
||||||
return d
|
return d
|
||||||
elif query.type == dns.SRV:
|
elif query.type == dns.SRV:
|
||||||
d = threads.deferToThread(srv, localname)
|
d = threads.deferToThread(srv, localname)
|
||||||
d.addCallback(lambda a: (a, [], []))
|
|
||||||
return d
|
return d
|
||||||
elif query.type == dns.A:
|
elif query.type == dns.A:
|
||||||
d = threads.deferToThread(host, localname)
|
d = threads.deferToThread(host, localname)
|
||||||
d.addCallback(lambda a: (a, [], []))
|
|
||||||
return d
|
|
||||||
else:
|
|
||||||
answers = []
|
|
||||||
d.callback((answers, [], []))
|
|
||||||
if query.type != dns.AAAA:
|
|
||||||
print("Unsupported request", query)
|
|
||||||
return d
|
return d
|
||||||
|
elif query.type != dns.AAAA:
|
||||||
|
print("Unsupported request", query)
|
||||||
|
d.callback(([], [], []))
|
||||||
|
return d
|
||||||
|
|
||||||
def query(self, query, timeout=None):
|
def query(self, query, timeout=None):
|
||||||
if self._dynamicResponseRequired(query):
|
if self._dynamicResponseRequired(query):
|
||||||
|
|||||||
Reference in New Issue
Block a user