forked from HorizenOfficial/eleos
-
Notifications
You must be signed in to change notification settings - Fork 2
/
xterm.js
108 lines (90 loc) · 2.94 KB
/
xterm.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// @flow
/*jshint esversion: 6 */
/*jslint node: true */
"use strict";
const express = require("express");
const getUserDataDir = require("./main.js").getUserDataDir();
const app = express();
const expressWs = require("express-ws")(app);
const os = require("os");
const auth = require("http-auth");
let basic = auth.basic({
realm: "eleos",
file: (getUserDataDir + "/eleos.htpasswd")
});
let terminals = {},
logs = {};
app.use(auth.connect(basic));
app.use("/resources/", express.static(__dirname + "/resources"));
app.use("/xterm/", express.static(__dirname + "/node_modules/xterm/dist"));
app.get("/", function (req, res) {
res.sendFile(__dirname + "/console.html");
});
app.get("/console.html", function (req, res) {
res.sendFile(__dirname + "/console.html");
});
app.get("/style.css", function (req, res) {
res.sendFile(__dirname + "/resources/xtermjs.css");
});
app.get("/main.js", function (req, res) {
res.sendFile(__dirname + "/resources/xtermjs.js");
});
app.get("/fetch.min.js", function (req, res) {
res.sendFile(__dirname + "/resources/fetch.min.js");
});
app.post("/terminals", function (req, res) {
const pty = require("node-pty");
let cols = parseInt(req.query.cols);
let rows = parseInt(req.query.rows);
let shell = process.platform === "win32" ? "cmd.exe" : "bash";
let command = process.platform === "win32" ? ["/k", "cd " + __dirname] : ["-c", "cd " + __dirname + " && bash"];
let term = pty.spawn(shell, command, {
name: "xterm-color",
cols: cols || 80,
rows: rows || 24,
cwd: process.env.HOME,
env: process.env
});
console.log("Created terminal with PID: " + term.pid);
terminals[term.pid] = term;
logs[term.pid] = "";
term.on("data", function (data) {
logs[term.pid] += data;
});
res.send(term.pid.toString());
res.end();
});
app.post("/terminals/:pid/size", function (req, res) {
let pid = parseInt(req.params.pid);
let cols = parseInt(req.query.cols);
let rows = parseInt(req.query.rows);
let term = terminals[pid];
term.resize(cols, rows);
console.log("Resized terminal " + pid + " to " + cols + " cols and " + rows + " rows.");
res.end();
});
app.ws("/terminals/:pid", function (ws, req) {
let term = terminals[parseInt(req.params.pid)];
console.log("Connected to terminal " + term.pid);
ws.send(logs[term.pid]);
term.on("data", function (data) {
try {
ws.send(data);
} catch (ex) {
// The WebSocket is not open, ignore
}
});
ws.on("message", function (msg) {
term.write(msg);
});
ws.on("close", function () {
term.kill();
console.log("Closed terminal " + term.pid);
// Clean things up
delete terminals[term.pid];
delete logs[term.pid];
});
});
let port = process.env.PORT || 3000;
let host = os.platform() === "win32" ? "127.0.0.1" : "0.0.0.0";
app.listen(port, host);