government filter

So i hear the UK government wants to make a porn filter. About bloody time i reckon. I’m fed up of happily browsing the Internet for boobs, only to have non-porn related subject matter thrust down my face hole.

So taking inspiration from other great Internet filtering nations such as North Korea, China, Syria, Iran, Cuba, Bahrain, Belarus, Burma, Uzbekistan, Saudia Arabia and Vietnam I decided to help out the UK government and build an Internet filter that only allows pornographic material through.

You’re Welcome.

Setting Up

All code is available here

Just follow this simple step-by-step video walk-through and you’ll have a porn filter running in no time!

Nerd Stuff

The filter is a dns server which checks all queries against the OpenDNS FamilySheild DNS server. Any request that is denied by OpenDNS is then allowed by our DNS server, and any request allowed by OpenDNS is blocked by us.

The server itself it built using the python Twisted framework which handles both the DNS requests and acts as a simple web-server to host the denial page.

#!/usr/bin/env python
# is a simple DNS server which only allows pornographic material through.
# Bits and pieces robbed from here:
import socket
import dns.resolver as DNS
from twisted.internet.protocol import Factory, Protocol
from twisted.internet import reactor
from twisted.names import dns
from twisted.names import client, server
from twisted.web import server as webserver
from twisted.web import resource
import sys
index = open('index.html').read()

class Simple(resource.Resource):
    isLeaf = True
    def render_GET(self, request):
        return index

class DNSServerFactory(server.DNSServerFactory):

    def gotResolverResponse(self, (ans, auth, add), protocol, message, address):
        qname = message.queries[0]
        print qname
        blocked_resolver = DNS.Resolver()
        blocked_resolver.nameservers = ['','']

        porn = False
        results = blocked_resolver.query(qname)
        for r in results:
          print r
          if str(r).startswith('67.215.65'):
            print "PRON"
            porn = True
        if porn == False:
          print "NOT PRON"
          for answer in ans:
            if answer.type != dns.A:
            answer.payload.address = socket.inet_aton(ip_address)
            answer.payload.ttl = 60      
        #address = ('', 43160)
        args = (self, (ans, auth, add), protocol, message, address)
        print result
        return result
verbosity = 0

ip_address = ""
if len(sys.argv) > 1:
    ip_address = sys.argv[1]
    ip_address = ''
resolver = client.Resolver(servers=[('', 53)])
factory = DNSServerFactory(clients=[resolver], verbose=verbosity)
protocol = dns.DNSDatagramProtocol(factory)
factory.noisy = protocol.noisy = verbosity
reactor.listenUDP(53, protocol)
reactor.listenTCP(53, factory)
site = webserver.Site(Simple())
reactor.listenTCP(8080, site)