asyncresolver.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. # Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license
  2. # Copyright (C) 2003-2017 Nominum, Inc.
  3. #
  4. # Permission to use, copy, modify, and distribute this software and its
  5. # documentation for any purpose with or without fee is hereby granted,
  6. # provided that the above copyright notice and this permission notice
  7. # appear in all copies.
  8. #
  9. # THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES
  10. # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR
  12. # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  15. # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. """Asynchronous DNS stub resolver."""
  17. import socket
  18. import time
  19. from typing import Any, Dict, List, Optional, Union
  20. import dns._ddr
  21. import dns.asyncbackend
  22. import dns.asyncquery
  23. import dns.exception
  24. import dns.name
  25. import dns.query
  26. import dns.rdataclass
  27. import dns.rdatatype
  28. import dns.resolver # lgtm[py/import-and-import-from]
  29. # import some resolver symbols for brevity
  30. from dns.resolver import NXDOMAIN, NoAnswer, NoRootSOA, NotAbsolute
  31. # for indentation purposes below
  32. _udp = dns.asyncquery.udp
  33. _tcp = dns.asyncquery.tcp
  34. class Resolver(dns.resolver.BaseResolver):
  35. """Asynchronous DNS stub resolver."""
  36. async def resolve(
  37. self,
  38. qname: Union[dns.name.Name, str],
  39. rdtype: Union[dns.rdatatype.RdataType, str] = dns.rdatatype.A,
  40. rdclass: Union[dns.rdataclass.RdataClass, str] = dns.rdataclass.IN,
  41. tcp: bool = False,
  42. source: Optional[str] = None,
  43. raise_on_no_answer: bool = True,
  44. source_port: int = 0,
  45. lifetime: Optional[float] = None,
  46. search: Optional[bool] = None,
  47. backend: Optional[dns.asyncbackend.Backend] = None,
  48. ) -> dns.resolver.Answer:
  49. """Query nameservers asynchronously to find the answer to the question.
  50. *backend*, a ``dns.asyncbackend.Backend``, or ``None``. If ``None``,
  51. the default, then dnspython will use the default backend.
  52. See :py:func:`dns.resolver.Resolver.resolve()` for the
  53. documentation of the other parameters, exceptions, and return
  54. type of this method.
  55. """
  56. resolution = dns.resolver._Resolution(
  57. self, qname, rdtype, rdclass, tcp, raise_on_no_answer, search
  58. )
  59. if not backend:
  60. backend = dns.asyncbackend.get_default_backend()
  61. start = time.time()
  62. while True:
  63. (request, answer) = resolution.next_request()
  64. # Note we need to say "if answer is not None" and not just
  65. # "if answer" because answer implements __len__, and python
  66. # will call that. We want to return if we have an answer
  67. # object, including in cases where its length is 0.
  68. if answer is not None:
  69. # cache hit!
  70. return answer
  71. assert request is not None # needed for type checking
  72. done = False
  73. while not done:
  74. (nameserver, tcp, backoff) = resolution.next_nameserver()
  75. if backoff:
  76. await backend.sleep(backoff)
  77. timeout = self._compute_timeout(start, lifetime, resolution.errors)
  78. try:
  79. response = await nameserver.async_query(
  80. request,
  81. timeout=timeout,
  82. source=source,
  83. source_port=source_port,
  84. max_size=tcp,
  85. backend=backend,
  86. )
  87. except Exception as ex:
  88. (_, done) = resolution.query_result(None, ex)
  89. continue
  90. (answer, done) = resolution.query_result(response, None)
  91. # Note we need to say "if answer is not None" and not just
  92. # "if answer" because answer implements __len__, and python
  93. # will call that. We want to return if we have an answer
  94. # object, including in cases where its length is 0.
  95. if answer is not None:
  96. return answer
  97. async def resolve_address(
  98. self, ipaddr: str, *args: Any, **kwargs: Any
  99. ) -> dns.resolver.Answer:
  100. """Use an asynchronous resolver to run a reverse query for PTR
  101. records.
  102. This utilizes the resolve() method to perform a PTR lookup on the
  103. specified IP address.
  104. *ipaddr*, a ``str``, the IPv4 or IPv6 address you want to get
  105. the PTR record for.
  106. All other arguments that can be passed to the resolve() function
  107. except for rdtype and rdclass are also supported by this
  108. function.
  109. """
  110. # We make a modified kwargs for type checking happiness, as otherwise
  111. # we get a legit warning about possibly having rdtype and rdclass
  112. # in the kwargs more than once.
  113. modified_kwargs: Dict[str, Any] = {}
  114. modified_kwargs.update(kwargs)
  115. modified_kwargs["rdtype"] = dns.rdatatype.PTR
  116. modified_kwargs["rdclass"] = dns.rdataclass.IN
  117. return await self.resolve(
  118. dns.reversename.from_address(ipaddr), *args, **modified_kwargs
  119. )
  120. async def resolve_name(
  121. self,
  122. name: Union[dns.name.Name, str],
  123. family: int = socket.AF_UNSPEC,
  124. **kwargs: Any,
  125. ) -> dns.resolver.HostAnswers:
  126. """Use an asynchronous resolver to query for address records.
  127. This utilizes the resolve() method to perform A and/or AAAA lookups on
  128. the specified name.
  129. *qname*, a ``dns.name.Name`` or ``str``, the name to resolve.
  130. *family*, an ``int``, the address family. If socket.AF_UNSPEC
  131. (the default), both A and AAAA records will be retrieved.
  132. All other arguments that can be passed to the resolve() function
  133. except for rdtype and rdclass are also supported by this
  134. function.
  135. """
  136. # We make a modified kwargs for type checking happiness, as otherwise
  137. # we get a legit warning about possibly having rdtype and rdclass
  138. # in the kwargs more than once.
  139. modified_kwargs: Dict[str, Any] = {}
  140. modified_kwargs.update(kwargs)
  141. modified_kwargs.pop("rdtype", None)
  142. modified_kwargs["rdclass"] = dns.rdataclass.IN
  143. if family == socket.AF_INET:
  144. v4 = await self.resolve(name, dns.rdatatype.A, **modified_kwargs)
  145. return dns.resolver.HostAnswers.make(v4=v4)
  146. elif family == socket.AF_INET6:
  147. v6 = await self.resolve(name, dns.rdatatype.AAAA, **modified_kwargs)
  148. return dns.resolver.HostAnswers.make(v6=v6)
  149. elif family != socket.AF_UNSPEC:
  150. raise NotImplementedError(f"unknown address family {family}")
  151. raise_on_no_answer = modified_kwargs.pop("raise_on_no_answer", True)
  152. lifetime = modified_kwargs.pop("lifetime", None)
  153. start = time.time()
  154. v6 = await self.resolve(
  155. name,
  156. dns.rdatatype.AAAA,
  157. raise_on_no_answer=False,
  158. lifetime=self._compute_timeout(start, lifetime),
  159. **modified_kwargs,
  160. )
  161. # Note that setting name ensures we query the same name
  162. # for A as we did for AAAA. (This is just in case search lists
  163. # are active by default in the resolver configuration and
  164. # we might be talking to a server that says NXDOMAIN when it
  165. # wants to say NOERROR no data.
  166. name = v6.qname
  167. v4 = await self.resolve(
  168. name,
  169. dns.rdatatype.A,
  170. raise_on_no_answer=False,
  171. lifetime=self._compute_timeout(start, lifetime),
  172. **modified_kwargs,
  173. )
  174. answers = dns.resolver.HostAnswers.make(
  175. v6=v6, v4=v4, add_empty=not raise_on_no_answer
  176. )
  177. if not answers:
  178. raise NoAnswer(response=v6.response)
  179. return answers
  180. # pylint: disable=redefined-outer-name
  181. async def canonical_name(self, name: Union[dns.name.Name, str]) -> dns.name.Name:
  182. """Determine the canonical name of *name*.
  183. The canonical name is the name the resolver uses for queries
  184. after all CNAME and DNAME renamings have been applied.
  185. *name*, a ``dns.name.Name`` or ``str``, the query name.
  186. This method can raise any exception that ``resolve()`` can
  187. raise, other than ``dns.resolver.NoAnswer`` and
  188. ``dns.resolver.NXDOMAIN``.
  189. Returns a ``dns.name.Name``.
  190. """
  191. try:
  192. answer = await self.resolve(name, raise_on_no_answer=False)
  193. canonical_name = answer.canonical_name
  194. except dns.resolver.NXDOMAIN as e:
  195. canonical_name = e.canonical_name
  196. return canonical_name
  197. async def try_ddr(self, lifetime: float = 5.0) -> None:
  198. """Try to update the resolver's nameservers using Discovery of Designated
  199. Resolvers (DDR). If successful, the resolver will subsequently use
  200. DNS-over-HTTPS or DNS-over-TLS for future queries.
  201. *lifetime*, a float, is the maximum time to spend attempting DDR. The default
  202. is 5 seconds.
  203. If the SVCB query is successful and results in a non-empty list of nameservers,
  204. then the resolver's nameservers are set to the returned servers in priority
  205. order.
  206. The current implementation does not use any address hints from the SVCB record,
  207. nor does it resolve addresses for the SCVB target name, rather it assumes that
  208. the bootstrap nameserver will always be one of the addresses and uses it.
  209. A future revision to the code may offer fuller support. The code verifies that
  210. the bootstrap nameserver is in the Subject Alternative Name field of the
  211. TLS certficate.
  212. """
  213. try:
  214. expiration = time.time() + lifetime
  215. answer = await self.resolve(
  216. dns._ddr._local_resolver_name, "svcb", lifetime=lifetime
  217. )
  218. timeout = dns.query._remaining(expiration)
  219. nameservers = await dns._ddr._get_nameservers_async(answer, timeout)
  220. if len(nameservers) > 0:
  221. self.nameservers = nameservers
  222. except Exception:
  223. pass
  224. default_resolver = None
  225. def get_default_resolver() -> Resolver:
  226. """Get the default asynchronous resolver, initializing it if necessary."""
  227. if default_resolver is None:
  228. reset_default_resolver()
  229. assert default_resolver is not None
  230. return default_resolver
  231. def reset_default_resolver() -> None:
  232. """Re-initialize default asynchronous resolver.
  233. Note that the resolver configuration (i.e. /etc/resolv.conf on UNIX
  234. systems) will be re-read immediately.
  235. """
  236. global default_resolver
  237. default_resolver = Resolver()
  238. async def resolve(
  239. qname: Union[dns.name.Name, str],
  240. rdtype: Union[dns.rdatatype.RdataType, str] = dns.rdatatype.A,
  241. rdclass: Union[dns.rdataclass.RdataClass, str] = dns.rdataclass.IN,
  242. tcp: bool = False,
  243. source: Optional[str] = None,
  244. raise_on_no_answer: bool = True,
  245. source_port: int = 0,
  246. lifetime: Optional[float] = None,
  247. search: Optional[bool] = None,
  248. backend: Optional[dns.asyncbackend.Backend] = None,
  249. ) -> dns.resolver.Answer:
  250. """Query nameservers asynchronously to find the answer to the question.
  251. This is a convenience function that uses the default resolver
  252. object to make the query.
  253. See :py:func:`dns.asyncresolver.Resolver.resolve` for more
  254. information on the parameters.
  255. """
  256. return await get_default_resolver().resolve(
  257. qname,
  258. rdtype,
  259. rdclass,
  260. tcp,
  261. source,
  262. raise_on_no_answer,
  263. source_port,
  264. lifetime,
  265. search,
  266. backend,
  267. )
  268. async def resolve_address(
  269. ipaddr: str, *args: Any, **kwargs: Any
  270. ) -> dns.resolver.Answer:
  271. """Use a resolver to run a reverse query for PTR records.
  272. See :py:func:`dns.asyncresolver.Resolver.resolve_address` for more
  273. information on the parameters.
  274. """
  275. return await get_default_resolver().resolve_address(ipaddr, *args, **kwargs)
  276. async def resolve_name(
  277. name: Union[dns.name.Name, str], family: int = socket.AF_UNSPEC, **kwargs: Any
  278. ) -> dns.resolver.HostAnswers:
  279. """Use a resolver to asynchronously query for address records.
  280. See :py:func:`dns.asyncresolver.Resolver.resolve_name` for more
  281. information on the parameters.
  282. """
  283. return await get_default_resolver().resolve_name(name, family, **kwargs)
  284. async def canonical_name(name: Union[dns.name.Name, str]) -> dns.name.Name:
  285. """Determine the canonical name of *name*.
  286. See :py:func:`dns.resolver.Resolver.canonical_name` for more
  287. information on the parameters and possible exceptions.
  288. """
  289. return await get_default_resolver().canonical_name(name)
  290. async def try_ddr(timeout: float = 5.0) -> None:
  291. """Try to update the default resolver's nameservers using Discovery of Designated
  292. Resolvers (DDR). If successful, the resolver will subsequently use
  293. DNS-over-HTTPS or DNS-over-TLS for future queries.
  294. See :py:func:`dns.resolver.Resolver.try_ddr` for more information.
  295. """
  296. return await get_default_resolver().try_ddr(timeout)
  297. async def zone_for_name(
  298. name: Union[dns.name.Name, str],
  299. rdclass: dns.rdataclass.RdataClass = dns.rdataclass.IN,
  300. tcp: bool = False,
  301. resolver: Optional[Resolver] = None,
  302. backend: Optional[dns.asyncbackend.Backend] = None,
  303. ) -> dns.name.Name:
  304. """Find the name of the zone which contains the specified name.
  305. See :py:func:`dns.resolver.Resolver.zone_for_name` for more
  306. information on the parameters and possible exceptions.
  307. """
  308. if isinstance(name, str):
  309. name = dns.name.from_text(name, dns.name.root)
  310. if resolver is None:
  311. resolver = get_default_resolver()
  312. if not name.is_absolute():
  313. raise NotAbsolute(name)
  314. while True:
  315. try:
  316. answer = await resolver.resolve(
  317. name, dns.rdatatype.SOA, rdclass, tcp, backend=backend
  318. )
  319. assert answer.rrset is not None
  320. if answer.rrset.name == name:
  321. return name
  322. # otherwise we were CNAMEd or DNAMEd and need to look higher
  323. except (NXDOMAIN, NoAnswer):
  324. pass
  325. try:
  326. name = name.parent()
  327. except dns.name.NoParent: # pragma: no cover
  328. raise NoRootSOA
  329. async def make_resolver_at(
  330. where: Union[dns.name.Name, str],
  331. port: int = 53,
  332. family: int = socket.AF_UNSPEC,
  333. resolver: Optional[Resolver] = None,
  334. ) -> Resolver:
  335. """Make a stub resolver using the specified destination as the full resolver.
  336. *where*, a ``dns.name.Name`` or ``str`` the domain name or IP address of the
  337. full resolver.
  338. *port*, an ``int``, the port to use. If not specified, the default is 53.
  339. *family*, an ``int``, the address family to use. This parameter is used if
  340. *where* is not an address. The default is ``socket.AF_UNSPEC`` in which case
  341. the first address returned by ``resolve_name()`` will be used, otherwise the
  342. first address of the specified family will be used.
  343. *resolver*, a ``dns.asyncresolver.Resolver`` or ``None``, the resolver to use for
  344. resolution of hostnames. If not specified, the default resolver will be used.
  345. Returns a ``dns.resolver.Resolver`` or raises an exception.
  346. """
  347. if resolver is None:
  348. resolver = get_default_resolver()
  349. nameservers: List[Union[str, dns.nameserver.Nameserver]] = []
  350. if isinstance(where, str) and dns.inet.is_address(where):
  351. nameservers.append(dns.nameserver.Do53Nameserver(where, port))
  352. else:
  353. answers = await resolver.resolve_name(where, family)
  354. for address in answers.addresses():
  355. nameservers.append(dns.nameserver.Do53Nameserver(address, port))
  356. res = dns.asyncresolver.Resolver(configure=False)
  357. res.nameservers = nameservers
  358. return res
  359. async def resolve_at(
  360. where: Union[dns.name.Name, str],
  361. qname: Union[dns.name.Name, str],
  362. rdtype: Union[dns.rdatatype.RdataType, str] = dns.rdatatype.A,
  363. rdclass: Union[dns.rdataclass.RdataClass, str] = dns.rdataclass.IN,
  364. tcp: bool = False,
  365. source: Optional[str] = None,
  366. raise_on_no_answer: bool = True,
  367. source_port: int = 0,
  368. lifetime: Optional[float] = None,
  369. search: Optional[bool] = None,
  370. backend: Optional[dns.asyncbackend.Backend] = None,
  371. port: int = 53,
  372. family: int = socket.AF_UNSPEC,
  373. resolver: Optional[Resolver] = None,
  374. ) -> dns.resolver.Answer:
  375. """Query nameservers to find the answer to the question.
  376. This is a convenience function that calls ``dns.asyncresolver.make_resolver_at()``
  377. to make a resolver, and then uses it to resolve the query.
  378. See ``dns.asyncresolver.Resolver.resolve`` for more information on the resolution
  379. parameters, and ``dns.asyncresolver.make_resolver_at`` for information about the
  380. resolver parameters *where*, *port*, *family*, and *resolver*.
  381. If making more than one query, it is more efficient to call
  382. ``dns.asyncresolver.make_resolver_at()`` and then use that resolver for the queries
  383. instead of calling ``resolve_at()`` multiple times.
  384. """
  385. res = await make_resolver_at(where, port, family, resolver)
  386. return await res.resolve(
  387. qname,
  388. rdtype,
  389. rdclass,
  390. tcp,
  391. source,
  392. raise_on_no_answer,
  393. source_port,
  394. lifetime,
  395. search,
  396. backend,
  397. )