Initial Commit

This commit is contained in:
LukeGus
2024-12-04 21:04:46 -06:00
commit b6a3f881a8
36 changed files with 16998 additions and 0 deletions

7
backend/entrypoint.sh Normal file
View File

@@ -0,0 +1,7 @@
#!/bin/sh
# Start the backend server
node /backend/server.js &
# Start nginx in the foreground
exec nginx -g 'daemon off;'

7986
backend/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

58
backend/package.json Normal file
View File

@@ -0,0 +1,58 @@
{
"name": "codespaces-react",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^14.4.3",
"express": "^4.21.1",
"guacamole-common-js": "^1.5.0",
"http-proxy-middleware": "^3.0.3",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"ssh2": "^1.16.0",
"web-vitals": "^3.1.0",
"ws": "^8.18.0",
"xterm": "^5.3.0",
"xterm-addon-fit": "^0.8.0"
},
"overrides": {
"@svgr/webpack": "^8.0.1",
"@adobe/css-tools": "^4.3.1",
"postcss": "^8.4.31"
},
"scripts": {
"start-vite": "cross-env BROWSER=none WDS_SOCKET_PORT=0 vite --port 8080",
"start-server": "node 'D:/Programming Projects/SSH-Project/ssh-project/backend/server.js'",
"start": "npm run start-vite && npm run start-server",
"build": "vite build",
"preview": "vite preview",
"test": "vitest"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"@vitejs/plugin-react": "^4.1.1",
"cross-env": "^7.0.3",
"jsdom": "^22.1.0",
"vite": "^4.5.5",
"vitest": "^0.34.6"
}
}

98
backend/server.js Normal file
View File

@@ -0,0 +1,98 @@
const WebSocket = require('ws');
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');
});
// 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');
let conn = null; // Declare SSH client outside to manage lifecycle
ws.on('message', (message) => {
try {
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 (conn) {
conn.end(); // Close any previous connection before starting a new one
}
conn = new ssh2.Client(); // Create a new SSH connection instance
// 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
});
}
} 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.');
}
}
});
// 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('WebSocket server is listening on ws://localhost:8081');
});