def recvd_ptr(result): answer, auth, additional = result if len(answer) != 1: raise LookupException("Not exactly one answer in PTR response for %s" % ip) hostname = str(answer[0].payload.name) def recvd_addr(result): answers, auth, additional = result if not answers: raise LookupException("No answers in A/AAAA response for %s" % hostname) addresses = [ inet_ntop(ipver, answer.payload.address) for answer in answers ] if ip not in addresses: raise VerificationException("IP mismatch: %s is not in %s (%s)" % (ip, repr(addresses), hostname)) d.callback(hostname) if ipobj.version == 4: client.lookupAddress(hostname, *args, **kwargs).addCallback(recvd_addr).addErrback(d.errback) else: client.lookupIPV6Address(hostname, *args, **kwargs).addCallback(recvd_addr).addErrback(d.errback)
def test_lookupIPV6Address(self): """ See L{test_lookupAddress} """ d = client.lookupIPV6Address(self.hostname) d.addCallback(self.checkResult, dns.AAAA) return d
def lookupAAAAs(hostname, *args, **kwargs): def callback(result): answer, auth, add = result answer = [x for x in answer if x.type == dns.AAAA] if len(answer) == 0: raise LookupException("No ANSWERS in AAAA response for %s." % repr(hostname)) return [expandIPv6(x.payload._address) for x in answer] return client.lookupIPV6Address(hostname, *args, **kwargs).addCallback(callback)
def recvd_ptr(result): answer, auth, additional = result if len(answer) != 1: raise LookupException( "Not exactly one answer in PTR response for %s" % ip) hostname = str(answer[0].payload.name) def recvd_addr(result): answers, auth, additional = result if not answers: raise LookupException("No answers in A/AAAA response for %s" % hostname) addresses = [ inet_ntop(ipver, answer.payload.address) for answer in answers ] if ip not in addresses: raise VerificationException( "IP mismatch: %s is not in %s (%s)" % (ip, repr(addresses), hostname)) d.callback(hostname) if ipobj.version == 4: client.lookupAddress(hostname, *args, **kwargs).addCallback(recvd_addr).addErrback( d.errback) else: client.lookupIPV6Address( hostname, *args, **kwargs).addCallback(recvd_addr).addErrback(d.errback)
def resolveHostname(self): """Attempts to resolve the server's hostname to an IP address for better reliability.""" timeout = [1, 2, 5] lookups = [] query = dns.Query(self.host, dns.A) lookups.append(client.lookupAddress(self.host, timeout ).addCallback(self._lookupFinished, socket.AF_INET, query)) query = dns.Query(self.host, dns.AAAA) lookups.append(client.lookupIPV6Address(self.host, timeout ).addCallback(self._lookupFinished, socket.AF_INET6, query)) return defer.DeferredList(lookups).addBoth(self._hostnameResolved)
def lookup_host(bot, user, channel, msg, guess_best): hostname = msg if len(hostname): if guess_best: d = client.getHostByName(hostname) d.addCallback(resultCb, hostname, bot, user, channel) d.addErrback(errorCb, hostname, bot, user, channel) else: pending = [] pending.append(client.lookupAddress(hostname)) pending.append(client.lookupIPV6Address(hostname)) allResults = defer.DeferredList(pending, consumeErrors=False) allResults.addCallback(resultCb, hostname, bot, user, channel) allResults.addErrback(errorCb, hostname, bot, user, channel) else: bot.msg(channel, 'Usage: !host <hostname>')
def resolveHostname(self): """Attempts to resolve the server's hostname to an IP address for better reliability.""" timeout = [1, 2, 5] lookups = [] query = dns.Query(self.host, dns.A) lookups.append( client.lookupAddress(self.host, timeout).addCallback(self._lookupFinished, socket.AF_INET, query)) query = dns.Query(self.host, dns.AAAA) lookups.append( client.lookupIPV6Address(self.host, timeout).addCallback( self._lookupFinished, socket.AF_INET6, query)) return defer.DeferredList(lookups).addBoth(self._hostnameResolved)
def implReverseProxyRenderKeepHost(self, request): """ Actual resource rendering. If client connects over IPv4: query C{DNS} for the host's C{AAAA} record. Or if client connects over IPv6: use the hostname of the webproxy. Use resulting IPv6 address to establish the TCP connection, but keep the hostname as value of the C{Host} HTTP header. A C{X-Forwarded-For} header is added to avoid circular references. """ if isinstance(request.client, address.IPv6Address ) and not request.client.host.startswith("::ffff:"): self.host = ip426webproxy dns_result = yield lookupIPV6Address(self.host) self.host = socket.inet_ntop(socket.AF_INET6, dns_result[0][0].payload.address) request.content.seek(0, 0) qs = urllib_parse.urlparse(request.uri)[4] if qs: rest = self.path + b"?" + qs else: rest = self.path headers = request.getAllHeaders() headers[b"x-forwarded-for"] = request.getClientIP().encode("utf-8") clientFactory = self.proxyClientFactoryClass( request.method, rest, request.clientproto, headers, request.content.read(), request, ) self.reactor.connectTCP(self.host, self.port, clientFactory)