httpns/client/run.py
Klaus-Uwe Mitterer ec2de76d77 Forgot self.
2017-10-09 19:57:50 +02:00

57 lines
1.4 KiB
Python

#!/usr/bin/env python3
import ipaddress
import dns.resolver
import dnslib
import dnslib.server
dnshandler = False
httphandler = False
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
SuperResolver.__init__(self, raddr, rport, timeout)
self.forwarding = forwarding
def resolve(self, req, handler):
rep = req.reply()
rep.header.rcode = getattr(dnslib.RCODE, "NXDOMAIN")
if self.forwarding and rep.header.rcode == getattr(dnslib.RCODE, "NXDOMAIN"):
rep = dnshandler.resolve(req, handler)
return rep
class DNSResolver(SuperResolver):
def __init__(self, raddr = "8.8.8.8", rport = 53, timeout = 0):
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()