本文整理汇总了Python中dnslib.DNSRecord.parse方法的典型用法代码示例。如果您正苦于以下问题:Python DNSRecord.parse方法的具体用法?Python DNSRecord.parse怎么用?Python DNSRecord.parse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类dnslib.DNSRecord
的用法示例。
在下文中一共展示了DNSRecord.parse方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_reply
# 需要导入模块: from dnslib import DNSRecord [as 别名]
# 或者: from dnslib.DNSRecord import parse [as 别名]
def get_reply(self,data):
global dns_cache
global args
host,port = self.server.resolver.address,self.server.resolver.port
request = DNSRecord.parse(data)
domain=str(request.q.qname)
if domain in dns_cache:
if time.time()<dns_cache[domain][0]:
if args is not None and args.verbose:
display("[i] domain %s served from cache"%domain)
rep=request.reply()
rep.add_answer(*dns_cache[domain][1])
return rep.pack()
if args is not None and args.verbose:
display("[i] domain %s requested using TCP server %s"%(domain, args.dns_server))
data = struct.pack("!H",len(data)) + data
response = send_tcp(data,host,port)
response = response[2:]
reply = DNSRecord.parse(response)
#print(repr(reply))
ttl=3600
try:
ttl=reply.rr[0].ttl
except Exception:
try:
ttl=reply.rr.ttl
except Exception:
pass
dns_cache[domain]=(int(time.time())+ttl, reply.rr)
if len(dns_cache)>DNS_CACHE_SIZE:
dns_cache.popitem(last=False)
return response
示例2: resolve
# 需要导入模块: from dnslib import DNSRecord [as 别名]
# 或者: from dnslib.DNSRecord import parse [as 别名]
def resolve(self, request, handler):
try:
proxy_r = request.send(self.address, self.port, tcp=True)
except socket.error:
return DNSRecord()
reply = DNSRecord.parse(proxy_r)
return reply
示例3: on_udp_query
# 需要导入模块: from dnslib import DNSRecord [as 别名]
# 或者: from dnslib.DNSRecord import parse [as 别名]
def on_udp_query(self, req_data, addr):
start_time = time.time()
try:
request = DNSRecord.parse(req_data)
if len(request.questions) != 1:
xlog.warn("query num:%d %s", len(request.questions), request)
return
domain = str(request.questions[0].qname)
type = request.questions[0].qtype
if type not in [1, 28]:
xlog.warn("query:%s type:%d", domain, type)
# xlog.debug("DNS query:%s type:%d from %s", domain, type, addr)
ips = self.query(domain, type)
reply = DNSRecord(DNSHeader(id=request.header.id, qr=1, aa=1, ra=1, auth=1), q=request.q)
for ip_cn in ips:
ipcn_p = ip_cn.split("|")
ip = ipcn_p[0]
if "." in ip and type == 1:
reply.add_answer(RR(domain, ttl=60, rdata=A(ip)))
elif ":" in ip and type == 28:
reply.add_answer(RR(domain, rtype=type, ttl=60, rdata=AAAA(ip)))
res_data = reply.pack()
self.serverSock.sendto(res_data, addr)
xlog.debug("query:%s type:%d from:%s, return ip num:%d cost:%d", domain, type, addr,
len(reply.rr), (time.time()-start_time)*1000)
except Exception as e:
xlog.exception("on_query except:%r", e)
示例4: resolve
# 需要导入模块: from dnslib import DNSRecord [as 别名]
# 或者: from dnslib.DNSRecord import parse [as 别名]
def resolve(self,request,handler):
reply = request.reply()
qname = request.q.qname
qtype = QTYPE[request.q.qtype]
# Try to resolve locally unless on skip list
if not any([qname.matchGlob(s) for s in self.skip]):
for name,rtype,rr in self.zone:
if qname.matchGlob(name) and (qtype in (rtype,'ANY','CNAME')):
a = copy.copy(rr)
a.rname = qname
reply.add_answer(a)
# Check for NXDOMAIN
if any([qname.matchGlob(s) for s in self.nxdomain]):
reply.header.rcode = getattr(RCODE,'NXDOMAIN')
return reply
# Otherwise proxy
if not reply.rr:
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.header.rcode = getattr(RCODE,'NXDOMAIN')
return reply
示例5: cache
# 需要导入模块: from dnslib import DNSRecord [as 别名]
# 或者: from dnslib.DNSRecord import parse [as 别名]
def cache(self, request, server):
v6enable = server[1][0].find('.') == -1
tcpenable = server[1][0] == '8.8.4.4'
proxy_r = request.send(server[1][0],server[1][1],tcp=tcpenable,timeout=self.timeout,ipv6=v6enable)
reply = DNSRecord.parse(proxy_r)
save_cache(server, str(request.q.qname)[:-1], reply)
return reply
示例6: query_server
# 需要导入模块: from dnslib import DNSRecord [as 别名]
# 或者: from dnslib.DNSRecord import parse [as 别名]
def query_server(server, packet, results, errors):
'Query a DNS server'
s = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
s.connect(server)
s.send(packet) # Forward the request to the nameserver
# Q: Why would we ever get this big of a packet in return?
# A: Because with IPv6, it's technically feasible
response, addr = s.recvfrom(65535)
try:
data = DNSRecord.parse(response) # Parse the response
except Exception:
return # For some reason the packet was bad
error = data.header.get_rcode()
if error:
errors.append(error) # There was an error with the request
return
try:
qtype = data.q.qtype
answers = [i.rdata for i in data.rr if i.rtype == qtype]
results.append(answers[0]) # Just add the first acceptable answer
except IndexError:
errors.append(3)
示例7: switched_to
# 需要导入模块: from dnslib import DNSRecord [as 别名]
# 或者: from dnslib.DNSRecord import parse [as 别名]
def switched_to(self, environment):
rtype, current_val = self.current_value
wanted_val = self.environments[environment]
if set(current_val) != set(wanted_val):
raise BespinError(
"The current value in ultradns is different than the specified value for this environment",
environment=environment,
ultradns_has=current_val,
specified=wanted_val,
)
log.info("Seeing if %s has switched to %s(%s)", self.domain, environment, current_val)
if rtype == "A":
info = socket.getaddrinfo(self.domain, 80)
found = [sockaddr[0] for _, _, _, _, sockaddr in info]
if set(found) == set(current_val):
return True
else:
log.info("Current value is %s", list(set(found)))
if rtype == "CNAME":
answer = DNSRecord.parse(DNSRecord(q=DNSQuestion(self.domain, QTYPE.CNAME)).send("8.8.8.8", 53)).short()
if not answer:
raise BespinError("couldn't resolve the domain", domain=self.domain)
if answer == current_val[0]:
return True
else:
log.info("Current value is %s", answer)
return False
示例8: dns_handler
# 需要导入模块: from dnslib import DNSRecord [as 别名]
# 或者: from dnslib.DNSRecord import parse [as 别名]
def dns_handler(s, peer, data):
request = DNSRecord.parse(data)
id = request.header.id
qname = request.q.qname
qtype = request.q.qtype
print "------ Request (%s): %r (%s)" % (str(peer),
qname.label, QTYPE[qtype])
print "\n".join([ " %s" % l for l in str(request).split("\n")])
reply = DNSRecord(DNSHeader(id=id, qr=1, aa=1, ra=1), q=request.q)
if qtype == QTYPE.A:
reply.add_answer(RR(qname, qtype, rdata=A(IP)))
if qtype == QTYPE.AAAA:
reply.add_answer(RR(qname, qtype, rdata=AAAA(IPV6)))
elif qtype == QTYPE['*']:
reply.add_answer(RR(qname, QTYPE.A, rdata=A(IP)))
reply.add_answer(RR(qname, QTYPE.MX, rdata=MX(IP)))
reply.add_answer(RR(qname, QTYPE.TXT, rdata=TXT(MSG)))
else:
reply.add_answer(RR(qname, QTYPE.CNAME, rdata=CNAME(MSG)))
print "------ Reply"
print "\n".join([ " %s" % l for l in str(reply).split("\n")])
s.sendto(reply.pack(), peer)
示例9: resolve
# 需要导入模块: from dnslib import DNSRecord [as 别名]
# 或者: from dnslib.DNSRecord import parse [as 别名]
def resolve(self,request,handler):
if handler.protocol == 'udp':
proxy_r = request.send(self.address,self.port)
else:
proxy_r = request.send(self.address,self.port,tcp=True)
reply = DNSRecord.parse(proxy_r)
return reply
示例10: lookup_upstream
# 需要导入模块: from dnslib import DNSRecord [as 别名]
# 或者: from dnslib.DNSRecord import parse [as 别名]
def lookup_upstream(request, server, proxy):
"""
use TCP mode when proxy enable
"""
reply = None
try:
message = '\tForward to server %(ip)s:%(port)s(%(priority)s)' % server
message += ' with %s mode' % ('TCP' if server['tcp'] else 'UDP')
if server['proxy'] and proxy:
message += ' and proxy %(type)s://%(ip)s:%(port)s' % proxy
logger.info(message)
r_data = sendto_upstream(
request.pack(),
server['ip'],
server['port'],
tcp=server['tcp'],
timeout=server['timeout'],
proxy=proxy if server['proxy'] else None,
)
reply = DNSRecord.parse(r_data)
if reply.rr:
logger.info('\tReturn from %(ip)s:%(port)s:' % server)
bogus_rr = []
for r in reply.rr:
rqn = r.rname
rqt = QTYPE[r.rtype]
if rqt in ['A', 'AAAA'] and str(r.rdata) in globalvars.bogus_nxdomain:
bogus_rr.append(r)
logger.warn('\t*** Bogus Answer: %s(%s) ***' % (r.rdata, rqt))
else:
logger.info('\t\t%s(%s)' % (r.rdata, rqt))
if bogus_rr:
for r in bogus_rr:
reply.rr.remove(r)
hack_ip = globalvars.config['smartdns']['bogus_nxdomain']['hack_ip']
if hack_ip:
rqt = 'AAAA' if ':' in hack_ip else 'A'
hack_r = RR(
rname=rqn,
rtype=getattr(QTYPE, rqt),
rclass=1, ttl=60 * 5,
rdata=getattr(dnslib, rqt)(hack_ip),
)
reply.rr.append(hack_r)
reply.set_header_qa()
else:
logger.info('\tReturn from %(ip)s:%(port)s: \n\t\tN/A' % server)
except socket.error as err:
frm = '%(ip)s:%(port)s(%(priority)s)' % server
if server['proxy']:
frm += ' (with proxy %(ip)s:%(port)s)' % proxy
logger.error('\tError when lookup from %s: %s' % (frm, err))
except Exception as err:
if logger.isEnabledFor(logging.DEBUG):
traceback.print_exc()
frm = '%(ip)s:%(port)s(%(priority)s)' % server
logger.error('\tError when lookup from %s: %s' % (frm, err))
return reply
示例11: get_reply
# 需要导入模块: from dnslib import DNSRecord [as 别名]
# 或者: from dnslib.DNSRecord import parse [as 别名]
def get_reply(self,data):
host,port = self.server.resolver.address,self.server.resolver.port
request = DNSRecord.parse(data)
self.log_request(request)
if self.protocol == 'tcp':
data = struct.pack("!H",len(data)) + data
response = send_tcp(data,host,port)
response = response[2:]
else:
response = send_udp(data,host,port)
reply = DNSRecord.parse(response)
self.log_reply(reply)
return response
示例12: handle
# 需要导入模块: from dnslib import DNSRecord [as 别名]
# 或者: from dnslib.DNSRecord import parse [as 别名]
def handle(self, data, peer):
rec = DNSRecord.parse(data)
addr = None
if rec.q.qtype in (QTYPE.A, QTYPE.AAAA):
addr = self._registry.resolve(rec.q.qname.idna())
if not addr:
addr = self._resolve('.'.join(rec.q.qname.label))
self.socket.sendto(self._reply(rec, addr), peer)
示例13: getRecord
# 需要导入模块: from dnslib import DNSRecord [as 别名]
# 或者: from dnslib.DNSRecord import parse [as 别名]
def getRecord(host, query):
d = DNSRecord.question(query, "A")
check, responce = queryNode(d.pack(), (host, 53))
try:
record = DNSRecord.parse(responce)
return record
except DNSError, e:
return False
示例14: send_req_to_server
# 需要导入模块: from dnslib import DNSRecord [as 别名]
# 或者: from dnslib.DNSRecord import parse [as 别名]
def send_req_to_server(server_addr, name):
client_req = DNSRecord(DNSHeader(id=ID), q=DNSQuestion(name, getattr(QTYPE, "A")))
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect((server_addr, 53))
s.send(client_req.pack())
resp = s.recv(2048)
dns_resp = DNSRecord.parse(resp)
logging.warning("Got answer from server {}".format(dns_resp))
示例15: _greenlet_runnable
# 需要导入模块: from dnslib import DNSRecord [as 别名]
# 或者: from dnslib.DNSRecord import parse [as 别名]
def _greenlet_runnable(self):
while True:
(buf, address) = self.sock.recvfrom(10240)
try:
record = DNSRecord.parse(buf)
except struct.error:
pass # log maybe later
else:
self.handle(record, address)