Exemple #1
0
    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 dns_query(domain: str, rtype: str = 'TXT', **kwargs):
    try:
        resolver = Resolver()
        response = await resolver.resolve(domain, rdtype=rtype, **kwargs)
        return response.rrset
    except:
        return None
Exemple #3
0
 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])
Exemple #4
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
Exemple #5
0
 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))
Exemple #6
0
async def query(hostname: str) -> List[BaseDnsRecord]:
    """Quqery DNS records

    Arguments:
        hostname {str} -- A hostname to query

    Returns:
        List[BaseDnsRecord] -- A list of DNS records
    """
    resolver = Resolver()
    tasks = [
        partial(resolve, resolver, hostname, record_type)
        for record_type in TYPES
    ]
    results = await aiometer.run_all(tasks)
    return sum(results, [])
Exemple #7
0
    from dns.exception import DNSException
except ImportError:
    raise ImportError(
        "the module psycopg._dns requires the package 'dnspython' installed"
    )

from . import pq
from . import errors as e

if TYPE_CHECKING:
    from dns.rdtypes.IN.SRV import SRV

resolver = Resolver()
resolver.cache = Cache()

async_resolver = AsyncResolver()
async_resolver.cache = Cache()


async def resolve_hostaddr_async(params: Dict[str, Any]) -> Dict[str, Any]:
    """
    Perform async DNS lookup of the hosts and return a new params dict.

    :param params: The input parameters, for instance as returned by
        `~psycopg.conninfo.conninfo_to_dict()`.

    If a ``host`` param is present but not ``hostname``, resolve the host
    addresses dynamically.

    The function may change the input ``host``, ``hostname``, ``port`` to allow
    to connect without further DNS lookups, eventually removing hosts that are