Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
A DNS64 resolver with AAAA filter-like behavior (similar to the Internet Shareing on OSX El Capitan)
#!/usr/bin/env python2
from twisted.internet import reactor
from twisted.names import client, dns, server
import ipaddr
import socket
class SixFourResolver(client.Resolver):
def __init__(
resolv=None, servers=None, timeout=(1, 3, 11, 45),
client.Resolver.__init__(self, resolv, servers, timeout)
self.prefix = prefix
def _to_nat64_address(self, v4addr_packed):
v4addr_str = socket.inet_ntoa(v4addr_packed)
v6addr = ipaddr.IPv6Address(
int(ipaddr.IPv6Address(self.prefix)) |
return v6addr.compressed
def six_to_four(self, message):
answers, authority, additional = message
for answer in answers:
if answer.type == dns.A:
answer.type = dns.AAAA
answer.payload = dns.Record_AAAA(
answer.payload.address), answer.payload.ttl)
return (answers, authority, additional)
def lookupIPV6Address(self, name, timeout=None):
d = self._lookup(name, dns.IN, dns.A, timeout)
return d
def main():
Run the server.
factory = server.DNSServerFactory(
protocol = dns.DNSDatagramProtocol(controller=factory)
reactor.listenUDP(10053, protocol)
reactor.listenTCP(10053, factory)
if __name__ == '__main__':
raise SystemExit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment