-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.cjs
274 lines (228 loc) · 8.45 KB
/
server.cjs
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
//@ts-check
const express = require("express");
const expressWs = require("express-ws");
const pty = require("node-pty");
const crypto = require("crypto");
const rateLimit = require("express-rate-limit").default;
const WebSocket = require("ws");
const argv = require("minimist")(process.argv.slice(2), { boolean: ["openExternal"] });
const { getOpenablePorts } = require("./out/supervisor-helper.cjs");
const { PortsStatus } = require("@gitpod/supervisor-api-grpc/lib/status_pb");
const { EventEmitter } = require("events");
const port = process.env.PORT ? parseInt(process.env.PORT, 10) : 23000;
const host = "0.0.0.0";
const config = {
reuseTerminals: false,
};
const rateLimiter = rateLimit({
windowMs: 60 * 1000,
limit: 50,
message: "Too many requests from this IP, please try again after 1 minute",
standardHeaders: true,
legacyHeaders: false,
});
function startServer() {
const app = express();
expressWs(app);
const terminals = {};
const logs = {};
app.get("/", (_req, res) => {
res.sendFile(__dirname + "/index.html");
});
app.get("/version", (_req, res) => {
res.sendFile(__dirname + "/commit.txt");
});
app.use("/dist", express.static(__dirname + "/dist"));
app.use("/assets", express.static(__dirname + "/assets"));
app.use("/src", express.static(__dirname + "/src"));
app.post("/terminals", rateLimiter, (req, res) => {
if (!req.query.cols || !req.query.rows) {
res.statusCode = 400;
res.send("`cols` and `rows` are required");
res.end();
return;
} else if (typeof req.query.cols !== "string" || typeof req.query.rows !== "string") {
res.statusCode = 400;
res.send("`cols` and `rows` must be strings");
res.end();
return;
}
const cols = parseInt(req.query.cols, 10);
const rows = parseInt(req.query.rows, 10);
if (isNaN(cols) || isNaN(rows)) {
res.statusCode = 400;
res.send("`cols` and `rows` must be parsable as integers");
res.end();
return;
}
if (config.reuseTerminals && Object.keys(terminals).length > 0) {
const term = Object.values(terminals)[0];
console.log(`Using existing terminal with PID ${term.pid}`);
res.send(term.pid.toString());
res.end();
return;
}
const env = Object.assign({}, process.env);
env["COLORTERM"] = "truecolor";
const term = pty.spawn(process.env.SHELL || "/bin/bash", [], {
name: "xterm-256color",
cols: cols || 80,
rows: rows || 24,
cwd: env.GITPOD_REPO_ROOT || env.PWD,
env,
encoding: null,
});
console.log(`Created terminal with PID: ${term.pid}`);
terminals[term.pid] = term;
logs[term.pid] = "";
term.onData((data) => {
logs[term.pid] += data;
});
term.onExit((_e) => {
delete terminals[term.pid];
console.log(`Closed terminal ${term.pid}`);
});
res.statusCode = 201; // HTTP 201 Created
res.send(term.pid.toString());
res.end();
});
app.post("/terminals/:pid/size", rateLimiter, (req, res) => {
if (!req.query.cols || !req.query.rows) {
res.statusCode = 400;
res.send("`cols` and `rows` are required");
res.end();
return;
} else if (typeof req.query.cols !== "string" || typeof req.query.rows !== "string") {
res.statusCode = 400;
res.send("`cols` and `rows` must be strings");
res.end();
return;
}
const cols = parseInt(req.query.cols, 10);
const rows = parseInt(req.query.rows, 10);
const pid = parseInt(req.params.pid);
if (isNaN(cols) || isNaN(rows) || isNaN(pid)) {
res.statusCode = 400;
res.send("`cols`, `rows` & `pid` must be parsable as integers");
res.end();
return;
}
const term = terminals[pid];
term.resize(cols, rows);
console.log(`Resized terminal ${pid} to ${cols} cols and ${rows} rows.`);
res.end();
});
const em = new EventEmitter();
app.ws("/terminals/remote-communication-channel/", (ws, _req) => {
console.info(`Client joined remote communication channel`);
ws.on("message", (msg) => {
try {
msg = JSON.parse(msg);
} catch (e) {
console.error("Invalid JSON");
return;
}
em.emit("message", msg);
console.info(`Client sent message: ${JSON.stringify(msg)}`);
});
em.on("message", (msg) => {
ws.send(JSON.stringify(msg));
});
async function sendPortUpdates() {
for await (const ports of getOpenablePorts()) {
for (const port of ports) {
if (!port.exposed || !port.exposed.url) {
continue;
}
const id = crypto.randomUUID();
if (port.onOpen === PortsStatus.OnOpenAction.NOTIFY) {
ws.send(
JSON.stringify({
action: "notifyAboutUrl",
data: { url: port.exposed.url, port: port.localPort, name: port.name },
id,
}),
);
} else {
ws.send(JSON.stringify({ action: "openUrl", data: port.exposed.url, id }));
}
}
}
}
async function init() {
if (process.env["XTERM_CONFIRM_BROWSER_EXIT"] === "true") {
ws.send(JSON.stringify({ action: "confirmExit" }));
}
sendPortUpdates();
}
init();
});
let clientForExternalMessages = null;
app.ws("/terminals/:pid", (ws, req) => {
const term = terminals[parseInt(req.params.pid)];
console.log(`Client connected to terminal ${term.pid}`);
ws.send(logs[term.pid]);
clientForExternalMessages = term.pid;
// binary message buffering
function bufferUtf8(socket, timeout) {
let buffer = [];
let sender = null;
let length = 0;
return (data) => {
buffer.push(data);
length += data.length;
if (!sender) {
sender = setTimeout(() => {
socket.send(Buffer.concat(buffer, length));
buffer = [];
sender = null;
length = 0;
}, timeout);
}
};
}
const send = bufferUtf8(ws, 5);
// WARNING: This is a naive implementation that will not throttle the flow of data. This means
// it could flood the communication channel and make the terminal unresponsive. Learn more about
// the problem and how to implement flow control at https://xtermjs.org/docs/guides/flowcontrol/
term.on("data", (data) => {
try {
send(data);
} catch (ex) {
// The WebSocket is not open, ignore
}
});
term.on("exit", (_e) => {
ws.send(`\r\nThis terminal has been closed. Refresh the page to create a new one.`);
});
ws.on("message", (msg) => {
term.write(msg);
});
ws.on("close", () => {
console.log(`Client closed terminal ${term.pid}`);
});
});
console.log(`App listening to http://127.0.0.1:${port}`);
app.listen(port, host, 511);
}
if (argv.openExternal) {
const url = argv._[0];
const { port } = argv;
if (!url) {
console.error("Please provide a URL");
process.exit(1);
}
const webSocketUrl = `ws://localhost:${port}/terminals/remote-communication-channel/`;
const ws = new WebSocket(webSocketUrl);
console.info(webSocketUrl);
ws.on("open", () => {
const id = crypto.randomUUID();
ws.send(JSON.stringify({ action: "openUrl", data: url, id }));
console.info("Sent openUrl message");
ws.close();
process.exit(0);
});
} else if (require.main === module) {
startServer();
}
module.exports = startServer;