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/ddns/__init__.py
Normal file
0
docker/routlin-dash/app/pages/ddns/__init__.py
Normal file
247
docker/routlin-dash/app/pages/ddns/action.py
Normal file
247
docker/routlin-dash/app/pages/ddns/action.py
Normal file
|
|
@ -0,0 +1,247 @@
|
|||
import copy
|
||||
import os
|
||||
from flask import Blueprint, request, redirect, flash, send_file, abort
|
||||
from auth import require_level
|
||||
from config_utils import load_config, verify_config_hash, save_config_with_snapshot, CONFIGS_DIR
|
||||
import sanitize
|
||||
import validation as validate
|
||||
|
||||
bp = Blueprint('ddns', __name__)
|
||||
|
||||
VIEW = '/view/view_ddns'
|
||||
LOG_FILE = f'{CONFIGS_DIR}/ddns.log'
|
||||
|
||||
|
||||
@bp.route('/action/ddns_cardaddaccount_add', methods=['POST'])
|
||||
@require_level('administrator')
|
||||
def ddns_cardaddaccount_add():
|
||||
provider_type = sanitize.filtervalue(request.form.get('provider', ''), validate.VALID_DDNS_PROVIDERS)
|
||||
description = sanitize.description(request.form.get('description', ''))
|
||||
hostnames = sanitize.domainlist(request.form.get('hostnames', '').splitlines())
|
||||
|
||||
if not description:
|
||||
flash('Description is required.', 'error')
|
||||
return redirect(VIEW)
|
||||
if not hostnames:
|
||||
flash('At least one hostname is required.', 'error')
|
||||
return redirect(VIEW)
|
||||
if not provider_type:
|
||||
flash('Unknown provider type.', '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)
|
||||
|
||||
entry = {
|
||||
'description': description,
|
||||
'provider': provider_type,
|
||||
'enabled': True,
|
||||
'hostnames': hostnames,
|
||||
}
|
||||
if provider_type == 'noip':
|
||||
entry['username'] = request.form.get('username', '').strip()
|
||||
entry['password'] = request.form.get('password', '').strip()
|
||||
else:
|
||||
entry['api_token'] = request.form.get('api_token', '').strip()
|
||||
|
||||
cfg = load_config()
|
||||
cfg.setdefault('ddns', {}).setdefault('providers', []).append(entry)
|
||||
flash(save_config_with_snapshot(
|
||||
cfg, path='ddns', key=description, operation='add',
|
||||
before=None, after=copy.deepcopy(entry),
|
||||
description=f'Added DDNS provider: {description}',
|
||||
cmd='ddns update',
|
||||
queue=False,
|
||||
), 'success')
|
||||
return redirect(VIEW)
|
||||
|
||||
|
||||
@bp.route('/action/ddns_tableaccounts_rowedit', methods=['POST'])
|
||||
@require_level('administrator')
|
||||
def ddns_tableaccounts_rowedit():
|
||||
try:
|
||||
row_index = int(request.form.get('row_index', -1))
|
||||
except (TypeError, ValueError):
|
||||
flash('Invalid row index.', 'error')
|
||||
return redirect(VIEW)
|
||||
|
||||
provider_type = sanitize.filtervalue(request.form.get('provider', ''), validate.VALID_DDNS_PROVIDERS)
|
||||
description = sanitize.description(request.form.get('description', ''))
|
||||
hostnames = [h.strip() for h in request.form.get('hostnames', '').splitlines() if h.strip()]
|
||||
enabled = request.form.get('enabled') == 'on'
|
||||
|
||||
if not provider_type:
|
||||
flash('Unknown provider type.', '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()
|
||||
providers = cfg.setdefault('ddns', {}).setdefault('providers', [])
|
||||
if row_index < 0 or row_index >= len(providers):
|
||||
flash('Invalid provider index.', 'error')
|
||||
return redirect(VIEW)
|
||||
|
||||
before = copy.deepcopy(providers[row_index])
|
||||
entry = {
|
||||
'description': description,
|
||||
'provider': provider_type,
|
||||
'enabled': enabled,
|
||||
'hostnames': hostnames,
|
||||
}
|
||||
if provider_type == 'noip':
|
||||
entry['username'] = request.form.get('username', '').strip()
|
||||
entry['password'] = request.form.get('password', '').strip()
|
||||
else:
|
||||
entry['api_token'] = request.form.get('api_token', '').strip()
|
||||
|
||||
providers[row_index] = entry
|
||||
flash(save_config_with_snapshot(
|
||||
cfg, path='ddns', key=description, operation='edit',
|
||||
before=before, after=copy.deepcopy(entry),
|
||||
description=f'Edited DDNS provider: {description}',
|
||||
cmd='ddns update',
|
||||
queue=False,
|
||||
), 'success')
|
||||
return redirect(VIEW)
|
||||
|
||||
|
||||
@bp.route('/action/ddns_tableaccounts_rowdelete', methods=['POST'])
|
||||
@require_level('administrator')
|
||||
def ddns_tableaccounts_rowdelete():
|
||||
try:
|
||||
row_index = int(request.form.get('row_index', -1))
|
||||
except (TypeError, ValueError):
|
||||
flash('Invalid row index.', '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()
|
||||
providers = cfg.setdefault('ddns', {}).setdefault('providers', [])
|
||||
if row_index < 0 or row_index >= len(providers):
|
||||
flash('Invalid provider index.', 'error')
|
||||
return redirect(VIEW)
|
||||
|
||||
before = copy.deepcopy(providers[row_index])
|
||||
description = before.get('description', str(row_index))
|
||||
del providers[row_index]
|
||||
flash(save_config_with_snapshot(
|
||||
cfg, path='ddns', key=description, operation='delete',
|
||||
before=before, after=None,
|
||||
description=f'Deleted DDNS provider: {description}',
|
||||
cmd='ddns update',
|
||||
queue=False,
|
||||
), 'success')
|
||||
return redirect(VIEW)
|
||||
|
||||
|
||||
@bp.route('/action/ddns_cardipcheckinterval_save', methods=['POST'])
|
||||
@require_level('administrator')
|
||||
def ddns_cardipcheckinterval_save():
|
||||
raw = request.form.get('timer_interval', '').strip()
|
||||
try:
|
||||
mins = int(raw)
|
||||
if mins < 1:
|
||||
raise ValueError
|
||||
except ValueError:
|
||||
flash('Interval must be a whole number of minutes >= 1.', 'error')
|
||||
return redirect(VIEW)
|
||||
timer_interval = f'{mins}m'
|
||||
|
||||
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('ddns', {}).get('general', {}))
|
||||
cfg.setdefault('ddns', {}).setdefault('general', {})['timer_interval'] = timer_interval
|
||||
flash(save_config_with_snapshot(
|
||||
cfg, path='ddns', key='general', operation='edit',
|
||||
before=before, after=copy.deepcopy(cfg['ddns']['general']),
|
||||
description='Updated DDNS check interval',
|
||||
cmd='core apply',
|
||||
), 'success')
|
||||
return redirect(VIEW)
|
||||
|
||||
|
||||
@bp.route('/action/ddns_cardipcheckservices_save', methods=['POST'])
|
||||
@require_level('administrator')
|
||||
def ddns_cardipcheckservices_save():
|
||||
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)
|
||||
|
||||
http_services = [u.strip() for u in request.form.getlist('http_services') if u.strip()]
|
||||
dig_services = [u.strip() for u in request.form.getlist('dig_services') if u.strip()]
|
||||
|
||||
if not http_services and not dig_services:
|
||||
flash('At least one IP check service is required.', 'error')
|
||||
return redirect(VIEW)
|
||||
|
||||
cfg = load_config()
|
||||
before = copy.deepcopy(cfg.get('ddns', {}).get('ip_check_services', []))
|
||||
services = [{'type': 'http', 'url': u} for u in http_services]
|
||||
services += [{'type': 'dig', 'url': u} for u in dig_services]
|
||||
cfg.setdefault('ddns', {})['ip_check_services'] = services
|
||||
flash(save_config_with_snapshot(
|
||||
cfg, path='ddns', key='ip_check_services', operation='edit',
|
||||
before=before, after=copy.deepcopy(services),
|
||||
description='Updated DDNS IP check services',
|
||||
cmd='ddns update',
|
||||
queue=False,
|
||||
), 'success')
|
||||
return redirect(VIEW)
|
||||
|
||||
|
||||
@bp.route('/action/ddns_cardlogging_save', methods=['POST'])
|
||||
@require_level('administrator')
|
||||
def ddns_cardlogging_save():
|
||||
log_max_kb = validate.int_range(request.form.get('log_max_kb', '').strip(), 64, None)
|
||||
if log_max_kb is None:
|
||||
flash('Max Log Size must be a number >= 64.', 'error')
|
||||
return redirect(VIEW)
|
||||
log_errors_only = 'log_errors_only' in request.form
|
||||
|
||||
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('ddns', {}).get('general', {}))
|
||||
cfg.setdefault('ddns', {}).setdefault('general', {}).update({
|
||||
'log_max_kb': log_max_kb,
|
||||
'log_errors_only': log_errors_only,
|
||||
})
|
||||
flash(save_config_with_snapshot(
|
||||
cfg, path='ddns', key='general', operation='edit',
|
||||
before=before, after=copy.deepcopy(cfg['ddns']['general']),
|
||||
description='Updated DDNS logging settings',
|
||||
cmd='ddns update',
|
||||
queue=False,
|
||||
), 'success')
|
||||
return redirect(VIEW)
|
||||
|
||||
|
||||
@bp.route('/action/ddns_cardlogging_clear', methods=['POST'])
|
||||
@require_level('administrator')
|
||||
def ddns_cardlogging_clear():
|
||||
try:
|
||||
open(LOG_FILE, 'w').close()
|
||||
flash('DDNS log cleared.', 'success')
|
||||
except Exception as ex:
|
||||
flash(f'Could not clear log: {ex}', 'error')
|
||||
return redirect(VIEW)
|
||||
|
||||
|
||||
@bp.route('/action/ddns_cardlogging_download', methods=['GET'])
|
||||
@require_level('administrator')
|
||||
def ddns_cardlogging_download():
|
||||
if not os.path.isfile(LOG_FILE):
|
||||
abort(404)
|
||||
return send_file(LOG_FILE, as_attachment=True, download_name='ddns.log', mimetype='text/plain')
|
||||
295
docker/routlin-dash/app/pages/ddns/content.json
Normal file
295
docker/routlin-dash/app/pages/ddns/content.json
Normal file
|
|
@ -0,0 +1,295 @@
|
|||
{
|
||||
"id": "view_ddns",
|
||||
"client_requirement": "client_is_viewer+",
|
||||
"items": [
|
||||
{
|
||||
"type": "header_page_title",
|
||||
"items": [
|
||||
{
|
||||
"type": "h1",
|
||||
"text": "DDNS"
|
||||
},
|
||||
{
|
||||
"type": "p",
|
||||
"text": "Dynamic DNS provider status and last known IP update."
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "stat_card_grid",
|
||||
"items": [
|
||||
{
|
||||
"type": "stat_card",
|
||||
"label": "Current Public IP",
|
||||
"value": "%STAT_PUBLIC_IP%",
|
||||
"sub": "%STAT_PUBLIC_IP_LAST_OBTAINED%"
|
||||
},
|
||||
{
|
||||
"type": "stat_card",
|
||||
"label": "IP Check Interval",
|
||||
"value": "%DDNS_TIMER_INTERVAL%",
|
||||
"sub": "%STAT_PUBLIC_IP_LAST_CHECKED%",
|
||||
"edit_action": "/action/ddns_cardipcheckinterval_save",
|
||||
"edit_field": "timer_interval",
|
||||
"edit_input_type": "number",
|
||||
"edit_min": "1",
|
||||
"edit_suffix": "minutes",
|
||||
"edit_value": "%DDNS_TIMER_INTERVAL_MINS%"
|
||||
},
|
||||
{
|
||||
"type": "stat_card",
|
||||
"label": "IP Check Services",
|
||||
"value": "%STAT_IP_CHECK_TOTAL%",
|
||||
"sub": "%STAT_IP_CHECK_SUB%",
|
||||
"reveal_card_id": "ip-check-services-edit"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "card",
|
||||
"id": "ip-check-services-edit",
|
||||
"label": "IP Check Services",
|
||||
"hidden": true,
|
||||
"client_requirement": "client_is_administrator+",
|
||||
"items": [
|
||||
{
|
||||
"type": "form",
|
||||
"action": "/action/ddns_cardipcheckservices_save",
|
||||
"method": "post",
|
||||
"items": [
|
||||
{
|
||||
"type": "editable_list",
|
||||
"label": "HTTP APIs",
|
||||
"name": "http_services",
|
||||
"item_placeholder": "https://...",
|
||||
"add_label": "Add HTTP API",
|
||||
"items": "%IP_CHECK_HTTP_JSON%"
|
||||
},
|
||||
{
|
||||
"type": "editable_list",
|
||||
"label": "Dig APIs",
|
||||
"name": "dig_services",
|
||||
"item_placeholder": "e.g. @1.1.1.1 ch txt whoami.cloudflare",
|
||||
"add_label": "Add Dig API",
|
||||
"items": "%IP_CHECK_DIG_JSON%"
|
||||
},
|
||||
{
|
||||
"type": "button_row",
|
||||
"items": [
|
||||
{
|
||||
"type": "button_primary",
|
||||
"action": "/action/ddns_cardipcheckservices_save",
|
||||
"method": "post",
|
||||
"text": "Save"
|
||||
},
|
||||
{
|
||||
"type": "button_cancel",
|
||||
"text": "Cancel",
|
||||
"class": "js-hide-card"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "table",
|
||||
"datasource": "config:ddns_providers",
|
||||
"empty_message": "No DDNS providers configured.",
|
||||
"columns": [
|
||||
{
|
||||
"label": "Description",
|
||||
"field": "description"
|
||||
},
|
||||
{
|
||||
"label": "Provider",
|
||||
"field": "provider"
|
||||
},
|
||||
{
|
||||
"label": "Hostname(s)",
|
||||
"field": "hostnames",
|
||||
"render": "tag_list"
|
||||
},
|
||||
{
|
||||
"label": "Status",
|
||||
"field": "enabled",
|
||||
"render": "badge_enabled_disabled"
|
||||
},
|
||||
{
|
||||
"label": "Credentials",
|
||||
"field": "credentials",
|
||||
"render": "raw_html"
|
||||
}
|
||||
],
|
||||
"row_actions": [
|
||||
{
|
||||
"client_requirement": "client_is_administrator+",
|
||||
"action": "/action/ddns_tableaccounts_rowedit",
|
||||
"method": "inline_edit",
|
||||
"text": "Edit",
|
||||
"class": "btn-ghost btn-sm",
|
||||
"fields": [
|
||||
{
|
||||
"col": "description",
|
||||
"input_type": "text"
|
||||
},
|
||||
{
|
||||
"col": "provider",
|
||||
"input_type": "select",
|
||||
"options": "%DDNS_PROVIDER_OPTIONS%"
|
||||
},
|
||||
{
|
||||
"col": "hostnames",
|
||||
"input_type": "textarea"
|
||||
},
|
||||
{
|
||||
"col": "enabled",
|
||||
"input_type": "checkbox"
|
||||
},
|
||||
{
|
||||
"col": "credentials",
|
||||
"input_type": "credentials"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"client_requirement": "client_is_administrator+",
|
||||
"action": "/action/ddns_tableaccounts_rowdelete",
|
||||
"method": "post",
|
||||
"text": "Delete",
|
||||
"class": "btn-danger btn-sm"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "card",
|
||||
"label": "Add DDNS Account",
|
||||
"client_requirement": "client_is_administrator+",
|
||||
"items": [
|
||||
{
|
||||
"type": "form",
|
||||
"action": "/action/ddns_cardaddaccount_add",
|
||||
"method": "post",
|
||||
"items": [
|
||||
{
|
||||
"type": "field",
|
||||
"label": "Description",
|
||||
"name": "description",
|
||||
"input_type": "text",
|
||||
"placeholder": "e.g. My DuckDNS Account"
|
||||
},
|
||||
{
|
||||
"type": "field",
|
||||
"label": "Provider",
|
||||
"name": "provider",
|
||||
"input_type": "select",
|
||||
"options": "%DDNS_PROVIDER_OPTIONS%"
|
||||
},
|
||||
{
|
||||
"type": "field",
|
||||
"label": "Hostnames (one per line)",
|
||||
"name": "hostnames",
|
||||
"input_type": "textarea",
|
||||
"placeholder": "e.g. myhome.duckdns.org"
|
||||
},
|
||||
{
|
||||
"type": "credential_fields",
|
||||
"provider_select": "provider"
|
||||
},
|
||||
{
|
||||
"type": "button_row",
|
||||
"items": [
|
||||
{
|
||||
"type": "button_primary",
|
||||
"action": "/action/ddns_cardaddaccount_add",
|
||||
"method": "post",
|
||||
"text": "Add Provider"
|
||||
},
|
||||
{
|
||||
"type": "button_cancel",
|
||||
"text": "Cancel"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "card",
|
||||
"label": "Logging",
|
||||
"client_requirement": "client_is_administrator+",
|
||||
"items": [
|
||||
{
|
||||
"type": "pre_block",
|
||||
"text": "%DDNS_LOG_TAIL%",
|
||||
"scroll_to_bottom": true
|
||||
},
|
||||
{
|
||||
"type": "raw_html",
|
||||
"html": "%DDNS_LOG_SUMMARY%"
|
||||
},
|
||||
{
|
||||
"type": "button_row",
|
||||
"justify": "space-between",
|
||||
"items": [
|
||||
{
|
||||
"type": "button_ghost",
|
||||
"action": "/action/ddns_cardlogging_download",
|
||||
"text": "Download Log"
|
||||
},
|
||||
{
|
||||
"type": "button_danger",
|
||||
"action": "/action/ddns_cardlogging_clear",
|
||||
"method": "post",
|
||||
"text": "Clear Log"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "hr"
|
||||
},
|
||||
{
|
||||
"type": "form",
|
||||
"action": "/action/ddns_cardlogging_save",
|
||||
"method": "post",
|
||||
"items": [
|
||||
{
|
||||
"type": "field",
|
||||
"label": "Max Log Size (KB)",
|
||||
"name": "log_max_kb",
|
||||
"input_type": "number",
|
||||
"layout": "inline",
|
||||
"value": "%DDNS_GEN_LOG_MAX_KB%",
|
||||
"min": "64"
|
||||
},
|
||||
{
|
||||
"type": "field",
|
||||
"label": "",
|
||||
"name": "log_errors_only",
|
||||
"input_type": "checkbox",
|
||||
"checkbox_label": "Only record errors to log",
|
||||
"value": "%DDNS_GEN_LOG_ERRORS_ONLY%"
|
||||
},
|
||||
{
|
||||
"type": "button_row",
|
||||
"items": [
|
||||
{
|
||||
"type": "button_primary",
|
||||
"action": "/action/ddns_cardlogging_save",
|
||||
"method": "post",
|
||||
"text": "Save"
|
||||
},
|
||||
{
|
||||
"type": "button_cancel",
|
||||
"text": "Cancel"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue