-
-
Save DeadPackets/60be344f5dbea4cd1dbf11f441871a2e to your computer and use it in GitHub Desktop.
Converts hosts file to privoxy action
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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