| 78 | raise MissingNameServers() |
| 79 | |
| 80 | async def dns_request(self, flow: dns.DNSFlow) -> None: |
| 81 | if self._should_resolve(flow): |
| 82 | all_ip_lookups = ( |
| 83 | flow.request.query |
| 84 | and flow.request.op_code == dns.op_codes.QUERY |
| 85 | and flow.request.question |
| 86 | and flow.request.question.class_ == dns.classes.IN |
| 87 | and flow.request.question.type in (dns.types.A, dns.types.AAAA) |
| 88 | ) |
| 89 | if all_ip_lookups: |
| 90 | try: |
| 91 | flow.response = await self.resolve(flow.request) |
| 92 | except MissingNameServers: |
| 93 | flow.error = Error("Cannot resolve, dns_name_servers unknown.") |
| 94 | elif name_servers := self.name_servers(): |
| 95 | # For other records, the best we can do is to forward the query |
| 96 | # to an upstream server. |
| 97 | flow.server_conn.address = (name_servers[0], 53) |
| 98 | else: |
| 99 | flow.error = Error("Cannot resolve, dns_name_servers unknown.") |
| 100 | |
| 101 | @staticmethod |
| 102 | def _should_resolve(flow: dns.DNSFlow) -> bool: |