* Nano and timeout error fix #1 * Nano and timeout error fix #2 * Nano and timeout error fix #3 * Nano and timeout error fix #4 * Nano and timeout error fix #5 * Nano and timeout error fix #6 * Nano and timeout error fix #7 (plus css changes for hide buttons) * Nano and timeout error fix #8 (zoom method) * Nano zoom fix #9 * Nano zoom fix #10 * Nano zoom fix #11 * Nano zoom fix #12 * Nano zoom fix #13 * Nano zoom fix #14 * Nano zoom fix #15 * Nano zoom fix #16 (ssh-2-promise) * Nano zoom fix #17 (ssh-2-promise port fix) * Full return back to old code with minor fixes * Terminal size fix #18 (rows & cols fix) * Test ntfy build notification system * Silent resize cmds & Auto resize terminal #1 * Silent resize cmds & Auto resize terminal #2 * Docker build update * Docker build update #2 * Docker build update #3 * Docker build update #4 (cache and cleanup) * Docker build update #5 (cache and cleanup) * Docker build update #6 (cache and cleanup) * Docker build update #7 (final) * Docker build update #8 (nevermind not finanl) * Docker build update #9 (nevermind not finanl) * Docker build update #10 * Docker build update #10 (I hope final) * Docker build update #10 (actual final) * Release 1.0!!!! * Repo clean-up * Remove files ignored by .gitignore * Change project name, create logo, change README.md to be soon updated. * Update README.md #1 (also added MIT License) * Update README.md #2 * Update README.md #3 * Update README.md #4 * Update README.md #5 * Update README.md #6 * Update README.md #7 * Update README.md * Add images * Update image name * Update README.md * Final changes to release-1.0 * Update README.md * Update name (need to fix logo) * Final updates (I hope for real this time) * Fix web-socket timeout. * Fix timeout on close.
105 lines
3.3 KiB
JavaScript
105 lines
3.3 KiB
JavaScript
const WebSocket = require('ws');
|
|
const ssh2 = require('ssh2');
|
|
const http = require('http');
|
|
|
|
const server = http.createServer((req, res) => {
|
|
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
|
res.end('WebSocket server is running\n');
|
|
});
|
|
|
|
const wss = new WebSocket.Server({ server });
|
|
|
|
wss.on('connection', (ws) => {
|
|
console.log('WebSocket connection established');
|
|
|
|
let conn = new ssh2.Client();
|
|
let stream = null;
|
|
let currentCols = 80;
|
|
let currentRows = 24;
|
|
let interval = null;
|
|
|
|
const resizeTerminal = (cols, rows) => {
|
|
if (stream && stream.setWindow) {
|
|
stream.setWindow(rows, cols, rows * 100, cols * 100); // Adjust terminal size
|
|
console.log(`Terminal resized successfully: cols=${cols}, rows=${rows}`);
|
|
}
|
|
};
|
|
|
|
ws.on('message', (message) => {
|
|
const messageStr = message.toString();
|
|
|
|
let data;
|
|
try {
|
|
if (messageStr.trim().startsWith('{')) {
|
|
data = JSON.parse(messageStr);
|
|
} else if (stream && stream.writable) {
|
|
stream.write(messageStr);
|
|
return;
|
|
}
|
|
} catch (error) {
|
|
console.error('Failed to process message:', error);
|
|
return;
|
|
}
|
|
|
|
if (data?.host && data.port && data.username && data.password) {
|
|
conn.on('ready', () => {
|
|
console.log('SSH Connection established');
|
|
|
|
interval = setInterval(() => {
|
|
if (ws.readyState === WebSocket.OPEN) {
|
|
ws.ping();
|
|
} else {
|
|
clearInterval(interval);
|
|
}
|
|
}, 15000);
|
|
|
|
conn.shell({ term: 'xterm', cols: currentCols, rows: currentRows }, (error, newStream) => {
|
|
if (error) {
|
|
console.error(`SSH Shell Error: ${error}`);
|
|
ws.send(`Error: Could not establish a shell: ${error.message}`);
|
|
return;
|
|
}
|
|
|
|
stream = newStream;
|
|
|
|
stream.on('data', (chunk) => {
|
|
ws.send(chunk.toString());
|
|
});
|
|
|
|
stream.on('close', () => {
|
|
console.log('SSH stream closed');
|
|
conn.end();
|
|
});
|
|
});
|
|
}).on('error', (err) => {
|
|
console.log('SSH Connection Error:', err);
|
|
ws.send(`SSH Connection Error: ${err.message}`);
|
|
}).connect({
|
|
host: data.host,
|
|
port: data.port,
|
|
username: data.username,
|
|
password: data.password,
|
|
keepaliveInterval: 10000,
|
|
keepaliveCountMax: 5,
|
|
});
|
|
} else if (data?.cols && data.rows) {
|
|
currentCols = data.cols;
|
|
currentRows = data.rows;
|
|
resizeTerminal(currentCols, currentRows);
|
|
}
|
|
});
|
|
|
|
ws.on('close', () => {
|
|
console.log('WebSocket closed');
|
|
if (interval) {
|
|
clearInterval(interval);
|
|
}
|
|
if (conn) {
|
|
conn.end();
|
|
}
|
|
});
|
|
});
|
|
|
|
server.listen(8081, () => {
|
|
console.log('WebSocket server is listening on ws://localhost:8081');
|
|
}); |