Skip to content

Instantly share code, notes, and snippets.

@sunshinekitty
Created September 27, 2016 13:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sunshinekitty/458fb2c899effe53954b018f00c0bccd to your computer and use it in GitHub Desktop.
Save sunshinekitty/458fb2c899effe53954b018f00c0bccd to your computer and use it in GitHub Desktop.
A script to block ads via dnsmasq
#!/usr/bin/env python
from urllib2 import urlopen
from collections import OrderedDict
import re
import os
import sys
def is_valid_hostname(hostname):
if len(hostname) > 255:
return False
if "." not in hostname:
return False
if hostname[-1] == ".":
hostname = hostname[:-1] # strip exactly one dot from the right, if present
allowed = re.compile("(?!-)[A-Z\d-]{1,63}(?<!-)$", re.IGNORECASE)
return all(allowed.match(x) for x in hostname.split("."))
def hosts_to_dnsmasq(hosts, ignore=[], route="0.0.0.0"):
"""Converts a list of hosts to a dnsmasq formatted string
:param hosts: List of hosts to convert
:param route: Where to route requests
:param ignore: A list of hosts to ignore
:returns: A string of the dnsmasq file
"""
dnsmasq = ""
for line in hosts:
try:
if line.startswith("#") or '#' in line:
pass # don't want these
else:
if ' ' in line:
host = line.split(' ')[1]
elif ' ' in line:
host = line.split(' ')[1]
elif '\t' in line:
host = line.split('\t')[1]
except IndexError:
print line
try:
if host in ignore or not is_valid_hostname(host):
pass # ignore these
else:
dnsmasq = dnsmasq + "address=/{}/{}\n".format(host, route)
except UnboundLocalError:
print line
return dnsmasq
def hulu_dnsmasq(hulu_servers, server):
dnsmasq = ""
for hulu_server in hulu_servers:
dnsmasq = dnsmasq + "address=/{}/{}\n".format(hulu_server,server)
return dnsmasq
def gather_hosts(url_list):
"""Grabs hosts entries from a list of url's
:param url_list: URL list to block
:returns: List of hosts
"""
to_return = []
for host in url_list:
fetched = urlopen(host).read()
to_return = to_return + fetched.splitlines()
# this removes any dupes from the list
return list(OrderedDict.fromkeys(to_return))
ad_lists = [
"http://adblock.gjtech.net/?format=unix-hosts",
"https://adaway.org/hosts.txt",
"http://hosts-file.net/ad_servers.txt",
"http://www.malwaredomainlist.com/hostslist/hosts.txt",
"http://someonewhocares.org/hosts/hosts",
"http://pgl.yoyo.org/adservers/serverlist.php?hostformat=hosts&showintro=0&startdate%5Bday%5D=&startdate%5Bmonth%5D=&startdate%5Byear%5D=&mimetype=plaintext",
"http://winhelp2002.mvps.org/hosts.txt",
]
hulu_list = [
"ads.hulu.com",
"ads-v-darwin.hulu.com",
"a.huluad.com",
"ads.hulu.com"
"a.huluad.com",
"hulu.112.2o7.net",
"huludev.112.2o7.net",
"ll.a.hulu.com",
"t2.hulu.com",
"t2.huluim.com",
"t-ak.hulu.com",
"track.hulu.com",
"tw.i.hulu.com",
"urlcheck.hulu.com"
]
ignore_list = [
"btg.mtvnservices.com", # required, don't remove for hulu
"tears.io"
]
video_server = "192.168.0.138"
config_file = "/etc/dnsmasq.d/dnsmasq.adlist.conf"
hosts = gather_hosts(ad_lists)
dnsmasq = hosts_to_dnsmasq(hosts, ignore_list)
dnsmasq = dnsmasq + hulu_dnsmasq(hulu_list, video_server)
try:
f = open(config_file,'w')
f.write(dnsmasq)
f.close()
except IOError:
print("Failed to write to /etc/dnsmasq.d/dnsmasq.adlist.conf, permission denied")
sys.exit(1)
os.system("/etc/init.d/dnsmasq force-reload")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment