feat: Add SSH routes and connection handler
This commit is contained in:
parent
7d80e10604
commit
2d19f49091
3 changed files with 56 additions and 50 deletions
57
app/app.js
57
app/app.js
|
@ -1,3 +1,4 @@
|
||||||
|
// server
|
||||||
// app/app.js
|
// app/app.js
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
|
@ -8,6 +9,7 @@ const path = require('path')
|
||||||
const bodyParser = require('body-parser')
|
const bodyParser = require('body-parser')
|
||||||
const config = require('./config')
|
const config = require('./config')
|
||||||
const socketHandler = require('./socket')
|
const socketHandler = require('./socket')
|
||||||
|
const sshRoutes = require('./routes')
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates and configures the Express application
|
* Creates and configures the Express application
|
||||||
|
@ -19,28 +21,15 @@ function createApp() {
|
||||||
// Resolve the correct path to the webssh2_client module
|
// Resolve the correct path to the webssh2_client module
|
||||||
const clientPath = path.resolve(__dirname, '..', 'node_modules', 'webssh2_client', 'client', 'public')
|
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
|
// Handle POST and GET parameters
|
||||||
app.use(bodyParser.urlencoded({ extended: true }))
|
app.use(bodyParser.urlencoded({ extended: true }))
|
||||||
app.use(bodyParser.json())
|
app.use(bodyParser.json())
|
||||||
|
|
||||||
// Serve client.htm with injected configuration
|
// Serve static files from the webssh2_client module
|
||||||
app.get('/ssh', (req, res) => {
|
app.use('/ssh', express.static(clientPath))
|
||||||
res.sendFile(path.join(clientPath, 'client.htm'))
|
|
||||||
})
|
// Use the SSH routes
|
||||||
|
app.use('/ssh', sshRoutes)
|
||||||
|
|
||||||
return app
|
return app
|
||||||
}
|
}
|
||||||
|
@ -86,38 +75,6 @@ function setupSocketIOListeners(io) {
|
||||||
socketHandler(io, config)
|
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
|
* Initializes and starts the server
|
||||||
* @returns {Object} An object containing the server, io, and app instances
|
* @returns {Object} An object containing the server, io, and app instances
|
||||||
|
|
31
app/connectionHandler.js
Normal file
31
app/connectionHandler.js
Normal file
|
@ -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;
|
18
app/routes.js
Normal file
18
app/routes.js
Normal file
|
@ -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;
|
Loading…
Reference in a new issue