138 lines
4.5 KiB
JavaScript
138 lines
4.5 KiB
JavaScript
function getCsrfToken() {
|
|
return document.querySelector('meta[name="csrf-token"]')?.content || '';
|
|
}
|
|
|
|
const terminalInput = document.getElementById('terminalInput');
|
|
const terminalOutput = document.getElementById('terminalOutput');
|
|
const connectionStatus = document.getElementById('connectionStatus');
|
|
|
|
terminalInput.addEventListener('keydown', function(e) {
|
|
if (e.key === 'Enter') {
|
|
executeCommand();
|
|
}
|
|
});
|
|
|
|
function escapeHtml(str) {
|
|
const d = document.createElement('div');
|
|
d.textContent = str;
|
|
return d.innerHTML;
|
|
}
|
|
|
|
function executeCommand() {
|
|
const command = terminalInput.value.trim();
|
|
if (!command) return;
|
|
|
|
appendTerminalLine(command, 'command');
|
|
|
|
terminalInput.value = '';
|
|
terminalInput.disabled = true;
|
|
document.getElementById('executeBtn').disabled = true;
|
|
|
|
updateStatus('executing', 'Executing...');
|
|
|
|
fetch('/terminal/' + serverId + '/execute', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Accept': 'application/json',
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
'X-CSRF-Token': getCsrfToken(),
|
|
},
|
|
body: 'command=' + encodeURIComponent(command) + '&_csrf_token=' + encodeURIComponent(getCsrfToken()),
|
|
})
|
|
.then(r => r.json())
|
|
.then(data => {
|
|
terminalInput.disabled = false;
|
|
document.getElementById('executeBtn').disabled = false;
|
|
terminalInput.focus();
|
|
|
|
if (data.success) {
|
|
if (data.output) {
|
|
appendTerminalLine(data.output, 'output');
|
|
}
|
|
if (data.stderr) {
|
|
appendTerminalLine(data.stderr, 'error');
|
|
}
|
|
if (data.history) {
|
|
appendHistoryRow(data.history);
|
|
}
|
|
updateStatus('connected', 'Connected');
|
|
} else {
|
|
appendTerminalLine(data.message || 'Command failed', 'error');
|
|
updateStatus('error', 'Error');
|
|
}
|
|
|
|
const terminalBody = document.getElementById('terminalOutput');
|
|
terminalBody.scrollTop = terminalBody.scrollHeight;
|
|
})
|
|
.catch(err => {
|
|
terminalInput.disabled = false;
|
|
document.getElementById('executeBtn').disabled = false;
|
|
appendTerminalLine('Connection error: ' + err.message, 'error');
|
|
updateStatus('error', 'Error');
|
|
});
|
|
}
|
|
|
|
function appendTerminalLine(text, type) {
|
|
const line = document.createElement('div');
|
|
line.className = 'terminal-line terminal-' + type;
|
|
if (type === 'command') {
|
|
line.innerHTML = '<span class="terminal-prompt-inline">$</span> ' + escapeHtml(text);
|
|
} else {
|
|
line.innerHTML = '<pre>' + escapeHtml(text) + '</pre>';
|
|
}
|
|
terminalOutput.appendChild(line);
|
|
terminalOutput.scrollTop = terminalOutput.scrollHeight;
|
|
}
|
|
|
|
function runQuickCmd(cmd) {
|
|
terminalInput.value = cmd;
|
|
executeCommand();
|
|
}
|
|
|
|
function clearTerminal() {
|
|
terminalOutput.innerHTML = '';
|
|
}
|
|
|
|
function appendHistoryRow(h) {
|
|
const tbody = document.querySelector('.card-body .table tbody');
|
|
if (!tbody) return;
|
|
const emptyState = tbody.closest('.card-body')?.querySelector('.empty-state');
|
|
if (emptyState) emptyState.remove();
|
|
const row = document.createElement('tr');
|
|
const time = h.executed_at ? new Date(h.executed_at.replace(' ', 'T')).toLocaleTimeString('en-US', { hour12: false }) : '';
|
|
row.innerHTML =
|
|
'<td>' + time + '</td>' +
|
|
'<td><code>' + escapeHtml(h.command) + '</code></td>' +
|
|
'<td><span class="badge ' + (h.exit_status === 0 ? 'badge-success' : 'badge-danger') + '">' + (h.exit_status === 0 ? 'OK' : 'ERR') + '</span></td>' +
|
|
'<td>' + escapeHtml(h.username || 'System') + '</td>';
|
|
tbody.prepend(row);
|
|
const container = tbody.closest('.table-responsive');
|
|
if (container) container.scrollTop = 0;
|
|
}
|
|
|
|
function clearHistory() {
|
|
if (!confirm('Clear all command history for this server?')) return;
|
|
|
|
fetch('/terminal/' + serverId + '/clear-history', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
'X-CSRF-Token': getCsrfToken(),
|
|
},
|
|
body: '_csrf_token=' + encodeURIComponent(getCsrfToken()),
|
|
})
|
|
.then(r => r.json())
|
|
.then(data => {
|
|
showToast(data.success ? 'success' : 'error', data.message);
|
|
if (data.success) location.reload();
|
|
});
|
|
}
|
|
|
|
function updateStatus(status, text) {
|
|
const dot = connectionStatus.querySelector('.status-dot');
|
|
dot.className = 'status-dot ' + status;
|
|
connectionStatus.lastChild.textContent = ' ' + text;
|
|
}
|
|
|
|
terminalInput.focus();
|