62 lines
1.3 KiB
Python
62 lines
1.3 KiB
Python
import json, subprocess, hashlib
|
|
from markupsafe import Markup
|
|
|
|
_APPLY_CMD = 'sudo python3 ~/router/core.py --apply'
|
|
|
|
|
|
def apply_msg():
|
|
"""Return a Markup flash message for the apply reminder."""
|
|
return Markup(
|
|
f'Configuration updated. To apply changes, run: '
|
|
f'<code><strong>{_APPLY_CMD}</strong></code>'
|
|
)
|
|
|
|
CONFIGS_DIR = '/configs'
|
|
CORE_FILE = f'{CONFIGS_DIR}/core.json'
|
|
|
|
|
|
def load_core():
|
|
try:
|
|
with open(CORE_FILE) as f:
|
|
return json.load(f)
|
|
except Exception:
|
|
return {}
|
|
|
|
|
|
def save_core(data):
|
|
with open(CORE_FILE, 'w') as f:
|
|
json.dump(data, f, indent=2)
|
|
|
|
|
|
def core_hash():
|
|
try:
|
|
with open(CORE_FILE, 'rb') as f:
|
|
return hashlib.md5(f.read()).hexdigest()
|
|
except Exception:
|
|
return ''
|
|
|
|
|
|
def verify_core_hash(submitted):
|
|
if not submitted:
|
|
return True
|
|
return submitted == core_hash()
|
|
|
|
|
|
def run_apply():
|
|
try:
|
|
subprocess.run(
|
|
['python3', f'{CONFIGS_DIR}/core.py', '--apply'],
|
|
capture_output=True, timeout=30
|
|
)
|
|
except Exception:
|
|
pass
|
|
|
|
|
|
def run_update_blocklists():
|
|
try:
|
|
subprocess.run(
|
|
['python3', f'{CONFIGS_DIR}/core.py', '--update-blocklists'],
|
|
capture_output=True, timeout=120
|
|
)
|
|
except Exception:
|
|
pass
|