2016-02-03 21:31:33 +01:00
|
|
|
#!/usr/bin/env python
|
2016-01-25 14:07:49 +01:00
|
|
|
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import logging
|
|
|
|
import time
|
2016-02-03 21:31:33 +01:00
|
|
|
import urllib
|
2016-01-25 14:07:49 +01:00
|
|
|
from urlparse import urlparse
|
2016-02-03 21:31:33 +01:00
|
|
|
try:
|
|
|
|
from config import *
|
|
|
|
except ImportError:
|
|
|
|
print("Please create config.py using config.py.sample")
|
|
|
|
exit()
|
2016-01-25 14:07:49 +01:00
|
|
|
|
|
|
|
domain_files = [os.path.join(dp, f) for dp, dn, fn in os.walk(os.path.expanduser(blacklists_dir)) for f in fn if re.match(r"domains*", f)]
|
|
|
|
|
|
|
|
def make_list(files):
|
2016-02-03 21:31:33 +01:00
|
|
|
blacklists = []
|
2016-01-25 14:07:49 +01:00
|
|
|
for f in files:
|
|
|
|
splitlist = f.split("/")
|
|
|
|
list_type = splitlist[len(splitlist)-2]
|
|
|
|
blacklists.append([list_type,f])
|
|
|
|
return blacklists
|
|
|
|
|
|
|
|
def make_db(blacklist_files):
|
|
|
|
lib = dict()
|
|
|
|
for blacklist in blacklist_files:
|
2016-02-03 21:31:33 +01:00
|
|
|
cache = dict()
|
|
|
|
values = []
|
2016-01-25 14:07:49 +01:00
|
|
|
f = open(blacklist[1], "r")
|
|
|
|
for line in f:
|
|
|
|
cache[line.strip("\n")] = True
|
2016-02-03 21:31:33 +01:00
|
|
|
lib[blacklist[0]] = cache
|
2016-01-25 14:07:49 +01:00
|
|
|
return lib
|
|
|
|
|
2016-02-03 21:31:33 +01:00
|
|
|
def compare(outline,blacklist_cache,blacklists):
|
2016-01-25 14:07:49 +01:00
|
|
|
result = False
|
2016-02-03 21:31:33 +01:00
|
|
|
for blacklist in blacklists:
|
|
|
|
while not result and outline != "":
|
|
|
|
try:
|
|
|
|
result = blacklist_cache[blacklist][outline]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
outline = outline.partition('.')[2]
|
2016-01-25 14:07:49 +01:00
|
|
|
return result
|
|
|
|
|
2016-02-03 21:31:33 +01:00
|
|
|
def squid_response(response):
|
|
|
|
sys.stdout.write("%s\n" % response)
|
2016-01-25 14:07:49 +01:00
|
|
|
sys.stdout.flush()
|
|
|
|
|
|
|
|
|
|
|
|
blacklist_cache=[]
|
|
|
|
blacklist_files = make_list(domain_files)
|
|
|
|
blacklist_cache = make_db(blacklist_files)
|
|
|
|
|
|
|
|
while True:
|
|
|
|
line = sys.stdin.readline().strip()
|
|
|
|
outline = urlparse(line).netloc
|
|
|
|
if line:
|
2016-02-03 21:31:33 +01:00
|
|
|
if compare(outline,blacklist_cache,blacklists):
|
|
|
|
squid_response("OK")
|
2016-01-25 14:07:49 +01:00
|
|
|
else:
|
2016-02-03 21:31:33 +01:00
|
|
|
squid_response("ERR")
|