def normal_response(self, data): labels, _ = cache.unpack_name(data, 12) logger.debug("udp resolv:%s", '.'.join(labels)) sock = socket(AF_INET, SOCK_DGRAM) # socket for the remote DNS server sock.connect((DOMESTIC_DNS, REMOTE_UDP_DNS_PORT)) sock.sendall(data) sock.settimeout(5) try: resp = sock.recv(65535) except Exception, e: logging.debug('%s ignored.', e) return ''
def http_response(self, dns_req): logger.debug("req :%s", dns_req.encode("hex")) dns_header, dlen = unpack_dns_header(dns_req) name_start = dlen labels, name_end = unpack_name(dns_req, name_start) domain_name = '.'.join(labels) resp = httpdns_request(domain_name) logger.debug("ips:%s", str(resp)) resp_header = construct_resp_header(dns_header, len(resp)) QTYPE_SIZE = 2 QCLASS_SIZE = 2 queries = dns_req[name_start:name_end + QTYPE_SIZE + QCLASS_SIZE] records = ''.join([pack_a_record(name_start, r) for r in resp]) dns_resp = resp_header + queries + records logger.debug("resp:%s", dns_resp.encode("hex")) return dns_resp