add optional web UI: busybox httpd + shell CGI, dark theme
- /etc/init.d/wirecon-web supervises `busybox httpd` on WEB_PORT (8080).
- /var/www/wirecon: single-page UI with Status / Config / Logs tabs.
- Status auto-refreshes every 5s: service states + wg0 endpoint,
handshake age, MTU, rx/tx.
- Config edits wg0.conf + wirecon.conf, saves atomically, restarts
dependents, surfaces their output.
- Logs tails /var/log/wirecon-{watchdog,monitor}.log.
- CGI returns plain text (key=value or raw) — no JSON-escape pain.
- HTTP Basic Auth template in /etc/wirecon/httpd.conf.
- Disable via `rc-service wirecon-web stop && rc-update del wirecon-web`.
This commit is contained in:
1 parent
2d07a590e2
commit
4e7e0f34ba
12 files changed
+543
-4
No files matched your search
@@ -0,0 +1,117 @@
|
||||
// wirecon web UI — vanilla JS. Talks to CGI scripts in /cgi-bin/ that
|
||||
// return plain text (key=value for status, raw text for config/logs).
|
||||
|
||||
const $ = s => document.querySelector(s);
|
||||
const $$ = s => document.querySelectorAll(s);
|
||||
|
||||
// --- tabs ------------------------------------------------------------------
|
||||
$$('nav button').forEach(btn => btn.addEventListener('click', () => {
|
||||
$$('nav button').forEach(b => b.classList.toggle('active', b === btn));
|
||||
const tab = btn.dataset.tab;
|
||||
$$('.tab').forEach(s => s.classList.toggle('active', s.id === `tab-${tab}`));
|
||||
if (tab === 'config') loadConfigs();
|
||||
if (tab === 'logs') loadLogs();
|
||||
}));
|
||||
|
||||
// --- helpers ---------------------------------------------------------------
|
||||
const parseKV = txt => Object.fromEntries(
|
||||
txt.trim().split('\n').filter(Boolean).map(l => {
|
||||
const i = l.indexOf('=');
|
||||
return [l.slice(0, i), l.slice(i + 1)];
|
||||
})
|
||||
);
|
||||
|
||||
const setOverall = ok => {
|
||||
const el = $('#overall');
|
||||
el.textContent = ok ? 'all systems ok' : 'attention';
|
||||
el.className = 'pill ' + (ok ? 'ok' : 'err');
|
||||
};
|
||||
|
||||
// --- status ----------------------------------------------------------------
|
||||
async function refreshStatus() {
|
||||
try {
|
||||
const r = await fetch('cgi-bin/status.sh', { cache: 'no-store' });
|
||||
const kv = parseKV(await r.text());
|
||||
|
||||
const services = ['wg-quick.wg0', 'wirecon-watchdog', 'wirecon-monitor', 'wirecon-web'];
|
||||
const tbody = $('#svc-table tbody');
|
||||
tbody.innerHTML = '';
|
||||
let allOk = true;
|
||||
for (const s of services) {
|
||||
const state = kv[`svc.${s}`] || 'unknown';
|
||||
if (state !== 'running') allOk = false;
|
||||
tbody.insertAdjacentHTML('beforeend',
|
||||
`<tr><td>${s}</td><td class="state-${state}">${state}</td></tr>`);
|
||||
}
|
||||
|
||||
const age = kv['wg0.handshake_age_s'];
|
||||
const stale = age && parseInt(age, 10) > 180;
|
||||
if (stale) allOk = false;
|
||||
|
||||
const dl = $('#wg-info');
|
||||
dl.innerHTML = '';
|
||||
const fields = [
|
||||
['interface', kv['wg0.iface'] || '—'],
|
||||
['endpoint', kv['wg0.endpoint'] || '—'],
|
||||
['MTU', kv['wg0.mtu'] || '—'],
|
||||
['last handshake', age === undefined ? '—' : age === '-1' ? 'never' : `${age}s ago`],
|
||||
['rx / tx', `${kv['wg0.rx_bytes'] || '—'} / ${kv['wg0.tx_bytes'] || '—'}`],
|
||||
];
|
||||
for (const [k, v] of fields) {
|
||||
dl.insertAdjacentHTML('beforeend', `<dt>${k}</dt><dd>${v}</dd>`);
|
||||
}
|
||||
|
||||
$('#status-updated').textContent = `updated ${new Date().toLocaleTimeString()}`;
|
||||
setOverall(allOk);
|
||||
} catch (e) {
|
||||
setOverall(false);
|
||||
$('#status-updated').textContent = `error: ${e.message}`;
|
||||
}
|
||||
}
|
||||
|
||||
// --- config ----------------------------------------------------------------
|
||||
async function loadConfig(which) {
|
||||
const r = await fetch(`cgi-bin/config-get.sh?file=${which}`, { cache: 'no-store' });
|
||||
if (!r.ok) { $(`#cfg-${which}`).value = `[failed to load: HTTP ${r.status}]`; return; }
|
||||
$(`#cfg-${which}`).value = await r.text();
|
||||
}
|
||||
|
||||
async function loadConfigs() { await Promise.all([loadConfig('wg0'), loadConfig('wirecon')]); }
|
||||
|
||||
$$('button[data-save]').forEach(btn => btn.addEventListener('click', async () => {
|
||||
const which = btn.dataset.save;
|
||||
const res = $(`#res-${which}`);
|
||||
res.className = 'result';
|
||||
res.textContent = 'saving…';
|
||||
try {
|
||||
const body = $(`#cfg-${which}`).value;
|
||||
const r = await fetch(`cgi-bin/config-save.sh?file=${which}`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'text/plain' },
|
||||
body,
|
||||
});
|
||||
const txt = await r.text();
|
||||
res.textContent = txt;
|
||||
res.className = 'result ' + (r.ok ? 'ok' : 'err');
|
||||
if (r.ok) refreshStatus();
|
||||
} catch (e) {
|
||||
res.className = 'result err';
|
||||
res.textContent = `error: ${e.message}`;
|
||||
}
|
||||
}));
|
||||
|
||||
// --- logs ------------------------------------------------------------------
|
||||
async function loadLog(which) {
|
||||
const r = await fetch(`cgi-bin/logs.sh?service=${which}&lines=200`, { cache: 'no-store' });
|
||||
$(`#log-${which}`).textContent = await r.text();
|
||||
// scroll to bottom
|
||||
const el = $(`#log-${which}`);
|
||||
el.scrollTop = el.scrollHeight;
|
||||
}
|
||||
async function loadLogs() { await Promise.all([loadLog('watchdog'), loadLog('monitor')]); }
|
||||
|
||||
$$('button[data-reload]').forEach(btn => btn.addEventListener('click', () => loadLog(btn.dataset.reload)));
|
||||
|
||||
// --- boot ------------------------------------------------------------------
|
||||
refreshStatus();
|
||||
setInterval(refreshStatus, 5000);
|
||||
Executable
+26
@@ -0,0 +1,26 @@
|
||||
#!/bin/bash
|
||||
# CGI: return a config file's contents as text/plain.
|
||||
# file=wg0 → /etc/wireguard/wg0.conf
|
||||
# file=wirecon → /etc/wirecon/wirecon.conf
|
||||
set -u
|
||||
|
||||
file=""
|
||||
case "${QUERY_STRING:-}" in
|
||||
*file=wg0*) file=/etc/wireguard/wg0.conf ;;
|
||||
*file=wirecon*) file=/etc/wirecon/wirecon.conf ;;
|
||||
esac
|
||||
|
||||
if [[ -z "$file" ]]; then
|
||||
printf 'Status: 400 Bad Request\r\nContent-Type: text/plain\r\n\r\n'
|
||||
echo "bad file parameter"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
if [[ ! -r "$file" ]]; then
|
||||
printf 'Status: 404 Not Found\r\nContent-Type: text/plain\r\n\r\n'
|
||||
echo "not readable: $file"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
printf 'Content-Type: text/plain\r\n\r\n'
|
||||
cat "$file"
|
||||
Executable
+62
@@ -0,0 +1,62 @@
|
||||
#!/bin/bash
|
||||
# CGI: accept POST body as the new contents of a config file, write it
|
||||
# atomically, then restart the services that depend on it.
|
||||
# Query: file=wg0|wirecon
|
||||
# Body: raw text
|
||||
set -u
|
||||
|
||||
file=""; label=""; restart=()
|
||||
case "${QUERY_STRING:-}" in
|
||||
*file=wg0*)
|
||||
file=/etc/wireguard/wg0.conf
|
||||
label="wg0.conf"
|
||||
restart=(wg-quick.wg0 wirecon-watchdog wirecon-monitor)
|
||||
mode=0600
|
||||
;;
|
||||
*file=wirecon*)
|
||||
file=/etc/wirecon/wirecon.conf
|
||||
label="wirecon.conf"
|
||||
restart=(wirecon-watchdog wirecon-monitor)
|
||||
mode=0644
|
||||
;;
|
||||
*)
|
||||
printf 'Status: 400 Bad Request\r\nContent-Type: text/plain\r\n\r\n'
|
||||
echo "bad file parameter"; exit 0
|
||||
;;
|
||||
esac
|
||||
|
||||
if [[ "${REQUEST_METHOD:-}" != "POST" ]]; then
|
||||
printf 'Status: 405 Method Not Allowed\r\nContent-Type: text/plain\r\n\r\n'
|
||||
echo "POST required"; exit 0
|
||||
fi
|
||||
|
||||
# Read body (CONTENT_LENGTH bytes; fallback to EOF).
|
||||
len="${CONTENT_LENGTH:-0}"
|
||||
tmp=$(mktemp "${file}.XXXXXX")
|
||||
trap 'rm -f "$tmp"' EXIT
|
||||
|
||||
if [[ "$len" -gt 0 ]]; then
|
||||
head -c "$len" > "$tmp"
|
||||
else
|
||||
cat > "$tmp"
|
||||
fi
|
||||
|
||||
# Refuse empty writes — likely a bug, not intent.
|
||||
if [[ ! -s "$tmp" ]]; then
|
||||
printf 'Status: 400 Bad Request\r\nContent-Type: text/plain\r\n\r\n'
|
||||
echo "refusing empty body"; exit 0
|
||||
fi
|
||||
|
||||
chmod "$mode" "$tmp"
|
||||
mv -f "$tmp" "$file"
|
||||
trap - EXIT
|
||||
|
||||
# Restart dependents, collect output for the UI.
|
||||
printf 'Content-Type: text/plain\r\n\r\n'
|
||||
echo "saved $file ($(wc -c < "$file") bytes)"
|
||||
|
||||
for svc in "${restart[@]}"; do
|
||||
echo ""
|
||||
echo "--- restart $svc ---"
|
||||
rc-service "$svc" restart 2>&1 || echo "(restart of $svc returned non-zero)"
|
||||
done
|
||||
Executable
+29
@@ -0,0 +1,29 @@
|
||||
#!/bin/bash
|
||||
# CGI: return the tail of a log file as text/plain.
|
||||
# Query: service=watchdog|monitor lines=N (capped to 1000)
|
||||
set -u
|
||||
|
||||
service=""; lines=100
|
||||
case "${QUERY_STRING:-}" in
|
||||
*service=watchdog*) service=watchdog ;;
|
||||
*service=monitor*) service=monitor ;;
|
||||
esac
|
||||
|
||||
# pull lines= from query string
|
||||
if [[ "${QUERY_STRING:-}" =~ lines=([0-9]+) ]]; then
|
||||
lines="${BASH_REMATCH[1]}"
|
||||
(( lines > 1000 )) && lines=1000
|
||||
fi
|
||||
|
||||
if [[ -z "$service" ]]; then
|
||||
printf 'Status: 400 Bad Request\r\nContent-Type: text/plain\r\n\r\n'
|
||||
echo "service must be watchdog or monitor"; exit 0
|
||||
fi
|
||||
|
||||
file="/var/log/wirecon-${service}.log"
|
||||
printf 'Content-Type: text/plain\r\n\r\n'
|
||||
if [[ -r "$file" ]]; then
|
||||
tail -n "$lines" "$file"
|
||||
else
|
||||
echo "(no log yet at $file)"
|
||||
fi
|
||||
Executable
+40
@@ -0,0 +1,40 @@
|
||||
#!/bin/bash
|
||||
# CGI: emit service + tunnel state as key=value lines.
|
||||
set -u
|
||||
|
||||
printf 'Content-Type: text/plain\r\n\r\n'
|
||||
|
||||
for svc in wg-quick.wg0 wirecon-watchdog wirecon-monitor wirecon-web; do
|
||||
if rc-service "$svc" status >/dev/null 2>&1; then
|
||||
state=running
|
||||
else
|
||||
state=stopped
|
||||
fi
|
||||
printf 'svc.%s=%s\n' "$svc" "$state"
|
||||
done
|
||||
|
||||
IFACE="${WG_INTERFACE:-wg0}"
|
||||
|
||||
if ip link show "$IFACE" >/dev/null 2>&1; then
|
||||
printf 'wg0.iface=up\n'
|
||||
mtu=$(ip -o link show "$IFACE" | awk '{for(i=1;i<=NF;i++) if($i=="mtu"){print $(i+1); exit}}')
|
||||
printf 'wg0.mtu=%s\n' "${mtu:-?}"
|
||||
|
||||
# peer dump: pub psk endpoint allowed_ips last_handshake rx tx keepalive
|
||||
dump=$(wg show "$IFACE" dump 2>/dev/null | awk 'NR==2')
|
||||
if [[ -n "$dump" ]]; then
|
||||
endpoint=$(awk '{print $3}' <<<"$dump")
|
||||
hs_ts=$(awk '{print $5}' <<<"$dump")
|
||||
rx=$(awk '{print $6}' <<<"$dump")
|
||||
tx=$(awk '{print $7}' <<<"$dump")
|
||||
now=$(date +%s)
|
||||
age=-1
|
||||
[[ "$hs_ts" != "0" && -n "$hs_ts" ]] && age=$((now - hs_ts))
|
||||
printf 'wg0.endpoint=%s\n' "${endpoint:-none}"
|
||||
printf 'wg0.handshake_age_s=%s\n' "$age"
|
||||
printf 'wg0.rx_bytes=%s\n' "${rx:-0}"
|
||||
printf 'wg0.tx_bytes=%s\n' "${tx:-0}"
|
||||
fi
|
||||
else
|
||||
printf 'wg0.iface=down\n'
|
||||
fi
|
||||
@@ -0,0 +1,75 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width,initial-scale=1">
|
||||
<title>wirecon</title>
|
||||
<link rel="stylesheet" href="style.css">
|
||||
</head>
|
||||
<body>
|
||||
<header>
|
||||
<h1>wirecon</h1>
|
||||
<div id="overall" class="pill">checking…</div>
|
||||
</header>
|
||||
|
||||
<nav>
|
||||
<button data-tab="status" class="active">Status</button>
|
||||
<button data-tab="config">Config</button>
|
||||
<button data-tab="logs">Logs</button>
|
||||
</nav>
|
||||
|
||||
<main>
|
||||
<!-- STATUS ------------------------------------------------------------ -->
|
||||
<section id="tab-status" class="tab active">
|
||||
<div class="row">
|
||||
<h2>services</h2>
|
||||
<span class="muted" id="status-updated"></span>
|
||||
</div>
|
||||
<table id="svc-table">
|
||||
<thead><tr><th>name</th><th>state</th></tr></thead>
|
||||
<tbody></tbody>
|
||||
</table>
|
||||
|
||||
<h2>tunnel</h2>
|
||||
<dl id="wg-info" class="kv"></dl>
|
||||
</section>
|
||||
|
||||
<!-- CONFIG ------------------------------------------------------------ -->
|
||||
<section id="tab-config" class="tab">
|
||||
<div class="config-block">
|
||||
<div class="row">
|
||||
<h2>/etc/wireguard/wg0.conf</h2>
|
||||
<button data-save="wg0">save + restart tunnel</button>
|
||||
</div>
|
||||
<textarea id="cfg-wg0" spellcheck="false" rows="18"></textarea>
|
||||
<pre class="result" id="res-wg0"></pre>
|
||||
</div>
|
||||
|
||||
<div class="config-block">
|
||||
<div class="row">
|
||||
<h2>/etc/wirecon/wirecon.conf</h2>
|
||||
<button data-save="wirecon">save + restart watchdog/monitor</button>
|
||||
</div>
|
||||
<textarea id="cfg-wirecon" spellcheck="false" rows="18"></textarea>
|
||||
<pre class="result" id="res-wirecon"></pre>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<!-- LOGS -------------------------------------------------------------- -->
|
||||
<section id="tab-logs" class="tab">
|
||||
<div class="row">
|
||||
<h2>watchdog</h2>
|
||||
<button data-reload="watchdog">reload</button>
|
||||
</div>
|
||||
<pre id="log-watchdog" class="log"></pre>
|
||||
<div class="row">
|
||||
<h2>monitor</h2>
|
||||
<button data-reload="monitor">reload</button>
|
||||
</div>
|
||||
<pre id="log-monitor" class="log"></pre>
|
||||
</section>
|
||||
</main>
|
||||
|
||||
<script src="app.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,101 @@
|
||||
:root {
|
||||
--bg: #0e0e11;
|
||||
--panel: #17171c;
|
||||
--panel-2: #1f1f27;
|
||||
--border: #2a2a33;
|
||||
--text: #e7e7ea;
|
||||
--muted: #8b8b95;
|
||||
--accent: #2dd4bf;
|
||||
--ok: #4ade80;
|
||||
--warn: #fbbf24;
|
||||
--err: #f87171;
|
||||
--mono: ui-monospace, "JetBrains Mono", "SFMono-Regular", Menlo, Consolas, monospace;
|
||||
--sans: system-ui, -apple-system, "Segoe UI", Roboto, Inter, sans-serif;
|
||||
}
|
||||
|
||||
* { box-sizing: border-box; }
|
||||
html, body { margin: 0; padding: 0; background: var(--bg); color: var(--text); font-family: var(--sans); font-size: 14px; line-height: 1.45; }
|
||||
h1, h2 { margin: 0; font-weight: 600; }
|
||||
h1 { font-size: 18px; letter-spacing: 0.02em; }
|
||||
h2 { font-size: 13px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); }
|
||||
|
||||
header {
|
||||
display: flex; align-items: center; justify-content: space-between;
|
||||
padding: 14px 22px;
|
||||
border-bottom: 1px solid var(--border);
|
||||
background: var(--panel);
|
||||
}
|
||||
|
||||
nav {
|
||||
display: flex; gap: 4px;
|
||||
padding: 10px 18px;
|
||||
border-bottom: 1px solid var(--border);
|
||||
background: var(--panel);
|
||||
}
|
||||
nav button {
|
||||
appearance: none; border: 0; background: transparent;
|
||||
color: var(--muted); font-family: var(--sans); font-size: 13px;
|
||||
padding: 6px 14px; border-radius: 6px; cursor: pointer;
|
||||
}
|
||||
nav button:hover { color: var(--text); background: var(--panel-2); }
|
||||
nav button.active { color: var(--text); background: var(--panel-2); box-shadow: inset 0 -2px 0 var(--accent); }
|
||||
|
||||
main { padding: 22px; max-width: 960px; margin: 0 auto; }
|
||||
.tab { display: none; }
|
||||
.tab.active { display: block; }
|
||||
|
||||
.row { display: flex; align-items: center; justify-content: space-between; margin: 22px 0 10px; }
|
||||
.row h2 { margin: 0; }
|
||||
|
||||
.pill {
|
||||
padding: 3px 10px; border-radius: 999px;
|
||||
font-family: var(--mono); font-size: 12px;
|
||||
background: var(--panel-2); color: var(--muted);
|
||||
border: 1px solid var(--border);
|
||||
}
|
||||
.pill.ok { color: var(--ok); border-color: color-mix(in srgb, var(--ok) 40%, var(--border)); }
|
||||
.pill.warn { color: var(--warn); border-color: color-mix(in srgb, var(--warn) 40%, var(--border)); }
|
||||
.pill.err { color: var(--err); border-color: color-mix(in srgb, var(--err) 40%, var(--border)); }
|
||||
|
||||
table { width: 100%; border-collapse: collapse; background: var(--panel); border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
|
||||
th, td { text-align: left; padding: 10px 14px; border-bottom: 1px solid var(--border); font-family: var(--mono); font-size: 13px; }
|
||||
th { color: var(--muted); font-weight: 500; text-transform: uppercase; font-size: 11px; letter-spacing: 0.08em; }
|
||||
tr:last-child td { border-bottom: 0; }
|
||||
td.state-running { color: var(--ok); }
|
||||
td.state-stopped { color: var(--err); }
|
||||
td.state-unknown { color: var(--muted); }
|
||||
|
||||
.kv { display: grid; grid-template-columns: max-content 1fr; gap: 6px 18px;
|
||||
margin: 0; padding: 14px; background: var(--panel); border: 1px solid var(--border); border-radius: 8px;
|
||||
font-family: var(--mono); font-size: 13px; }
|
||||
.kv dt { color: var(--muted); }
|
||||
.kv dd { margin: 0; }
|
||||
|
||||
textarea {
|
||||
width: 100%; display: block;
|
||||
background: #0a0a0d; color: var(--text); border: 1px solid var(--border); border-radius: 8px;
|
||||
font-family: var(--mono); font-size: 12.5px; line-height: 1.55;
|
||||
padding: 12px; resize: vertical; tab-size: 2;
|
||||
}
|
||||
textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: var(--accent); }
|
||||
|
||||
button[data-save], button[data-reload] {
|
||||
appearance: none; border: 1px solid var(--border); background: var(--panel-2); color: var(--text);
|
||||
padding: 6px 12px; border-radius: 6px; cursor: pointer; font-family: var(--sans); font-size: 12px;
|
||||
}
|
||||
button[data-save]:hover, button[data-reload]:hover { border-color: var(--accent); color: var(--accent); }
|
||||
|
||||
.config-block + .config-block { margin-top: 28px; }
|
||||
|
||||
.result { margin-top: 10px; min-height: 0; padding: 0; background: transparent; border: 0;
|
||||
font-family: var(--mono); font-size: 12px; color: var(--muted); white-space: pre-wrap; }
|
||||
.result.ok { color: var(--ok); }
|
||||
.result.err { color: var(--err); }
|
||||
|
||||
.log {
|
||||
background: #0a0a0d; border: 1px solid var(--border); border-radius: 8px;
|
||||
padding: 12px; max-height: 360px; overflow: auto;
|
||||
font-family: var(--mono); font-size: 12px; white-space: pre-wrap;
|
||||
}
|
||||
|
||||
.muted { color: var(--muted); font-size: 12px; font-family: var(--mono); }
|
||||
Reference in new issue
Block a user