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('action_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', ), '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', ), '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', ), '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', ), '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', ), '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')