Development

This commit is contained in:
Matthew Grotke 2026-05-27 22:04:04 -04:00
parent eed1d295dc
commit d9f3bd8289
45 changed files with 635 additions and 666 deletions

View file

@ -1,3 +1,4 @@
from pathlib import Path
import copy
import os
from flask import Blueprint, request, redirect, flash, send_file, abort
@ -6,32 +7,33 @@ from config_utils import load_config, verify_config_hash, save_config_with_snaps
import sanitize
import validation as validate
bp = Blueprint('ddns', __name__)
_PAGE = Path(__file__).parent.name
bp = Blueprint(_PAGE, __name__)
VIEW = '/view/view_ddns'
LOG_FILE = f'{CONFIGS_DIR}/ddns.log'
@bp.route('/action/ddns_cardaddaccount_add', methods=['POST'])
@bp.route('/action/ddns/addaccount_add', methods=['POST'])
@require_level('administrator')
def ddns_cardaddaccount_add():
def addaccount_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)
return redirect(f'/{_PAGE}')
if not hostnames:
flash('At least one hostname is required.', 'error')
return redirect(VIEW)
return redirect(f'/{_PAGE}')
if not provider_type:
flash('Unknown provider type.', 'error')
return redirect(VIEW)
return redirect(f'/{_PAGE}')
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)
return redirect(f'/{_PAGE}')
entry = {
'description': description,
@ -54,17 +56,17 @@ def ddns_cardaddaccount_add():
cmd='ddns update',
queue=False,
), 'success')
return redirect(VIEW)
return redirect(f'/{_PAGE}')
@bp.route('/action/ddns_tableaccounts_rowedit', methods=['POST'])
@bp.route('/action/ddns/accounts_edit', methods=['POST'])
@require_level('administrator')
def ddns_tableaccounts_rowedit():
def accounts_edit():
try:
row_index = int(request.form.get('row_index', -1))
except (TypeError, ValueError):
flash('Invalid row index.', 'error')
return redirect(VIEW)
return redirect(f'/{_PAGE}')
provider_type = sanitize.filtervalue(request.form.get('provider', ''), validate.VALID_DDNS_PROVIDERS)
description = sanitize.description(request.form.get('description', ''))
@ -73,17 +75,17 @@ def ddns_tableaccounts_rowedit():
if not provider_type:
flash('Unknown provider type.', 'error')
return redirect(VIEW)
return redirect(f'/{_PAGE}')
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)
return redirect(f'/{_PAGE}')
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)
return redirect(f'/{_PAGE}')
before = copy.deepcopy(providers[row_index])
entry = {
@ -106,27 +108,27 @@ def ddns_tableaccounts_rowedit():
cmd='ddns update',
queue=False,
), 'success')
return redirect(VIEW)
return redirect(f'/{_PAGE}')
@bp.route('/action/ddns_tableaccounts_rowdelete', methods=['POST'])
@bp.route('/action/ddns/accounts_delete', methods=['POST'])
@require_level('administrator')
def ddns_tableaccounts_rowdelete():
def accounts_delete():
try:
row_index = int(request.form.get('row_index', -1))
except (TypeError, ValueError):
flash('Invalid row index.', 'error')
return redirect(VIEW)
return redirect(f'/{_PAGE}')
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)
return redirect(f'/{_PAGE}')
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)
return redirect(f'/{_PAGE}')
before = copy.deepcopy(providers[row_index])
description = before.get('description', str(row_index))
@ -138,12 +140,12 @@ def ddns_tableaccounts_rowdelete():
cmd='ddns update',
queue=False,
), 'success')
return redirect(VIEW)
return redirect(f'/{_PAGE}')
@bp.route('/action/ddns_cardipcheckinterval_save', methods=['POST'])
@bp.route('/action/ddns/ipcheckinterval_save', methods=['POST'])
@require_level('administrator')
def ddns_cardipcheckinterval_save():
def ipcheckinterval_save():
raw = request.form.get('timer_interval', '').strip()
try:
mins = int(raw)
@ -151,12 +153,12 @@ def ddns_cardipcheckinterval_save():
raise ValueError
except ValueError:
flash('Interval must be a whole number of minutes >= 1.', 'error')
return redirect(VIEW)
return redirect(f'/{_PAGE}')
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)
return redirect(f'/{_PAGE}')
cfg = load_config()
before = copy.deepcopy(cfg.get('ddns', {}).get('general', {}))
@ -167,22 +169,22 @@ def ddns_cardipcheckinterval_save():
description='Updated DDNS check interval',
cmd='core apply',
), 'success')
return redirect(VIEW)
return redirect(f'/{_PAGE}')
@bp.route('/action/ddns_cardipcheckservices_save', methods=['POST'])
@bp.route('/action/ddns/ipcheckservices_save', methods=['POST'])
@require_level('administrator')
def ddns_cardipcheckservices_save():
def ipcheckservices_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)
return redirect(f'/{_PAGE}')
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)
return redirect(f'/{_PAGE}')
cfg = load_config()
before = copy.deepcopy(cfg.get('ddns', {}).get('ip_check_services', []))
@ -196,21 +198,21 @@ def ddns_cardipcheckservices_save():
cmd='ddns update',
queue=False,
), 'success')
return redirect(VIEW)
return redirect(f'/{_PAGE}')
@bp.route('/action/ddns_cardlogging_save', methods=['POST'])
@bp.route('/action/ddns/logging_save', methods=['POST'])
@require_level('administrator')
def ddns_cardlogging_save():
def logging_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)
return redirect(f'/{_PAGE}')
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)
return redirect(f'/{_PAGE}')
cfg = load_config()
before = copy.deepcopy(cfg.get('ddns', {}).get('general', {}))
@ -225,23 +227,23 @@ def ddns_cardlogging_save():
cmd='ddns update',
queue=False,
), 'success')
return redirect(VIEW)
return redirect(f'/{_PAGE}')
@bp.route('/action/ddns_cardlogging_clear', methods=['POST'])
@bp.route('/action/ddns/logging_clear', methods=['POST'])
@require_level('administrator')
def ddns_cardlogging_clear():
def logging_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)
return redirect(f'/{_PAGE}')
@bp.route('/action/ddns_cardlogging_download', methods=['GET'])
@bp.route('/action/ddns/logging_download', methods=['GET'])
@require_level('administrator')
def ddns_cardlogging_download():
def logging_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')

View file

@ -1,5 +1,4 @@
{
"id": "view_ddns",
"client_requirement": "client_is_viewer+",
"items": [
{
@ -29,7 +28,7 @@
"label": "IP Check Interval",
"value": "%DDNS_TIMER_INTERVAL%",
"sub": "%STAT_PUBLIC_IP_LAST_CHECKED%",
"edit_action": "/action/ddns_cardipcheckinterval_save",
"edit_action": "/action/ddns/ipcheckinterval_save",
"edit_field": "timer_interval",
"edit_input_type": "number",
"edit_min": "1",
@ -54,7 +53,7 @@
"items": [
{
"type": "form",
"action": "/action/ddns_cardipcheckservices_save",
"action": "/action/ddns/ipcheckservices_save",
"method": "post",
"items": [
{
@ -78,7 +77,7 @@
"items": [
{
"type": "button_primary",
"action": "/action/ddns_cardipcheckservices_save",
"action": "/action/ddns/ipcheckservices_save",
"method": "post",
"text": "Save"
},
@ -125,7 +124,7 @@
"row_actions": [
{
"client_requirement": "client_is_administrator+",
"action": "/action/ddns_tableaccounts_rowedit",
"action": "/action/ddns/accounts_edit",
"method": "inline_edit",
"text": "Edit",
"class": "btn-ghost btn-sm",
@ -155,7 +154,7 @@
},
{
"client_requirement": "client_is_administrator+",
"action": "/action/ddns_tableaccounts_rowdelete",
"action": "/action/ddns/accounts_delete",
"method": "post",
"text": "Delete",
"class": "btn-danger btn-sm"
@ -169,7 +168,7 @@
"items": [
{
"type": "form",
"action": "/action/ddns_cardaddaccount_add",
"action": "/action/ddns/addaccount_add",
"method": "post",
"items": [
{
@ -202,7 +201,7 @@
"items": [
{
"type": "button_primary",
"action": "/action/ddns_cardaddaccount_add",
"action": "/action/ddns/addaccount_add",
"method": "post",
"text": "Add Provider"
},
@ -236,12 +235,12 @@
"items": [
{
"type": "button_ghost",
"action": "/action/ddns_cardlogging_download",
"action": "/action/ddns/logging_download",
"text": "Download Log"
},
{
"type": "button_danger",
"action": "/action/ddns_cardlogging_clear",
"action": "/action/ddns/logging_clear",
"method": "post",
"text": "Clear Log"
}
@ -252,7 +251,7 @@
},
{
"type": "form",
"action": "/action/ddns_cardlogging_save",
"action": "/action/ddns/logging_save",
"method": "post",
"items": [
{
@ -277,7 +276,7 @@
"items": [
{
"type": "button_primary",
"action": "/action/ddns_cardlogging_save",
"action": "/action/ddns/logging_save",
"method": "post",
"text": "Save"
},