From 93d78e7b418f9157f0aafa1e6f8ccaf50451b7e7 Mon Sep 17 00:00:00 2001 From: Paul Lecuq Date: Sun, 14 Feb 2016 12:30:57 +0100 Subject: [PATCH] add cdb flavour --- py-squid-blacklists-cdb.py | 68 ++++++++++++++++++++++++++++++++++++++ py-squid-blacklists.py | 5 --- 2 files changed, 68 insertions(+), 5 deletions(-) create mode 100755 py-squid-blacklists-cdb.py diff --git a/py-squid-blacklists-cdb.py b/py-squid-blacklists-cdb.py new file mode 100755 index 0000000..c748d5d --- /dev/null +++ b/py-squid-blacklists-cdb.py @@ -0,0 +1,68 @@ +#!/usr/bin/env python2.7 + +import sys +import os +import re +import urllib +from urlparse import urlparse +try: + import config +except ImportError: + print("Please create config.py using config.py.sample") + exit() +try: + import cdb +except ImportError: + print("Please install python-cdb from pypi or via package manager") + exit() + +def make_list(files): + blacklists = [] + for l in files: + splitlist = l.split("/") + list_type = splitlist[len(splitlist)-2] + blacklists.append([list_type,l]) + return blacklists + +def make_db(blacklist_files,blacklists): + lib = dict() + for bl in blacklist_files: + if(bl[0] in blacklists): + cache = dict() + f = open(bl[1], "r") + for line in f: + cache[line.strip("\n")] = True + lib[bl[0]] = cache + del cache + return lib + +def compare(outline,blacklist_cache): + result = False + for blacklist in blacklist_cache: + tmpline = outline + while not result and tmpline != "": + try: + result = blacklist_cache[blacklist][tmpline] + pass + except KeyError: + pass + tmpline = tmpline.partition('.')[2] + return result + +def squid_response(response): + sys.stdout.write("%s\n" % response) + sys.stdout.flush() + +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) +blacklist_cache = make_db(blacklist_files,config.blacklists) + +while True: + line = sys.stdin.readline().strip() + outline = urlparse(line).netloc + if line: + if compare(outline,blacklist_cache): + squid_response("OK") + else: + squid_response("ERR") diff --git a/py-squid-blacklists.py b/py-squid-blacklists.py index c748d5d..a21695a 100755 --- a/py-squid-blacklists.py +++ b/py-squid-blacklists.py @@ -10,11 +10,6 @@ try: except ImportError: print("Please create config.py using config.py.sample") exit() -try: - import cdb -except ImportError: - print("Please install python-cdb from pypi or via package manager") - exit() def make_list(files): blacklists = []