Skip to content

Instantly share code, notes, and snippets.

@DeadPackets
Forked from chew-z/hosts2privoxy.py
Created June 18, 2017 09:11
Show Gist options
  • Save DeadPackets/60be344f5dbea4cd1dbf11f441871a2e to your computer and use it in GitHub Desktop.
Save DeadPackets/60be344f5dbea4cd1dbf11f441871a2e to your computer and use it in GitHub Desktop.
Converts hosts file to privoxy action
#!/usr/bin/env python
# Takes amalgamated hosts file and converts to privoxy action file
# so you could block malicious hosts via proxy rather then /etc/hosts file
# See http://www.privoxy.org/faq/misc.html#HOSTSFILE
#
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(
'^(?!\#)(.*)localhost(.*)$|^(?!\#)(.*)broadcasthost(.*)$|^0.0.0.0(\t*)local\n')
comment_pattern = re.compile('#(.*)\n')
os.unlink("hosts.action")
# touch hosts.action first if it doesn't exist
output = open("hosts.action", "w")
output.write("# Block hosts from amalgamated hosts file\n")
output.write("# See - https://github.com/StevenBlack/hosts\n")
output.write("\n# This is privoxy statement!\n{+block{bad-guys.}}\n\n")
with open('hosts', 'r') as f:
for line in f.readlines():
if re.match(localhost_pattern, line):
pass
elif re.match(comment_pattern, line):
output.write(line)
else:
m = badguys_pattern.match(line)
if m:
if m.group(2) is not None:
output.write("." + m.group(2) + "\n")
output.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment