Full return back to old code with minor fixes
This commit is contained in:
@@ -1,91 +1,100 @@
|
||||
const WebSocket = require('ws');
|
||||
const SSH = require('ssh2-promise');
|
||||
const ssh2 = require('ssh2');
|
||||
const http = require('http');
|
||||
|
||||
// Create an HTTP server to serve WebSocket connections
|
||||
const server = http.createServer((req, res) => {
|
||||
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
||||
res.end('WebSocket server is running\n');
|
||||
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
||||
res.end('WebSocket server is running\n');
|
||||
});
|
||||
|
||||
// Create a WebSocket server attached to the HTTP server
|
||||
const wss = new WebSocket.Server({ server });
|
||||
|
||||
// WebSocket connection handling
|
||||
wss.on('connection', (ws) => {
|
||||
console.log('WebSocket connection established');
|
||||
console.log('WebSocket connection established');
|
||||
|
||||
let ssh = null;
|
||||
let termDimensions = { rows: 0, cols: 0, height: 0, weight: 0 };
|
||||
|
||||
ws.on('message', async (message) => {
|
||||
try {
|
||||
const data = JSON.parse(message);
|
||||
if (data.type === 'resize') {
|
||||
termDimensions = data;
|
||||
} else if (data.username && data.password) {
|
||||
ssh = new SSH({
|
||||
host: data.host,
|
||||
username: data.username,
|
||||
password: data.password
|
||||
});
|
||||
let conn = null; // Declare SSH client outside to manage lifecycle
|
||||
|
||||
ws.on('message', (message) => {
|
||||
try {
|
||||
await ssh.connect();
|
||||
const stream = await ssh.shell();
|
||||
const data = JSON.parse(message); // Try parsing the incoming message as JSON
|
||||
|
||||
stream.on('data', (data) => {
|
||||
const dataString = data.toString();
|
||||
ws.send(dataString);
|
||||
});
|
||||
// Check if message contains SSH connection details
|
||||
if (data.host && data.username && data.password) {
|
||||
if (conn) {
|
||||
conn.end(); // Close any previous connection before starting a new one
|
||||
}
|
||||
|
||||
stream.stderr.on('data', (data) => {
|
||||
const errorString = data.toString();
|
||||
console.error('SSH error:', errorString);
|
||||
});
|
||||
conn = new ssh2.Client(); // Create a new SSH connection instance
|
||||
|
||||
ws.on('message', (message) => {
|
||||
try {
|
||||
const data = JSON.parse(message);
|
||||
if (data.type === 'resize') {
|
||||
termDimensions = data;
|
||||
}
|
||||
} catch (err) {
|
||||
stream.write(message);
|
||||
// When the SSH connection is ready
|
||||
conn.on('ready', () => {
|
||||
console.log('SSH Connection established');
|
||||
|
||||
// Start an interactive shell session
|
||||
conn.shell((err, stream) => {
|
||||
if (err) {
|
||||
console.log(`SSH Error: ${err}`);
|
||||
ws.send(`Error: ${err}`);
|
||||
return;
|
||||
}
|
||||
|
||||
// Handle data from SSH session
|
||||
stream.on('data', (data) => {
|
||||
console.log(`SSH Output: ${data}`);
|
||||
ws.send(data.toString()); // Send the SSH output back to WebSocket client
|
||||
});
|
||||
|
||||
// Handle stream close event
|
||||
stream.on('close', () => {
|
||||
console.log('SSH stream closed');
|
||||
conn.end();
|
||||
});
|
||||
|
||||
// When the WebSocket client sends a message (from terminal input), forward it to the SSH stream
|
||||
ws.on('message', (message) => {
|
||||
console.log(`Received message from WebSocket: ${message}`);
|
||||
stream.write(message); // Write the message (input) to the SSH shell
|
||||
});
|
||||
});
|
||||
}).on('error', (err) => {
|
||||
console.log('SSH Connection Error: ', err);
|
||||
ws.send(`SSH Error: ${err}`);
|
||||
}).connect({
|
||||
host: data.host, // Host provided from the client
|
||||
port: 22, // 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
|
||||
keepaliveCountMax: 5, // Allow three missed heartbeats before considering the connection dead
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
// If message is not valid JSON (i.e., terminal input), treat it as raw text and send it to SSH
|
||||
console.log('Received non-JSON message, sending to SSH session:', message);
|
||||
if (conn) {
|
||||
const stream = conn._stream; // Access the SSH stream directly
|
||||
if (stream && stream.writable) {
|
||||
stream.write(message); // Write raw input message to SSH stream
|
||||
}
|
||||
} else {
|
||||
console.error('SSH connection is not established yet.');
|
||||
}
|
||||
});
|
||||
|
||||
stream.on('close', () => {
|
||||
console.log('Stream closed');
|
||||
ssh.close();
|
||||
});
|
||||
} catch (err) {
|
||||
console.error('SSH connection error:', err);
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Message processing error:', err);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
ws.on('close', () => {
|
||||
console.log('WebSocket closed');
|
||||
if (ssh) {
|
||||
ssh.close();
|
||||
}
|
||||
});
|
||||
|
||||
ws.on('error', (err) => {
|
||||
console.error('WebSocket error:', err);
|
||||
});
|
||||
|
||||
// Ping-pong is used to keep the connection alive.
|
||||
const interval = setInterval(() => {
|
||||
if (ws.readyState === WebSocket.OPEN) {
|
||||
ws.ping();
|
||||
} else {
|
||||
clearInterval(interval);
|
||||
}
|
||||
}, 5000);
|
||||
// Handle WebSocket close event
|
||||
ws.on('close', () => {
|
||||
console.log('WebSocket closed');
|
||||
if (conn) {
|
||||
conn.end(); // Close SSH connection when WebSocket client disconnects
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Start the WebSocket server on port 8081
|
||||
server.listen(8081, () => {
|
||||
console.log('Server listening on port 8081');
|
||||
console.log('WebSocket server is listening on ws://localhost:8081');
|
||||
});
|
||||
Reference in New Issue
Block a user