Skip to content

Instantly share code, notes, and snippets.

@misaka4e21
Created July 20, 2015 16:22
Show Gist options
  • Save misaka4e21/e8e7a8927c2293fc593b to your computer and use it in GitHub Desktop.
Save misaka4e21/e8e7a8927c2293fc593b to your computer and use it in GitHub Desktop.
A DNS server returns only IPv6 address when AAAA record is available.
#!/usr/bin/env python
# Copyright (C) R.O.C.2015 Misaka Network <[email protected]>
# License under WTFPL
from dnslib.server import *
from dnslib import *
class TestResolv:
def resolve(self,request,handler):
ipv4=False
if request.q.qtype == QTYPE.A:
ipv4=True
request.q.qtype=QTYPE.AAAA
fw=DNSRecord(q=request.q)
b=fw.send("2001:470:20::2",port=53,tcp=True,ipv6=True)
reply=request.reply()
answer = DNSRecord.parse(b)
result_a = []
result_aaaa = []
for i in answer.rr:
if i.rtype == QTYPE.AAAA:
result_aaaa.append(i)
elif i.rtype == QTYPE.A:
result_a.append(i)
else:
reply.add_answer(i)
if ipv4:
if result_aaaa == []:
request.q.qtype=QTYPE.A
b=DNSRecord(q=request.q).send("2001:470:20::2",tcp=True,ipv6=True)
for i in DNSRecord.parse(b).rr:
reply.add_answer(i)
return reply
else:
for i in result_aaaa:
reply.add_answer(i)
for i in reply.questions:
i.qtype=QTYPE.A
reply.set_header_qa()
return reply
resolver = TestResolv()
logger = DNSLogger(prefix=False)
server = DNSServer(resolver,port=53,address="0.0.0.0")
server.start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment