Nano zoom fix #16 (ssh-2-promise)

This commit is contained in:
LukeGus
2024-12-05 20:25:23 -06:00
parent 95573f7e37
commit b3e43a45af
3 changed files with 84 additions and 97 deletions

18
.idea/workspace.xml generated
View File

@@ -4,7 +4,7 @@
<option name="autoReloadType" value="SELECTIVE" />
</component>
<component name="ChangeListManager">
<list default="true" id="8497df64-d86b-4c98-ac58-c157d9d3fb1e" name="Changes" comment="Nano zoom fix #14">
<list default="true" id="8497df64-d86b-4c98-ac58-c157d9d3fb1e" name="Changes" comment="Nano zoom fix #15">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/backend/server.js" beforeDir="false" afterPath="$PROJECT_DIR$/backend/server.js" afterDir="false" />
<change beforePath="$PROJECT_DIR$/info.txt" beforeDir="false" afterPath="$PROJECT_DIR$/info.txt" afterDir="false" />
@@ -64,6 +64,7 @@
"npm.run_start_vite.executor": "Run",
"npm.start.executor": "Run",
"settings.editor.selected.configurable": "ml.llm.LLMConfigurable",
"ts.external.directory.path": "D:\\Program Files (x86)\\Applications\\Jetbrains Webstorm\\WebStorm 2024.3.1\\plugins\\javascript-plugin\\jsLanguageServicesImpl\\external",
"vue.rearranger.settings.migration": "true"
}
}]]></component>
@@ -114,7 +115,7 @@
<option name="presentableId" value="Default" />
<updated>1733439468142</updated>
<workItem from="1733439479708" duration="5489000" />
<workItem from="1733448523969" duration="2106000" />
<workItem from="1733448523969" duration="3316000" />
</task>
<task id="LOCAL-00001" summary="Nano zoom fix #11">
<option name="closed" value="true" />
@@ -164,7 +165,15 @@
<option name="project" value="LOCAL" />
<updated>1733450304640</updated>
</task>
<option name="localTasksCounter" value="7" />
<task id="LOCAL-00007" summary="Nano zoom fix #15">
<option name="closed" value="true" />
<created>1733450681262</created>
<option name="number" value="00007" />
<option name="presentableId" value="LOCAL-00007" />
<option name="project" value="LOCAL" />
<updated>1733450681262</updated>
</task>
<option name="localTasksCounter" value="8" />
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
@@ -175,6 +184,7 @@
<MESSAGE value="Nano zoom fix #12" />
<MESSAGE value="Nano zoom fix #13" />
<MESSAGE value="Nano zoom fix #14" />
<option name="LAST_COMMIT_MESSAGE" value="Nano zoom fix #14" />
<MESSAGE value="Nano zoom fix #15" />
<option name="LAST_COMMIT_MESSAGE" value="Nano zoom fix #15" />
</component>
</project>

View File

@@ -1,5 +1,5 @@
const WebSocket = require('ws');
const ssh2 = require('ssh2');
const SSH = require('ssh2-promise');
const http = require('http');
const server = http.createServer((req, res) => {
@@ -11,105 +11,81 @@ const wss = new WebSocket.Server({ server });
wss.on('connection', (ws) => {
console.log('WebSocket connection established');
let conn = null;
let termDimensions = { rows: 0, cols: 0, height: 0, width: 0 }; // Store terminal dimensions
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
});
try {
await ssh.connect();
const stream = await ssh.shell();
stream.on('data', (data) => {
const dataString = data.toString();
ws.send(dataString);
});
stream.stderr.on('data', (data) => {
const errorString = data.toString();
console.error('SSH error:', errorString);
});
ws.on('message', (message) => {
try {
const data = JSON.parse(message);
if (data.type === 'resize') {
termDimensions = data;
}
} catch (err) {
stream.write(message);
}
});
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);
}
}, 15000);
ws.on('pong', () => {
console.log('Received pong from client');
});
ws.on('message', (message) => {
try {
const data = JSON.parse(message);
if (data.host && data.username && data.password) {
if (conn) {
conn.end();
}
conn = new ssh2.Client();
conn
.on('ready', () => {
console.log('SSH Connection established');
conn.shell((err, stream) => {
if (err) {
ws.send(`Error: ${err.message}`);
return;
}
stream.on('data', (data) => {
const dataString = data.toString();
ws.send(dataString);
if (exitCode === 0) {
stream.setWindow(termDimensions.rows, termDimensions.cols, termDimensions.height, termDimensions.width);
}
});
stream.stderr.on('data', (data) => {
console.error('SSH stderr:', data.toString())
})
stream.on('close', () => {
console.log('SSH Stream closed');
conn.end();
ws.send(JSON.stringify({ type: 'process_closed' })); // Signal process has closed
});
ws.on('message', (message) => {
try {
const data = JSON.parse(message);
if (data.type === 'resize' && data.rows && data.cols) {
console.log('Resize event received:', data);
termDimensions = data; // Store received dimensions
stream.setWindow(data.rows, data.cols, data.height, data.width);
}
} catch (err) {
console.log('User Input:', message);
stream.write(message);
}
});
});
})
.on('error', (err) => {
console.log('SSH Error:', err.message);
ws.send(`SSH Error: ${err.message}`);
})
.on('close', () => {
console.log('SSH Connection closed');
})
.connect({
host: data.host,
port: 22,
username: data.username,
password: data.password,
keepaliveInterval: 20000,
keepaliveCountMax: 5,
});
}
} catch (error) {
console.log('Non-JSON message received:', message);
}
});
ws.on('close', () => {
console.log('WebSocket closed');
clearInterval(interval);
if (conn) {
conn.end();
}
});
}, 5000);
});
server.listen(8081, () => {
console.log('WebSocket server is running on ws://localhost:8081');
server.listen(8000, () => {
console.log('Server listening on port 8000');
});

View File

@@ -2,6 +2,7 @@ Currently:
Fix issue after nano where the input no longer goes down to the bottom.
Fix issue where SSH randomly disconnects
ntfy notifcation after build push
numbers dont work in ssh
Overall Features:
SSH/RDP(?)/VNC(?)