Improvements to the DNS plugin

This commit is contained in:
Daniel Dayley 2020-06-17 10:30:07 -06:00
parent 5d91b7366b
commit 3220276b7a

View File

@ -17,9 +17,7 @@ class ServiceDelegate(ServiceBase) :
def lookup(self,subject) :
# Note : hostname lookups always use system resolver.
# TODO: Make host lookups use the config-sepcified resolver.
# Note : some hostname lookups always use system resolver.
try :
subjecttuple = socket.gethostbyname_ex(subject)
# Can't make an IP of the subject
@ -33,30 +31,32 @@ class ServiceDelegate(ServiceBase) :
subjecttuple = [subject,[],['']]
# Can't make IPv4 out of it
except socket.gaierror :
self.error.append('Unable to resolve ' + subject)
self._error.append('Unable to resolve ' + subject)
subjecttuple = [subject,[],['']]
# Get additional records
return_dict = {}
mx = self.get_records(subjecttuple[0],'MX')
ns = self.get_records(subjecttuple[0],'NS')
txt = self.get_records(subjecttuple[0],'TXT')
mx.sort()
ns.sort()
txt.sort()
if mx and len(mx) > 0 :
return_dict.update({'mx':mx})
if ns and len(ns) > 0 :
return_dict.update({'ns':ns})
if txt and len(txt) > 0 :
return_dict.update({'txt':txt})
# Write final dictionary
for record_type in ['A','AAAA','CNAME','MX','NS','TXT'] :
recs = sorted(self.get_records(subject,record_type))
if recs and len(recs) > 0 :
return_dict.update({record_type.lower():recs})
# Combine Addresses
if len(subjecttuple[2]) > 0 and subjecttuple[2][0] != '':
return_dict['addresses'] = list(subjecttuple[2])
if subjecttuple[0] and subjecttuple[0] != '' :
if 'a' in return_dict :
return_dict['addresses'] += [x for x in return_dict['a'] if x != subjecttuple[2]]
if 'aaaa' in return_dict :
return_dict['addresses'] += [x for x in return_dict['aaaa'] if x != subjecttuple[2]]
if 'addresses' in return_dict :
return_dict['addresses'] = list(set(return_dict['addresses']))
# Include name and aliases
if subjecttuple[0] and subjecttuple[0] != '' and len(return_dict) > 0:
return_dict['name'] = subjecttuple[0]
if len(subjecttuple[1]) > 0 :
return_dict['aliases'] = list(subjecttuple[1])
return return_dict
if len(return_dict) > 0 :
return return_dict
else :
return None
def get_records(self,subject,recordtype) :
@ -64,7 +64,7 @@ class ServiceDelegate(ServiceBase) :
try :
results = self.resolver.query(subject,recordtype)
for result in results :
recs.append(result.to_text())
recs.append(result.to_text().strip('\"'))
return recs
except Exception as exception :
return recs