httpns/client/run.py

58 lines
1.4 KiB
Python
Raw Permalink Normal View History

2017-10-09 10:50:20 +00:00
#!/usr/bin/env python3
import ipaddress
import dns.resolver
import dnslib
import dnslib.server
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):
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)
self.forwarding = forwarding
2017-10-09 10:50:20 +00:00
def resolve(self, req, handler):
rep = req.reply()
rep.header.rcode = getattr(dnslib.RCODE, "NXDOMAIN")
2017-10-09 17:57:50 +00:00
if self.forwarding and rep.header.rcode == getattr(dnslib.RCODE, "NXDOMAIN"):
rep = dnshandler.resolve(req, handler)
2017-10-09 10:50:20 +00:00
return rep
class DNSResolver(SuperResolver):
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
if __name__ == "__main__":
dnshandler = DNSResolver()
httphandler = HTTPResolver()