Пример #1
0
def arctestdns(name):
    try:
        q = DNSRecord.question(name.decode("utf-8"), "TXT")
        a = q.send("localhost", int(sys.argv[2]))
        r = DNSRecord.parse(a)
        if not r.get_a().rdata:
            return None
        return "".join([x.decode('utf-8') for x in r.get_a().rdata.data])
    except:
        return None
Пример #2
0
    def resolve(self, request, handler):
        """
        If the requested domain is local, resolve it to localhost.
        Otherwise, proxy or drop the request.
        """

        reply = request.reply()
        qname = request.q.qname

        with self.lock:
            local_domain = str(qname) in self.domains

        if local_domain:  # If we have to handle this domain:
            # Replace labels with request label
            for rr in self.rrs:
                a = copy.copy(rr)
                a.rname = qname
                reply.add_answer(a)
        else:  # Otherwise proxy to upstream
            if not self.proxy:
                # Signal to the handler that this request has to be ignored.
                raise DNSError(
                    "{} not in local domain list and upstream proxy disabled.".format(str(qname))
                )
            else:
                if handler.protocol == 'udp':
                    proxy_r = request.send(self.upstream, self.upstream_port)
                else:
                    proxy_r = request.send(
                        self.upstream, self.upstream_port, tcp=True)
                reply = DNSRecord.parse(proxy_r)

        return reply
Пример #3
0
 def resolve(self, request, handler):
     if request.q.qtype != QTYPE.A:  # Only IPv4 is supported
         reply = request.reply()
         reply.header.rcode = getattr(RCODE, 'NXDOMAIN')
         return reply
     hostname = str(request.q.qname)
     if hostname.endswith(".local."):
         reply = request.reply()
         address = address = self._mdns_resolver.resolve(hostname)
         if address:
             rr = RR(rname=request.q.qname,
                     rtype=request.q.qtype,
                     rdata=dns.A(address),
                     ttl=300)
             reply.add_answer(rr)
         else:
             reply.header.rcode = getattr(RCODE, 'NXDOMAIN')
         return reply
     else:
         try:
             if handler.protocol == 'udp':
                 proxy_r = request.send(self.address,
                                        self.port,
                                        timeout=self.timeout)
             else:
                 proxy_r = request.send(self.address,
                                        self.port,
                                        tcp=True,
                                        timeout=self.timeout)
             reply = DNSRecord.parse(proxy_r)
         except socket.timeout:
             reply = request.reply()
             reply.header.rcode = getattr(RCODE, 'NXDOMAIN')
         return reply
Пример #4
0
 def log_recv(self, handler, data):
     """
     保存请求原始报文,暂不处理
     :param handler: 
     :param data: 
     :return: 
     """
     """
     print("%sReceived: [%s:%d] (%s) <%d> : %s" % (
         self.log_prefix(handler),
         handler.client_address[0],
         handler.client_address[1],
         handler.protocol,
         len(data),
         binascii.hexlify(data)))
     """
     self.logger.info("*******client*******")
     try:
         # bin = binascii.hexlify(data).decode()
         # packet = binascii.unhexlify(bin)
         message = DNSRecord.parse(data)
         print(message)
         # self.logger.info("\n" + message)
     except UnicodeDecodeError as e:
         pass
     self.logger.info("*******client*******")
     pass
Пример #5
0
    def resolve(self, request, handler):
        """
        If the requested domain is local, resolve it to localhost.
        Otherwise, proxy or drop the request.
        """

        reply = request.reply()
        qname = request.q.qname

        with self.lock:
            local_domain = str(qname) in self.domains

        if local_domain:  # If we have to handle this domain:
            # Replace labels with request label
            for rr in self.rrs:
                a = copy.copy(rr)
                a.rname = qname
                reply.add_answer(a)
        else:  # Otherwise proxy to upstream
            if not self.proxy:
                # Signal to the handler that this request has to be ignored.
                raise DNSError(
                    "{} not in local domain list and upstream proxy disabled.".
                    format(str(qname)))
            else:
                if handler.protocol == 'udp':
                    proxy_r = request.send(self.upstream, self.upstream_port)
                else:
                    proxy_r = request.send(self.upstream,
                                           self.upstream_port,
                                           tcp=True)
                reply = DNSRecord.parse(proxy_r)

        return reply
Пример #6
0
    def resolve(self, request, handler):
        # Build a request reply.
        reply = request.reply()
        # If it is of type A, IPv4:
        if request.q.qtype == QTYPE.A:
            addr = "-4"
            ip_cls = dns.A  # Class to wrap the ip string with.
        # If it is of type AAAA, IPv6:
        elif request.q.qtype == QTYPE.AAAA:
            addr = "-6"
            ip_cls = dns.AAAA  # Class to wrap the ip string with.
        else:
            # Return nothing.
            return reply

        # Strip trailing period.
        host = str(request.q.qname).rstrip(".")

        if not host.endswith(".local"):
            a = DNSRecord.parse(DNSRecord.question(host).send("8.8.8.8", 53))
            for rr in a.rr:
                if rr.rtype == request.q.qtype:
                    reply.add_answer(rr)
            return reply

        try:
            # Use avahi-resolve to determine the .local host IP address.
            result = subprocess.check_output(
                ["avahi-resolve", "--name", addr, host], timeout=1)
            # Parse output.
            [host, ip] = result.decode("UTF-8").strip("\n").split("\t")
            # Build a result and add the answer to the reply.
            rr = RR(
                rname=request.q.qname,
                rtype=request.q.qtype,
                rdata=ip_cls(ip),
                ttl=300,
            )
            reply.add_answer(rr)
        except subprocess.TimeoutExpired:
            # Time out == host not found.
            pass
        except BaseException:
            raise
        # Return the reply.
        return reply
Пример #7
0
 def resolve(self, request, handler):
     reply = request.reply()
     if request.q.qtype == 15:
         mxrr = RR.fromZone("{} IN MX 10 mail.{}".format(
             request.q.qname, request.q.qname))
         reply.add_answer(*mxrr)
     elif request.q.qtype == 1 and "mail." in str(request.q.qname):
         arr = RR.fromZone("{} IN A {}".format(request.q.qname,
                                               DEST_SERVER))
         reply.add_answer(*arr)
     else:
         try:
             if handler.protocol == 'udp':
                 proxy_r = request.send('1.1.1.1', 53, timeout=10)
             else:
                 proxy_r = request.send('1.1.1.1', 53, tcp=True, timeout=10)
             reply = DNSRecord.parse(proxy_r)
         except socket.timeout:
             reply.header.rcode = getattr(RCODE, 'NXDOMAIN')
     return reply