Development
This commit is contained in:
parent
d8d1d46fd2
commit
eed1d295dc
69 changed files with 3355 additions and 3230 deletions
0
docker/routlin-dash/app/pages/dnsblocking/__init__.py
Normal file
0
docker/routlin-dash/app/pages/dnsblocking/__init__.py
Normal file
234
docker/routlin-dash/app/pages/dnsblocking/action.py
Normal file
234
docker/routlin-dash/app/pages/dnsblocking/action.py
Normal file
|
|
@ -0,0 +1,234 @@
|
|||
import copy
|
||||
import re
|
||||
from flask import Blueprint, request, redirect, flash
|
||||
from auth import require_level
|
||||
from config_utils import load_config, save_config_with_snapshot, verify_config_hash, queued_msg
|
||||
import sanitize
|
||||
import validation as validate
|
||||
|
||||
bp = Blueprint('dnsblocking', __name__)
|
||||
|
||||
VIEW = '/view/view_dnsblocking'
|
||||
|
||||
_VALID_FORMATS_STR = ', '.join(sorted(validate.VALID_BLOCKLIST_FORMATS))
|
||||
|
||||
|
||||
def _row_index():
|
||||
try:
|
||||
return int(request.form.get('row_index', ''))
|
||||
except (ValueError, TypeError):
|
||||
return None
|
||||
|
||||
|
||||
def _hash_ok():
|
||||
if not verify_config_hash(request.form.get('config_hash', '')):
|
||||
flash('Configuration was modified by another session. Please refresh and try again.', 'error')
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def _save_as_from_name(name):
|
||||
slug = re.sub(r'[^a-z0-9_-]', '_', name.lower()).strip('_')
|
||||
return f'{slug}.conf'
|
||||
|
||||
|
||||
def _parse_fields():
|
||||
name = sanitize.name(request.form.get('name', ''))
|
||||
description = sanitize.description(request.form.get('description', ''))
|
||||
fmt = sanitize.filtervalue(request.form.get('format', ''), validate.VALID_BLOCKLIST_FORMATS)
|
||||
url = sanitize.url(request.form.get('url', ''))
|
||||
|
||||
if not name:
|
||||
flash('The configuration has not been saved because a name is required.', 'error')
|
||||
return None, True
|
||||
if not url:
|
||||
flash('The configuration has not been saved because a URL is required.', 'error')
|
||||
return None, True
|
||||
if not fmt:
|
||||
flash(f'The configuration has not been saved because the format is invalid. '
|
||||
f'Accepted formats: {_VALID_FORMATS_STR}.', 'error')
|
||||
return None, True
|
||||
|
||||
return {'name': name, 'description': description, 'format': fmt, 'url': url}, None
|
||||
|
||||
|
||||
@bp.route('/action/dnsblocking_tableblocklists_rowdelete', methods=['POST'])
|
||||
@require_level('administrator')
|
||||
def dnsblocking_tableblocklists_rowdelete():
|
||||
idx = _row_index()
|
||||
if idx is None:
|
||||
flash('Invalid request.', 'error')
|
||||
return redirect(VIEW)
|
||||
|
||||
if not _hash_ok():
|
||||
return redirect(VIEW)
|
||||
|
||||
cfg = load_config()
|
||||
items = cfg.get('dns_blocking', {}).get('blocklists', [])
|
||||
if idx < 0 or idx >= len(items):
|
||||
flash('Entry not found.', 'error')
|
||||
return redirect(VIEW)
|
||||
|
||||
before = copy.deepcopy(items[idx])
|
||||
name = before.get('name', str(idx))
|
||||
items.pop(idx)
|
||||
errors = validate.validate_config(cfg)
|
||||
if errors:
|
||||
for msg in errors:
|
||||
flash(msg, 'error')
|
||||
return redirect(VIEW)
|
||||
flash(save_config_with_snapshot(
|
||||
cfg, path='dns_blocking', key=name, operation='delete',
|
||||
before=before, after=None,
|
||||
description=f'Deleted blocklist: {name}',
|
||||
queue=False,
|
||||
), 'success')
|
||||
return redirect(VIEW)
|
||||
|
||||
|
||||
@bp.route('/action/dnsblocking_tableblocklists_rowedit', methods=['POST'])
|
||||
@require_level('administrator')
|
||||
def dnsblocking_tableblocklists_rowedit():
|
||||
idx = _row_index()
|
||||
if idx is None:
|
||||
flash('Invalid request.', 'error')
|
||||
return redirect(VIEW)
|
||||
|
||||
fields, err = _parse_fields()
|
||||
if err:
|
||||
return redirect(VIEW)
|
||||
|
||||
if not _hash_ok():
|
||||
return redirect(VIEW)
|
||||
|
||||
cfg = load_config()
|
||||
items = cfg.get('dns_blocking', {}).get('blocklists', [])
|
||||
if idx < 0 or idx >= len(items):
|
||||
flash('Entry not found.', 'error')
|
||||
return redirect(VIEW)
|
||||
|
||||
before = copy.deepcopy(items[idx])
|
||||
items[idx].update({
|
||||
'name': fields['name'],
|
||||
'description': fields['description'],
|
||||
'format': fields['format'],
|
||||
'url': fields['url'],
|
||||
})
|
||||
errors = validate.validate_config(cfg)
|
||||
if errors:
|
||||
for msg in errors:
|
||||
flash(msg, 'error')
|
||||
return redirect(VIEW)
|
||||
flash(save_config_with_snapshot(
|
||||
cfg, path='dns_blocking', key=fields['name'], operation='edit',
|
||||
before=before, after=copy.deepcopy(items[idx]),
|
||||
description=f'Edited blocklist: {fields["name"]}',
|
||||
queue=False,
|
||||
), 'success')
|
||||
return redirect(VIEW)
|
||||
|
||||
|
||||
@bp.route('/action/dnsblocking_cardaddblocklist_add', methods=['POST'])
|
||||
@require_level('administrator')
|
||||
def dnsblocking_cardaddblocklist_add():
|
||||
fields, err = _parse_fields()
|
||||
if err:
|
||||
return redirect(VIEW)
|
||||
|
||||
if not _hash_ok():
|
||||
return redirect(VIEW)
|
||||
|
||||
cfg = load_config()
|
||||
blocklists = cfg.setdefault('dns_blocking', {}).setdefault('blocklists', [])
|
||||
|
||||
if any(b.get('name', '').lower() == fields['name'].lower() for b in blocklists):
|
||||
flash('The configuration has not been saved because a blocklist with that name already exists.', 'error')
|
||||
return redirect(VIEW)
|
||||
|
||||
entry = {
|
||||
'name': fields['name'],
|
||||
'description': fields['description'],
|
||||
'format': fields['format'],
|
||||
'url': fields['url'],
|
||||
'save_as': _save_as_from_name(fields['name']),
|
||||
}
|
||||
blocklists.append(entry)
|
||||
errors = validate.validate_config(cfg)
|
||||
if errors:
|
||||
for msg in errors:
|
||||
flash(msg, 'error')
|
||||
return redirect(VIEW)
|
||||
flash(save_config_with_snapshot(
|
||||
cfg, path='dns_blocking', key=fields['name'], operation='add',
|
||||
before=None, after=copy.deepcopy(entry),
|
||||
description=f'Added blocklist: {fields["name"]}',
|
||||
queue=False,
|
||||
), 'success')
|
||||
return redirect(VIEW)
|
||||
|
||||
|
||||
@bp.route('/action/dnsblocking_cardblocklistrefresh_save', methods=['POST'])
|
||||
@require_level('administrator')
|
||||
def dnsblocking_cardblocklistrefresh_save():
|
||||
daily_execute_time = validate.time_24h(sanitize.time_24h(request.form.get('daily_execute_time_24hr_local', '')))
|
||||
|
||||
if not daily_execute_time:
|
||||
flash('Daily Refresh Time must be a valid 24-hour time (e.g. 02:30).', 'error')
|
||||
return redirect(VIEW)
|
||||
|
||||
if not verify_config_hash(request.form.get('config_hash', '')):
|
||||
flash('Configuration was modified by another session. Please refresh and try again.', 'error')
|
||||
return redirect(VIEW)
|
||||
|
||||
cfg = load_config()
|
||||
before = copy.deepcopy(cfg.get('dns_blocking', {}).get('general', {}))
|
||||
cfg.setdefault('dns_blocking', {}).setdefault('general', {})['daily_execute_time_24hr_local'] = daily_execute_time
|
||||
flash(save_config_with_snapshot(
|
||||
cfg, path='dns_blocking', key='general', operation='edit',
|
||||
before=before, after=copy.deepcopy(cfg['dns_blocking']['general']),
|
||||
description='Updated daily blocklist refresh time',
|
||||
cmd='core apply',
|
||||
), 'success')
|
||||
return redirect(VIEW)
|
||||
|
||||
|
||||
@bp.route('/action/dnsblocking_cardblocklistrefresh_refreshnow', methods=['POST'])
|
||||
@require_level('administrator')
|
||||
def dnsblocking_cardblocklistrefresh_refreshnow():
|
||||
flash(queued_msg('core update-blocklists', action_label='Blocklist refresh queued'), 'success')
|
||||
return redirect(VIEW)
|
||||
|
||||
|
||||
@bp.route('/action/dnsblocking_cardlogging_save', methods=['POST'])
|
||||
@require_level('administrator')
|
||||
def dnsblocking_cardlogging_save():
|
||||
log_max_kb_raw = request.form.get('log_max_kb', '').strip()
|
||||
log_errors_only = 'log_errors_only' in request.form
|
||||
|
||||
log_max_kb = validate.int_range(log_max_kb_raw, 64, None)
|
||||
if log_max_kb is None:
|
||||
flash('Max Log Size must be a number >= 64.', 'error')
|
||||
return redirect(VIEW)
|
||||
|
||||
if not verify_config_hash(request.form.get('config_hash', '')):
|
||||
flash('Configuration was modified by another session. Please refresh and try again.', 'error')
|
||||
return redirect(VIEW)
|
||||
|
||||
cfg = load_config()
|
||||
before = copy.deepcopy(cfg.get('dns_blocking', {}).get('general', {}))
|
||||
cfg.setdefault('dns_blocking', {}).setdefault('general', {}).update({
|
||||
'log_max_kb': log_max_kb,
|
||||
'log_errors_only': log_errors_only,
|
||||
})
|
||||
errors = validate.validate_config(cfg)
|
||||
if errors:
|
||||
for msg in errors:
|
||||
flash(msg, 'error')
|
||||
return redirect(VIEW)
|
||||
flash(save_config_with_snapshot(
|
||||
cfg, path='dns_blocking', key='general', operation='edit',
|
||||
before=before, after=copy.deepcopy(cfg['dns_blocking']['general']),
|
||||
description='Updated DNS blocking log settings',
|
||||
queue=False,
|
||||
), 'success')
|
||||
return redirect(VIEW)
|
||||
247
docker/routlin-dash/app/pages/dnsblocking/content.json
Normal file
247
docker/routlin-dash/app/pages/dnsblocking/content.json
Normal file
|
|
@ -0,0 +1,247 @@
|
|||
{
|
||||
"id": "view_dnsblocking",
|
||||
"client_requirement": "client_is_viewer+",
|
||||
"items": [
|
||||
{
|
||||
"type": "header_page_title",
|
||||
"items": [
|
||||
{
|
||||
"type": "h1",
|
||||
"text": "DNS Blocking"
|
||||
},
|
||||
{
|
||||
"type": "p",
|
||||
"text": "Domain level blocking via dnsmasq."
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "table",
|
||||
"datasource": "config:blocklists",
|
||||
"empty_message": "No blocklists configured.",
|
||||
"columns": [
|
||||
{
|
||||
"label": "Name",
|
||||
"field": "name"
|
||||
},
|
||||
{
|
||||
"label": "Description",
|
||||
"field": "description"
|
||||
},
|
||||
{
|
||||
"label": "Format",
|
||||
"field": "format",
|
||||
"class": "col-mono"
|
||||
},
|
||||
{
|
||||
"label": "Source URL",
|
||||
"field": "url",
|
||||
"class": "col-mono"
|
||||
}
|
||||
],
|
||||
"row_actions": [
|
||||
{
|
||||
"client_requirement": "client_is_administrator+",
|
||||
"action": "/action/dnsblocking_tableblocklists_rowedit",
|
||||
"method": "inline_edit",
|
||||
"text": "Edit",
|
||||
"class": "btn-ghost btn-sm",
|
||||
"fields": [
|
||||
{
|
||||
"col": "name",
|
||||
"input_type": "text",
|
||||
"validate": "dashname"
|
||||
},
|
||||
{
|
||||
"col": "description",
|
||||
"input_type": "text"
|
||||
},
|
||||
{
|
||||
"col": "format",
|
||||
"input_type": "select",
|
||||
"options": "%BLOCKLIST_FORMAT_OPTIONS%"
|
||||
},
|
||||
{
|
||||
"col": "url",
|
||||
"input_type": "text",
|
||||
"validate": "url"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"client_requirement": "client_is_administrator+",
|
||||
"action": "/action/dnsblocking_tableblocklists_rowdelete",
|
||||
"method": "post",
|
||||
"text": "Delete",
|
||||
"class": "btn-danger btn-sm"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "card",
|
||||
"id": "add-form",
|
||||
"label": "Add Blocklist",
|
||||
"client_requirement": "client_is_administrator+",
|
||||
"items": [
|
||||
{
|
||||
"type": "form",
|
||||
"action": "/action/dnsblocking_cardaddblocklist_add",
|
||||
"method": "post",
|
||||
"items": [
|
||||
{
|
||||
"type": "field",
|
||||
"label": "Name",
|
||||
"name": "name",
|
||||
"input_type": "text",
|
||||
"validate": "dashname",
|
||||
"placeholder": "e.g. steven-black"
|
||||
},
|
||||
{
|
||||
"type": "field",
|
||||
"label": "Description",
|
||||
"name": "description",
|
||||
"input_type": "text",
|
||||
"placeholder": "e.g. Steven Black (ads, malware, trackers)"
|
||||
},
|
||||
{
|
||||
"type": "field",
|
||||
"label": "Format",
|
||||
"name": "format",
|
||||
"input_type": "select",
|
||||
"options": "%BLOCKLIST_FORMAT_OPTIONS%"
|
||||
},
|
||||
{
|
||||
"type": "field",
|
||||
"label": "Source URL",
|
||||
"name": "url",
|
||||
"input_type": "text",
|
||||
"validate": "url",
|
||||
"placeholder": "https://..."
|
||||
},
|
||||
{
|
||||
"type": "button_row",
|
||||
"items": [
|
||||
{
|
||||
"type": "button_primary",
|
||||
"action": "/action/dnsblocking_cardaddblocklist_add",
|
||||
"method": "post",
|
||||
"text": "Add Blocklist"
|
||||
},
|
||||
{
|
||||
"type": "button_cancel",
|
||||
"text": "Cancel"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "card",
|
||||
"label": "Blocklist Refresh",
|
||||
"client_requirement": "client_is_administrator+",
|
||||
"items": [
|
||||
{
|
||||
"type": "raw_html",
|
||||
"html": "%BLOCKLIST_STATS_HTML%"
|
||||
},
|
||||
{
|
||||
"type": "hr"
|
||||
},
|
||||
{
|
||||
"type": "button_row",
|
||||
"items": [
|
||||
{
|
||||
"type": "button_secondary",
|
||||
"action": "/action/dnsblocking_cardblocklistrefresh_refreshnow",
|
||||
"method": "post",
|
||||
"text": "Refresh All Now"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "hr"
|
||||
},
|
||||
{
|
||||
"type": "form",
|
||||
"action": "/action/dnsblocking_cardblocklistrefresh_save",
|
||||
"method": "post",
|
||||
"items": [
|
||||
{
|
||||
"type": "field",
|
||||
"label": "Daily Refresh Time",
|
||||
"name": "daily_execute_time_24hr_local",
|
||||
"input_type": "text",
|
||||
"validate": "time_24h",
|
||||
"value": "%GENERAL_DAILY_EXECUTE_TIME%",
|
||||
"placeholder": "e.g. 02:30",
|
||||
"hint": "24-hour local time for the daily blocklist refresh."
|
||||
},
|
||||
{
|
||||
"type": "button_row",
|
||||
"items": [
|
||||
{
|
||||
"type": "button_primary",
|
||||
"action": "/action/dnsblocking_cardblocklistrefresh_save",
|
||||
"method": "post",
|
||||
"text": "Save"
|
||||
},
|
||||
{
|
||||
"type": "button_cancel",
|
||||
"text": "Cancel"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "card",
|
||||
"label": "Logging",
|
||||
"client_requirement": "client_is_administrator+",
|
||||
"items": [
|
||||
{
|
||||
"type": "form",
|
||||
"action": "/action/dnsblocking_cardlogging_save",
|
||||
"method": "post",
|
||||
"items": [
|
||||
{
|
||||
"type": "field",
|
||||
"label": "Max Log Size (KB)",
|
||||
"name": "log_max_kb",
|
||||
"input_type": "number",
|
||||
"value": "%GENERAL_LOG_MAX_KB%",
|
||||
"min": 64,
|
||||
"hint": "Log is cleared and restarted when it exceeds this size."
|
||||
},
|
||||
{
|
||||
"type": "field",
|
||||
"label": "Only record errors to log",
|
||||
"name": "log_errors_only",
|
||||
"input_type": "checkbox",
|
||||
"value": "%GENERAL_LOG_ERRORS_ONLY%",
|
||||
"hint": "Only write error-level messages to the log."
|
||||
},
|
||||
{
|
||||
"type": "button_row",
|
||||
"items": [
|
||||
{
|
||||
"type": "button_primary",
|
||||
"action": "/action/dnsblocking_cardlogging_save",
|
||||
"method": "post",
|
||||
"text": "Save"
|
||||
},
|
||||
{
|
||||
"type": "button_cancel",
|
||||
"text": "Cancel"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue