|
| 1 | +# Copyright (C) 2023-2025 Apple Inc. All rights reserved. |
| 2 | +# |
| 3 | +# Redistribution and use in source and binary forms, with or without |
| 4 | +# modification, are permitted provided that the following conditions |
| 5 | +# are met: |
| 6 | +# 1. Redistributions of source code must retain the above copyright |
| 7 | +# notice, this list of conditions and the following disclaimer. |
| 8 | +# 2. Redistributions in binary form must reproduce the above copyright |
| 9 | +# notice, this list of conditions and the following disclaimer in the |
| 10 | +# documentation and/or other materials provided with the distribution. |
| 11 | +# |
| 12 | +# THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS "AS IS" AND |
| 13 | +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 14 | +# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 15 | +# DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR |
| 16 | +# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 17 | +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 18 | +# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 19 | +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 20 | +# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 21 | +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 22 | + |
| 23 | +from dnslib import CLASS, QTYPE, RR, RCODE |
| 24 | +from dnslib.server import BaseResolver, DNSServer |
| 25 | +import logging |
| 26 | +from threading import Thread |
| 27 | + |
| 28 | +_log = logging.getLogger(__name__) |
| 29 | + |
| 30 | + |
| 31 | +class Resolver(BaseResolver): |
| 32 | + def __init__(self, allowed_hosts=[]): |
| 33 | + super().__init__() |
| 34 | + self.hosts = list(map(lambda x: x if x.endswith(".") else x + ".", allowed_hosts)) |
| 35 | + _log.debug("Initializing Resolver with hosts: {}".format(self.hosts)) |
| 36 | + |
| 37 | + def resolve(self, request, handler): |
| 38 | + question = request.q |
| 39 | + reply = request.reply() |
| 40 | + _log.debug("Received request for {}".format(question.qname)) |
| 41 | + if question.qtype == QTYPE.A and (question.qname in self.hosts or question.qname.matchSuffix("localhost")): |
| 42 | + reply.add_answer(*RR.fromZone("{} 3600 A 127.0.0.1".format(question.qname))) |
| 43 | + else: |
| 44 | + reply.header.rcode = getattr(RCODE, "NXDOMAIN") |
| 45 | + return reply |
| 46 | + |
| 47 | + |
| 48 | +if __name__ == "__main__": |
| 49 | + # This script is not intended to be run on its own, and should only be used for testing purposes. |
| 50 | + server = DNSServer(Resolver(["site.example"]), port=8053, address="127.0.0.1") |
| 51 | + server.start_thread() |
| 52 | + try: |
| 53 | + Thread.join() |
| 54 | + except Exception: |
| 55 | + pass |
| 56 | + finally: |
| 57 | + server.stop() |
0 commit comments