From 2d19f49091ab8330bffd314e3f617e643423f3d7 Mon Sep 17 00:00:00 2001 From: Bill Church Date: Thu, 18 Jul 2024 14:59:03 +0000 Subject: [PATCH] feat: Add SSH routes and connection handler --- app/app.js | 57 +++++----------------------------------- app/connectionHandler.js | 31 ++++++++++++++++++++++ app/routes.js | 18 +++++++++++++ 3 files changed, 56 insertions(+), 50 deletions(-) create mode 100644 app/connectionHandler.js create mode 100644 app/routes.js diff --git a/app/app.js b/app/app.js index 04d9dd1..6d13b67 100644 --- a/app/app.js +++ b/app/app.js @@ -1,3 +1,4 @@ +// server // app/app.js 'use strict' @@ -8,6 +9,7 @@ const path = require('path') const bodyParser = require('body-parser') const config = require('./config') const socketHandler = require('./socket') +const sshRoutes = require('./routes') /** * Creates and configures the Express application @@ -19,28 +21,15 @@ function createApp() { // Resolve the correct path to the webssh2_client module const clientPath = path.resolve(__dirname, '..', 'node_modules', 'webssh2_client', 'client', 'public') - // Middleware to inject configuration - app.use('/ssh', (req, res, next) => { - res.locals.webssh2Config = { - socket: { - url: `${req.protocol}://${req.get('host')}`, - path: '/ssh/socket.io' - } - } - next() - }) - - // Serve static files from the webssh2_client module - app.use('/ssh', express.static(clientPath)) - // Handle POST and GET parameters app.use(bodyParser.urlencoded({ extended: true })) app.use(bodyParser.json()) - // Serve client.htm with injected configuration - app.get('/ssh', (req, res) => { - res.sendFile(path.join(clientPath, 'client.htm')) - }) + // Serve static files from the webssh2_client module + app.use('/ssh', express.static(clientPath)) + + // Use the SSH routes + app.use('/ssh', sshRoutes) return app } @@ -86,38 +75,6 @@ function setupSocketIOListeners(io) { socketHandler(io, config) } -/** - * Handles a new Socket.IO connection - * @param {import('socket.io').Socket} socket - The Socket.IO socket - */ -function handleConnection(socket) { - logNewConnection(socket) - setupDisconnectListener(socket) -} - -/** - * Logs information about a new connection - * @param {import('socket.io').Socket} socket - The Socket.IO socket - */ -function logNewConnection(socket) { - console.log( - 'New connection:', - socket.id, - 'Transport:', - socket.conn.transport.name - ) -} - -/** - * Sets up the disconnect listener for a socket - * @param {import('socket.io').Socket} socket - The Socket.IO socket - */ -function setupDisconnectListener(socket) { - socket.on('disconnect', (reason) => { - console.log('Client disconnected:', socket.id, reason) - }) -} - /** * Initializes and starts the server * @returns {Object} An object containing the server, io, and app instances diff --git a/app/connectionHandler.js b/app/connectionHandler.js new file mode 100644 index 0000000..23c4eb5 --- /dev/null +++ b/app/connectionHandler.js @@ -0,0 +1,31 @@ +// server +// app/connectionHandler.js +var path = require('path'); +var extend = require('util')._extend; + +function handleConnection(req, res, urlParams) { + urlParams = urlParams || {}; + + // The path to the client directory of the webssh2 module. + var clientPath = path.resolve(__dirname, '..', 'node_modules', 'webssh2_client', 'client', 'public'); + + // Combine URL parameters, query parameters, and form data + var connectionParams = extend({}, urlParams); + extend(connectionParams, req.query); + extend(connectionParams, req.body || {}); + + // Inject configuration + res.locals.webssh2Config = { + socket: { + url: req.protocol + '://' + req.get('host'), + path: '/ssh/socket.io' + } + }; + + // You can process connectionParams here or pass them to the client + + // Serve the client HTML + res.sendFile(path.join(clientPath, 'client.htm')); +} + +module.exports = handleConnection; \ No newline at end of file diff --git a/app/routes.js b/app/routes.js new file mode 100644 index 0000000..7406883 --- /dev/null +++ b/app/routes.js @@ -0,0 +1,18 @@ +// server +// /app/routes.js +const express = require('express'); +const path = require('path'); +const router = express.Router(); +const handleConnection = require('./connectionHandler'); + +// Route for host in URL +router.get('/host/:host', (req, res) => { + handleConnection(req, res, { host: req.params.host }); +}); + +// Default route +router.get('/', (req, res) => { + handleConnection(req, res); +}); + +module.exports = router; \ No newline at end of file