| 64 | |
| 65 | |
| 66 | class _SrvResolver: |
| 67 | def __init__( |
| 68 | self, |
| 69 | fqdn: str, |
| 70 | connect_timeout: Optional[float], |
| 71 | srv_service_name: str, |
| 72 | srv_max_hosts: int = 0, |
| 73 | ): |
| 74 | self.__fqdn = fqdn |
| 75 | self.__srv = srv_service_name |
| 76 | self.__connect_timeout = connect_timeout or CONNECT_TIMEOUT |
| 77 | self.__srv_max_hosts = srv_max_hosts or 0 |
| 78 | # Validate the fully qualified domain name. |
| 79 | try: |
| 80 | ipaddress.ip_address(fqdn) |
| 81 | raise ConfigurationError(_INVALID_HOST_MSG % ("an IP address",)) |
| 82 | except ValueError: |
| 83 | pass |
| 84 | try: |
| 85 | split_fqdn = self.__fqdn.split(".") |
| 86 | self.__plist = split_fqdn[1:] if len(split_fqdn) > 2 else split_fqdn |
| 87 | except Exception: |
| 88 | raise ConfigurationError(_INVALID_HOST_MSG % (fqdn,)) from None |
| 89 | self.__slen = len(self.__plist) |
| 90 | self.nparts = len(split_fqdn) |
| 91 | |
| 92 | async def get_options(self) -> Optional[str]: |
| 93 | from dns import resolver |
| 94 | |
| 95 | try: |
| 96 | results = await _resolve(self.__fqdn, "TXT", lifetime=self.__connect_timeout) |
| 97 | except (resolver.NoAnswer, resolver.NXDOMAIN): |
| 98 | # No TXT records |
| 99 | return None |
| 100 | except Exception as exc: |
| 101 | raise ConfigurationError(str(exc)) from exc |
| 102 | if len(results) > 1: |
| 103 | raise ConfigurationError("Only one TXT record is supported") |
| 104 | return (b"&".join([b"".join(res.strings) for res in results])).decode("utf-8") # type: ignore[attr-defined] |
| 105 | |
| 106 | async def _resolve_uri(self, encapsulate_errors: bool) -> resolver.Answer: |
| 107 | try: |
| 108 | results = await _resolve( |
| 109 | "_" + self.__srv + "._tcp." + self.__fqdn, "SRV", lifetime=self.__connect_timeout |
| 110 | ) |
| 111 | except Exception as exc: |
| 112 | if not encapsulate_errors: |
| 113 | # Raise the original error. |
| 114 | raise |
| 115 | # Else, raise all errors as ConfigurationError. |
| 116 | raise ConfigurationError(str(exc)) from exc |
| 117 | return results |
| 118 | |
| 119 | async def _get_srv_response_and_hosts( |
| 120 | self, encapsulate_errors: bool |
| 121 | ) -> tuple[resolver.Answer, list[tuple[str, Any]]]: |
| 122 | results = await self._resolve_uri(encapsulate_errors) |
| 123 |
no outgoing calls
no test coverage detected