795 lines
26 KiB
PHP
795 lines
26 KiB
PHP
<?php
|
|
// ─── Config ──────────────────────────────────────────────────────────────────
|
|
define('CADDY_ADMIN_URL', getenv('CADDY_ADMIN_URL') ?: 'http://localhost:2019');
|
|
define('CADDYFILE_STORE', getenv('CADDYFILE_STORE') ?: '/data/Caddyfile');
|
|
define('CADDYFILE_IMPORT', getenv('CADDYFILE_IMPORT') ?: '');
|
|
|
|
// ─── Helpers ─────────────────────────────────────────────────────────────────
|
|
function dedent(array $lines): array {
|
|
$nonempty = array_filter($lines, fn($l) => trim($l) !== '');
|
|
if (!$nonempty) return $lines;
|
|
preg_match('/^(\s*)/', reset($nonempty), $ind);
|
|
$prefix = $ind[1];
|
|
foreach ($nonempty as $l) {
|
|
preg_match('/^(\s*)/', $l, $m);
|
|
$len = min(strlen($prefix), strlen($m[1]));
|
|
while ($len > 0 && substr($prefix, 0, $len) !== substr($m[1], 0, $len)) $len--;
|
|
$prefix = substr($prefix, 0, $len);
|
|
}
|
|
return $prefix !== '' ? array_map(fn($l) => substr($l, strlen($prefix)), $lines) : $lines;
|
|
}
|
|
|
|
function parseCaddyfile(string $content): array {
|
|
$result = ['global' => '', 'sites' => []];
|
|
$lines = explode("\n", $content);
|
|
$n = count($lines);
|
|
$i = 0;
|
|
|
|
while ($i < $n) {
|
|
$trimmed = trim($lines[$i]);
|
|
|
|
if ($trimmed === '' || str_starts_with($trimmed, '#')) { $i++; continue; }
|
|
|
|
// Global options block: lone opening brace
|
|
if ($trimmed === '{') {
|
|
$block = []; $depth = 1; $i++;
|
|
while ($i < $n && $depth > 0) {
|
|
$depth += substr_count($lines[$i], '{') - substr_count($lines[$i], '}');
|
|
if ($depth > 0) $block[] = $lines[$i];
|
|
$i++;
|
|
}
|
|
$result['global'] = implode("\n", dedent($block));
|
|
continue;
|
|
}
|
|
|
|
// Site block: address(es) {
|
|
if (preg_match('/^(.+?)\s*\{(.*)$/', $trimmed, $m)) {
|
|
$address = trim($m[1]);
|
|
$block = trim($m[2]) !== '' ? [trim($m[2])] : [];
|
|
$depth = 1; $i++;
|
|
while ($i < $n && $depth > 0) {
|
|
$depth += substr_count($lines[$i], '{') - substr_count($lines[$i], '}');
|
|
if ($depth > 0) $block[] = $lines[$i];
|
|
$i++;
|
|
}
|
|
$result['sites'][] = ['address' => $address, 'body' => implode("\n", dedent($block))];
|
|
continue;
|
|
}
|
|
|
|
$i++;
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
function buildCaddyfile(string $global, array $sites): string {
|
|
$out = '';
|
|
if (trim($global) !== '') $out .= "{\n" . $global . "\n}\n\n";
|
|
foreach ($sites as $site) {
|
|
$out .= trim($site['address']) . " {\n" . $site['body'] . "\n}\n\n";
|
|
}
|
|
return rtrim($out) . "\n";
|
|
}
|
|
|
|
function caddyLoad(string $caddyfile): array {
|
|
$url = rtrim(CADDY_ADMIN_URL, '/') . '/load';
|
|
$ch = curl_init($url);
|
|
curl_setopt_array($ch, [
|
|
CURLOPT_POST => true,
|
|
CURLOPT_POSTFIELDS => $caddyfile,
|
|
CURLOPT_HTTPHEADER => ['Content-Type: text/caddyfile'],
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_TIMEOUT => 10,
|
|
]);
|
|
$body = curl_exec($ch);
|
|
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
$err = curl_error($ch);
|
|
curl_close($ch);
|
|
|
|
if ($err) return ['ok' => false, 'error' => 'Cannot reach Caddy admin API: ' . $err];
|
|
if ($status === 200) return ['ok' => true];
|
|
// Try to extract a clean message from Caddy's JSON error response
|
|
$decoded = json_decode($body, true);
|
|
$msg = $decoded['error'] ?? trim($body) ?: "HTTP $status";
|
|
return ['ok' => false, 'error' => $msg];
|
|
}
|
|
|
|
function checkUpstream(string $host, int $port): bool {
|
|
$fp = @fsockopen($host, $port, $errno, $errstr, 3);
|
|
if ($fp) { fclose($fp); return true; }
|
|
return false;
|
|
}
|
|
|
|
// ─── API handlers ─────────────────────────────────────────────────────────────
|
|
if (isset($_GET['action'])) {
|
|
while (@ob_end_clean());
|
|
ini_set('display_errors', 0);
|
|
header('Content-Type: application/json');
|
|
|
|
switch ($_GET['action']) {
|
|
|
|
case 'check_admin':
|
|
$parsed = parse_url(CADDY_ADMIN_URL);
|
|
$host = $parsed['host'] ?? 'localhost';
|
|
$port = $parsed['port'] ?? 2019;
|
|
$fp = @fsockopen($host, $port, $errno, $errstr, 3);
|
|
if ($fp) { fclose($fp); echo json_encode(['ok' => true]); }
|
|
else { echo json_encode(['ok' => false, 'error' => "Cannot reach {$host}:{$port} — {$errstr}"]); }
|
|
exit;
|
|
|
|
case 'load':
|
|
if (file_exists(CADDYFILE_STORE)) {
|
|
$raw = file_get_contents(CADDYFILE_STORE);
|
|
} elseif (CADDYFILE_IMPORT && file_exists(CADDYFILE_IMPORT)) {
|
|
$raw = file_get_contents(CADDYFILE_IMPORT);
|
|
file_put_contents(CADDYFILE_STORE, $raw);
|
|
} else {
|
|
$raw = '';
|
|
}
|
|
echo json_encode(['ok' => true, 'data' => parseCaddyfile($raw)]);
|
|
exit;
|
|
|
|
case 'save':
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
if (!$input) { echo json_encode(['ok' => false, 'error' => 'Invalid JSON']); exit; }
|
|
|
|
$cf = buildCaddyfile($input['global'] ?? '', $input['sites'] ?? []);
|
|
$result = caddyLoad($cf);
|
|
if ($result['ok']) file_put_contents(CADDYFILE_STORE, $cf);
|
|
echo json_encode($result);
|
|
exit;
|
|
|
|
case 'check_upstreams':
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
$targets = $input['targets'] ?? [];
|
|
$results = [];
|
|
foreach ($targets as $t) {
|
|
$host = $t['host'] ?? '';
|
|
$port = (int)($t['port'] ?? 80);
|
|
if ($host === '') continue;
|
|
$results[] = [
|
|
'host' => $host,
|
|
'port' => $port,
|
|
'reachable' => checkUpstream($host, $port),
|
|
];
|
|
}
|
|
echo json_encode(['ok' => true, 'results' => $results]);
|
|
exit;
|
|
}
|
|
|
|
echo json_encode(['ok' => false, 'error' => 'Unknown action']);
|
|
exit;
|
|
}
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>KonCad</title>
|
|
<style>
|
|
:root {
|
|
--bg: #0f1117;
|
|
--surface: #1a1d27;
|
|
--border: #2a2d3a;
|
|
--text: #e2e8f0;
|
|
--muted: #64748b;
|
|
--accent: #7c8cf8;
|
|
--ok: #4ade80;
|
|
--warn: #f59e0b;
|
|
--error: #f87171;
|
|
}
|
|
|
|
* { box-sizing: border-box; margin: 0; padding: 0; }
|
|
|
|
body {
|
|
background: var(--bg);
|
|
color: var(--text);
|
|
font-family: system-ui, sans-serif;
|
|
min-height: 100vh;
|
|
}
|
|
|
|
header {
|
|
background: var(--surface);
|
|
border-bottom: 1px solid var(--border);
|
|
padding: 0.85rem 1.5rem;
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 1rem;
|
|
position: sticky;
|
|
top: 0;
|
|
z-index: 10;
|
|
}
|
|
|
|
header h1 { font-size: 1.2rem; font-weight: 700; color: var(--accent); flex: 1; }
|
|
|
|
#status { font-size: 0.82rem; color: var(--muted); }
|
|
#status.ok { color: var(--ok); }
|
|
#status.error { color: var(--error); }
|
|
#status.warn { color: var(--warn); }
|
|
|
|
main {
|
|
max-width: 960px;
|
|
margin: 0 auto;
|
|
padding: 1.5rem;
|
|
display: flex;
|
|
flex-direction: column;
|
|
gap: 1.5rem;
|
|
}
|
|
|
|
/* ── Panels ── */
|
|
.panel {
|
|
background: var(--surface);
|
|
border: 1px solid var(--border);
|
|
border-radius: 8px;
|
|
overflow: hidden;
|
|
}
|
|
|
|
.panel-header {
|
|
padding: 0.7rem 1rem;
|
|
border-bottom: 1px solid var(--border);
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 0.75rem;
|
|
cursor: pointer;
|
|
user-select: none;
|
|
}
|
|
.panel-header:hover { background: rgba(255,255,255,0.03); }
|
|
|
|
.panel-title { font-size: 0.875rem; font-weight: 600; flex: 1; }
|
|
|
|
.panel-body { padding: 1rem; }
|
|
.panel-body.hidden { display: none; }
|
|
|
|
/* ── Chevron ── */
|
|
.chev { color: var(--muted); font-size: 0.75rem; transition: transform 0.18s; }
|
|
.chev.open { transform: rotate(90deg); }
|
|
|
|
/* ── Badges ── */
|
|
.badge {
|
|
font-size: 0.72rem;
|
|
padding: 0.15rem 0.45rem;
|
|
border-radius: 4px;
|
|
background: rgba(255,255,255,0.06);
|
|
color: var(--muted);
|
|
}
|
|
.badge.on { background: rgba(124,140,248,0.15); color: var(--accent); }
|
|
.badge.warn { background: rgba(245,158,11,0.15); color: var(--warn); }
|
|
|
|
/* ── Toolbar ── */
|
|
.toolbar {
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 0.5rem;
|
|
flex-wrap: wrap;
|
|
margin-bottom: 0.75rem;
|
|
}
|
|
|
|
.toggle-wrap {
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 0.4rem;
|
|
font-size: 0.82rem;
|
|
color: var(--muted);
|
|
cursor: pointer;
|
|
user-select: none;
|
|
}
|
|
.toggle-wrap input { accent-color: var(--accent); cursor: pointer; }
|
|
.toggle-hint { font-size: 0.75rem; color: var(--muted); }
|
|
|
|
/* ── Code editor ── */
|
|
textarea.code {
|
|
width: 100%;
|
|
background: #0d0f18;
|
|
border: 1px solid var(--border);
|
|
border-radius: 6px;
|
|
color: var(--text);
|
|
font-family: 'JetBrains Mono', 'Cascadia Code', 'Fira Code', monospace;
|
|
font-size: 0.82rem;
|
|
line-height: 1.65;
|
|
padding: 0.75rem;
|
|
resize: vertical;
|
|
min-height: 100px;
|
|
outline: none;
|
|
tab-size: 4;
|
|
transition: border-color 0.15s;
|
|
}
|
|
textarea.code:focus { border-color: var(--accent); }
|
|
|
|
/* ── Site cards ── */
|
|
.sites-bar {
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 0.75rem;
|
|
margin-bottom: 0.75rem;
|
|
}
|
|
.sites-bar-title { font-size: 0.9rem; font-weight: 600; flex: 1; }
|
|
|
|
.site-card { background: var(--surface); border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
|
|
.site-card + .site-card { margin-top: 0.75rem; }
|
|
|
|
.site-card-header {
|
|
padding: 0.6rem 0.9rem;
|
|
border-bottom: 1px solid var(--border);
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 0.6rem;
|
|
cursor: pointer;
|
|
user-select: none;
|
|
}
|
|
.site-card-header:hover { background: rgba(255,255,255,0.03); }
|
|
|
|
.site-address {
|
|
flex: 1;
|
|
background: transparent;
|
|
border: none;
|
|
color: var(--accent);
|
|
font-family: 'JetBrains Mono', monospace;
|
|
font-size: 0.875rem;
|
|
font-weight: 600;
|
|
outline: none;
|
|
cursor: text;
|
|
min-width: 0;
|
|
}
|
|
.site-address::placeholder { color: var(--muted); font-weight: 400; }
|
|
|
|
.site-card-body { padding: 0.75rem 1rem; }
|
|
.site-card-body.hidden { display: none; }
|
|
|
|
.upstream-warnings {
|
|
margin-top: 0.5rem;
|
|
display: flex;
|
|
flex-direction: column;
|
|
gap: 0.25rem;
|
|
}
|
|
.upstream-warn-item {
|
|
font-size: 0.78rem;
|
|
color: var(--warn);
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 0.4rem;
|
|
}
|
|
|
|
#no-sites { color: var(--muted); font-size: 0.875rem; padding: 0.25rem 0; display: none; }
|
|
|
|
/* ── Buttons ── */
|
|
.btn {
|
|
padding: 0.4rem 0.9rem;
|
|
border-radius: 6px;
|
|
border: 1px solid var(--border);
|
|
background: rgba(255,255,255,0.06);
|
|
color: var(--text);
|
|
font-size: 0.82rem;
|
|
cursor: pointer;
|
|
transition: background 0.15s;
|
|
white-space: nowrap;
|
|
}
|
|
.btn:hover { background: rgba(255,255,255,0.11); }
|
|
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
|
|
|
|
.btn-accent { background: var(--accent); border-color: var(--accent); color: #0f1117; font-weight: 700; }
|
|
.btn-accent:hover { background: #9ba8ff; border-color: #9ba8ff; }
|
|
|
|
.btn-danger { border-color: var(--error); color: var(--error); background: transparent; }
|
|
.btn-danger:hover { background: rgba(248,113,113,0.12); }
|
|
|
|
.btn-warn { border-color: var(--warn); color: var(--warn); background: transparent; }
|
|
.btn-warn:hover { background: rgba(245,158,11,0.12); }
|
|
|
|
.btn-sm { padding: 0.25rem 0.55rem; font-size: 0.775rem; }
|
|
|
|
/* ── Modal ── */
|
|
.modal-overlay {
|
|
position: fixed; inset: 0;
|
|
background: rgba(0,0,0,0.6);
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
z-index: 50;
|
|
display: none;
|
|
}
|
|
.modal-overlay.visible { display: flex; }
|
|
|
|
.modal {
|
|
background: var(--surface);
|
|
border: 1px solid var(--border);
|
|
border-radius: 10px;
|
|
padding: 1.5rem;
|
|
max-width: 480px;
|
|
width: 90%;
|
|
}
|
|
.modal h2 { font-size: 1rem; margin-bottom: 0.75rem; color: var(--warn); }
|
|
.modal p { font-size: 0.85rem; color: var(--muted); margin-bottom: 1rem; }
|
|
|
|
.modal-list {
|
|
list-style: none;
|
|
margin-bottom: 1.25rem;
|
|
display: flex;
|
|
flex-direction: column;
|
|
gap: 0.35rem;
|
|
}
|
|
.modal-list li {
|
|
font-size: 0.82rem;
|
|
font-family: monospace;
|
|
color: var(--text);
|
|
background: rgba(245,158,11,0.08);
|
|
border: 1px solid rgba(245,158,11,0.2);
|
|
border-radius: 5px;
|
|
padding: 0.35rem 0.6rem;
|
|
}
|
|
|
|
.modal-actions { display: flex; gap: 0.5rem; justify-content: flex-end; }
|
|
|
|
/* ── Toast ── */
|
|
#toast {
|
|
position: fixed;
|
|
bottom: 1.5rem; right: 1.5rem;
|
|
padding: 0.7rem 1.1rem;
|
|
border-radius: 8px;
|
|
font-size: 0.85rem;
|
|
display: none;
|
|
z-index: 100;
|
|
max-width: 420px;
|
|
word-break: break-word;
|
|
}
|
|
#toast.ok { background: #0f2a1a; border: 1px solid var(--ok); color: var(--ok); }
|
|
#toast.error { background: #2a0f0f; border: 1px solid var(--error); color: var(--error); }
|
|
#toast.warn { background: #2a1f0f; border: 1px solid var(--warn); color: var(--warn); }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
|
|
<header>
|
|
<h1>KonCad</h1>
|
|
<span id="status">Loading…</span>
|
|
<button class="btn btn-accent" id="deploy-btn" onclick="preDeploy()">Deploy</button>
|
|
</header>
|
|
|
|
<main>
|
|
<!-- Global Options -->
|
|
<div class="panel">
|
|
<div class="panel-header" onclick="togglePanel('global')">
|
|
<span class="chev open" id="global-chev">▶</span>
|
|
<span class="panel-title">Global Options</span>
|
|
<span class="badge on" id="admin-badge" style="display:none">admin enabled</span>
|
|
</div>
|
|
<div class="panel-body" id="global-body">
|
|
<div class="toolbar">
|
|
<label class="toggle-wrap">
|
|
<input type="checkbox" id="admin-toggle" onchange="toggleAdmin(this.checked)">
|
|
Enable Admin API
|
|
</label>
|
|
<span class="toggle-hint">— required for KonCad to reach Caddy</span>
|
|
</div>
|
|
<textarea class="code" id="global-editor" rows="8"
|
|
placeholder="# Global options email admin@example.com # Enable admin API for KonCad: admin :2019"></textarea>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Sites -->
|
|
<div>
|
|
<div class="sites-bar">
|
|
<span class="sites-bar-title">Sites</span>
|
|
<button class="btn btn-sm" onclick="addSite()">+ Add Site</button>
|
|
</div>
|
|
<div id="sites-container"></div>
|
|
<div id="no-sites">No sites configured. Click "+ Add Site" to create one.</div>
|
|
</div>
|
|
</main>
|
|
|
|
<!-- Upstream warning modal -->
|
|
<div class="modal-overlay" id="modal-overlay">
|
|
<div class="modal">
|
|
<h2>⚠ Unreachable Upstreams</h2>
|
|
<p>The following reverse_proxy targets could not be reached. They may be down, not yet started, or the hostname/port is wrong.</p>
|
|
<ul class="modal-list" id="modal-list"></ul>
|
|
<div class="modal-actions">
|
|
<button class="btn" onclick="closeModal()">Cancel</button>
|
|
<button class="btn btn-warn" onclick="doDeploy()">Deploy anyway</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div id="toast"></div>
|
|
|
|
<script>
|
|
let sites = [];
|
|
|
|
// ─── Init ────────────────────────────────────────────────────────────────────
|
|
async function init() {
|
|
try {
|
|
const [loadRes, adminRes] = await Promise.all([
|
|
fetch('?action=load'),
|
|
fetch('?action=check_admin'),
|
|
]);
|
|
const d = await loadRes.json();
|
|
if (!d.ok) throw new Error(d.error);
|
|
document.getElementById('global-editor').value = d.data.global;
|
|
sites = d.data.sites;
|
|
syncAdminToggle();
|
|
renderSites();
|
|
|
|
try {
|
|
const admin = await adminRes.json();
|
|
if (!admin.ok) {
|
|
setEditable(false, admin.error || 'Caddy admin API unreachable');
|
|
return;
|
|
}
|
|
} catch (e) {
|
|
setEditable(false, 'Caddy admin API unreachable');
|
|
return;
|
|
}
|
|
|
|
setStatus('Ready', '');
|
|
} catch (e) {
|
|
setStatus('Load error: ' + e.message, 'error');
|
|
}
|
|
}
|
|
|
|
function setEditable(enabled, reason) {
|
|
document.querySelectorAll('textarea, input, button').forEach(el => el.disabled = !enabled);
|
|
if (!enabled) {
|
|
setStatus(reason, 'error');
|
|
document.getElementById('deploy-btn').disabled = true;
|
|
}
|
|
}
|
|
|
|
// ─── Deploy ───────────────────────────────────────────────────────────────────
|
|
async function preDeploy() {
|
|
collectSites();
|
|
|
|
// Extract all reverse_proxy targets from all site bodies
|
|
const targets = extractUpstreams();
|
|
if (targets.length === 0) { doDeploy(); return; }
|
|
|
|
setStatus('Checking upstreams…', '');
|
|
setDeployBtn(true, 'Checking…');
|
|
|
|
try {
|
|
const r = await fetch('?action=check_upstreams', {
|
|
method: 'POST',
|
|
headers: {'Content-Type': 'application/json'},
|
|
body: JSON.stringify({ targets }),
|
|
});
|
|
const d = await r.json();
|
|
const unreachable = (d.results || []).filter(x => !x.reachable);
|
|
|
|
if (unreachable.length > 0) {
|
|
// Show warning modal — let user decide
|
|
showModal(unreachable);
|
|
setStatus('Upstream warning', 'warn');
|
|
setDeployBtn(false, 'Deploy');
|
|
} else {
|
|
doDeploy();
|
|
}
|
|
} catch (e) {
|
|
// Upstream check failed — proceed with deploy anyway
|
|
doDeploy();
|
|
}
|
|
}
|
|
|
|
async function doDeploy() {
|
|
closeModal();
|
|
setDeployBtn(true, 'Deploying…');
|
|
setStatus('Deploying…', '');
|
|
|
|
|
|
try {
|
|
const payload = {
|
|
global: document.getElementById('global-editor').value,
|
|
sites: sites,
|
|
};
|
|
const r = await fetch('?action=save', {
|
|
method: 'POST',
|
|
headers: {'Content-Type': 'application/json'},
|
|
body: JSON.stringify(payload),
|
|
});
|
|
const d = await r.json();
|
|
if (!d.ok) {
|
|
setStatus('Deploy failed', 'error');
|
|
toast(d.error, 'error');
|
|
} else {
|
|
setStatus('Deployed', 'ok');
|
|
toast('Deployed successfully', 'ok');
|
|
}
|
|
} catch (e) {
|
|
setStatus('Error: ' + e.message, 'error');
|
|
toast(e.message, 'error');
|
|
} finally {
|
|
setDeployBtn(false, 'Deploy');
|
|
}
|
|
}
|
|
|
|
// ─── Upstream extraction ──────────────────────────────────────────────────────
|
|
function extractUpstreams() {
|
|
const targets = [];
|
|
const seen = new Set();
|
|
|
|
sites.forEach(s => {
|
|
const body = s.body || '';
|
|
// Match: reverse_proxy [to] [scheme://]host[:port] ...
|
|
const lines = body.split('\n');
|
|
lines.forEach(line => {
|
|
const m = line.match(/^\s*reverse_proxy\s+(?:to\s+)?(.+)/);
|
|
if (!m) return;
|
|
// Split on whitespace to handle multiple backends
|
|
m[1].split(/\s+/).forEach(part => {
|
|
// Strip scheme
|
|
part = part.replace(/^https?:\/\//, '');
|
|
// Stop at block open or empty
|
|
if (!part || part === '{') return;
|
|
const colonIdx = part.lastIndexOf(':');
|
|
let host, port;
|
|
if (colonIdx > 0 && /^\d+$/.test(part.slice(colonIdx + 1))) {
|
|
host = part.slice(0, colonIdx);
|
|
port = parseInt(part.slice(colonIdx + 1), 10);
|
|
} else {
|
|
host = part;
|
|
port = 80;
|
|
}
|
|
if (!host || host.includes('{')) return; // skip Caddy placeholders
|
|
const key = host + ':' + port;
|
|
if (!seen.has(key)) { seen.add(key); targets.push({ host, port }); }
|
|
});
|
|
});
|
|
});
|
|
|
|
return targets;
|
|
}
|
|
|
|
// ─── Sites ────────────────────────────────────────────────────────────────────
|
|
function collectSites() {
|
|
document.querySelectorAll('.site-card').forEach((card, i) => {
|
|
sites[i] = {
|
|
address: card.querySelector('.site-address').value.trim(),
|
|
body: card.querySelector('textarea.code').value,
|
|
};
|
|
});
|
|
}
|
|
|
|
function renderSites() {
|
|
const container = document.getElementById('sites-container');
|
|
const noSites = document.getElementById('no-sites');
|
|
container.innerHTML = '';
|
|
|
|
if (sites.length === 0) { noSites.style.display = 'block'; return; }
|
|
noSites.style.display = 'none';
|
|
|
|
sites.forEach((site, i) => {
|
|
const card = document.createElement('div');
|
|
card.className = 'site-card';
|
|
card.dataset.idx = i;
|
|
card.innerHTML = `
|
|
<div class="site-card-header" onclick="toggleSite(${i})">
|
|
<span class="chev open" id="sc-${i}">▶</span>
|
|
<input class="site-address" type="text"
|
|
value="${esc(site.address)}"
|
|
placeholder="example.com or :8080"
|
|
onclick="event.stopPropagation()"
|
|
oninput="event.stopPropagation()">
|
|
<button class="btn btn-sm btn-danger"
|
|
onclick="event.stopPropagation(); removeSite(${i})">Remove</button>
|
|
</div>
|
|
<div class="site-card-body" id="sb-${i}">
|
|
<textarea class="code" rows="7"
|
|
placeholder=" reverse_proxy localhost:3000">${esc(site.body)}</textarea>
|
|
<div class="upstream-warnings" id="uw-${i}"></div>
|
|
</div>`;
|
|
container.appendChild(card);
|
|
});
|
|
|
|
document.querySelectorAll('textarea.code').forEach(ta => ta.addEventListener('keydown', handleTab));
|
|
}
|
|
|
|
function addSite() {
|
|
collectSites();
|
|
sites.push({ address: '', body: '' });
|
|
renderSites();
|
|
const inputs = document.querySelectorAll('.site-address');
|
|
if (inputs.length) inputs[inputs.length - 1].focus();
|
|
}
|
|
|
|
function removeSite(i) {
|
|
collectSites();
|
|
sites.splice(i, 1);
|
|
renderSites();
|
|
}
|
|
|
|
function toggleSite(i) {
|
|
const body = document.getElementById(`sb-${i}`);
|
|
const chev = document.getElementById(`sc-${i}`);
|
|
const open = !body.classList.contains('hidden');
|
|
body.classList.toggle('hidden', open);
|
|
chev.classList.toggle('open', !open);
|
|
}
|
|
|
|
// ─── Global panel ─────────────────────────────────────────────────────────────
|
|
function togglePanel(id) {
|
|
const body = document.getElementById(`${id}-body`);
|
|
const chev = document.getElementById(`${id}-chev`);
|
|
const open = !body.classList.contains('hidden');
|
|
body.classList.toggle('hidden', open);
|
|
chev.classList.toggle('open', !open);
|
|
}
|
|
|
|
// ─── Admin toggle ─────────────────────────────────────────────────────────────
|
|
function syncAdminToggle() {
|
|
const val = document.getElementById('global-editor').value;
|
|
const enabled = /^\s*admin(?!\s+off)\b/m.test(val);
|
|
document.getElementById('admin-toggle').checked = enabled;
|
|
document.getElementById('admin-badge').style.display = enabled ? '' : 'none';
|
|
}
|
|
|
|
function toggleAdmin(enable) {
|
|
const ta = document.getElementById('global-editor');
|
|
if (enable) {
|
|
if (!/^\s*admin(?!\s+off)\b/m.test(ta.value)) {
|
|
ta.value = (ta.value.trimEnd() ? ta.value.trimEnd() + '\n' : '') + ' admin :2019\n';
|
|
}
|
|
} else {
|
|
ta.value = ta.value.replace(/^[ \t]*admin\b.*\n?/mg, '');
|
|
}
|
|
syncAdminToggle();
|
|
}
|
|
|
|
// ─── Modal ────────────────────────────────────────────────────────────────────
|
|
function showModal(unreachable) {
|
|
const list = document.getElementById('modal-list');
|
|
list.innerHTML = unreachable
|
|
.map(u => `<li>⚠ ${esc(u.host)}:${u.port}</li>`)
|
|
.join('');
|
|
document.getElementById('modal-overlay').classList.add('visible');
|
|
}
|
|
|
|
function closeModal() {
|
|
document.getElementById('modal-overlay').classList.remove('visible');
|
|
}
|
|
|
|
// Close modal on overlay click
|
|
document.getElementById('modal-overlay').addEventListener('click', e => {
|
|
if (e.target === e.currentTarget) closeModal();
|
|
});
|
|
|
|
// ─── Helpers ─────────────────────────────────────────────────────────────────
|
|
function setStatus(msg, cls) {
|
|
const el = document.getElementById('status');
|
|
el.textContent = msg;
|
|
el.className = cls || '';
|
|
}
|
|
|
|
function setDeployBtn(disabled, label) {
|
|
const btn = document.getElementById('deploy-btn');
|
|
btn.disabled = disabled;
|
|
btn.textContent = label;
|
|
}
|
|
|
|
function toast(msg, type) {
|
|
const el = document.getElementById('toast');
|
|
el.textContent = msg;
|
|
el.className = type;
|
|
el.style.display = 'block';
|
|
clearTimeout(el._t);
|
|
el._t = setTimeout(() => el.style.display = 'none', 5000);
|
|
}
|
|
|
|
function esc(s) {
|
|
return String(s ?? '')
|
|
.replace(/&/g, '&').replace(/</g, '<')
|
|
.replace(/>/g, '>').replace(/"/g, '"');
|
|
}
|
|
|
|
function handleTab(e) {
|
|
if (e.key !== 'Tab') return;
|
|
e.preventDefault();
|
|
const ta = e.target, s = ta.selectionStart, end = ta.selectionEnd;
|
|
ta.value = ta.value.slice(0, s) + ' ' + ta.value.slice(end);
|
|
ta.selectionStart = ta.selectionEnd = s + 4;
|
|
}
|
|
|
|
document.addEventListener('DOMContentLoaded', () => {
|
|
document.getElementById('global-editor').addEventListener('input', syncAdminToggle);
|
|
init();
|
|
});
|
|
</script>
|
|
</body>
|
|
</html>
|