httpns/client/run.py

45 lines
1.1 KiB
Python
Raw 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
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):
2017-10-09 10:50:20 +00:00
SuperResolver.__init__(self, raddr, rport, timeout)
def resolve(self, req, handler):
rep = req.reply()
rep.header.rcode = getattr(dnslib.RCODE, "NXDOMAIN")
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