Comparison on flavors

Code cleaning ...
This commit is contained in:
Paul 2016-02-20 12:40:51 +01:00
parent 080505a032
commit 43c6164e10
2 changed files with 110 additions and 92 deletions

View File

@ -5,69 +5,78 @@ import os
import re import re
import urllib import urllib
from urlparse import urlparse from urlparse import urlparse
try: try:
import config import config
except ImportError: except ImportError:
print("Please create config.py using config.py.sample") print("Please create config.py using config.py.sample")
exit() exit()
try: try:
import cdb import cdb
except ImportError: except ImportError:
print("Please install python-cdb from pypi or via package manager") print("Please install python-cdb from pypi or via package manager")
exit() exit()
def make_list(files): def make_list(files):
blacklists = [] blacklists = []
for l in files: for l in files:
splitlist = l.split("/") splitlist = l.split("/")
list_type = splitlist[len(splitlist)-2] list_type = splitlist[len(splitlist) - 2]
blacklists.append([list_type,l]) blacklists.append([list_type, l])
return blacklists return blacklists
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)
cache = dict()
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): def make_db(blacklist_files, config):
result = False lib = []
for blacklist in blacklist_cache: for bl in blacklist_files:
cdb_file = cdb.init(blacklist) bl_cdb_file = ("%s/%s.cdb" % (config.blacklists_dir, bl[0]))
tmpline = outline bl_cdb_file_tmp = ("%s/%s.tmp" % (config.blacklists_dir, bl[0]))
while not result and tmpline != "": if (bl[0] in config.blacklists):
try: if not os.path.isfile(bl_cdb_file):
result = cdb_file[tmpline] cdb_file = cdb.cdbmake(bl_cdb_file, bl_cdb_file_tmp)
pass cache = dict()
except KeyError: f = open(bl[1], "r")
pass for line in f:
tmpline = tmpline.partition('.')[2] cdb_file.add(line.strip("\n"), "True")
return result 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
def squid_response(response): def squid_response(response):
sys.stdout.write("%s\n" % response) sys.stdout.write("%s\n" % response)
sys.stdout.flush() 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)] 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_files = make_list(domain_files)
blacklist_cache = make_db(blacklist_files,config) blacklist_cache = make_db(blacklist_files, config)
while True: while True:
line = sys.stdin.readline().strip() try:
outline = urlparse(line).netloc line = sys.stdin.readline().strip()
if line: outline = urlparse(line).netloc
if compare(outline,blacklist_cache): if line:
squid_response("OK") if compare(outline, blacklist_cache):
else: squid_response("OK")
squid_response("ERR") else:
squid_response("ERR")
except KeyboardInterrupt:
break

View File

@ -5,59 +5,68 @@ import os
import re import re
import urllib import urllib
from urlparse import urlparse from urlparse import urlparse
try: try:
import config import config
except ImportError: except ImportError:
print("Please create config.py using config.py.sample") print("Please create config.py using config.py.sample")
exit() exit()
def make_list(files): def make_list(files):
blacklists = [] blacklists = []
for l in files: for l in files:
splitlist = l.split("/") splitlist = l.split("/")
list_type = splitlist[len(splitlist)-2] list_type = splitlist[len(splitlist) - 2]
blacklists.append([list_type,l]) blacklists.append([list_type, l])
return blacklists 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): def make_db(blacklist_files, config):
result = False lib = dict()
for blacklist in blacklist_cache: for bl in blacklist_files:
tmpline = outline if (bl[0] in config.blacklists):
while not result and tmpline != "": cache = dict()
try: f = open(bl[1], "r")
result = blacklist_cache[blacklist][tmpline] for line in f:
pass cache[line.strip("\n")] = True
except KeyError: lib[bl[0]] = cache
pass del cache
tmpline = tmpline.partition('.')[2] return lib
return result
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): def squid_response(response):
sys.stdout.write("%s\n" % response) sys.stdout.write("%s\n" % response)
sys.stdout.flush() 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)] 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_files = make_list(domain_files)
blacklist_cache = make_db(blacklist_files,config.blacklists) blacklist_cache = make_db(blacklist_files, config)
while True: while True:
line = sys.stdin.readline().strip() try:
outline = urlparse(line).netloc line = sys.stdin.readline().strip()
if line: outline = urlparse(line).netloc
if compare(outline,blacklist_cache): if line:
squid_response("OK") if compare(outline, blacklist_cache):
else: squid_response("OK")
squid_response("ERR") else:
squid_response("ERR")
except KeyboardInterrupt:
break