/* Sovran_SystemsOS Hub — First-Boot Onboarding Wizard
Drives the 6-step post-install setup flow. */
"use strict";
// ── Constants ─────────────────────────────────────────────────────
const TOTAL_STEPS = 6;
// Domains that may need configuration, with service unit mapping for enabled check
const DOMAIN_DEFS = [
{ name: "matrix", label: "Matrix (Synapse)", unit: "matrix-synapse.service", needsDdns: true },
{ name: "haven", label: "Haven Nostr Relay", unit: "haven-relay.service", needsDdns: true },
{ name: "element-calling", label: "Element Video/Audio Calling", unit: "livekit.service", needsDdns: true },
{ name: "vaultwarden", label: "Vaultwarden (Password Vault)", unit: "vaultwarden.service", needsDdns: true },
{ name: "btcpayserver", label: "BTCPay Server", unit: "btcpayserver.service", needsDdns: true },
{ name: "nextcloud", label: "Nextcloud", unit: "phpfpm-nextcloud.service", needsDdns: true },
{ name: "wordpress", label: "WordPress", unit: "phpfpm-wordpress.service", needsDdns: true },
];
const REBUILD_POLL_INTERVAL = 2000;
// ── State ─────────────────────────────────────────────────────────
var _currentStep = 1;
var _servicesData = null;
var _domainsData = null;
var _featuresData = null;
var _rebuildPollTimer = null;
var _rebuildLogOffset = 0;
var _rebuildFinished = false;
// ── Helpers ───────────────────────────────────────────────────────
function escHtml(str) {
return String(str)
.replace(/&/g, "&")
.replace(//g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
async function apiFetch(path, options) {
var res = await fetch(path, options || {});
if (!res.ok) {
var detail = res.status + " " + res.statusText;
try {
var body = await res.json();
if (body && body.detail) detail = body.detail;
} catch (e) {}
throw new Error(detail);
}
return res.json();
}
function setStatus(elId, msg, type) {
var el = document.getElementById(elId);
if (!el) return;
el.textContent = msg;
el.className = "onboarding-save-status" + (type ? " onboarding-save-status--" + type : "");
}
// ── Progress / step navigation ────────────────────────────────────
function updateProgress(step) {
var fill = document.getElementById("onboarding-progress-fill");
if (fill) {
fill.style.width = Math.round(((step - 1) / (TOTAL_STEPS - 1)) * 100) + "%";
}
var dots = document.querySelectorAll(".onboarding-step-dot");
dots.forEach(function(dot) {
var ds = parseInt(dot.dataset.step, 10);
dot.classList.remove("active", "completed");
if (ds < step) dot.classList.add("completed");
if (ds === step) dot.classList.add("active");
});
}
function showStep(step) {
for (var i = 1; i <= TOTAL_STEPS; i++) {
var panel = document.getElementById("step-" + i);
if (panel) panel.style.display = (i === step) ? "" : "none";
}
_currentStep = step;
updateProgress(step);
// Lazy-load step content
if (step === 2) loadStep2();
if (step === 3) loadStep3();
if (step === 4) loadStep4();
if (step === 5) loadStep5();
}
// ── Step 1: Welcome ───────────────────────────────────────────────
async function loadStep1() {
try {
var cfg = await apiFetch("/api/config");
var badge = document.getElementById("onboarding-role-badge");
if (badge && cfg.role_label) badge.textContent = cfg.role_label;
} catch (_) {}
}
// ── Step 2: Domain Configuration ─────────────────────────────────
async function loadStep2() {
var body = document.getElementById("step-2-body");
if (!body) return;
try {
// Fetch services, domains, and network info in parallel
var results = await Promise.all([
apiFetch("/api/services"),
apiFetch("/api/domains/status"),
apiFetch("/api/network"),
]);
_servicesData = results[0];
_domainsData = results[1];
var networkData = results[2];
} catch (err) {
body.innerHTML = '
⚠ Could not load service data: ' + escHtml(err.message) + '
';
return;
}
var externalIp = (networkData && networkData.external_ip) || "Unknown (could not retrieve)";
// Build set of enabled service units
var enabledUnits = new Set();
(_servicesData || []).forEach(function(svc) {
if (svc.enabled) enabledUnits.add(svc.unit);
});
// Filter domain defs to only those whose service is enabled
var relevantDomains = DOMAIN_DEFS.filter(function(d) {
return enabledUnits.has(d.unit);
});
var html = "";
if (relevantDomains.length === 0) {
html += '
No domain-based services are enabled for your role. You can skip this step.
⚠ Could not load network data: ' + escHtml(err.message) + '
';
return;
}
var internalIp = (networkData && networkData.internal_ip) || "unknown";
var ip = escHtml(internalIp);
var html = '
'
+ '⚠ Each port only needs to be forwarded once — all services share the same ports.'
+ '
';
html += '
';
html += ' Forward ports to this machine\'s internal IP:';
html += ' ' + ip + '';
html += '
';
// Required ports table
html += '
';
html += '
Required Ports — open these on your router:
';
html += '
';
html += '
Port
Protocol
Forward to
Purpose
';
html += '';
html += '
80
TCP
' + ip + '
HTTP
';
html += '
443
TCP
' + ip + '
HTTPS
';
html += '
22
TCP
' + ip + '
SSH Remote Access
';
html += '
8448
TCP
' + ip + '
Matrix Federation
';
html += '
';
html += '
';
// Optional ports table
html += '
';
html += '
Optional — Only needed if you enable Element Calling:
';
html += '
These 5 additional port openings are required on top of the 4 required ports above.
';
html += '
';
html += '
Port
Protocol
Forward to
Purpose
';
html += '';
html += '
7881
TCP
' + ip + '
LiveKit WebRTC signalling
';
html += '
7882–7894
UDP
' + ip + '
LiveKit media streams
';
html += '
5349
TCP
' + ip + '
TURN over TLS
';
html += '
3478
UDP
' + ip + '
TURN (STUN/relay)
';
html += '
30000–40000
TCP/UDP
' + ip + '
TURN relay (WebRTC)
';
html += '
';
html += '
';
// Totals
html += '
';
html += 'Total port openings: 4 (without Element Calling) ';
html += 'Total port openings: 9 (with Element Calling — 4 required + 5 optional)';
html += '
';
html += '
'
+ '⚠ Ports 80 and 443 must be forwarded first. '
+ 'Caddy uses these to obtain SSL certificates from Let\'s Encrypt. '
+ 'If they are closed, HTTPS will not work and your services will be unreachable from outside your network.'
+ '
';
html += ''
+ 'How to set up port forwarding'
+ ''
+ '
Open your router\'s admin panel — usually http://192.168.1.1 or http://192.168.0.1
'
+ '
Look for "Port Forwarding", "NAT", or "Virtual Server" in the settings
'
+ '
Create a new rule for each port listed above
'
+ '
Set the destination/internal IP to ' + ip + '
'
+ '
Set both internal and external port to the same number
'
+ '
Save and apply changes
'
+ ''
+ '';
body.innerHTML = html;
}
// ── Step 4: Credentials ───────────────────────────────────────────
async function loadStep4() {
var body = document.getElementById("step-4-body");
if (!body) return;
body.innerHTML = '
⚠ Could not load services: ' + escHtml(err.message) + '
';
return;
}
}
// Find services with credentials that are enabled
var credsServices = (_servicesData || []).filter(function(svc) {
return svc.has_credentials && svc.enabled;
});
if (credsServices.length === 0) {
body.innerHTML = '
No credentials found for your current configuration.
';
return;
}
body.innerHTML = '
Loading credentials…
';
// Fetch all credentials in parallel
var fetches = credsServices.map(function(svc) {
return apiFetch("/api/credentials/" + encodeURIComponent(svc.unit))
.then(function(data) { return { svc: svc, data: data, error: null }; })
.catch(function(err) { return { svc: svc, data: null, error: err.message }; });
});
var allCreds = await Promise.all(fetches);
// Group by category
var CATEGORY_ORDER_LOCAL = [
["infrastructure", "🔧 Infrastructure"],
["bitcoin-base", "₿ Bitcoin Base"],
["bitcoin-apps", "₿ Bitcoin Apps"],
["communication", "💬 Communication"],
["apps", "📦 Self-Hosted Apps"],
["nostr", "📡 Nostr"],
];
var grouped = {};
allCreds.forEach(function(item) {
var cat = item.svc.category || "other";
if (!grouped[cat]) grouped[cat] = [];
grouped[cat].push(item);
});
var html = '
💡 Save these credentials somewhere safe. You can always view them again from the Hub dashboard.
';
CATEGORY_ORDER_LOCAL.forEach(function(pair) {
var catKey = pair[0];
var catLabel = pair[1];
if (!grouped[catKey] || grouped[catKey].length === 0) return;
html += '
';
html += '
' + escHtml(catLabel) + '
';
grouped[catKey].forEach(function(item) {
html += '
';
html += '
' + escHtml(item.svc.name) + '
';
if (item.error) {
html += '
⚠ ' + escHtml(item.error) + '
';
} else if (item.data && item.data.credentials) {
item.data.credentials.forEach(function(cred) {
html += '
';
html += '' + escHtml(cred.label || "") + '';
if (cred.value) {
var isSecret = /password|secret|key|token/i.test(cred.label || "");
if (isSecret) {
html += ''
+ '••••••••'
+ '' + escHtml(cred.value) + ''
+ ''
+ '';
} else {
html += '' + escHtml(cred.value) + '';
}
}
html += '
';
});
}
html += '
';
});
html += '
';
});
// Remaining categories not in the order
Object.keys(grouped).forEach(function(catKey) {
var inOrder = CATEGORY_ORDER_LOCAL.some(function(p) { return p[0] === catKey; });
if (inOrder || !grouped[catKey] || grouped[catKey].length === 0) return;
html += '
';
html += '
' + escHtml(catKey) + '
';
grouped[catKey].forEach(function(item) {
html += '
';
html += '
' + escHtml(item.svc.name) + '
';
if (item.error) {
html += '
⚠ ' + escHtml(item.error) + '
';
} else if (item.data && item.data.credentials) {
item.data.credentials.forEach(function(cred) {
if (cred.value) {
html += '
';
html += '' + escHtml(cred.label || "") + '';
html += '' + escHtml(cred.value) + '';
html += '
';
}
});
}
html += '
';
});
html += '
';
});
body.innerHTML = html;
// Wire up reveal buttons
body.querySelectorAll(".onboarding-cred-secret").forEach(function(el) {
var btn = el.querySelector(".onboarding-cred-reveal-btn");
var hidden = el.querySelector(".onboarding-cred-hidden");
var real = el.querySelector(".onboarding-cred-real");
if (!btn || !hidden || !real) return;
btn.addEventListener("click", function() {
if (real.style.display === "none") {
real.style.display = "";
hidden.style.display = "none";
btn.textContent = "Hide";
} else {
real.style.display = "none";
hidden.style.display = "";
btn.textContent = "Show";
}
});
});
}
// ── Step 5: Feature Manager ───────────────────────────────────────
async function loadStep5() {
var body = document.getElementById("step-5-body");
if (!body) return;
body.innerHTML = '