Commit 14dfed33 authored by Ad Schellevis's avatar Ad Schellevis Committed by Franco Fichtner

(proxy) add optional filter for remote blacklist

(cherry picked from commit 7df30b7d)
parent d99a5149
...@@ -126,46 +126,61 @@ if os.path.exists(acl_config_fn): ...@@ -126,46 +126,61 @@ if os.path.exists(acl_config_fn):
cnf = ConfigParser() cnf = ConfigParser()
cnf.read(acl_config_fn) cnf.read(acl_config_fn)
for section in cnf.sections(): for section in cnf.sections():
# check if tag enabled exists in section target_filename = acl_target_dir+'/'+section
if cnf.has_option(section,'enabled'): if cnf.has_option(section,'url'):
# if enabled fetch file # collect filters to apply
target_filename = acl_target_dir+'/'+section acl_filters = list()
if cnf.has_option(section,'url'): if cnf.has_option(section,'filter'):
# define targets for acl_filter in cnf.get(section,'filter').strip().split(','):
targets = {'domain': {'filename': target_filename, 'handle' : None}, if len(acl_filter.strip()) > 0:
'url': {'filename': '%s.url'%target_filename, 'handle': None}} acl_filters.append(acl_filter)
# download file # define targets
if cnf.get(section,'enabled') == '1': targets = {'domain': {'filename': target_filename, 'handle' : None},
# only generate files if enabled, otherwise dump empty files 'url': {'filename': '%s.url'%target_filename, 'handle': None}}
download_url = cnf.get(section,'url')
acl = ACLDownload(download_url, acl_max_timeout) # only generate files if enabled, otherwise dump empty files
all_filenames = list() if cnf.has_option(section,'enabled') and cnf.get(section,'enabled') == '1':
for filename, filetype, line in acl.download(): download_url = cnf.get(section,'url')
if filename not in all_filenames: acl = ACLDownload(download_url, acl_max_timeout)
all_filenames.append(filename) all_filenames = list()
if filetype in targets and targets[filetype]['handle'] is None: for filename, filetype, line in acl.download():
targets[filetype]['handle'] = open(targets[filetype]['filename'], 'wb') if filename not in all_filenames:
if filetype in targets: all_filenames.append(filename)
targets[filetype]['handle'].write('%s\n'%line)
# save index to disc if len(acl_filters) > 0:
with open('%s.index'%target_filename,'wb') as idx_out: acl_found = False
index_data = dict() for acl_filter in acl_filters:
for filename in all_filenames: if filename.find(acl_filter) > -1:
if len(filename.split('/')) > 3: acl_found = True
index_key = '/'.join(filename.split('/')[1:-1]) break
if index_key not in index_data: if not acl_found:
index_data[index_key] = index_key # skip this acl entry
idx_out.write(json.dumps(index_data)) continue
# cleanup
for filetype in targets: if filetype in targets and targets[filetype]['handle'] is None:
if targets[filetype]['handle'] is not None: targets[filetype]['handle'] = open(targets[filetype]['filename'], 'wb')
targets[filetype]['handle'].close() if filetype in targets:
elif cnf.get(section,'enabled') != '1': targets[filetype]['handle'].write('%s\n'%line)
if os.path.isfile(targets[filetype]['filename']): # save index to disc
# disabled, remove previous data with open('%s.index'%target_filename,'wb') as idx_out:
os.remove(targets[filetype]['filename']) index_data = dict()
elif not os.path.isfile(targets[filetype]['filename']): for filename in all_filenames:
# no data fetched and no file available, create new empty file if len(filename.split('/')) > 3:
with open(targets[filetype]['filename'], 'wb') as target_out: index_key = '/'.join(filename.split('/')[1:-1])
target_out.write("") if index_key not in index_data:
index_data[index_key] = index_key
idx_out.write(json.dumps(index_data))
# cleanup
for filetype in targets:
if targets[filetype]['handle'] is not None:
targets[filetype]['handle'].close()
elif cnf.has_option(section,'enabled') and cnf.get(section,'enabled') != '1':
if os.path.isfile(targets[filetype]['filename']):
# disabled, remove previous data
os.remove(targets[filetype]['filename'])
elif not os.path.isfile(targets[filetype]['filename']):
# no data fetched and no file available, create new empty file
with open(targets[filetype]['filename'], 'wb') as target_out:
target_out.write("")
...@@ -6,5 +6,6 @@ ...@@ -6,5 +6,6 @@
[{{blacklist.filename}}] [{{blacklist.filename}}]
url:{{blacklist.url}} url:{{blacklist.url}}
enabled:{{blacklist.enabled}} enabled:{{blacklist.enabled}}
filter:{{blacklist.filter|default('')}}
{% endfor %} {% endfor %}
{% endif %} {% endif %}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment