Terminal size fix #18 (rows & cols fix)

This commit is contained in:
LukeGus
2024-12-06 20:21:14 -06:00
parent fe09966196
commit bda3fe9b31
10 changed files with 149 additions and 59 deletions

View File

@@ -22,13 +22,21 @@ wss.on('connection', (ws) => {
const data = JSON.parse(message); // Try parsing the incoming message as JSON
// Check if message contains SSH connection details
if (data.host && data.username && data.password) {
if (data.host && data.port && data.username && data.password) {
if (conn) {
conn.end(); // Close any previous connection before starting a new one
}
conn = new ssh2.Client(); // Create a new SSH connection instance
const interval = setInterval(() => {
if (ws.readyState === WebSocket.OPEN) {
ws.ping();
} else {
clearInterval(interval);
}
}, 15000);
// When the SSH connection is ready
conn.on('ready', () => {
console.log('SSH Connection established');
@@ -41,6 +49,10 @@ wss.on('connection', (ws) => {
return;
}
// Set the terminal size dynamically based on the WebSocket message
const sttyCommand = `export TERM=xterm && stty rows ${data.rows} cols ${data.cols}`;
stream.write(sttyCommand + '\n'); // Send the stty command to set terminal size
// Handle data from SSH session
stream.on('data', (data) => {
console.log(`SSH Output: ${data}`);
@@ -64,7 +76,7 @@ wss.on('connection', (ws) => {
ws.send(`SSH Error: ${err}`);
}).connect({
host: data.host, // Host provided from the client
port: 22, // Default SSH port
port: data.port, // Default SSH port
username: data.username, // Username provided from the client
password: data.password, // Password provided from the client
keepaliveInterval: 10000, // Send a heartbeat every 10 seconds
@@ -88,6 +100,7 @@ wss.on('connection', (ws) => {
// Handle WebSocket close event
ws.on('close', () => {
console.log('WebSocket closed');
clearInterval(interval);
if (conn) {
conn.end(); // Close SSH connection when WebSocket client disconnects
}