-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathControlServer.js
56 lines (48 loc) · 1.69 KB
/
ControlServer.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
const net = require("net");
const WebSocket = require("ws");
/**
* Handles control of the remote desktop by proxying a VNC connection.
*/
module.exports = class ControlServer {
constructor(controlPath) {
this.controlPath = controlPath;
this.vncHost = process.env.CONTROL_SERVER_VNC_HOST || "localhost";
this.vncPort = process.env.CONTROL_SERVER_VNC_PORT || "5900";
}
start() {
this.proxy = this.createProxy();
}
createProxy() {
const proxy = new WebSocket.Server({
noServer: true,
path: this.controlPath
});
proxy.on("connection", client => {
if (this.client) {
console.log("Existing control connection, closing it");
const goingAway = 1001;
this.client.close(goingAway, "Breaking for new connection");
}
client.on("close", () => console.log("control connection closed"));
client.on("error", err => console.log("control connection err", err));
const clientStream = WebSocket.createWebSocketStream(client),
vncStream = net.createConnection(this.vncPort, this.vncHost);
clientStream.on("error", err => console.log("clientStream err", err));
vncStream.on("error", err => console.log("vncStream err", err));
clientStream.pipe(vncStream);
vncStream.pipe(clientStream);
console.log("control connection connected");
this.client = client;
});
proxy.on("close", () => console.log("ControlServer proxy closed"));
return proxy;
}
shouldHandle(req) {
return this.proxy.shouldHandle(req);
}
handleUpgrade(req, socket, head) {
this.proxy.handleUpgrade(req, socket, head, connection => {
this.proxy.emit("connection", connection, req);
});
}
};