Skip to content

Instantly share code, notes, and snippets.

@chew-z
Created February 19, 2016 10:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chew-z/3a43967812fdac788d56 to your computer and use it in GitHub Desktop.
Save chew-z/3a43967812fdac788d56 to your computer and use it in GitHub Desktop.
Converts hosts file to DNSCrypt blacklist-domains file
#!/usr/bin/env python
# Takes hosts file and converts to DNSCrypt blacklist-domains file
# so you could block malicious hosts on DNSCrypt
# (when alternating to DNSCrypt instead of dnsmasq in my scenario)
# hosts file from https://github.com/StevenBlack/hosts
# See https://dnscrypt.org/ IP/domain names blocking
import re
import os
badguys_pattern = re.compile(
'^0.0.0.0(\s*|\t*)(.*)\n|^127.0.0.1(\s*|\t*)(.*)\n')
localhost_pattern = re.compile(
'^0.0.0.0.*localhost\.localdomain.*$|^(?!\#)(.*)localhost.*$|^(?!\#).*broadcasthost.*$|^0.0.0.0.*local.*$')
comment_pattern = re.compile('#(.*)\n')
os.unlink("blacklist-domains")
# touch blacklist-domains first if it doesn't exist
output = open("blacklist-domains", "w")
with open('hosts.txt', 'r') as f:
for line in f.readlines():
if re.match(localhost_pattern, line):
pass
elif re.match(comment_pattern, line):
pass
else:
m = badguys_pattern.match(line)
if m:
if m.group(2) is not None:
output.write(m.group(2) + "\n")
output.close()
# if using DNSCrypt-OSXClient on Mac
# mv blacklist-ips /Library/Application\ Support/DNSCrypt/control/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment