2016-02-14 12:30:57 +01:00
|
|
|
#!/usr/bin/env python2.7
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
from urlparse import urlparse
|
2016-02-20 12:40:51 +01:00
|
|
|
|
2016-02-14 12:30:57 +01:00
|
|
|
try:
|
2016-02-20 12:40:51 +01:00
|
|
|
import config
|
2016-02-14 12:30:57 +01:00
|
|
|
except ImportError:
|
2016-02-20 12:40:51 +01:00
|
|
|
print("Please create config.py using config.py.sample")
|
|
|
|
exit()
|
2016-02-14 12:30:57 +01:00
|
|
|
try:
|
2016-02-20 12:40:51 +01:00
|
|
|
import cdb
|
2016-02-14 12:30:57 +01:00
|
|
|
except ImportError:
|
2016-02-20 12:40:51 +01:00
|
|
|
print("Please install python-cdb from pypi or via package manager")
|
|
|
|
exit()
|
|
|
|
|
2016-02-14 12:30:57 +01:00
|
|
|
|
|
|
|
def make_list(files):
|
2016-02-20 12:40:51 +01:00
|
|
|
blacklists = []
|
|
|
|
for l in files:
|
|
|
|
splitlist = l.split("/")
|
|
|
|
list_type = splitlist[len(splitlist) - 2]
|
|
|
|
blacklists.append([list_type, l])
|
|
|
|
return blacklists
|
|
|
|
|
2016-02-14 12:30:57 +01:00
|
|
|
|
2016-02-20 12:40:51 +01:00
|
|
|
def make_db(blacklist_files, config):
|
|
|
|
lib = []
|
|
|
|
for bl in blacklist_files:
|
|
|
|
bl_cdb_file = ("%s/%s.cdb" % (config.blacklists_dir, bl[0]))
|
|
|
|
bl_cdb_file_tmp = ("%s/%s.tmp" % (config.blacklists_dir, bl[0]))
|
|
|
|
if (bl[0] in config.blacklists):
|
|
|
|
if not os.path.isfile(bl_cdb_file):
|
|
|
|
cdb_file = cdb.cdbmake(bl_cdb_file, bl_cdb_file_tmp)
|
|
|
|
f = open(bl[1], "r")
|
|
|
|
for line in f:
|
|
|
|
cdb_file.add(line.strip("\n"), "True")
|
|
|
|
cdb_file.finish()
|
|
|
|
lib.append(bl_cdb_file)
|
|
|
|
return lib
|
|
|
|
|
|
|
|
|
|
|
|
def compare(outline, blacklist_cache):
|
|
|
|
result = False
|
|
|
|
for blacklist in blacklist_cache:
|
|
|
|
cdb_file = cdb.init(blacklist)
|
|
|
|
tmpline = outline
|
|
|
|
while not result and tmpline != "":
|
|
|
|
try:
|
|
|
|
result = cdb_file[tmpline]
|
|
|
|
pass
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
tmpline = tmpline.partition('.')[2]
|
|
|
|
return result
|
2016-02-14 12:30:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
def squid_response(response):
|
2016-02-20 12:40:51 +01:00
|
|
|
sys.stdout.write("%s\n" % response)
|
|
|
|
sys.stdout.flush()
|
|
|
|
|
2016-02-14 12:30:57 +01:00
|
|
|
|
|
|
|
domain_files = [os.path.join(dp, f) for dp, dn, fn in os.walk(os.path.expanduser(config.blacklists_dir)) for f in fn if re.match(r"domains*", f)]
|
|
|
|
|
|
|
|
blacklist_files = make_list(domain_files)
|
2016-02-20 12:40:51 +01:00
|
|
|
blacklist_cache = make_db(blacklist_files, config)
|
2016-02-14 12:30:57 +01:00
|
|
|
|
|
|
|
while True:
|
2016-02-20 12:40:51 +01:00
|
|
|
try:
|
|
|
|
line = sys.stdin.readline().strip()
|
|
|
|
outline = urlparse(line).netloc
|
|
|
|
if line:
|
|
|
|
if compare(outline, blacklist_cache):
|
|
|
|
squid_response("OK")
|
|
|
|
else:
|
|
|
|
squid_response("ERR")
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
break
|