2019-09-03 14:42:56 +02:00
|
|
|
// Reference: https://hub.packtpub.com/making-simple-web-based-ssh-client-using-nodejs-and-socketio/
|
|
|
|
|
2019-09-03 14:58:00 +02:00
|
|
|
var express = require('express');
|
|
|
|
var https = require('https');
|
|
|
|
var http = require('http');
|
|
|
|
var fs = require('fs');
|
2019-09-03 15:04:18 +02:00
|
|
|
var pty = require('node-pty');
|
2019-09-03 14:58:00 +02:00
|
|
|
|
2019-09-03 15:31:27 +02:00
|
|
|
// Set port
|
|
|
|
var port = 9774;
|
|
|
|
|
2019-09-03 14:58:00 +02:00
|
|
|
// Set up express server
|
|
|
|
let app = express();
|
|
|
|
|
2019-09-03 15:31:27 +02:00
|
|
|
// Set HTTP server root folder
|
2019-09-03 14:58:00 +02:00
|
|
|
app.use("/", express.static("./"));
|
|
|
|
|
|
|
|
// Creating an HTTP server
|
2019-09-03 15:31:27 +02:00
|
|
|
var server = http.createServer(app).listen(port);
|
|
|
|
console.log(`Listening on port ${port}`);
|
2019-09-03 14:58:00 +02:00
|
|
|
|
|
|
|
var io = require('socket.io')(server);
|
|
|
|
|
|
|
|
// When a new socket connects
|
|
|
|
io.on('connection', function (socket) {
|
2019-09-03 15:31:27 +02:00
|
|
|
console.log('Client connect');
|
2019-09-03 16:11:23 +02:00
|
|
|
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");
|
|
|
|
});
|
|
|
|
})
|
2019-09-03 14:58:00 +02:00
|
|
|
});
|