async def get_resolver(self, options): if options['nameservers']: mem_resolvconf = StringIO() for n in options['nameservers']: mem_resolvconf.write(f"nameserver {n}\n") mem_resolvconf.seek(0) r = Resolver(mem_resolvconf) else: r = Resolver() r.timeout = options['timeout'] return r
async def request_ittr(self, addresses): self._results = {} dnsResolver = Resolver() dnsResolver.timeout = 2 tasks = [] for address in addresses: tasks.append(dnsResolver.resolve_address(address)) responses = await asyncio.gather(*tasks, return_exceptions=True) for response in responses: if type(response) is dns.resolver.Answer: addr = ".".join( reversed(response.canonical_name.to_text().replace( '.in-addr.arpa.', '').split('.'))) for item in response.response.answer: if type(item) is dns.rrset.RRset and len(item.items) > 0: self._results[addr] = str(list(item.items)[0])
async def check(domain: str, transaction: Hub.current.scope.transaction, rtype: str = "A") -> dns.rrset.RRset: with transaction.start_child(op="task", description="Cloudflare Check"): log.info(f"CloudFlare checking: '{domain}'") if domain.count(":") > 0: processed_url = tld.get_fld(domain, fix_protocol=True) log.info(f"CloudFlare processed: '{domain}' to '{processed_url}'") domain = processed_url rs = Resolver() rs.nameservers = ["1.1.1.2"] rs.timeout = 5 try: dns_res: dns.resolver.Answer = await rs.resolve(domain, rdtype=rtype) except NXDOMAIN: log.warning(f"Couldn't resolve: '{domain}'") return None return dns_res.rrset
async def request_ittr(self): dnsResolver = Resolver() dnsResolver.timeout = 2 collected_errors = set() while len(self._request_queue) > 0: tasks = [] while len(tasks) < self.batch_size and len( self._request_queue) > 0: hostname = self._request_queue.pop() if hostname not in self._requested: self._domains_queued += 1 # make sure we only request a host once for record_type in ['A', 'AAAA']: tasks.append(dnsResolver.resolve( hostname, record_type)) self._requested.add(hostname) if len(tasks) > 0: responses = await asyncio.gather(*tasks, return_exceptions=True) for response in responses: if type(response) is dns.resolver.Answer: for item in response.response.answer: if type(item) is dns.rrset.RRset: for addr in item.items: if addr.rdtype is RdataType.CNAME: # query cname (recursion) self._request_queue.append(addr.target) else: self._response.add(addr.address) elif type(response) in [ dns.resolver.NXDOMAIN, dns.exception.Timeout, dns.resolver.NoNameservers ]: if str(response) not in collected_errors: syslog.syslog( syslog.LOG_ERR, '%s [for %s]' % (response, self._origin)) collected_errors.add(str(response)) if self._domains_queued % self.report_size == 0: syslog.syslog( syslog.LOG_NOTICE, 'requested %d hostnames for %s' % (self._domains_queued, self._origin))