2017-10-09 10:50:20 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import ipaddress
|
|
|
|
|
|
|
|
import dns.resolver
|
|
|
|
import dnslib
|
|
|
|
import dnslib.server
|
|
|
|
|
2017-10-09 17:57:17 +00:00
|
|
|
dnshandler = False
|
|
|
|
httphandler = False
|
|
|
|
|
2017-10-09 10:50:20 +00:00
|
|
|
class SuperResolver(dnslib.server.BaseResolver):
|
|
|
|
def __init__(self, raddr, rport, timeout = 0):
|
|
|
|
self.raddr = raddr
|
|
|
|
self.rport = rport
|
|
|
|
self.timeout = timeout
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.rip = str(ipaddress.ip_address(raddr))
|
|
|
|
except:
|
|
|
|
resolver = dns.resolver.Resolver()
|
|
|
|
resolver.nameservers = ["8.8.8.8", "8.8.4.4"]
|
|
|
|
self.rip = str(resolver.query(raddr)[0])
|
|
|
|
|
|
|
|
class HTTPResolver(SuperResolver):
|
2017-10-09 17:57:17 +00:00
|
|
|
def __init__(self, raddr = "httpns.kumi.systems", rport = 9539, timeout = 0, forwarding = False):
|
|
|
|
global dnshandler
|
|
|
|
|
2017-10-09 10:50:20 +00:00
|
|
|
SuperResolver.__init__(self, raddr, rport, timeout)
|
2017-10-09 17:57:17 +00:00
|
|
|
self.forwarding = forwarding
|
2017-10-09 10:50:20 +00:00
|
|
|
|
|
|
|
def resolve(self, req, handler):
|
|
|
|
rep = req.reply()
|
2017-10-09 17:27:51 +00:00
|
|
|
rep.header.rcode = getattr(dnslib.RCODE, "NXDOMAIN")
|
2017-10-09 17:57:17 +00:00
|
|
|
|
2017-10-09 17:57:50 +00:00
|
|
|
if self.forwarding and rep.header.rcode == getattr(dnslib.RCODE, "NXDOMAIN"):
|
2017-10-09 17:57:17 +00:00
|
|
|
rep = dnshandler.resolve(req, handler)
|
|
|
|
|
2017-10-09 10:50:20 +00:00
|
|
|
return rep
|
|
|
|
|
|
|
|
class DNSResolver(SuperResolver):
|
2017-10-09 17:27:51 +00:00
|
|
|
def __init__(self, raddr = "8.8.8.8", rport = 53, timeout = 0):
|
2017-10-09 10:50:20 +00:00
|
|
|
SuperResolver.__init__(self, raddr, rport, timeout)
|
|
|
|
|
|
|
|
def resolve(self, req, handler):
|
|
|
|
try:
|
|
|
|
fwd = req.send(self.rip, self.rport, timeout = self.timeout)
|
|
|
|
rep = dnslib.DNSRecord.parse(fwd)
|
|
|
|
except:
|
|
|
|
rep = req.reply()
|
|
|
|
rep.header.rcode = getattr(dnslib.RCODE, "NXDOMAIN")
|
|
|
|
|
|
|
|
return rep
|
|
|
|
|
2017-10-09 17:57:17 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
dnshandler = DNSResolver()
|
|
|
|
httphandler = HTTPResolver()
|