diff --git a/index.html b/index.html index aa60f09..93b3f76 100644 --- a/index.html +++ b/index.html @@ -4,51 +4,34 @@ - webssh_plus - - - - - + bash + + + + + + + -
- -
+
\ No newline at end of file diff --git a/main.js b/main.js index 26fb5df..559a2a1 100644 --- a/main.js +++ b/main.js @@ -1,55 +1,29 @@ -// Reference: https://hub.packtpub.com/making-simple-web-based-ssh-client-using-nodejs-and-socketio/ - var express = require('express'); -var https = require('https'); -var http = require('http'); -var fs = require('fs'); var pty = require('node-pty'); -// Set port -var port = 9774; +const app = express(); +const expressWs = require('express-ws')(app); -// Set up express server -let app = express(); +// Serve static assets from ./static +app.use(express.static('./')); -// Set HTTP server root folder -app.use("/", express.static("./")); +// Instantiate shell and set up data handlers +expressWs.app.ws('/shell', (ws, req) => { + // Spawn the shell + const shell = pty.spawn('/bin/bash', [], { + name: 'xterm-color', + cwd: process.env.PWD, + env: process.env + }); + // For all shell data send it to the websocket + shell.on('data', (data) => { + ws.send(data); + }); + // For all websocket data send it to the shell + ws.on('message', (msg) => { + shell.write(msg); + }); +}); -// Creating an HTTP server -var server = http.createServer(app).listen(port); -console.log(`Listening on port ${port}`); - -var io = require('socket.io')(server); - -// When a new socket connects -io.on('connection', function (socket) { - console.log('Client connect'); - socket.emit('client_identify'); - - socket.on('client_size', function (size) { - // Create terminal - console.log(size); - console.log({ cols: Math.floor(size.width / 9), rows: Math.floor(size.height / 10) }) - - var term = pty.spawn('sh', [], { - name: 'xterm-color', - cols: Math.floor(size.width / 9), - rows: Math.floor(size.height / 10), - cwd: process.env.HOME, - env: process.env - }); - // Listen on the terminal for output and send it to the client - term.on('data', function (data) { - socket.emit('output', data); - }); - // Listen on the client and send any input to the terminal - socket.on('input', function (data) { - term.write(data); - }); - // When socket disconnects, destroy the terminal - socket.on("disconnect", function () { - term.destroy(); - console.log("Client disconnect"); - }); - }) -}); \ No newline at end of file +// Start the application +app.listen(9774); \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 0a28f26..bc4e1f0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -269,6 +269,24 @@ "vary": "~1.1.2" } }, + "express-ws": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/express-ws/-/express-ws-4.0.0.tgz", + "integrity": "sha512-KEyUw8AwRET2iFjFsI1EJQrJ/fHeGiJtgpYgEWG3yDv4l/To/m3a2GaYfeGyB3lsWdvbesjF5XCMx+SVBgAAYw==", + "requires": { + "ws": "^5.2.0" + }, + "dependencies": { + "ws": { + "version": "5.2.2", + "resolved": "https://registry.npmjs.org/ws/-/ws-5.2.2.tgz", + "integrity": "sha512-jaHFD6PFv6UgoIVda6qZllptQsMlDEJkTQcybzzXDYM1XO9Y8em691FGMPmM46WGyLU4z9KMgQN+qrux/nhlHA==", + "requires": { + "async-limiter": "~1.0.0" + } + } + } + }, "finalhandler": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/finalhandler/-/finalhandler-1.1.2.tgz", diff --git a/package.json b/package.json index 0fd44ba..34474e3 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "license": "UNLICENSED", "dependencies": { "express": "^4.17.1", + "express-ws": "^4.0.0", "node-pty": "^0.8.1", "socket.io": "^2.2.0", "xterm": "^3.14.5"