From 3b89fdef60123f209a554af34dc791f25b168c2e Mon Sep 17 00:00:00 2001 From: stickyPiston Date: Tue, 3 Sep 2019 14:58:00 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Created=20server-side=20code.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.js | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/main.js b/main.js index b4cfcbd..469e9fe 100644 --- a/main.js +++ b/main.js @@ -1,4 +1,42 @@ // Reference: https://hub.packtpub.com/making-simple-web-based-ssh-client-using-nodejs-and-socketio/ -var os = require('os'); -var pty = require('node-pty'); \ No newline at end of file +var express = require('express'); +var https = require('https'); +var http = require('http'); +var fs = require('fs'); +var pty = require('pty.js'); + +// Set up express server +let app = express(); + +app.use("/", express.static("./")); + +// Creating an HTTP server +var server = http.createServer(app).listen(9774); + +var io = require('socket.io')(server); + +// When a new socket connects +io.on('connection', function (socket) { + // Create terminal + var term = pty.spawn('sh', [], { + name: 'xterm-color', + cols: 80, + rows: 30, + 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("bye"); + }); +}); \ No newline at end of file