defaults to master.conf // POST file= content= // // RESPONSE // GET {"ok":true,"content":"…","file":"…","allowed":["…"]} // POST {"ok":true,"push":[{"host","ok","ready","error"}, …]} // push is empty for host confs and on hosts with no partners // {"ok":false,"error":"Not allowed"|"Syntax error: …"|"Method not allowed"} // // DEPENDS ON // include/config.php vv_get_conf_files(), vv_read_conf_raw(), vv_write_conf_raw(), // vv_push_master_conf(), vv_push_setup_state() // ═══════════════════════════════════════════════════════════════════════════════════════════════ header('Content-Type: application/json'); require_once dirname(__DIR__) . '/include/config.php'; $allowed = vv_get_conf_files(); if ($_SERVER['REQUEST_METHOD'] === 'GET') { $file = trim($_GET['file'] ?? 'master.conf'); if (!in_array($file, $allowed, true) || str_contains($file, '..')) { echo json_encode(['ok' => false, 'error' => 'Not allowed']); exit; } echo json_encode(['ok' => true, 'content' => vv_read_conf_raw($file), 'file' => $file, 'allowed' => $allowed]); exit; } if ($_SERVER['REQUEST_METHOD'] === 'POST') { $file = trim($_POST['file'] ?? ''); $content = $_POST['content'] ?? ''; if (!in_array($file, $allowed, true) || str_contains($file, '..')) { echo json_encode(['ok' => false, 'error' => 'Not allowed']); exit; } // Every script sources these, and master.conf is pushed to every partner from here — a // syntax error saved through this endpoint would propagate the outage across the mesh. $check = tempnam(sys_get_temp_dir(), 'vvconf'); if ($check !== false) { file_put_contents($check, $content); $out = []; $rc = 0; exec('bash -n ' . escapeshellarg($check) . ' 2>&1', $out, $rc); @unlink($check); if ($rc !== 0) { $msg = implode(' ', array_filter(array_map('trim', $out))); echo json_encode([ 'ok' => false, 'error' => 'Syntax error: ' . str_replace($check, $file, $msg ?: 'conf does not parse'), 'push' => [], ]); exit; } } $written = vv_write_conf_raw($file, $content); $push = []; if ($written && $file === 'master.conf') { $push = vv_push_master_conf(); vv_push_setup_state(); } echo json_encode(['ok' => $written, 'push' => $push]); exit; } echo json_encode(['ok' => false, 'error' => 'Method not allowed']);