本文整理汇总了Python中dnslib.DNSRecord.add_question方法的典型用法代码示例。如果您正苦于以下问题:Python DNSRecord.add_question方法的具体用法?Python DNSRecord.add_question怎么用?Python DNSRecord.add_question使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类dnslib.DNSRecord
的用法示例。
在下文中一共展示了DNSRecord.add_question方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: manufactory_DNS
# 需要导入模块: from dnslib import DNSRecord [as 别名]
# 或者: from dnslib.DNSRecord import add_question [as 别名]
def manufactory_DNS(self):
response_packet = DNSRecord()
response_packet.header.id = self.query_id
response_packet.add_question(DNSQuestion(self.qname, self.qtype))
related_rr = filter(lambda rr: self.qname in rr, intercept_rr)
for answer in related_rr:
response_packet.add_answer(*RR.fromZone(answer))
self.response_packet = response_packet.pack().__str__()
示例2: send_dns_packet
# 需要导入模块: from dnslib import DNSRecord [as 别名]
# 或者: from dnslib.DNSRecord import add_question [as 别名]
def send_dns_packet(sock, server_addr, name, ip, id):
"""
Construct and send dns packet
"""
ans = DNSRecord(DNSHeader(id=id, qr=1))
ans.add_question(DNSQuestion(name))
ans.add_answer(RR(name, ttl=3600, rdata=A(ip)))
# print(ans)
sock.sendto(ans.pack(), (server_addr, 7131))
示例3: cache_hit
# 需要导入模块: from dnslib import DNSRecord [as 别名]
# 或者: from dnslib.DNSRecord import add_question [as 别名]
def cache_hit(self):
cache_key = "cache:{}:{}".format(self.qtype, self.qname)
cache_ret = cache_db.get(cache_key)
if cache_ret:
log.info("cache_hit: {}".format(self.query_id))
response_packet = DNSRecord()
response_packet.header.id = self.query_id
response_packet.add_question(DNSQuestion(self.qname))
for answer in loads(cache_ret):
response_packet.add_answer(answer)
log.info(response_packet.__str__())
log.info("DNS response id {}".format(self.query_id))
response_packet_str = response_packet.pack().__str__()
self.wfile.write(response_packet_str)
return True
示例4: send_request
# 需要导入模块: from dnslib import DNSRecord [as 别名]
# 或者: from dnslib.DNSRecord import add_question [as 别名]
def send_request(self, id, domain, server):
try:
d = DNSRecord(DNSHeader(id))
d.add_question(DNSQuestion(domain, QTYPE.A))
req4_pack = d.pack()
d = DNSRecord(DNSHeader(id))
d.add_question(DNSQuestion(domain, QTYPE.AAAA))
req6_pack = d.pack()
self.sock.sendto(req4_pack, (server, 53))
# xlog.debug("send req:%s to:%s", domain, server)
self.sock.sendto(req6_pack, (server, 53))
# xlog.debug("send req:%s to:%s", domain, server)
except Exception as e:
xlog.warn("send_request except:%r", e)
示例5: query_over_tcp
# 需要导入模块: from dnslib import DNSRecord [as 别名]
# 或者: from dnslib.DNSRecord import add_question [as 别名]
def query_over_tcp(self, domain, type=None):
if type is None:
types = [1, 28]
else:
types = [type]
ips = []
for t in types:
query_time = 0
for server_ip in self.dns_server.public_list:
query_time += 1
if query_time > 3:
break
try:
d = DNSRecord(DNSHeader())
d.add_question(DNSQuestion(domain, t))
a_pkt = d.send(server_ip, 53, tcp=True, timeout=1)
p = DNSRecord.parse(a_pkt)
if len(p.rr) == 0:
xlog.warn("query_over_tcp for %s type:%d server:%s return none",
domain, t, server_ip)
continue
for r in p.rr:
ip = str(r.rdata)
if "." in ip and g.ip_region.check_ip(ip):
cn = g.ip_region.cn
else:
cn = "XX"
ips.append(ip+"|"+cn)
break
except Exception as e:
xlog.warn("query_over_tcp %s type:%s server:%s except:%r", domain, type, server_ip,e)
if ips:
g.domain_cache.set_ips(domain, ips, type)
return ips
示例6: send_request
# 需要导入模块: from dnslib import DNSRecord [as 别名]
# 或者: from dnslib.DNSRecord import add_question [as 别名]
def send_request(self, id, domain):
try:
d = DNSRecord(DNSHeader(id))
d.add_question(DNSQuestion(domain, QTYPE.A))
req4_pack = d.pack()
d = DNSRecord()
d.add_question(DNSQuestion(domain, QTYPE.AAAA))
req6_pack = d.pack()
if "." in domain:
server = self.dns_server.get()
else:
server = self.dns_server.get_local_server()
self.sock.sendto(req4_pack, (server, 53))
# xlog.debug("send req:%s to:%s", domain, server)
self.sock.sendto(req6_pack, (server, 53))
# xlog.debug("send req:%s to:%s", domain, server)
except Exception as e:
xlog.warn("request dns except:%r", e)
示例7: recv_worker
# 需要导入模块: from dnslib import DNSRecord [as 别名]
# 或者: from dnslib.DNSRecord import add_question [as 别名]
def recv_worker(self):
while self.running:
try:
response, server = self.sock.recvfrom(8192)
except Exception as e:
# xlog.exception("sock.recvfrom except:%r", e)
continue
if not response:
continue
try:
p = DNSRecord.parse(response)
except Exception as e:
xlog.exception("parse response fail:%r", e)
continue
if len(p.questions) == 0:
xlog.warn("received response without question")
continue
id = p.header.id
if id not in self.waiters:
continue
que = self.waiters[id]
org_domain = que.domain
domain = str(p.questions[0].qname)
xlog.debug("recev %s from:%s domain:%s org:%s", len(p.rr), server, domain, org_domain)
ips = []
for r in p.rr:
ip = str(r.rdata)
if r.rtype == 5:
# CNAME
xlog.debug("local dns %s recv %s cname:%s from:%s", org_domain, domain, ip, server)
d = DNSRecord(DNSHeader(id))
d.add_question(DNSQuestion(ip, QTYPE.A))
req_pack = d.pack()
server = self.dns_server.get()
self.sock.sendto(req_pack, (server, 53))
d = DNSRecord()
d.add_question(DNSQuestion(ip, QTYPE.AAAA))
req_pack = d.pack()
server = self.dns_server.get()
self.sock.sendto(req_pack, (server, 53))
continue
if "." in ip and g.ip_region.check_ip(ip):
cn = g.ip_region.cn
else:
cn = "XX"
ips.append(ip+"|"+cn)
if len(ips):
g.domain_cache.set_ips(org_domain, ips)
que.notify_all()
xlog.info("DNS Client recv worker exit.")
self.sock.close()
示例8: serve
# 需要导入模块: from dnslib import DNSRecord [as 别名]
# 或者: from dnslib.DNSRecord import add_question [as 别名]
def serve(self):
try:
udpsocket = socket(AF_INET, SOCK_DGRAM)
udpsocket.bind((self.ip, self.port))
self.socket = udpsocket
logging.getLogger().debug("now serving on %s/%s" % (self.ip, self.port))
while self.stayAlive:
try:
data, addr = self.socket.recvfrom(512)
d = DNSRecord.parse(data)
# print "Question from ",addr
# print d
question = d.get_q()
qname = str(question.qname)
qtype = str(QTYPE[question.qtype]).upper()
try:
ansdict = self.rbldnsd.lookup(qname)
# logging.getLogger().debug("ansdict: %s"%ansdict)
if qtype == "SOA":
if ansdict["SOA"] != None:
response = DNSRecord(DNSHeader(id=d.header.id, gr=1, aa=1, ra=1, qr=1, q=d.get_q()))
soa = ansdict["SOA"]
packet = SOA()
packet.set_mname(soa[0])
packet.set_rname(soa[1])
packet.times = soa[2:]
if "SOATTL" in ansdict:
packet.ttl = ansdict["SOATTL"]
response.rr.append(packet)
self.socket.sendto(response.pack(), addr)
continue
else:
self.send_nxdomain(d, addr)
continue
elif qtype == "NS":
if ansdict["NS"] != None:
# TODO
pass
else:
self.send_nxdomain(d, addr)
continue
elif qtype == "A" or qtype == "TXT":
if "results" not in ansdict:
self.send_nxdomain(d, addr)
logging.getLogger().debug("client=%s q=%s %s -> NXDOMAIN" % (addr[0], qname, qtype))
continue
anslist = ansdict["results"]
anspacklist = []
for answer in anslist:
if answer == None:
continue
if qtype not in answer:
continue
packet = RR(
question.qname, question.qtype, rdata=RDMAP[QTYPE[question.qtype]](answer[qtype])
)
if "TTL" in answer and answer["TTL"] != None:
packet.ttl = answer["TTL"]
anspacklist.append(packet)
if len(anspacklist) > 0:
response = DNSRecord(
DNSHeader(id=d.header.id, bitmap=d.header.bitmap, aa=1, ra=0, qr=1, q=1)
)
response.add_question(question)
response.rr.extend(anspacklist)
response.set_header_qa()
# logging.getLogger().debug(response)
# make sure answer bit is set
# response.header.qr=1
self.socket.sendto(response.pack(), addr)
logging.getLogger().debug("client=%s q=%s %s -> NOERROR" % (addr[0], qname, qtype))
else:
self.send_nxdomain(d, addr)
logging.getLogger().debug("client=%s q=%s %s -> NXDOMAIN" % (addr[0], qname, qtype))
continue
else:
logging.getLogger().warning("unsupported qtype %s" % qtype)
except:
fmt = traceback.format_exc()
logging.getLogger().error(fmt)
self.send_servfail(d, addr)
continue
except Exception:
fmt = traceback.format_exc()
logging.getLogger().error(fmt)
except:
fmt = traceback.format_exc()
logging.getLogger().error("Could not start serversocket on %s/%s: %s" % (self.ip, self.port, fmt))
logging.getLogger().debug("serve() complete")
示例9: recv_worker
# 需要导入模块: from dnslib import DNSRecord [as 别名]
# 或者: from dnslib.DNSRecord import add_question [as 别名]
def recv_worker(self):
while self.running:
try:
try:
response, server = self.sock.recvfrom(8192)
server, port = server
except Exception as e:
# xlog.exception("sock.recvfrom except:%r", e)
continue
if not response:
continue
try:
p = DNSRecord.parse(response)
except Exception as e:
xlog.exception("dns client parse response fail:%r", e)
continue
if len(p.questions) == 0:
xlog.warn("received response without question")
continue
id = p.header.id
if id not in self.waiters:
continue
que = self.waiters[id]
org_domain = que.domain
domain = str(p.questions[0].qname)
xlog.debug("recev %s from:%s domain:%s org:%s", len(p.rr), server, domain, org_domain)
ips = []
for r in p.rr:
ip = str(r.rdata)
if r.rtype == 5:
# CNAME
if ip.endswith("."):
ip = ip[:-1]
if ip == domain:
xlog.warn("recv domain[%s] == ip[%s]", domain, ip)
continue
query_count = g.domain_cache.get_query_count(domain)
if query_count >= 50:
xlog.warn("%s ip:%s query_count:%d", domain, ip, query_count)
continue
g.domain_cache.add_query_count(domain)
xlog.debug("local dns %s recv %s cname:%s from:%s", org_domain, domain, ip, server)
d = DNSRecord(DNSHeader(id))
d.add_question(DNSQuestion(ip, QTYPE.A))
req_pack = d.pack()
self.sock.sendto(req_pack, (server, 53))
d = DNSRecord(DNSHeader(id))
d.add_question(DNSQuestion(ip, QTYPE.AAAA))
req_pack = d.pack()
self.sock.sendto(req_pack, (server, 53))
continue
if "." in ip and g.ip_region.check_ip(ip):
cn = g.ip_region.cn
else:
cn = "XX"
ips.append(ip+"|"+cn)
if ips:
que.put(ips)
except Exception as e:
xlog.exception("dns recv_worker except:%r", e)
xlog.info("DNS Client recv worker exit.")
self.sock.close()