if (typeof globalThis === "undefined") window.globalThis = window; window.addEventListener("securitypolicyviolation", (e) => { console.log("[CSP]", e.violatedDirective, "blocked:", e.blockedURI, "sample:", e.sample); }); function escapeHtml(s) { return String(s).replace(/[&<>"']/g, function (m) { return ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[m]); }); } const PANEL_EWS_ICON_SIZE = "1.6rem"; function ensurePanelEwsSvgStyles() { if (document.getElementById("panel-ews-svg-styles")) return; const style = document.createElement("style"); style.id = "panel-ews-svg-styles"; style.textContent = ` .panel-ews-svg { width: ${PANEL_EWS_ICON_SIZE}; height: ${PANEL_EWS_ICON_SIZE}; display: block; flex: 0 0 auto; transition: transform 180ms ease; } .quick-action.qa-anim-rotate:hover .panel-ews-svg { transform: rotate(180deg); } `; document.head.appendChild(style); } function createPanelEwsSvg() { const template = document.createElement("template"); template.innerHTML = ` `.trim(); return template.content.firstElementChild; } function replaceLegacyPanelEwsIcons(root = document) { ensurePanelEwsSvgStyles(); root.querySelectorAll("i.fa-panel-ews").forEach((icon) => { const svg = createPanelEwsSvg(); const inlineStyle = icon.getAttribute("style"); if (inlineStyle) svg.setAttribute("style", `${inlineStyle};width:${PANEL_EWS_ICON_SIZE};height:${PANEL_EWS_ICON_SIZE};display:block;`); icon.replaceWith(svg); }); } window.addEventListener('DOMContentLoaded', () => { replaceLegacyPanelEwsIcons(); const splash = document.getElementById('splash-screen'); const splashImage = splash?.dataset.splashImage || window.__dashboardSplashImage || '/images/hello.webp'; const splashKey = window.__dashboardSplashKey || `dashboard-splash-cache:${window.location.origin}:${splashImage}`; const removeSplash = () => { if (splash && splash.isConnected) splash.remove(); }; const hideSplash = () => { if (!splash || !splash.isConnected) return; splash.classList.add('hidden'); window.setTimeout(removeSplash, 600); }; const markSplashCached = () => { try { localStorage.setItem(splashKey, '1'); } catch (_) { } }; const isSplashCached = async () => { try { const response = await fetch(splashImage, { cache: 'only-if-cached', mode: 'same-origin' }); return response.ok; } catch (_) { return false; } }; const showSplashForFirstCacheMiss = () => { if (!splash) return; const displayTime = 5000; const hardTimeout = 7000; const preload = new Image(); let hideScheduled = false; const hardTimer = window.setTimeout(() => { if (hideScheduled) return; markSplashCached(); hideSplash(); }, hardTimeout); const scheduleHide = () => { if (hideScheduled) return; hideScheduled = true; window.clearTimeout(hardTimer); markSplashCached(); window.setTimeout(hideSplash, displayTime); }; preload.decoding = 'async'; preload.onload = scheduleHide; preload.onerror = () => { window.clearTimeout(hardTimer); removeSplash(); }; preload.src = splashImage; if (preload.complete) { scheduleHide(); } }; if (splash) { const skipSplash = document.documentElement.classList.contains('skip-dashboard-splash'); if (skipSplash) { removeSplash(); } else { isSplashCached() .then((cached) => { if (cached) { markSplashCached(); removeSplash(); return; } showSplashForFirstCacheMiss(); }) .catch(showSplashForFirstCacheMiss); } } document.querySelectorAll('[data-scroll-target]').forEach(button => { button.addEventListener('click', () => { const targetId = button.getAttribute('data-scroll-target'); const section = document.getElementById(targetId); if (section) section.scrollIntoView({ behavior: 'smooth' }); }); }); document.querySelectorAll('.bot-card').forEach(card => { const overlay = card.querySelector('[data-offline-overlay]'); if (!overlay) return; const nodeOffline = card.classList.contains('node-offline'); overlay.style.display = nodeOffline ? 'flex' : 'none'; card.classList.toggle('is-offline', nodeOffline); }); function updateNodeOfflineStatus(name, nodeOnline) { const card = document.querySelector(`.bot-card[data-name="${name.toLowerCase()}"]`); if (!card) return; const overlay = card.querySelector('[data-offline-overlay]'); if (nodeOnline) { card.classList.remove('node-offline'); card.classList.remove('is-offline'); if (overlay) overlay.style.display = 'none'; } else { card.classList.add('node-offline'); card.classList.add('is-offline'); if (overlay) overlay.style.display = 'flex'; } } window.updateNodeOfflineStatus = updateNodeOfflineStatus; function formatBytes(mb) { if (mb === null || mb === undefined) return null; if (mb === 0) return '0 MB'; if (mb >= 1024) return `${(mb / 1024).toFixed(1)} GB`; return `${Math.round(mb)} MB`; } function formatResourceLabel(usedMb, limitMb) { const used = formatBytes(usedMb); const limit = formatBytes(limitMb); if (limitMb > 0) return `${used || '0 MB'}/${limit}`; if (usedMb > 0) return used; return 'N/A'; } function updateCardFromResourceData(data) { if (!data || !data.server) return; const name = data.server.toLowerCase(); const card = document.querySelector(`.bot-card[data-name="${name}"]`); if (!card) return; const status = data.status; if (status) { const isOnline = status === 'running' || status === 'online'; const isStopped = status === 'stopped' || status === 'exited'; if (!isOnline && !isStopped && card.classList.contains('status-online')) { } else { card.classList.remove('status-online', 'status-stopped', 'status-unknown'); const statusEl = card.querySelector('.bot-card__status'); if (statusEl) statusEl.classList.remove('status-online', 'status-stopped', 'status-unknown'); if (isOnline) { card.classList.add('status-online'); if (statusEl) statusEl.classList.add('status-online'); } else if (isStopped) { card.classList.add('status-stopped'); if (statusEl) statusEl.classList.add('status-stopped'); } else { card.classList.add('status-unknown'); if (statusEl) statusEl.classList.add('status-unknown'); } } } if (data.nodeOnline === true && typeof window.updateNodeOfflineStatus === 'function') { window.updateNodeOfflineStatus(data.server, true); } if (data.memory != null) { let usedMb = 0, limitMb = 0, pct = 0; if (typeof data.memory === 'object') { usedMb = data.memory.used || 0; limitMb = data.memory.total || 0; pct = data.memory.percent ?? (limitMb > 0 ? Math.min(100, Math.round((usedMb / limitMb) * 100)) : 0); } else { usedMb = data.memory; limitMb = 0; pct = 0; } const memLabel = card.querySelector('.bot-card__stat-icon--memory')?.closest('.bot-card__stat')?.querySelector('.bot-card__stat-chip span'); const memProgress = card.querySelector('.bot-card__stat-icon--memory')?.closest('.bot-card__stat')?.querySelector('.bot-card__progress span'); if (memLabel) memLabel.textContent = formatResourceLabel(usedMb, limitMb); if (memProgress) { memProgress.style.width = `${Math.min(100, Math.max(0, pct))}%`; } } if (data.disk != null) { let usedMb = 0, limitMb = 0, pct = 0; if (typeof data.disk === 'object') { usedMb = (data.disk.used || 0) * 1024; limitMb = (data.disk.total || 0) * 1024; pct = data.disk.percent ?? (limitMb > 0 ? Math.min(100, Math.round((usedMb / limitMb) * 100)) : 0); } else { usedMb = data.disk; limitMb = 0; pct = 0; } const diskLabel = card.querySelector('.bot-card__stat-icon--disk')?.closest('.bot-card__stat')?.querySelector('.bot-card__stat-chip span'); const diskProgress = card.querySelector('.bot-card__stat-icon--disk')?.closest('.bot-card__stat')?.querySelector('.bot-card__progress span'); if (diskLabel) diskLabel.textContent = formatResourceLabel(usedMb, limitMb); if (diskProgress) { diskProgress.style.width = `${Math.min(100, Math.max(0, pct))}%`; } } if (data.cpu != null) { const cpuMax = data.cpuLimit || 100; const cpuPct = Math.min(cpuMax, Math.max(0, data.cpu || 0)); const cpuBarWidth = cpuMax > 0 ? Math.min(100, (cpuPct / cpuMax) * 100) : 0; const cpuLabel = card.querySelector('.bot-card__stat-icon--cpu')?.closest('.bot-card__stat')?.querySelector('.bot-card__stat-chip span'); const cpuProgress = card.querySelector('.bot-card__stat-icon--cpu')?.closest('.bot-card__stat')?.querySelector('.bot-card__progress span'); if (cpuLabel) cpuLabel.textContent = cpuPct > 0 ? `${cpuPct.toFixed(1)}%` : 'N/A'; if (cpuProgress) cpuProgress.style.width = `${cpuBarWidth}%`; } } let _dashResourceSocket = null; let _dashResourceSubscribed = false; window._dashResourceSocket = null; function getDashboardServerNames() { return Array.from(document.querySelectorAll('[data-bot-card]')) .map(c => c.dataset.name) .filter(Boolean); } function subscribeDashboardResources(socket) { if (!socket || !socket.connected) return; const names = getDashboardServerNames(); if (!names.length) return; socket.emit('resources:subscribe-bulk', { servers: names }); _dashResourceSubscribed = true; } function unsubscribeDashboardResources(socket) { if (!socket) return; socket.emit('resources:unsubscribe-all'); _dashResourceSubscribed = false; } function initDashboardResourceSocket() { if (!window.io) return; const socket = window.io({ transports: ['websocket', 'polling'], reconnection: true, reconnectionDelay: 1000, reconnectionDelayMax: 5000 }); _dashResourceSocket = socket; window._dashResourceSocket = socket; socket.on('connect', () => { subscribeDashboardResources(socket); }); socket.on('disconnect', () => { _dashResourceSubscribed = false; }); socket.on('resources:data', updateCardFromResourceData); document.addEventListener('visibilitychange', () => { if (document.visibilityState === 'hidden') { unsubscribeDashboardResources(socket); } else { subscribeDashboardResources(socket); } }); window.addEventListener('beforeunload', () => { unsubscribeDashboardResources(socket); }); } initDashboardResourceSocket(); document.querySelectorAll('[data-href]').forEach(el => { el.addEventListener('click', (event) => { event.preventDefault(); event.stopPropagation(); const href = el.getAttribute('data-href'); if (!href) return; const target = el.getAttribute('data-target'); if (target === '_blank') { window.open(href, '_blank', 'noopener,noreferrer'); } else { window.location.href = href; } }); }); document.querySelectorAll('[data-bot-card]').forEach(card => { const cover = card.dataset.cover; if (cover) card.style.setProperty('--bot-cover', `url("${cover.replace(/"/g, '\\"')}")`); }); document.querySelectorAll('.bot-card__progress span[data-pct]').forEach(span => { span.style.width = (span.dataset.pct || '0') + '%'; }); const searchInput = document.getElementById('searchBots'); const botGrid = document.querySelector('[data-bot-grid]'); const emptyStateTemplate = document.getElementById('empty-state'); const dashPagination = document.getElementById('dashPagination'); function navigateDashboard(page, search) { const params = new URLSearchParams(); if (page && page > 1) params.set('page', String(page)); if (search) params.set('search', search); const qs = params.toString(); window.location.href = '/' + (qs ? '?' + qs : ''); } function updateBotVisibility() { if (dashPagination) { clearTimeout(_searchTimer); _searchTimer = setTimeout(() => navigateDashboard(1, (searchInput?.value || '').trim()), 500); return; } if (!botGrid) return; const term = (searchInput?.value || '').trim().toLowerCase(); const botCards = Array.from(botGrid.querySelectorAll('[data-bot-card]')); let visible = 0; botCards.forEach(card => { const name = card.getAttribute('data-name'); const match = !term || name.includes(term); card.classList.toggle('is-hidden', !match); if (match) visible++; }); const existing = botGrid.querySelector('.empty-state'); if (visible === 0) { if (!existing) { const clone = emptyStateTemplate.content.cloneNode(true); botGrid.appendChild(clone); } } else if (existing) existing.remove(); } let _searchTimer; searchInput?.addEventListener('input', () => { clearTimeout(_searchTimer); _searchTimer = setTimeout(updateBotVisibility, dashPagination ? 500 : 150); }); if (botGrid && !dashPagination) updateBotVisibility(); const urlParams = new URLSearchParams(window.location.search); if (searchInput && urlParams.get('search')) { searchInput.value = urlParams.get('search'); } if (dashPagination) { dashPagination.querySelectorAll('button[data-dash-page]').forEach(btn => { btn.addEventListener('click', () => { const p = parseInt(btn.dataset.dashPage, 10); if (p >= 1) navigateDashboard(p, (searchInput?.value || '').trim()); }); }); } const topBar = document.getElementById('topBar'); const spacer = document.getElementById('topbar-spacer'); const spacerExtraOffset = spacer ? parseInt(spacer.getAttribute('data-topbar-extra-offset') || '0', 10) || 0 : 0; function measureExpandedHeight() { const hadCompact = topBar.classList.contains('top-bar--compact'); topBar.classList.remove('top-bar--compact'); const h = Math.ceil(topBar.getBoundingClientRect().height); if (hadCompact) topBar.classList.add('top-bar--compact'); return h; } function applySpacerHeight() { if (!spacer) return; spacer.style.height = Math.max(0, expandedHeight - spacerExtraOffset) + 'px'; } let expandedHeight = measureExpandedHeight(); applySpacerHeight(); let resizeRAF = null; window.addEventListener('resize', () => { if (resizeRAF) cancelAnimationFrame(resizeRAF); resizeRAF = requestAnimationFrame(() => { expandedHeight = measureExpandedHeight(); applySpacerHeight(); }); }); new ResizeObserver(() => { expandedHeight = measureExpandedHeight(); applySpacerHeight(); fitHero(); }).observe(topBar); function fitHero() { const hero = document.querySelector('.hero'); if (!hero) return; hero.style.minHeight = ''; hero.style.marginBottom = ''; if (document.querySelector('.quick-actions')) return; const rect = hero.getBoundingClientRect(); const scrollTop = window.scrollY || document.documentElement.scrollTop; const absTop = rect.top + scrollTop; const heroHeight = hero.offsetHeight; const totalContentHeight = absTop + heroHeight; const viewHeight = window.innerHeight; const diff = viewHeight - totalContentHeight; if (diff > 0) { hero.style.marginBottom = `${diff + 32}px`; } } window.addEventListener('resize', fitHero); fitHero(); let lastY = window.scrollY || 0, ticking = false, pinnedTimeout = null; function onScrollTick() { const y = window.scrollY || 0, d = y - lastY; if (y > 36) topBar.classList.add('top-bar--compact'); else topBar.classList.remove('top-bar--compact'); if (y <= 24) { topBar.classList.remove('top-bar--hidden', 'top-bar--pinned'); lastY = y; ticking = false; return; } if (Math.abs(d) > 4) { if (d > 0 && y > 120) { topBar.classList.add('top-bar--hidden'); topBar.classList.remove('top-bar--pinned'); } else if (d < 0) { topBar.classList.remove('top-bar--hidden'); topBar.classList.add('top-bar--pinned'); if (pinnedTimeout) clearTimeout(pinnedTimeout); pinnedTimeout = setTimeout(() => topBar.classList.remove('top-bar--pinned'), 900); } } lastY = y; ticking = false; } window.addEventListener('scroll', () => { if (!ticking) { requestAnimationFrame(onScrollTick); ticking = true; } }, { passive: true }); if ((window.scrollY || 0) > 36) topBar.classList.add('top-bar--compact'); else topBar.classList.remove('top-bar--compact'); topBar.classList.remove('top-bar--hidden'); function deleteAllClientCookies() { const cookies = document.cookie ? document.cookie.split('; ') : []; for (const cookie of cookies) { const eqPos = cookie.indexOf('='); const name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie; document.cookie = `${name}=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/`; document.cookie = `${name}=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/;domain=${location.hostname}`; const parts = location.hostname.split('.'); for (let i = 0; i < parts.length - 1; i++) { const dom = '.' + parts.slice(i).join('.'); document.cookie = `${name}=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/;domain=${dom}`; } } } document.getElementById('logoutBtn')?.addEventListener('click', async (e) => { e.preventDefault(); try { deleteAllClientCookies(); await fetch('/logout', { method: 'POST', credentials: 'include' }); location.href = '/login'; } catch { alert('Failed to log out'); } }); const accountBtn = document.getElementById('accountBtn'); const accountBackBtn = document.getElementById('accountBackBtn'); const accountPage = document.getElementById('accountPage'); const mainContent = document.getElementById('mainContent'); const footerEl = document.getElementById('upload'); async function showAccountPage() { if (!accountPage) return; if (window.location.hash !== '#account') { window.history.pushState(null, null, '#account'); } if (mainContent) mainContent.style.display = 'none'; if (footerEl) footerEl.style.display = 'none'; accountPage.style.display = 'block'; try { const res = await fetch('/api/account', { credentials: 'include' }); if (!res.ok) throw new Error('Failed to load account'); const data = await res.json(); const usernameEl = document.getElementById('accountUsername'); const emailEl = document.getElementById('accountEmail'); const usernameValEl = document.getElementById('accountUsernameValue'); const emailValEl = document.getElementById('accountEmailValue'); const roleValEl = document.getElementById('accountRoleValue'); const idValEl = document.getElementById('accountIdValue'); const avatarImgEl = document.getElementById('accountAvatarImg'); const statusTextEl = document.getElementById('accountStatusText'); const cooldownEl = document.getElementById('usernameCooldown'); const badgeClass = data.isAdmin ? 'admin' : 'user'; const badgeText = data.isAdmin ? 'Admin' : 'User'; if (usernameEl) usernameEl.innerHTML = `${escapeHtml(data.username)} ${badgeText}`; if (emailEl) emailEl.textContent = data.email; if (usernameValEl) usernameValEl.textContent = data.username; if (emailValEl) emailValEl.textContent = data.email; if (roleValEl) roleValEl.textContent = data.isAdmin ? 'Administrator' : 'Standard User'; if (idValEl) idValEl.textContent = data.userId || 'N/A'; if (avatarImgEl && data.avatarUrl) avatarImgEl.src = data.avatarUrl; if (statusTextEl) statusTextEl.textContent = data.status || 'Available'; window._accountData = data; syncAccountSecurityUi(); if (cooldownEl && data.usernameChangeCooldown) { const days = Math.ceil(data.usernameChangeCooldown / (24 * 60 * 60 * 1000)); cooldownEl.textContent = `You can change your username again in ${days} day${days !== 1 ? 's' : ''}`; cooldownEl.style.display = 'block'; } else if (cooldownEl) { cooldownEl.style.display = 'none'; } } catch (err) { console.error('[account] Failed to load:', err); } window.scrollTo({ top: 0, behavior: 'smooth' }); } function hideAccountPage() { if (!accountPage) return; if (window.location.hash === '#account') { history.pushState("", document.title, window.location.pathname + window.location.search); } accountPage.style.display = 'none'; if (mainContent) mainContent.style.display = ''; if (footerEl) footerEl.style.display = ''; window.scrollTo({ top: 0, behavior: 'smooth' }); } const avatarWrapper = document.getElementById('accountAvatarWrapper'); const avatarFileInput = document.getElementById('avatarFileInput'); avatarWrapper?.addEventListener('click', () => { avatarFileInput?.click(); }); avatarFileInput?.addEventListener('change', async (e) => { const file = e.target.files?.[0]; if (!file) return; if (file.size > 5 * 1024 * 1024) { alert('File too large. Maximum size is 5MB.'); return; } const formData = new FormData(); formData.append('avatar', file); try { const res = await fetch('/api/account/avatar/upload', { method: 'POST', credentials: 'include', body: formData }); const data = await res.json(); if (!res.ok) throw new Error(data.error || 'Failed to upload avatar'); const avatarImgEl = document.getElementById('accountAvatarImg'); if (avatarImgEl) avatarImgEl.src = data.avatarUrl; const headerAvatarImg = document.querySelector('.account-btn-avatar'); if (headerAvatarImg) headerAvatarImg.src = data.avatarUrl; } catch (err) { alert('Failed to upload avatar: ' + err.message); } e.target.value = ''; }); const usernameEditBtn = document.getElementById('usernameEditBtn'); const usernameSaveBtn = document.getElementById('usernameSaveBtn'); const usernameCancelBtn = document.getElementById('usernameCancelBtn'); const usernameInput = document.getElementById('usernameInput'); const usernameValEl = document.getElementById('accountUsernameValue'); usernameEditBtn?.addEventListener('click', () => { if (usernameInput && usernameValEl) { usernameInput.value = usernameValEl.textContent || ''; usernameValEl.style.display = 'none'; usernameInput.style.display = 'block'; usernameEditBtn.style.display = 'none'; usernameSaveBtn.style.display = 'inline-flex'; usernameCancelBtn.style.display = 'inline-flex'; usernameInput.focus(); } }); usernameCancelBtn?.addEventListener('click', () => { usernameValEl.style.display = 'inline'; usernameInput.style.display = 'none'; usernameEditBtn.style.display = 'inline-flex'; usernameSaveBtn.style.display = 'none'; usernameCancelBtn.style.display = 'none'; }); usernameSaveBtn?.addEventListener('click', async () => { const newUsername = usernameInput?.value?.trim(); if (!newUsername || newUsername.length < 3) { alert('Username must be at least 3 characters'); return; } usernameSaveBtn.disabled = true; try { const res = await fetch('/api/account/username', { method: 'POST', headers: { 'Content-Type': 'application/json' }, credentials: 'include', body: JSON.stringify({ username: newUsername }) }); const data = await res.json(); if (!res.ok) throw new Error(data.error || 'Failed to update username'); if (usernameValEl) usernameValEl.textContent = newUsername; const usernameEl = document.getElementById('accountUsername'); if (usernameEl && window._accountData) { const badgeClass = window._accountData.isAdmin ? 'admin' : 'user'; const badgeText = window._accountData.isAdmin ? 'Admin' : 'User'; usernameEl.innerHTML = `${escapeHtml(newUsername)} ${badgeText}`; } usernameCancelBtn?.click(); } catch (err) { alert(err.message); } finally { usernameSaveBtn.disabled = false; } }); const statusEditBtn = document.getElementById('statusEditBtn'); const statusModal = document.getElementById('statusModal'); const statusModalClose = document.getElementById('statusModalClose'); const statusCancelBtn = document.getElementById('statusCancelBtn'); const statusSaveBtn = document.getElementById('statusSaveBtn'); const statusInput = document.getElementById('statusInput'); const statusDuration = document.getElementById('statusDuration'); const statusError = document.getElementById('statusError'); const statusTextEl = document.getElementById('accountStatusText'); statusEditBtn?.addEventListener('click', () => { if (statusInput) statusInput.value = statusTextEl?.textContent || ''; statusModal?.classList.add('show'); }); statusModalClose?.addEventListener('click', () => { statusModal?.classList.remove('show'); }); statusCancelBtn?.addEventListener('click', () => { statusModal?.classList.remove('show'); }); statusModal?.addEventListener('click', (e) => { if (e.target === statusModal) statusModal.classList.remove('show'); }); statusSaveBtn?.addEventListener('click', async () => { const text = statusInput?.value?.trim() || 'Available'; const durationVal = statusDuration?.value; const expiresAt = durationVal === 'never' ? null : Date.now() + parseInt(durationVal || '86400000'); statusSaveBtn.disabled = true; if (statusError) statusError.classList.remove('show'); try { const res = await fetch('/api/me/status', { method: 'POST', headers: { 'Content-Type': 'application/json' }, credentials: 'include', body: JSON.stringify({ text, expiresAt }) }); const data = await res.json(); if (!res.ok) throw new Error(data.error || 'Failed to update status'); if (statusTextEl) statusTextEl.textContent = text; statusModal?.classList.remove('show'); } catch (err) { if (statusError) { statusError.textContent = err.message; statusError.classList.add('show'); } } finally { statusSaveBtn.disabled = false; } }); accountBtn?.addEventListener('click', showAccountPage); accountBackBtn?.addEventListener('click', hideAccountPage); if (window.location.hash === '#account') { showAccountPage(); } window.addEventListener('hashchange', () => { if (window.location.hash === '#account') { showAccountPage(); } else { hideAccountPage(); } }); function activateAccountTab(tabName) { const normalized = tabName === 'credentials' ? 'credentials' : 'info'; document.querySelectorAll('.account-tab').forEach((tab) => { tab.classList.toggle('active', tab.dataset.accountTab === normalized); }); document.querySelectorAll('.account-tab-content').forEach((content) => { content.classList.remove('active'); }); const tabId = normalized === 'info' ? 'accountInfoTab' : 'accountCredentialsTab'; document.getElementById(tabId)?.classList.add('active'); } document.querySelectorAll('.account-tab').forEach(tab => { tab.addEventListener('click', () => { activateAccountTab(tab.dataset.accountTab); }); }); const twofaModal = document.getElementById('twofaModal'); const change2faBtn = document.getElementById('change2faBtn'); const twofaModalClose = document.getElementById('twofaModalClose'); const twofaCancelBtn = document.getElementById('twofaCancelBtn'); const twofaConfirmBtn = document.getElementById('twofaConfirmBtn'); const currentCodeInput = document.getElementById('currentCodeInput'); const newCodeInput = document.getElementById('newCodeInput'); const qrCodeSection = document.getElementById('qrCodeSection'); const confirmSection = document.getElementById('confirmSection'); const newQrCode = document.getElementById('newQrCode'); const newSecretDisplay = document.getElementById('newSecretDisplay'); const twofaError = document.getElementById('twofaError'); const twofaSuccess = document.getElementById('twofaSuccess'); const twofaStep1Text = document.getElementById('twofaStep1Text'); let pendingNewSecret = null; let twoFaModalStep = 1; function accountHas2FA() { return !window._accountData || window._accountData.has2FA !== false; } function setStepVisibility(input, visible) { const step = input?.closest('.password-step, .email-step, .twofa-step'); if (step) step.style.display = visible ? '' : 'none'; } function setStepNumber(input, number) { const step = input?.closest('.password-step, .email-step, .twofa-step'); const badge = step?.querySelector('.password-step-num, .email-step-num, .twofa-step-num'); if (badge) badge.textContent = String(number); } function syncTwofaCard() { const has2FA = accountHas2FA(); if (change2faBtn) { change2faBtn.innerHTML = has2FA ? ' Change 2FA' : ' Enable 2FA'; } } function configureTwofaModalSecurity() { const has2FA = accountHas2FA(); const title = twofaModal?.querySelector('.twofa-modal-title'); const subtitle = twofaModal?.querySelector('.twofa-modal-subtitle'); if (title) title.textContent = has2FA ? 'Change 2FA Code' : 'Enable 2FA'; if (subtitle) subtitle.textContent = has2FA ? 'Update your authenticator app' : 'Add an authenticator app to this account'; if (twofaStep1Text) { twofaStep1Text.textContent = has2FA ? 'Enter your current 2FA code' : 'Enter your current password'; } if (currentCodeInput) { currentCodeInput.type = has2FA ? 'text' : 'password'; currentCodeInput.placeholder = has2FA ? '000000' : 'Current password'; currentCodeInput.maxLength = has2FA ? 6 : 1024; currentCodeInput.inputMode = has2FA ? 'numeric' : ''; currentCodeInput.autocomplete = has2FA ? 'one-time-code' : 'current-password'; currentCodeInput.classList.toggle('twofa-password-input', !has2FA); } const twofaForgotLink = document.getElementById('twofaForgotLink'); if (twofaForgotLink) twofaForgotLink.style.display = has2FA ? '' : 'none'; } function configurePasswordModalSecurity() { const has2FA = accountHas2FA(); const passwordCodeInput = document.getElementById('password2faInput'); const passwordNewInput = document.getElementById('newPasswordInput'); const passwordConfirmInput = document.getElementById('confirmPasswordInput'); setStepVisibility(passwordCodeInput, has2FA); setStepNumber(passwordNewInput, has2FA ? 3 : 2); setStepNumber(passwordConfirmInput, has2FA ? 4 : 3); const passwordForgotLink = document.getElementById('passwordForgotLink'); if (passwordForgotLink) passwordForgotLink.style.display = has2FA ? '' : 'none'; } function configureEmailModalSecurity() { const has2FA = accountHas2FA(); const emailCodeInput = document.getElementById('email2faInput'); const emailNewInput = document.getElementById('newEmailInput'); setStepVisibility(emailCodeInput, has2FA); setStepNumber(emailNewInput, has2FA ? 3 : 2); const emailForgotLink = document.getElementById('emailForgotLink'); if (emailForgotLink) emailForgotLink.style.display = has2FA ? '' : 'none'; } function syncAccountSecurityUi() { syncTwofaCard(); configureTwofaModalSecurity(); configurePasswordModalSecurity(); configureEmailModalSecurity(); } function resetTwofaModal() { twoFaModalStep = 1; pendingNewSecret = null; if (currentCodeInput) currentCodeInput.value = ''; if (newCodeInput) newCodeInput.value = ''; if (qrCodeSection) qrCodeSection.style.display = 'none'; if (confirmSection) confirmSection.style.display = 'none'; if (twofaError) twofaError.classList.remove('show'); if (twofaSuccess) twofaSuccess.classList.remove('show'); if (twofaConfirmBtn) { twofaConfirmBtn.textContent = accountHas2FA() ? 'Verify Current Code' : 'Verify Password'; twofaConfirmBtn.disabled = false; twofaConfirmBtn.style.display = ''; } if (twofaCancelBtn) twofaCancelBtn.textContent = 'Cancel'; const twofaStep1 = document.getElementById('twofaStep1'); if (twofaStep1) twofaStep1.style.display = ''; configureTwofaModalSecurity(); const qrStepNum = qrCodeSection?.querySelector('.twofa-step-num'); const confirmStepNum = confirmSection?.querySelector('.twofa-step-num'); if (qrStepNum) qrStepNum.textContent = '2'; if (confirmStepNum) confirmStepNum.textContent = '3'; } function showTwofaError(msg) { if (twofaError) { twofaError.textContent = msg; twofaError.classList.add('show'); } } function hideTwofaError() { if (twofaError) twofaError.classList.remove('show'); } change2faBtn?.addEventListener('click', () => { resetTwofaModal(); twofaModal?.classList.add('show'); }); twofaModalClose?.addEventListener('click', () => { twofaModal?.classList.remove('show'); }); twofaCancelBtn?.addEventListener('click', () => { twofaModal?.classList.remove('show'); }); twofaModal?.addEventListener('click', (e) => { if (e.target === twofaModal) twofaModal.classList.remove('show'); }); twofaConfirmBtn?.addEventListener('click', async () => { hideTwofaError(); if (twoFaModalStep === 1) { const has2FA = accountHas2FA(); const currentCredential = currentCodeInput?.value.trim() || ''; if (has2FA && (!currentCredential || currentCredential.length !== 6 || !/^\d+$/.test(currentCredential))) { showTwofaError('Please enter a valid 6-digit code'); return; } if (!has2FA && !currentCredential) { showTwofaError('Please enter your current password'); return; } twofaConfirmBtn.disabled = true; twofaConfirmBtn.textContent = 'Verifying...'; try { const res = await fetch('/api/account/2fa/generate', { method: 'POST', headers: { 'Content-Type': 'application/json' }, credentials: 'include', body: JSON.stringify(has2FA ? { currentCode: currentCredential } : { currentPassword: currentCredential }) }); const data = await res.json(); if (!res.ok) throw new Error(data.error || 'Verification failed'); pendingNewSecret = data.newSecret; if (newQrCode) newQrCode.src = data.qrCodeUrl; if (newSecretDisplay) newSecretDisplay.textContent = data.newSecret; if (qrCodeSection) qrCodeSection.style.display = 'block'; if (confirmSection) confirmSection.style.display = 'block'; twoFaModalStep = 2; twofaConfirmBtn.textContent = 'Confirm New 2FA'; twofaConfirmBtn.disabled = false; } catch (err) { showTwofaError(err.message); twofaConfirmBtn.textContent = accountHas2FA() ? 'Verify Current Code' : 'Verify Password'; twofaConfirmBtn.disabled = false; } } else if (twoFaModalStep === 2) { const newCode = newCodeInput?.value.trim() || ''; if (!newCode || newCode.length !== 6 || !/^\d+$/.test(newCode)) { showTwofaError('Please enter a valid 6-digit code from your new authenticator'); return; } twofaConfirmBtn.disabled = true; twofaConfirmBtn.textContent = 'Updating...'; try { const res = await fetch('/api/account/2fa/confirm', { method: 'POST', headers: { 'Content-Type': 'application/json' }, credentials: 'include', body: JSON.stringify({ newCode, newSecret: pendingNewSecret }) }); const data = await res.json(); if (!res.ok) throw new Error(data.error || 'Failed to update 2FA'); if (twofaSuccess) twofaSuccess.classList.add('show'); if (window._accountData) window._accountData.has2FA = true; syncAccountSecurityUi(); twofaConfirmBtn.style.display = 'none'; if (twofaCancelBtn) twofaCancelBtn.textContent = 'Done'; setTimeout(() => { twofaModal?.classList.remove('show'); resetTwofaModal(); }, 2000); } catch (err) { showTwofaError(err.message); twofaConfirmBtn.textContent = 'Confirm New 2FA'; twofaConfirmBtn.disabled = false; } } }); currentCodeInput?.addEventListener('input', (e) => { if (accountHas2FA()) { e.target.value = e.target.value.replace(/\D/g, '').slice(0, 6); } }); newCodeInput?.addEventListener('input', (e) => { e.target.value = e.target.value.replace(/\D/g, '').slice(0, 6); }); const passwordModal = document.getElementById('passwordModal'); const changePasswordBtn = document.getElementById('changePasswordBtn'); const passwordModalClose = document.getElementById('passwordModalClose'); const passwordCancelBtn = document.getElementById('passwordCancelBtn'); const passwordConfirmBtn = document.getElementById('passwordConfirmBtn'); const currentPasswordInput = document.getElementById('currentPasswordInput'); const password2faInput = document.getElementById('password2faInput'); const newPasswordInput = document.getElementById('newPasswordInput'); const confirmPasswordInput = document.getElementById('confirmPasswordInput'); const passwordStep1 = document.getElementById('passwordStep1'); const passwordStep2 = document.getElementById('passwordStep2'); const passwordError = document.getElementById('passwordError'); const passwordSuccess = document.getElementById('passwordSuccess'); let passwordModalStep = 1; function resetPasswordModal() { passwordModalStep = 1; if (currentPasswordInput) currentPasswordInput.value = ''; if (password2faInput) password2faInput.value = ''; if (newPasswordInput) newPasswordInput.value = ''; if (confirmPasswordInput) confirmPasswordInput.value = ''; if (passwordStep1) passwordStep1.style.display = 'block'; if (passwordStep2) passwordStep2.style.display = 'none'; if (passwordError) passwordError.classList.remove('show'); if (passwordSuccess) passwordSuccess.classList.remove('show'); if (passwordConfirmBtn) { passwordConfirmBtn.textContent = 'Verify'; passwordConfirmBtn.disabled = false; passwordConfirmBtn.style.display = ''; } if (passwordCancelBtn) passwordCancelBtn.textContent = 'Cancel'; configurePasswordModalSecurity(); } function showPasswordError(msg) { if (passwordError) { passwordError.textContent = msg; passwordError.classList.add('show'); } } function hidePasswordError() { if (passwordError) passwordError.classList.remove('show'); } changePasswordBtn?.addEventListener('click', () => { resetPasswordModal(); passwordModal?.classList.add('show'); }); passwordModalClose?.addEventListener('click', () => { passwordModal?.classList.remove('show'); }); passwordCancelBtn?.addEventListener('click', () => { passwordModal?.classList.remove('show'); }); passwordModal?.addEventListener('click', (e) => { if (e.target === passwordModal) passwordModal.classList.remove('show'); }); passwordConfirmBtn?.addEventListener('click', async () => { hidePasswordError(); if (passwordModalStep === 1) { const currentPw = currentPasswordInput?.value || ''; const code = password2faInput?.value.trim() || ''; if (!currentPw) { showPasswordError('Please enter your current password'); return; } if (accountHas2FA() && (!code || code.length !== 6 || !/^\d+$/.test(code))) { showPasswordError('Please enter a valid 6-digit 2FA code'); return; } passwordConfirmBtn.disabled = true; passwordConfirmBtn.textContent = 'Verifying...'; try { const res = await fetch('/api/account/password/verify', { method: 'POST', headers: { 'Content-Type': 'application/json' }, credentials: 'include', body: JSON.stringify({ currentPassword: currentPw, twoFactorCode: accountHas2FA() ? code : '' }) }); const data = await res.json(); if (!res.ok) throw new Error(data.error || 'Verification failed'); passwordStep1.style.display = 'none'; passwordStep2.style.display = 'block'; passwordModalStep = 2; passwordConfirmBtn.textContent = 'Change Password'; passwordConfirmBtn.disabled = false; } catch (err) { showPasswordError(err.message); passwordConfirmBtn.textContent = 'Verify'; passwordConfirmBtn.disabled = false; } } else if (passwordModalStep === 2) { const newPw = newPasswordInput?.value || ''; const confirmPw = confirmPasswordInput?.value || ''; if (!newPw || newPw.length < 8) { showPasswordError('Password must be at least 8 characters'); return; } if (newPw !== confirmPw) { showPasswordError('Passwords do not match'); return; } passwordConfirmBtn.disabled = true; passwordConfirmBtn.textContent = 'Updating...'; try { const res = await fetch('/api/account/password/change', { method: 'POST', headers: { 'Content-Type': 'application/json' }, credentials: 'include', body: JSON.stringify({ currentPassword: currentPasswordInput?.value || '', twoFactorCode: accountHas2FA() ? (password2faInput?.value.trim() || '') : '', newPassword: newPw }) }); const data = await res.json(); if (!res.ok) throw new Error(data.error || 'Failed to change password'); if (passwordSuccess) passwordSuccess.classList.add('show'); passwordConfirmBtn.style.display = 'none'; if (passwordCancelBtn) passwordCancelBtn.textContent = 'Done'; setTimeout(() => { passwordModal?.classList.remove('show'); resetPasswordModal(); }, 2000); } catch (err) { showPasswordError(err.message); passwordConfirmBtn.textContent = 'Change Password'; passwordConfirmBtn.disabled = false; } } }); password2faInput?.addEventListener('input', (e) => { e.target.value = e.target.value.replace(/\D/g, '').slice(0, 6); }); document.querySelectorAll('.password-toggle-eye').forEach(btn => { const targetId = btn.dataset.target; const input = document.getElementById(targetId); if (input) { input.addEventListener('input', () => { btn.style.display = input.value.length > 0 ? 'block' : 'none'; }); btn.addEventListener('click', () => { const isPassword = input.type === 'password'; input.type = isPassword ? 'text' : 'password'; btn.querySelector('i').className = isPassword ? 'fa-solid fa-eye-slash' : 'fa-solid fa-eye'; }); } }); const emailModal = document.getElementById('emailModal'); const changeEmailBtn = document.getElementById('changeEmailBtn'); const emailModalClose = document.getElementById('emailModalClose'); const emailCancelBtn = document.getElementById('emailCancelBtn'); const emailConfirmBtn = document.getElementById('emailConfirmBtn'); const emailPasswordInput = document.getElementById('emailPasswordInput'); const email2faInput = document.getElementById('email2faInput'); const newEmailInput = document.getElementById('newEmailInput'); const emailStep1 = document.getElementById('emailStep1'); const emailStep2 = document.getElementById('emailStep2'); const emailError = document.getElementById('emailError'); const emailSuccess = document.getElementById('emailSuccess'); let emailModalStep = 1; function resetEmailModal() { emailModalStep = 1; if (emailPasswordInput) emailPasswordInput.value = ''; if (email2faInput) email2faInput.value = ''; if (newEmailInput) newEmailInput.value = ''; if (emailStep1) emailStep1.style.display = 'block'; if (emailStep2) emailStep2.style.display = 'none'; if (emailError) emailError.classList.remove('show'); if (emailSuccess) emailSuccess.classList.remove('show'); if (emailConfirmBtn) { emailConfirmBtn.textContent = 'Verify'; emailConfirmBtn.disabled = false; emailConfirmBtn.style.display = ''; } if (emailCancelBtn) emailCancelBtn.textContent = 'Cancel'; configureEmailModalSecurity(); } function showEmailError(msg) { if (emailError) { emailError.textContent = msg; emailError.classList.add('show'); } } function hideEmailError() { if (emailError) emailError.classList.remove('show'); } changeEmailBtn?.addEventListener('click', () => { resetEmailModal(); emailModal?.classList.add('show'); }); emailModalClose?.addEventListener('click', () => { emailModal?.classList.remove('show'); }); emailCancelBtn?.addEventListener('click', () => { emailModal?.classList.remove('show'); }); emailModal?.addEventListener('click', (e) => { if (e.target === emailModal) emailModal.classList.remove('show'); }); emailConfirmBtn?.addEventListener('click', async () => { hideEmailError(); if (emailModalStep === 1) { const currentPw = emailPasswordInput?.value || ''; const code = email2faInput?.value.trim() || ''; if (!currentPw) { showEmailError('Please enter your password'); return; } if (accountHas2FA() && (!code || code.length !== 6 || !/^\d+$/.test(code))) { showEmailError('Please enter a valid 6-digit 2FA code'); return; } emailConfirmBtn.disabled = true; emailConfirmBtn.textContent = 'Verifying...'; try { const res = await fetch('/api/account/email/verify', { method: 'POST', headers: { 'Content-Type': 'application/json' }, credentials: 'include', body: JSON.stringify({ currentPassword: currentPw, twoFactorCode: accountHas2FA() ? code : '' }) }); const data = await res.json(); if (!res.ok) throw new Error(data.error || 'Verification failed'); emailStep1.style.display = 'none'; emailStep2.style.display = 'block'; emailModalStep = 2; emailConfirmBtn.textContent = 'Change Email'; emailConfirmBtn.disabled = false; } catch (err) { showEmailError(err.message); emailConfirmBtn.textContent = 'Verify'; emailConfirmBtn.disabled = false; } } else if (emailModalStep === 2) { const newEmail = newEmailInput?.value.trim() || ''; const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; if (!newEmail || !emailRegex.test(newEmail)) { showEmailError('Please enter a valid email address'); return; } emailConfirmBtn.disabled = true; emailConfirmBtn.textContent = 'Updating...'; try { const res = await fetch('/api/account/email/change', { method: 'POST', headers: { 'Content-Type': 'application/json' }, credentials: 'include', body: JSON.stringify({ currentPassword: emailPasswordInput?.value || '', twoFactorCode: accountHas2FA() ? (email2faInput?.value.trim() || '') : '', newEmail: newEmail }) }); const data = await res.json(); if (!res.ok) throw new Error(data.error || 'Failed to change email'); if (emailSuccess) emailSuccess.classList.add('show'); emailConfirmBtn.style.display = 'none'; if (emailCancelBtn) emailCancelBtn.textContent = 'Done'; const accountEmailEl = document.getElementById('accountEmail'); const accountEmailValueEl = document.getElementById('accountEmailValue'); if (accountEmailEl) accountEmailEl.textContent = newEmail; if (accountEmailValueEl) accountEmailValueEl.textContent = newEmail; setTimeout(() => { emailModal?.classList.remove('show'); resetEmailModal(); }, 2000); } catch (err) { showEmailError(err.message); emailConfirmBtn.textContent = 'Change Email'; emailConfirmBtn.disabled = false; } } }); email2faInput?.addEventListener('input', (e) => { e.target.value = e.target.value.replace(/\D/g, '').slice(0, 6); }); document.querySelectorAll('.email-toggle-eye').forEach(btn => { const targetId = btn.dataset.target; const input = document.getElementById(targetId); if (input) { input.addEventListener('input', () => { btn.style.display = input.value.length > 0 ? 'block' : 'none'; }); btn.addEventListener('click', () => { const isPassword = input.type === 'password'; input.type = isPassword ? 'text' : 'password'; btn.querySelector('i').className = isPassword ? 'fa-solid fa-eye-slash' : 'fa-solid fa-eye'; }); } }); const recoveryModal = document.getElementById('recoveryModal'); const recoveryModalClose = document.getElementById('recoveryModalClose'); const recoveryCancelBtn = document.getElementById('recoveryCancelBtn'); const recoveryConfirmBtn = document.getElementById('recoveryConfirmBtn'); const recoveryCodeInput = document.getElementById('recoveryCodeInput'); const recoveryError = document.getElementById('recoveryError'); let recoveryReturnTo = null; function showRecoveryError(msg) { if (recoveryError) { recoveryError.textContent = msg; recoveryError.classList.add('show'); } } function hideRecoveryError() { if (recoveryError) recoveryError.classList.remove('show'); } function resetRecoveryModal() { if (recoveryCodeInput) recoveryCodeInput.value = ''; hideRecoveryError(); if (recoveryConfirmBtn) { recoveryConfirmBtn.textContent = 'Verify Code'; recoveryConfirmBtn.disabled = false; } recoveryReturnTo = null; } function openRecoveryModal(returnTo) { resetRecoveryModal(); recoveryReturnTo = returnTo; recoveryModal?.classList.add('show'); } async function openAccountFlow(flow) { await showAccountPage(); activateAccountTab('credentials'); switch (flow) { case 'change_password': resetPasswordModal(); passwordModal?.classList.add('show'); return true; case 'change_email': resetEmailModal(); emailModal?.classList.add('show'); return true; case 'change_2fa': resetTwofaModal(); twofaModal?.classList.add('show'); return true; case 'recover_password': openRecoveryModal('password'); return true; case 'recover_email': openRecoveryModal('email'); return true; case 'recover_2fa': openRecoveryModal('twofa'); return true; default: return false; } } window.ADPanelDashboardAccount = { showAccountPage, hideAccountPage, activateAccountTab, openAccountFlow, }; document.getElementById('twofaForgotLink')?.addEventListener('click', () => { twofaModal?.classList.remove('show'); openRecoveryModal('twofa'); }); document.getElementById('passwordForgotLink')?.addEventListener('click', () => { passwordModal?.classList.remove('show'); openRecoveryModal('password'); }); document.getElementById('emailForgotLink')?.addEventListener('click', () => { emailModal?.classList.remove('show'); openRecoveryModal('email'); }); recoveryModalClose?.addEventListener('click', () => { recoveryModal?.classList.remove('show'); }); recoveryCancelBtn?.addEventListener('click', () => { recoveryModal?.classList.remove('show'); }); recoveryModal?.addEventListener('click', (e) => { if (e.target === recoveryModal) recoveryModal.classList.remove('show'); }); recoveryCodeInput?.addEventListener('input', (e) => { e.target.value = e.target.value.toUpperCase().replace(/[^A-Z0-9]/g, '').slice(0, 8); }); recoveryConfirmBtn?.addEventListener('click', async () => { hideRecoveryError(); const code = recoveryCodeInput?.value.trim().toUpperCase() || ''; if (!code || code.length !== 8) { showRecoveryError('Please enter a valid 8-character recovery code'); return; } recoveryConfirmBtn.disabled = true; recoveryConfirmBtn.textContent = 'Verifying...'; try { const res = await fetch('/api/account/recovery/verify', { method: 'POST', headers: { 'Content-Type': 'application/json' }, credentials: 'include', body: JSON.stringify({ recoveryCode: code }) }); const data = await res.json(); if (!res.ok) throw new Error(data.error || 'Invalid recovery code'); recoveryModal?.classList.remove('show'); if (recoveryReturnTo === 'twofa') { try { const genRes = await fetch('/api/account/2fa/generate-recovery', { method: 'POST', headers: { 'Content-Type': 'application/json' }, credentials: 'include' }); const genData = await genRes.json(); if (!genRes.ok) throw new Error(genData.error || 'Failed to generate new 2FA'); resetTwofaModal(); pendingNewSecret = genData.newSecret; if (newQrCode) newQrCode.src = genData.qrCodeUrl; if (newSecretDisplay) newSecretDisplay.textContent = genData.newSecret; const twofaStep1 = document.getElementById('twofaStep1'); if (twofaStep1) twofaStep1.style.display = 'none'; if (qrCodeSection) qrCodeSection.style.display = 'block'; if (confirmSection) confirmSection.style.display = 'block'; const qrStepNum = qrCodeSection?.querySelector('.twofa-step-num'); const confirmStepNum = confirmSection?.querySelector('.twofa-step-num'); if (qrStepNum) qrStepNum.textContent = '1'; if (confirmStepNum) confirmStepNum.textContent = '2'; twoFaModalStep = 2; if (twofaConfirmBtn) twofaConfirmBtn.textContent = 'Confirm New 2FA'; twofaModal?.classList.add('show'); } catch (err) { alert('Failed to generate 2FA: ' + err.message); } } else if (recoveryReturnTo === 'password') { resetPasswordModal(); if (passwordStep1) passwordStep1.style.display = 'none'; if (passwordStep2) passwordStep2.style.display = 'block'; document.getElementById('passwordForgotLink').style.display = 'none'; passwordModalStep = 2; if (passwordConfirmBtn) passwordConfirmBtn.textContent = 'Change Password'; passwordModal?.classList.add('show'); } else if (recoveryReturnTo === 'email') { resetEmailModal(); if (emailStep1) emailStep1.style.display = 'none'; if (emailStep2) emailStep2.style.display = 'block'; document.getElementById('emailForgotLink').style.display = 'none'; emailModalStep = 2; if (emailConfirmBtn) emailConfirmBtn.textContent = 'Change Email'; emailModal?.classList.add('show'); } } catch (err) { showRecoveryError(err.message); recoveryConfirmBtn.textContent = 'Verify Code'; recoveryConfirmBtn.disabled = false; } }); const createBtns = [ document.getElementById('createBtn'), document.getElementById('createBtnHero'), document.getElementById('createBtnFooter') ].filter(Boolean); const createModal = document.getElementById('createModal'); const createClose = document.getElementById('createClose'); const createCancel = document.getElementById('createCancel'); const createConfirm = document.getElementById('createConfirm'); const createSpinner = document.getElementById('createSpinner'); const serverNameEl = document.getElementById('serverName'); const templateGrid = document.getElementById('templateGrid'); const templateInfo = document.getElementById('templateInfo'); const nodeGrid = document.getElementById('nodeGrid'); const summaryNameEl = document.getElementById('summaryServerName'); const summaryTemplateEl = document.getElementById('summaryTemplateName'); const summaryNodeEl = document.getElementById('summaryNodeName'); const templateInfo2 = document.getElementById('templateInfo2'); const forkModal = document.getElementById('forkModal'); const forkClose = document.getElementById('forkClose'); const forkBack = document.getElementById('forkBack'); const forkConfirm = document.getElementById('forkConfirm'); const forkSpinner = document.getElementById('forkSpinner'); const forkGrid = document.getElementById('forkGrid'); const forkInfo = document.getElementById('forkInfo'); const portModal = document.getElementById('portModal'); const portClose = document.getElementById('portClose'); const portBack = document.getElementById('portBack'); const portConfirm = document.getElementById('portConfirm'); const portSpinner = document.getElementById('portSpinner'); const serverIpEl = document.getElementById('serverIp'); const serverPortEl = document.getElementById('serverPort'); const serverPortPreviewEl = document.getElementById('serverPortPreview'); const portDefaultValueEl = document.getElementById('portDefaultValue'); const portTitleTextEl = document.getElementById('portTitleText'); const portSubtitleEl = document.getElementById('portSubtitle'); const resourcesModal = document.getElementById('resourcesModal'); const resourcesClose = document.getElementById('resourcesClose'); const resourcesBack = document.getElementById('resourcesBack'); const resourcesConfirm = document.getElementById('resourcesConfirm'); const resourcesSpinner = document.getElementById('resourcesSpinner'); const resourceRamEl = document.getElementById('resourceRam'); const resourceCpuCoresEl = document.getElementById('resourceCpuCores'); const resourceSwapEl = document.getElementById('resourceSwap'); const resourceStorageEl = document.getElementById('resourceStorage'); const resourceBackupsEl = document.getElementById('resourceBackups'); const resourceMaxSchedulesEl = document.getElementById('resourceMaxSchedules'); const resourceIoWeightEl = document.getElementById('resourceIoWeight'); const resourceCpuWeightEl = document.getElementById('resourceCpuWeight'); const resourcePidsLimitEl = document.getElementById('resourcePidsLimit'); const resourceFileLimitEl = document.getElementById('resourceFileLimit'); const resourceNetworkInboundLimitEl = document.getElementById('resourceNetworkInboundLimit'); const resourceNetworkOutboundLimitEl = document.getElementById('resourceNetworkOutboundLimit'); const resourceNetworkResetMinutesEl = document.getElementById('resourceNetworkResetMinutes'); const nodeRamLimitEl = document.getElementById('nodeRamLimit'); const nodeCpuLimitEl = document.getElementById('nodeCpuLimit'); const nodeStorageLimitEl = document.getElementById('nodeStorageLimit'); const resourceDiskEl = document.getElementById('resourceDisk'); const resourceDiskHintEl = document.getElementById('resourceDiskHint'); const liveNodeCapacityCache = new Map(); const RESOURCE_NETWORK_LIMIT_MAX_MB = 1073741824; const RESOURCE_NETWORK_RESET_MAX_MINUTES = 43200; function resolveNodeCacheKey(node) { return String(node?.id || node?.uuid || node?.name || '').trim(); } function configuredNodeCapacity(node) { return { ramMb: Math.max(0, Math.trunc(Number(node?.ram_mb || node?.buildConfig?.ram_mb || 0))), cpuCores: Math.max(0, Number(node?.cpu_cores || node?.buildConfig?.cpu_cores || node?.buildConfig?.cpuCores || 0)), diskGb: Math.max(0, Number(node?.disk_gb || node?.buildConfig?.disk_gb || node?.buildConfig?.diskGb || 0)) }; } function mergeNodeCapacityValue(configured, live, round, preferLive) { const cfg = Number(configured); const detected = Number(live); let value = 0; if (preferLive && Number.isFinite(detected) && detected > 0) value = detected; else if (Number.isFinite(cfg) && cfg > 0 && Number.isFinite(detected) && detected > 0) value = Math.min(cfg, detected); else if (Number.isFinite(detected) && detected > 0) value = detected; else if (Number.isFinite(cfg) && cfg > 0) value = cfg; return round ? Math.trunc(value) : value; } function resolveEffectiveNodeCapacity(node, liveCapacity) { const configured = configuredNodeCapacity(node); const live = (liveCapacity && typeof liveCapacity === 'object') ? liveCapacity : {}; return { ramMb: mergeNodeCapacityValue(configured.ramMb, live.ramMb, true, true), cpuCores: mergeNodeCapacityValue(configured.cpuCores, live.cpuCores, false), diskGb: mergeNodeCapacityValue(configured.diskGb, live.diskGb, false) }; } function applyNodeCapacityToResourceModal(capacity) { const ramMb = Number(capacity?.ramMb || 0); const cpuCores = Number(capacity?.cpuCores || 0); const diskGb = Number(capacity?.diskGb || 0); const diskMb = diskGb > 0 ? Math.trunc(diskGb * 1024) : 0; if (nodeRamLimitEl) nodeRamLimitEl.textContent = ramMb > 0 ? `${ramMb.toLocaleString()} MB` : 'Unlimited'; if (nodeCpuLimitEl) nodeCpuLimitEl.textContent = cpuCores > 0 ? `${cpuCores} cores` : 'Unlimited'; if (nodeStorageLimitEl) nodeStorageLimitEl.textContent = diskMb > 0 ? (diskMb >= 1010 ? `${(diskMb / 1024).toFixed(2).replace(/\.?0+$/, '')} GB` : `${diskMb.toLocaleString()} MB`) : 'Unlimited'; if (resourceRamEl) { if (ramMb > 0) resourceRamEl.max = ramMb; else resourceRamEl.removeAttribute('max'); } if (resourceCpuCoresEl) { if (cpuCores > 0) resourceCpuCoresEl.max = cpuCores; else resourceCpuCoresEl.removeAttribute('max'); } if (resourceStorageEl) { if (diskMb > 0) resourceStorageEl.max = diskMb; else resourceStorageEl.removeAttribute('max'); } } async function fetchLiveNodeCapacity(node) { const cacheKey = resolveNodeCacheKey(node); if (!cacheKey) return null; if (liveNodeCapacityCache.has(cacheKey)) return liveNodeCapacityCache.get(cacheKey); try { const res = await fetch(`/api/admin/nodes/${encodeURIComponent(cacheKey)}/stats`, { cache: 'no-store' }); const data = await res.json().catch(() => ({})); if (!res.ok || !data.ok || !data.online || !data.stats) return null; const liveCapacity = { ramMb: Math.max(0, Math.trunc(Number(data.stats?.ram?.totalMb || 0))), cpuCores: Math.max(0, Number(data.stats?.cpu?.cores || 0)), diskGb: Math.max(0, Number(data.stats?.disk?.totalGb || 0)) }; liveNodeCapacityCache.set(cacheKey, liveCapacity); return liveCapacity; } catch { return null; } } async function getEffectiveNodeCapacity(node) { const liveCapacity = await fetchLiveNodeCapacity(node); return resolveEffectiveNodeCapacity(node, liveCapacity); } function formatDiskOptionUsage(disk) { const used = Number(disk?.usedGb || 0); const limit = Number(disk?.limitGb || 0); const usedStr = (used >= 10 ? used.toFixed(0) : used.toFixed(1)).replace(/\.0$/, ''); return limit > 0 ? `${usedStr}/${limit} GB` : `${usedStr} GB used`; } async function populateNodeDiskOptions(node) { if (!resourceDiskEl) return; const primaryOpt = ''; resourceDiskEl.innerHTML = primaryOpt; resourceDiskEl.value = 'primary'; if (resourceDiskHintEl) resourceDiskHintEl.textContent = 'Server files are stored on the selected disk.'; const id = node && (node.id || node.uuid || node.name); if (!id) return; try { const res = await fetch(`/api/nodes/${encodeURIComponent(id)}/disks`, { cache: 'no-store' }); const data = await res.json().catch(() => ({})); if (!res.ok || !Array.isArray(data.disks)) return; // Keep the modal in sync only if the same node is still selected. const current = getSelectedNode(); if (!current || resolveNodeCacheKey(current) !== resolveNodeCacheKey(node)) return; const options = []; const additional = []; for (const d of data.disks) { const isPrimary = !!d.primary || String(d.id) === 'primary'; if (isPrimary) continue; additional.push(d); } options.push(primaryOpt); for (const d of additional) { const label = `${d.mountPath} (${formatDiskOptionUsage(d)})`; options.push(``); } resourceDiskEl.innerHTML = options.join(''); resourceDiskEl.value = 'primary'; if (resourceDiskHintEl) { resourceDiskHintEl.textContent = additional.length ? 'Primary is used by default. Select an additional disk to place this server elsewhere.' : 'This node has only the primary disk available.'; } } catch { // Leave the primary-only default in place on failure. } } if (resourceStorageEl) { resourceStorageEl.addEventListener('input', () => { const hint = resourceStorageEl.closest('.enterprise-field')?.querySelector('.enterprise-hint'); if (!hint) return; const val = parseInt(resourceStorageEl.value, 10); if (!isNaN(val) && val > 0) { const display = val >= 1010 ? (val / 1024).toFixed(2).replace(/\.?0+$/, '') + ' GB' : val + ' MB'; hint.textContent = `Maximum disk space: ${display}`; } else { hint.textContent = 'Maximum disk space. Leave empty to use the node allocation.'; } }); } function readOptionalIntegerRange(input, label, min, max) { const raw = input && input.value != null ? String(input.value).trim() : ''; if (!raw) return null; if (!/^\d+$/.test(raw)) { throw new Error(`${label} must be a whole number.`); } const value = Number(raw); if (!Number.isSafeInteger(value) || value < min || value > max) { throw new Error(`${label} must be between ${min} and ${max}.`); } return value; } const startupModal = document.getElementById('startupModal'); const startupClose = document.getElementById('startupClose'); const startupBack = document.getElementById('startupBack'); const startupConfirm = document.getElementById('startupConfirm'); const startupSpinner = document.getElementById('startupSpinner'); const startupCommandEl = document.getElementById('startupCommand'); const startupCommandSourceEl = document.getElementById('startupCommandSource'); const startupCommandErrorEl = document.getElementById('startupCommandError'); const importModal = document.getElementById('importModal'); const importClose = document.getElementById('importClose'); const importBack = document.getElementById('importBack'); const importSkip = document.getElementById('importSkip'); const importConfirm = document.getElementById('importConfirm'); const importSpinner = document.getElementById('importSpinner'); const importUrlEl = document.getElementById('importUrl'); const importProgressContainer = document.getElementById('importProgressContainer'); const importProgressFill = document.getElementById('importProgressFill'); const importPercent = document.getElementById('importPercent'); const importDownloaded = document.getElementById('importDownloaded'); const importTotal = document.getElementById('importTotal'); const MC_VERSION = "1.21.8"; const MC_FORKS = [ { id: "paper", name: "Paper", desc: "Optimized performance with plugin support" }, { id: "pufferfish", name: "Pufferfish", desc: "TPS-focused optimizations" }, { id: "vanilla", name: "Vanilla", desc: "Standard Minecraft experience" }, { id: "purpur", name: "Purpur", desc: "Paper fork with extra settings" } ]; let templates = []; let nodes = []; let selectedTemplateId = null; let selectedForkId = null; let selectedNodeId = ""; let portBackTarget = "create"; let selectedStartupCommand = ""; let selectedStartupCommandMode = ""; let selectedImportUrl = ""; const BUILTIN_TEMPLATES = ['minecraft', 'nodejs']; const MAX_SERVER_NAME_LENGTH = 40; function isBuiltinTemplate(templateId) { return BUILTIN_TEMPLATES.includes((templateId || '').toLowerCase()); } function sanitizeName(s) { s = (s || '').trim(); s = s.replace(/\s+/g, '-').replace(/[^a-zA-Z0-9\-_]/g, ''); return s.slice(0, MAX_SERVER_NAME_LENGTH) || ''; } function getTemplateMeta(t) { const id = ((t && t.id) || '').toString().toLowerCase(); if (id.includes('minecraft') || id === 'mc') { return { icon: 'fa-solid fa-cube', badge: 'Minecraft' }; } if (id.includes('node') || id.includes('discord')) { return { icon: 'fa-brands fa-node-js', badge: 'Node.js' }; } if (id.includes('python') || id.includes('py')) { return { icon: 'fa-brands fa-python', badge: 'Python' }; } if (id.includes('proxy') || id.includes('web') || id.includes('http')) { return { icon: 'fa-solid fa-cloud', badge: 'Web' }; } return { icon: 'fa-solid fa-server', badge: 'Generic' }; } function getDefaultPortForTemplate(templateId) { const id = (templateId || '').toString().toLowerCase(); if (!id) return 25565; const configuredTemplate = (templates || []).find(t => String(t?.id || '').trim().toLowerCase() === id); const configuredPort = parseStrictPortValue( configuredTemplate?.defaultPort || configuredTemplate?.default_port || configuredTemplate?.docker?.ports?.[0] ); if (configuredPort) return configuredPort; if (id.includes('minecraft') || id === 'mc') return 25565; if (id.includes('node') || id.includes('discord')) return 3000; if (id.includes('http') || id.includes('web') || id.includes('proxy')) return 80; return 8080; } function coercePositiveNumber(value) { const numeric = Number(value); return Number.isFinite(numeric) && numeric > 0 ? numeric : 0; } function sanitizePortInputValue(value) { return String(value ?? '').replace(/[^\d]/g, '').slice(0, 5); } function parseStrictPortValue(value) { const str = String(value ?? '').trim(); if (!/^[1-9]\d{0,4}$/.test(str)) return null; const port = Number(str); if (!Number.isInteger(port) || port < 1 || port > 65535) return null; return port; } function getSelectedNode() { if (!selectedNodeId) return null; return (nodes || []).find(x => (x.uuid === selectedNodeId) || (x.id === selectedNodeId) || (x.name === selectedNodeId) ) || null; } async function verifySelectedNodePortAvailable(portVal) { const node = getSelectedNode(); if (!node || !portVal) return true; try { const nodeId = encodeURIComponent(node.uuid || node.id || node.name || selectedNodeId); const res = await fetch(`/api/nodes/${nodeId}/ports/check?port=${encodeURIComponent(String(portVal))}`); const data = await res.json().catch(() => ({})); if (res.ok && data && data.available === false) { alert(data.detail || data.error || `Port ${portVal} is already in use on this node.`); return false; } } catch (_) { } return true; } function bindStrictPortInput(input) { if (!input) return; input.setAttribute('inputmode', 'numeric'); input.addEventListener('input', () => { const sanitized = sanitizePortInputValue(input.value); if (input.value !== sanitized) input.value = sanitized; }); } function getDefaultStartupVolumes(templateId, docker) { const configured = Array.isArray(docker?.volumes) ? docker.volumes.map((entry) => String(entry || '').trim()).filter(Boolean) : []; if (configured.length) return configured; const normalizedTemplateId = String(templateId || '').trim().toLowerCase(); if (['nodejs', 'discord-bot', 'python', 'runtime'].includes(normalizedTemplateId)) { return ['{DATA_DIR}:/app']; } return ['{DATA_DIR}:/data']; } function getDefaultStartFileForTemplate(templateId) { const normalizedTemplateId = String(templateId || '').trim().toLowerCase(); if (normalizedTemplateId === 'minecraft') return 'server.jar'; if (normalizedTemplateId === 'python') return 'main.py'; if (normalizedTemplateId === 'nodejs' || normalizedTemplateId === 'discord-bot') return 'index.js'; return ''; } function getDefaultContainerDataDir(templateId, docker) { const volumes = getDefaultStartupVolumes(templateId, docker); const first = String(volumes[0] || '').trim(); if (first.includes(':')) { const parts = first.split(':'); if (parts[1]) return parts[1].trim(); } const normalizedTemplateId = String(templateId || '').trim().toLowerCase(); return ['nodejs', 'discord-bot', 'python', 'runtime'].includes(normalizedTemplateId) ? '/app' : '/data'; } function getDefaultRuntimeCommand(templateId, docker) { const explicit = String(docker?.command || '').trim(); if (explicit) return explicit; const normalizedTemplateId = String(templateId || '').trim().toLowerCase(); const dataDir = getDefaultContainerDataDir(normalizedTemplateId, docker); const startFile = getDefaultStartFileForTemplate(normalizedTemplateId); if (normalizedTemplateId === 'minecraft') { return `java -Xms128M -Xmx{RAM_MB}M -jar ${dataDir}/${startFile || 'server.jar'} nogui`; } if (normalizedTemplateId === 'python') { return `python ${dataDir}/${startFile || 'main.py'}`; } if (normalizedTemplateId === 'nodejs' || normalizedTemplateId === 'discord-bot') { return `node ${dataDir}/${startFile || 'index.js'}`; } return ''; } const RUNTIME_PROCESS_BLOCKED_EXECUTABLES = new Set([ 'sh', '/bin/sh', 'bash', '/bin/bash', 'ash', '/bin/ash', 'dash', '/bin/dash', 'zsh', '/bin/zsh', 'ksh', '/bin/ksh', ]); const RUNTIME_PROCESS_BLOCKED_CONTAINER_EXECUTABLES = new Set([ 'docker', 'docker-compose', 'podman', 'podman-compose', 'nerdctl', 'ctr', ]); const RUNTIME_PROCESS_ENV_WRAPPERS = new Set(['env']); const RUNTIME_PROCESS_BLOCKED_TOKENS = new Set([ '&&', '||', '|', ';', '&', '>', '>>', '<', '<<', '2>', '2>>', '2>&1', '|&', ]); function parseRuntimeProcessArgsInput(value) { const input = String(value || ''); const args = []; let current = ''; let quote = ''; let escape = false; for (let i = 0; i < input.length; i += 1) { const ch = input[i]; if (escape) { current += ch; escape = false; continue; } if (ch === '\\') { escape = true; continue; } if (quote) { if (ch === quote) quote = ''; else current += ch; continue; } if (ch === '"' || ch === "'") { quote = ch; continue; } if (/\s/.test(ch)) { if (current) { args.push(current); current = ''; } continue; } current += ch; } if (current) args.push(current); return args; } function getRuntimeProcessExecutableBaseNameInput(value) { const raw = String(value || '').trim(); if (!raw) return ''; let candidate = raw; if (/\s/.test(candidate)) { const nestedArgs = parseRuntimeProcessArgsInput(candidate); if (nestedArgs.length) candidate = String(nestedArgs[0] || '').trim(); } const normalized = candidate.toLowerCase().replace(/\\/g, '/'); const parts = normalized.split('/').filter(Boolean); return parts.pop() || normalized; } function isRuntimeEnvAssignmentTokenInput(value) { const token = String(value || '').trim(); if (!token || token.startsWith('-')) return false; const eqIdx = token.indexOf('='); if (eqIdx <= 0) return false; return /^[A-Za-z_][A-Za-z0-9_]*$/.test(token.slice(0, eqIdx)); } function resolveRuntimeProcessExecutableArgsInput(args) { const parts = Array.isArray(args) ? args : []; if (!parts.length) return { execName: '', index: -1 }; let index = 0; while (index < parts.length) { const token = String(parts[index] || '').trim(); if (!token) { index += 1; continue; } const execName = getRuntimeProcessExecutableBaseNameInput(token); if (RUNTIME_PROCESS_ENV_WRAPPERS.has(execName)) { index += 1; while (index < parts.length) { const envToken = String(parts[index] || '').trim(); if (!envToken) { index += 1; continue; } if (envToken === '--') { index += 1; break; } if (envToken.startsWith('-') || isRuntimeEnvAssignmentTokenInput(envToken)) { index += 1; continue; } break; } continue; } return { execName, index }; } return { execName: '', index: -1 }; } function validateRuntimeContainerLauncherInput(args) { const { execName } = resolveRuntimeProcessExecutableArgsInput(args); if (RUNTIME_PROCESS_BLOCKED_CONTAINER_EXECUTABLES.has(execName)) { return 'Container runtime CLI commands like "docker run" are not allowed here. Configure image, ports, volumes, env, and the in-container process separately.'; } return null; } function isBlockedRuntimeShellExecutableInput(value) { const execName = getRuntimeProcessExecutableBaseNameInput(value); return RUNTIME_PROCESS_BLOCKED_EXECUTABLES.has(execName); } function isRuntimeShellWrapperArgsInput(args, execIndex) { if (!Array.isArray(args) || execIndex < 0 || execIndex >= args.length) return false; if (!isBlockedRuntimeShellExecutableInput(args[execIndex])) return false; const flags = String(args[execIndex + 1] || '').trim().toLowerCase(); return flags.startsWith('-') && flags.includes('c'); } function validateRuntimeProcessCommandInput(value) { const command = String(value || '').trim(); if (!command) return null; if (command.length > 4000) return 'Process command is too long.'; if (command.includes('\n')) return 'Process command must be a single line.'; if (command.includes('`') || command.includes('$(')) { return 'Shell expansions are not allowed in process commands.'; } const args = parseRuntimeProcessArgsInput(command); if (!args.length) return 'Process command is empty.'; const { execName: resolvedExecName, index: execIndex } = resolveRuntimeProcessExecutableArgsInput(args); const containerRuntimeError = validateRuntimeContainerLauncherInput(args); if (containerRuntimeError) return containerRuntimeError; if (isBlockedRuntimeShellExecutableInput(resolvedExecName || args[0]) && isRuntimeShellWrapperArgsInput(args, execIndex)) { return 'Shell wrappers like sh -c or bash -lc are not allowed. Provide the executable and arguments directly.'; } for (const arg of args) { const token = String(arg || '').trim(); if (RUNTIME_PROCESS_BLOCKED_TOKENS.has(token)) { return `Shell control operator "${token}" is not allowed in process commands.`; } } return null; } function buildDefaultStartupCommandForTemplate(template) { if (!template || !template.docker) return ''; return getDefaultRuntimeCommand(template.id, template.docker); } function setStartupCommandSource(message) { if (!startupCommandSourceEl) return; startupCommandSourceEl.textContent = String(message || 'Using startup command from template.').trim(); } function setStartupCommandValidationError(message) { const text = String(message || '').trim(); if (startupCommandEl) startupCommandEl.setAttribute('aria-invalid', text ? 'true' : 'false'); if (!startupCommandErrorEl) return; startupCommandErrorEl.textContent = text; startupCommandErrorEl.hidden = !text; } function findTemplateById(templateId) { const id = String(templateId || '').trim(); if (!id) return null; return (templates || []).find(t => String(t?.id || '').trim() === id) || null; } function startupCommandSourceLabel(source, imageRef) { const normalized = String(source || '').trim().toLowerCase(); if (normalized === 'adpanel-startup') return 'Using the startup command defined by this template.'; if (normalized === 'image-default') { return imageRef ? `Using Docker image default ENTRYPOINT/CMD (${imageRef}); ADPanel will not override it.` : 'Using Docker image default ENTRYPOINT/CMD; ADPanel will not override it.'; } if (normalized === 'image-inspect') { return imageRef ? `Detected from Docker image inspect (${imageRef}); ADPanel will use the image default.` : 'Detected from Docker image inspect; ADPanel will use the image default.'; } if (normalized === 'template-command') return 'Using process command defined by this template.'; if (normalized === 'template-default') return 'Using template default process command.'; return 'Using auto-detected process command.'; } function quoteStartupArg(arg) { const value = String(arg || ''); if (!value) return '""'; if (!/[\s"'\\]/.test(value)) return value; return `"${value.replace(/(["\\])/g, '\\$1')}"`; } function joinStartupArgs(args) { if (!Array.isArray(args)) return ''; return args .map((item) => String(item || '').trim()) .filter(Boolean) .map(quoteStartupArg) .join(' ') .trim(); } function isShellEntrypoint(entrypoint) { if (!Array.isArray(entrypoint) || entrypoint.length < 2) return false; const first = String(entrypoint[0] || '').trim().toLowerCase(); const base = first.split('/').filter(Boolean).pop() || first; const shellLike = ['sh', 'bash', 'ash', 'dash', 'zsh', 'ksh'].includes(base); if (!shellLike) return false; const flags = String(entrypoint[1] || '').trim(); return flags.startsWith('-') && flags.includes('c'); } function isLikelyPathOnlyStartup(command) { const args = parseRuntimeProcessArgsInput(String(command || '').trim()); if (args.length !== 1) return false; const token = String(args[0] || '').trim(); if (!token.startsWith('/')) return false; const lower = token.toLowerCase(); if (lower.includes('entrypoint')) return true; if (lower.endsWith('.sh') || lower.endsWith('.bash')) return true; return false; } function pickInspectEnvStartupCommand(envObj) { if (!envObj || typeof envObj !== 'object' || Array.isArray(envObj)) return ''; const env = {}; Object.entries(envObj).forEach(([k, v]) => { env[String(k || '').trim().toUpperCase()] = String(v ?? '').trim(); }); const candidates = [ 'STARTUP', 'SERVER_START_CMD', 'START_CMD', 'START_COMMAND', 'RUN_CMD', 'APP_START_CMD', 'LAUNCH_CMD', 'COMMAND', 'CMD' ]; for (const key of candidates) { const val = String(env[key] || '').trim(); if (!val) continue; if (val.includes('${') || val.includes('{{')) continue; if (val.startsWith('$')) continue; return val; } return ''; } function inferStartupFromInspectMeta(template, inspectMeta, fallbackCommand, options = {}) { const allowUnsafe = !!options.allowUnsafe; function chooseCandidate(candidate) { const value = String(candidate || '').trim(); if (!value) return ''; if (allowUnsafe) return value; const err = validateRuntimeProcessCommandInput(value); if (err) return ''; return value; } const entrypoint = Array.isArray(inspectMeta?.entrypoint) ? inspectMeta.entrypoint.map((item) => String(item || '').trim()).filter(Boolean) : []; const cmd = Array.isArray(inspectMeta?.cmd) ? inspectMeta.cmd.map((item) => String(item || '').trim()).filter(Boolean) : []; const workingDir = String(inspectMeta?.workingDir || '').trim() || '/'; const envStartup = pickInspectEnvStartupCommand(inspectMeta?.env); if (envStartup) { const candidate = chooseCandidate(envStartup); if (candidate) return candidate; } if (isShellEntrypoint(entrypoint)) { if (cmd.length) { const shellCmd = cmd.join(' ').trim(); const candidate = chooseCandidate(shellCmd); if (candidate) return candidate; } if (entrypoint.length > 2) { const inlineShell = entrypoint.slice(2).join(' ').trim(); const candidate = chooseCandidate(inlineShell); if (candidate) return candidate; } } const assembled = joinStartupArgs([...(entrypoint || []), ...(cmd || [])]); const assembledCandidate = chooseCandidate(assembled); if (assembledCandidate) return assembledCandidate; const templateId = String(template?.id || '').trim().toLowerCase(); const imageName = String(template?.docker?.image || '').trim().toLowerCase(); const startFile = getDefaultStartFileForTemplate(templateId); const normalizedDir = workingDir === '/' ? '/app' : workingDir; let heuristic = ''; if (templateId.includes('node') || imageName.includes('node')) { heuristic = `node ${normalizedDir}/${startFile || 'index.js'}`; } else if (templateId.includes('python') || imageName.includes('python')) { heuristic = `python ${normalizedDir}/${startFile || 'main.py'}`; } else if (templateId.includes('minecraft') || imageName.includes('minecraft') || imageName.includes('java')) { heuristic = 'java -Xms128M -Xmx{RAM_MB}M -jar /data/server.jar nogui'; } if (heuristic) { const candidate = chooseCandidate(heuristic); if (candidate) return candidate; } const fallback = String(fallbackCommand || '').trim(); const fallbackCandidate = chooseCandidate(fallback); if (fallbackCandidate) return fallbackCandidate; if (allowUnsafe) { if (assembled) return assembled; if (entrypoint.length) return joinStartupArgs(entrypoint); if (cmd.length) return cmd.join(' ').trim(); if (fallback) return fallback; } return ''; } function isWrapperEntrypointOnlyCommand(command, inspectMeta) { if (!isLikelyPathOnlyStartup(command)) return false; const cmd = Array.isArray(inspectMeta?.cmd) ? inspectMeta.cmd.map((item) => String(item || '').trim()).filter(Boolean) : []; return cmd.length === 0; } function startupCommandCacheKey(templateId, nodeId) { return `${String(templateId || '').trim().toLowerCase()}::${String(nodeId || '').trim().toLowerCase()}`; } const startupCommandCache = new Map(); function setStartupCommandLoading(visible) { if (!startupSpinner) return; startupSpinner.classList.toggle('visible', Boolean(visible)); } async function resolveStartupCommandForSelection(options = {}) { const templateId = String(options.templateId ?? selectedTemplateId ?? '').trim(); const nodeId = String(options.nodeId ?? selectedNodeId ?? 'none').trim() || 'none'; const forceRefresh = !!options.forceRefresh; const showLoading = !!options.showLoading; setStartupCommandValidationError(''); const template = findTemplateById(templateId); if (!template) { selectedStartupCommand = ''; if (startupCommandEl && startupModal?.classList.contains('show')) startupCommandEl.value = ''; setStartupCommandSource('No template selected yet.'); setStartupCommandLoading(false); return ''; } const templateDefinedCommand = String(template?.docker?.command || '').trim(); const templateADPanelStartup = String( template?.docker?.adpanelStartup || template?.docker?.adpanel_startup || template?.docker?.pterodactylStartup || template?.docker?.pterodactyl_startup || '' ).trim(); const templateStartupDisplay = String( template?.docker?.startupDisplay || template?.docker?.startup_display || template?.docker?.startupPreview || template?.docker?.startup_preview || template?.docker?.imageDefaultStartup || template?.docker?.image_default_startup || '' ).trim(); if (startupCommandEl) startupCommandEl.readOnly = false; if (templateADPanelStartup) { selectedStartupCommand = templateADPanelStartup; selectedStartupCommandMode = 'adpanel-startup'; if (startupCommandEl && startupModal?.classList.contains('show')) { startupCommandEl.value = selectedStartupCommand; startupCommandEl.readOnly = true; } setStartupCommandValidationError(''); setStartupCommandSource('Using the startup command defined by this template.'); setStartupCommandLoading(false); return selectedStartupCommand; } if (templateStartupDisplay) { selectedStartupCommand = templateStartupDisplay; selectedStartupCommandMode = 'image-default'; if (startupCommandEl && startupModal?.classList.contains('show')) { startupCommandEl.value = selectedStartupCommand; startupCommandEl.readOnly = true; } setStartupCommandValidationError(''); setStartupCommandSource(startupCommandSourceLabel('image-default', template?.docker?.image && template?.docker?.tag ? `${template.docker.image}:${template.docker.tag}` : '')); setStartupCommandLoading(false); return selectedStartupCommand; } selectedStartupCommandMode = ''; const fallbackCommand = buildDefaultStartupCommandForTemplate(template); const candidate = String(templateDefinedCommand || fallbackCommand || '').trim(); const commandError = validateRuntimeProcessCommandInput(candidate); selectedStartupCommand = commandError ? '' : candidate; if (startupCommandEl && startupModal?.classList.contains('show')) { startupCommandEl.value = selectedStartupCommand; } if (selectedStartupCommand) { setStartupCommandSource(templateDefinedCommand ? 'Using startup command defined in this template.' : 'Using default startup command for this template.'); } else { setStartupCommandSource(commandError ? `Template startup command is invalid (${commandError}). Set the process command manually.` : 'Leave empty to use the Docker image default ENTRYPOINT/CMD, or set a process command manually.'); } if (!templateDefinedCommand) { const cacheKey = startupCommandCacheKey(templateId, nodeId); try { setStartupCommandLoading(showLoading); let detected = null; if (!forceRefresh && startupCommandCache.has(cacheKey)) { detected = startupCommandCache.get(cacheKey); } else { const response = await fetch('/api/templates/startup-command', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ templateId, nodeId }) }); const data = await response.json().catch(() => ({})); detected = { ok: response.ok && data?.ok, data, status: response.status }; startupCommandCache.set(cacheKey, detected); } if (detected?.ok) { const data = detected.data || {}; const sourceMode = String(data.source || '').trim().toLowerCase(); const usesDockerImageDefault = sourceMode === 'image-default' || sourceMode === 'image-inspect'; let detectedCommand = String(data.command || '').trim(); if (!detectedCommand && data.inspect) { detectedCommand = inferStartupFromInspectMeta(template, data.inspect, fallbackCommand, { allowUnsafe: false }); } if (!detectedCommand && usesDockerImageDefault && data.inspect) { const entrypoint = Array.isArray(data.inspect.entrypoint) ? data.inspect.entrypoint.map((item) => String(item || '').trim()).filter(Boolean) : []; const cmd = Array.isArray(data.inspect.cmd) ? data.inspect.cmd.map((item) => String(item || '').trim()).filter(Boolean) : []; detectedCommand = joinStartupArgs([...entrypoint, ...cmd]); } const detectedError = usesDockerImageDefault ? null : validateRuntimeProcessCommandInput(detectedCommand); if (detectedCommand && (!detectedError || usesDockerImageDefault)) { const activeNodeId = String(selectedNodeId ?? 'none').trim() || 'none'; if (String(selectedTemplateId || '').trim() !== templateId || activeNodeId !== nodeId) { return selectedStartupCommand; } selectedStartupCommand = detectedCommand; selectedStartupCommandMode = sourceMode === 'adpanel-startup' ? 'adpanel-startup' : (usesDockerImageDefault ? 'image-default' : ''); if (startupCommandEl && startupModal?.classList.contains('show')) { startupCommandEl.value = selectedStartupCommand; startupCommandEl.readOnly = selectedStartupCommandMode === 'adpanel-startup' || selectedStartupCommandMode === 'image-default'; } setStartupCommandSource(startupCommandSourceLabel(data.source, data.imageRef)); return selectedStartupCommand; } } else if (!selectedStartupCommand && detected?.data) { const detail = String(detected.data.detail || detected.data.error || '').trim(); if (detail) setStartupCommandSource(`Could not auto-detect startup command (${detail}). Set it manually.`); } } catch { if (!selectedStartupCommand) { setStartupCommandSource('Could not auto-detect startup command from Docker image. Set it manually.'); } } finally { setStartupCommandLoading(false); } } return selectedStartupCommand; } function openCreate() { if (!createModal) { window.location.href = '/status'; return; } document.body.classList.add('modal-open'); createModal.classList.add('show'); createModal.setAttribute('aria-hidden', 'false'); selectedTemplateId = null; selectedForkId = null; selectedNodeId = ''; portBackTarget = 'create'; selectedStartupCommand = ''; selectedImportUrl = ''; if (serverNameEl) serverNameEl.value = ''; if (templateGrid) templateGrid.innerHTML = '