pybl/py-squid-blacklists.py

67 lines
1.5 KiB
Python
Raw Normal View History

2016-02-12 17:37:57 +01:00
#!/usr/bin/env python2.7
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:
2016-02-12 17:37:57 +01:00
tmpline = outline
while not result and tmpline != "":
2016-02-03 21:31:33 +01:00
try:
2016-02-12 17:37:57 +01:00
result = blacklist_cache[blacklist][tmpline]
pass
2016-02-03 21:31:33 +01:00
except KeyError:
pass
2016-02-12 17:37:57 +01:00
tmpline = tmpline.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()
2016-02-12 17:37:57 +01:00
blacklist_cache=[]
2016-01-25 14:07:49 +01:00
blacklist_files = make_list(domain_files)
blacklist_cache = make_db(blacklist_files)
while True:
2016-02-12 17:37:57 +01:00
line = sys.stdin.readline().strip()
outline = urlparse(line).netloc
2016-01-25 14:07:49 +01:00
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")