annotate webtty @ 200:a7cc38a0168d

Actually restart RLGWebD if it crashes. Also, rlgwebd-stop will no longer crash if RLGWebD has already stopped.
author John "Elwin" Edwards
date Sat, 23 Apr 2016 18:53:08 -0400
parents 3bdee6371c3f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1 #!/usr/bin/env node
137
f14e92f6d955 webtty: don't hardcode paths.
John "Elwin" Edwards
parents: 86
diff changeset
2
f14e92f6d955 webtty: don't hardcode paths.
John "Elwin" Edwards
parents: 86
diff changeset
3 var localModules = '/usr/lib/node_modules/';
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
4 var http = require('http');
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
5 var url = require('url');
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
6 var path = require('path');
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
7 var fs = require('fs');
137
f14e92f6d955 webtty: don't hardcode paths.
John "Elwin" Edwards
parents: 86
diff changeset
8 var pty = require(path.join(localModules, "pty.js"));
140
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
9 var child_process = require("child_process");
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
10 var webSocketServer = require(path.join(localModules, "websocket")).server;
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
11
137
f14e92f6d955 webtty: don't hardcode paths.
John "Elwin" Edwards
parents: 86
diff changeset
12 var serveStaticRoot = fs.realpathSync(".");
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
13 var sessions = {};
153
c4a32007d2dc WebTTY: remove polling.
John "Elwin" Edwards
parents: 140
diff changeset
14 var nsessid = 0;
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
15
2
98bf7c94c954 webtty.js: set up the environment and working directory.
John "Elwin" Edwards <elwin@sdf.org>
parents: 0
diff changeset
16 var env_dontuse = {"TMUX": true, "TMUX_PANE": true};
98bf7c94c954 webtty.js: set up the environment and working directory.
John "Elwin" Edwards <elwin@sdf.org>
parents: 0
diff changeset
17
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
18 /* Constructor for TermSessions. Note that it opens the terminal and
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
19 * adds itself to the sessions dict.
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
20 */
140
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
21 function TermSessionWS(conn, h, w) {
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
22 var ss = this;
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
23 /* Set up the sizes. */
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
24 w = Math.floor(Number(w));
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
25 if (!(w > 0 && w < 256))
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
26 w = 80;
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
27 this.w = w;
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
28 h = Math.floor(Number(h));
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
29 if (!(h > 0 && h < 256))
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
30 h = 25;
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
31 this.h = h;
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
32 this.conn = conn;
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
33 /* Customize the environment. */
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
34 var childenv = {};
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
35 for (var key in process.env) {
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
36 if (!(key in env_dontuse))
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
37 childenv[key] = process.env[key];
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
38 }
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
39 var spawnopts = {"env": childenv, "cwd": process.env["HOME"],
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
40 "rows": this.h, "cols": this.w};
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
41 this.term = pty.spawn("bash", [], spawnopts);
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
42 this.alive = true;
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
43 this.term.on("data", function (datastr) {
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
44 var buf = new Buffer(datastr);
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
45 if (ss.conn.connected)
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
46 ss.conn.sendUTF(JSON.stringify({"t": "d", "d": buf.toString("hex")}));
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
47 });
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
48 this.term.on("exit", function () {
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
49 ss.alive = false;
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
50 /* Wait for all the data to get collected */
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
51 setTimeout(ss.cleanup, 1000);
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
52 });
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
53 this.conn.on("message", function (msg) {
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
54 try {
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
55 var msgObj = JSON.parse(msg.utf8Data);
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
56 }
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
57 catch (e) {
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
58 return;
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
59 }
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
60 if (msgObj.t == "d") {
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
61 var hexstr = msgObj["d"].replace(/[^0-9a-f]/gi, "");
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
62 if (hexstr.length % 2 != 0) {
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
63 return;
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
64 }
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
65 var keybuf = new Buffer(hexstr, "hex");
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
66 ss.term.write(keybuf);
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
67 }
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
68 });
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
69 this.conn.on("close", function (msg) {
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
70 if (ss.alive)
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
71 ss.term.kill('SIGHUP');
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
72 console.log("WebSocket connection closed.");
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
73 });
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
74 this.cleanup = function () {
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
75 /* Call this when the child is dead. */
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
76 if (ss.alive)
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
77 return;
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
78 if (ss.conn.connected) {
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
79 ss.conn.sendUTF(JSON.stringify({"t": "q"}));
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
80 }
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
81 };
153
c4a32007d2dc WebTTY: remove polling.
John "Elwin" Edwards
parents: 140
diff changeset
82 sessions[nsessid++] = this;
140
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
83 this.conn.sendUTF(JSON.stringify({"t": "l", "w": w, "h": h}));
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
84 console.log("New WebSocket connection.");
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
85 }
789c094675f4 WebTTY: use WebSockets when possible.
John "Elwin" Edwards
parents: 137
diff changeset
86
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
87 function randkey() {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
88 rnum = Math.floor(Math.random() * 65536 * 65536);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
89 hexstr = rnum.toString(16);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
90 while (hexstr.length < 8)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset