Skip to content

Instantly share code, notes, and snippets.

@yunazuno
Created September 2, 2015 05:29
Show Gist options
  • Save yunazuno/e42341bb3ce85a464c80 to your computer and use it in GitHub Desktop.
Save yunazuno/e42341bb3ce85a464c80 to your computer and use it in GitHub Desktop.
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__(
self,
resolv=None, servers=None, timeout=(1, 3, 11, 45),
prefix="64:ff9b::"):
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)) |
int(ipaddr.IPv4Address(v4addr_str)))
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(
self._to_nat64_address(
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)
d.addCallback(self.six_to_four)
return d
def main():
"""
Run the server.
"""
factory = server.DNSServerFactory(
clients=[SixFourResolver(resolv='/etc/resolv.conf')]
)
protocol = dns.DNSDatagramProtocol(controller=factory)
reactor.listenUDP(10053, protocol)
reactor.listenTCP(10053, factory)
reactor.run()
if __name__ == '__main__':
raise SystemExit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment