forked from butlerx/wetty
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
56 lines (48 loc) · 1.37 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
var express = require("express");
var http = require("http");
var path = require("path");
var server = require("socket.io");
var pty = require("pty.js");
var opts = require("optimist")
.options({
port: {
demand: true,
alias: "p",
description: "Dockshell listen port"
},
}).boolean("allow_discovery").argv;
process.on("uncaughtException", function(e) {
console.error("Error: " + e);
});
var app = express();
app.use("/", express.static(path.join(__dirname, "public")));
var httpserv = http.createServer(app).listen(opts.port, function() {
console.log("Serving dockshell on port " + opts.port);
});
var io = server(httpserv, {
path: "/dockshell/socket.io",
pingTimeout: 5000
});
io.on("connection", function(socket){
var term = pty.spawn("/bin/bash", [], {
name: "xterm-256color",
cols: 80,
rows: 30
});
console.log(`Started new session with PID: ${term.pid}`);
term.on("data", function(data) {
socket.emit("output", data);
});
term.on("exit", function(code) {
console.log(`Session with PID ${term.pid} ended`);
});
socket.on("resize", function(data) {
term.resize(data.col, data.row);
});
socket.on("input", function(data) {
term.write(data);
});
socket.on("disconnect", function() {
term.end();
});
})