2026-05-24 02:28:52 -04:00
|
|
|
import re
|
2026-05-17 03:26:01 -04:00
|
|
|
from flask import Blueprint, request, redirect, flash
|
|
|
|
|
from auth import require_level
|
2026-05-24 02:28:52 -04:00
|
|
|
from config_utils import load_core, save_core, verify_core_hash, queued_msg
|
2026-05-18 20:02:22 -04:00
|
|
|
import sanitize
|
2026-05-20 17:10:18 -04:00
|
|
|
import validation as validate
|
2026-05-17 03:26:01 -04:00
|
|
|
|
|
|
|
|
bp = Blueprint('action_apply_ddns_providers', __name__)
|
|
|
|
|
|
2026-05-23 04:14:58 -04:00
|
|
|
VIEW = '/view/view_ddns'
|
2026-05-17 03:26:01 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@bp.route('/action/add_ddns_provider', methods=['POST'])
|
|
|
|
|
@require_level('administrator')
|
|
|
|
|
def add_ddns_provider():
|
2026-05-18 20:02:22 -04:00
|
|
|
provider_type = sanitize.filtervalue(request.form.get('provider', ''), validate.VALID_DDNS_PROVIDERS)
|
|
|
|
|
description = sanitize.description(request.form.get('description', ''))
|
2026-05-23 04:14:58 -04:00
|
|
|
hostnames = sanitize.domainlist(request.form.get('hostnames', '').splitlines())
|
2026-05-17 03:26:01 -04:00
|
|
|
|
|
|
|
|
if not description:
|
|
|
|
|
flash('Description is required.', 'error')
|
2026-05-23 04:14:58 -04:00
|
|
|
return redirect(VIEW)
|
2026-05-17 03:26:01 -04:00
|
|
|
if not hostnames:
|
|
|
|
|
flash('At least one hostname is required.', 'error')
|
2026-05-23 04:14:58 -04:00
|
|
|
return redirect(VIEW)
|
2026-05-18 20:02:22 -04:00
|
|
|
if not provider_type:
|
2026-05-17 03:26:01 -04:00
|
|
|
flash('Unknown provider type.', 'error')
|
2026-05-23 04:14:58 -04:00
|
|
|
return redirect(VIEW)
|
2026-05-17 03:26:01 -04:00
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
2026-05-23 04:14:58 -04:00
|
|
|
core = load_core()
|
|
|
|
|
core.setdefault('ddns', {}).setdefault('providers', []).append(entry)
|
|
|
|
|
save_core(core)
|
|
|
|
|
flash(f'DDNS provider "{description}" added.', 'success')
|
|
|
|
|
return redirect(VIEW)
|
2026-05-17 03:26:01 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@bp.route('/action/edit_ddns_provider', methods=['POST'])
|
|
|
|
|
@require_level('administrator')
|
|
|
|
|
def edit_ddns_provider():
|
|
|
|
|
try:
|
|
|
|
|
row_index = int(request.form.get('row_index', -1))
|
|
|
|
|
except (TypeError, ValueError):
|
|
|
|
|
flash('Invalid row index.', 'error')
|
2026-05-23 04:14:58 -04:00
|
|
|
return redirect(VIEW)
|
2026-05-17 03:26:01 -04:00
|
|
|
|
2026-05-18 20:02:22 -04:00
|
|
|
provider_type = sanitize.filtervalue(request.form.get('provider', ''), validate.VALID_DDNS_PROVIDERS)
|
|
|
|
|
description = sanitize.description(request.form.get('description', ''))
|
2026-05-23 04:14:58 -04:00
|
|
|
hostnames = [h.strip() for h in request.form.get('hostnames', '').splitlines() if h.strip()]
|
2026-05-17 03:26:01 -04:00
|
|
|
enabled = request.form.get('enabled') == 'on'
|
|
|
|
|
|
2026-05-18 20:02:22 -04:00
|
|
|
if not provider_type:
|
|
|
|
|
flash('Unknown provider type.', 'error')
|
2026-05-23 04:14:58 -04:00
|
|
|
return redirect(VIEW)
|
2026-05-17 03:26:01 -04:00
|
|
|
|
2026-05-23 04:14:58 -04:00
|
|
|
core = load_core()
|
|
|
|
|
providers = core.setdefault('ddns', {}).setdefault('providers', [])
|
2026-05-17 03:26:01 -04:00
|
|
|
if row_index < 0 or row_index >= len(providers):
|
|
|
|
|
flash('Invalid provider index.', 'error')
|
2026-05-23 04:14:58 -04:00
|
|
|
return redirect(VIEW)
|
2026-05-17 03:26:01 -04:00
|
|
|
|
|
|
|
|
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
|
2026-05-23 04:14:58 -04:00
|
|
|
save_core(core)
|
|
|
|
|
flash('DDNS provider updated.', 'success')
|
|
|
|
|
return redirect(VIEW)
|
2026-05-17 03:26:01 -04:00
|
|
|
|
|
|
|
|
|
2026-05-24 02:28:52 -04:00
|
|
|
@bp.route('/action/ddns_cardlog_save', methods=['POST'])
|
|
|
|
|
@require_level('administrator')
|
|
|
|
|
def ddns_cardlog_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_core_hash(request.form.get('config_hash', '')):
|
|
|
|
|
flash('Configuration was modified by another session. Please refresh and try again.', 'error')
|
|
|
|
|
return redirect(VIEW)
|
|
|
|
|
core = load_core()
|
|
|
|
|
core.setdefault('ddns', {}).setdefault('general', {}).update({
|
|
|
|
|
'log_max_kb': log_max_kb,
|
|
|
|
|
'log_errors_only': log_errors_only,
|
|
|
|
|
})
|
|
|
|
|
save_core(core)
|
|
|
|
|
flash('DDNS log settings saved.', 'success')
|
|
|
|
|
return redirect(VIEW)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@bp.route('/action/ddns_cardinterval_save', methods=['POST'])
|
|
|
|
|
@require_level('administrator')
|
|
|
|
|
def ddns_cardinterval_save():
|
|
|
|
|
timer_interval = request.form.get('timer_interval', '').strip()
|
|
|
|
|
if not re.match(r'^\d+[mhd]$', timer_interval):
|
|
|
|
|
flash('Invalid interval. Use a number followed by m, h, or d (e.g. 10m, 1h).', 'error')
|
|
|
|
|
return redirect(VIEW)
|
|
|
|
|
if not verify_core_hash(request.form.get('config_hash', '')):
|
|
|
|
|
flash('Configuration was modified by another session. Please refresh and try again.', 'error')
|
|
|
|
|
return redirect(VIEW)
|
|
|
|
|
core = load_core()
|
|
|
|
|
core.setdefault('ddns', {}).setdefault('general', {})['timer_interval'] = timer_interval
|
|
|
|
|
save_core(core)
|
|
|
|
|
flash(queued_msg('core apply'), 'success')
|
|
|
|
|
return redirect(VIEW)
|
|
|
|
|
|
|
|
|
|
|
2026-05-17 03:26:01 -04:00
|
|
|
@bp.route('/action/delete_ddns_provider', methods=['POST'])
|
|
|
|
|
@require_level('administrator')
|
|
|
|
|
def delete_ddns_provider():
|
|
|
|
|
try:
|
|
|
|
|
row_index = int(request.form.get('row_index', -1))
|
|
|
|
|
except (TypeError, ValueError):
|
|
|
|
|
flash('Invalid row index.', 'error')
|
2026-05-23 04:14:58 -04:00
|
|
|
return redirect(VIEW)
|
2026-05-17 03:26:01 -04:00
|
|
|
|
2026-05-23 04:14:58 -04:00
|
|
|
core = load_core()
|
|
|
|
|
providers = core.setdefault('ddns', {}).setdefault('providers', [])
|
2026-05-17 03:26:01 -04:00
|
|
|
if row_index < 0 or row_index >= len(providers):
|
|
|
|
|
flash('Invalid provider index.', 'error')
|
2026-05-23 04:14:58 -04:00
|
|
|
return redirect(VIEW)
|
2026-05-17 03:26:01 -04:00
|
|
|
|
|
|
|
|
del providers[row_index]
|
2026-05-23 04:14:58 -04:00
|
|
|
save_core(core)
|
|
|
|
|
flash('DDNS provider deleted.', 'success')
|
|
|
|
|
return redirect(VIEW)
|