annotate rlgwebd.js @ 17:d3e3d6b4016b

rlgwebd: switch to dgamelaunch's SQLite database. The quickrypt utility is replaced with sqlickrypt, which takes a username and password pair and checks them against the SQLite password database used by dgamelaunch. This will be more extensible to using rlgwebd to register, change passwords, etc.
author John "Elwin" Edwards <elwin@sdf.org>
date Sun, 20 May 2012 15:52:07 -0700
parents ef6127ed6da3
children 188bbd857124
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
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
2
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
3 // If you can't quite trust node to find it on its own
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
4 var localModules = '/usr/local/lib/node_modules/';
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
5 var http = require('http');
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
6 var url = require('url');
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
7 var path = require('path');
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
8 var fs = require('fs');
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
9 var child_process = require('child_process');
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
10 var daemon = require(path.join(localModules, "daemon"));
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
11
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
12 var chrootDir = "/var/dgl/";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
13 var dropToUID = 501;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
14 var dropToGID = 501;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
15 var serveStaticRoot = "/var/www/"; // inside the chroot
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
16 var passwdfile = "/dgldir/dgl-login";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
17 var sessions = {};
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
18
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
19 var games = {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
20 "rogue3": {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
21 "name": "Rogue V3",
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
22 "uname": "rogue3",
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
23 "path": "/bin/rogue3"
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
24 },
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
25 "rogue4": {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
26 "name": "Rogue V4",
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
27 "uname": "rogue4",
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
28 "path": "/bin/rogue4"
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
29 },
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
30 "rogue5": {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
31 "name": "Rogue V5",
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
32 "uname": "rogue5",
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
33 "path": "/bin/rogue5"
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
34 },
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
35 "srogue": {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
36 "name": "Super-Rogue",
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
37 "uname": "srogue",
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
38 "path": "/bin/srogue"
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
39 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
40 };
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
41
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
42 /* 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
43 * adds itself to the sessions dict. It currently assumes the user has
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
44 * been authenticated.
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
45 */
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
46 function TermSession(game, user, files, dims) {
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
47 /* First make sure starting the game will work. */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
48 if (!(game in games)) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
49 // TODO: throw an exception instead
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
50 return null;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
51 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
52 /* This order seems to best avoid race conditions... */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
53 this.alive = false;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
54 this.sessid = randkey();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
55 while (this.sessid in sessions) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
56 this.sessid = randkey();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
57 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
58 /* Grab a spot in the sessions table. */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
59 sessions[this.sessid] = this;
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
60 /* State for messaging. */
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
61 this.nsend = 0;
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
62 this.nrecv = 0;
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
63 this.msgQ = []
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
64 /* Set up the sizes. */
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
65 this.w = Math.floor(Number(dims[1]));
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
66 if (!(this.w > 0 && this.w < 256))
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
67 this.w = 80;
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
68 this.h = Math.floor(Number(dims[0]));
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
69 if (!(this.h > 0 && this.h < 256))
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
70 this.h = 24;
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
71 /* Environment. */
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
72 var childenv = {};
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
73 for (var key in process.env) {
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
74 childenv[key] = process.env[key];
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
75 }
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
76 childenv["PTYHELPER"] = String(this.h) + "x" + String(this.w);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
77 /* TODO handle tty-opening errors */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
78 /* TODO make argument-finding into a method */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
79 args = [games[game].path, "-n", user.toString()];
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
80 this.child = child_process.spawn("/bin/ptyhelper", args, {"env": childenv});
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
81 var ss = this;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
82 this.alive = true;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
83 this.data = [];
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
84 this.lock = files[0];
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
85 fs.writeFile(this.lock, this.child.pid.toString() + '\n' + this.w + '\n' +
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
86 this.h + '\n', "utf8");
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
87 this.record = fs.createWriteStream(files[1], { mode: 0664 });
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
88 /* END setup */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
89 function ttyrec_chunk(buf) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
90 var ts = new Date();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
91 var chunk = new Buffer(buf.length + 12);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
92 /* TTYREC headers */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
93 chunk.writeUInt32LE(Math.floor(ts.getTime() / 1000), 0);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
94 chunk.writeUInt32LE(1000 * (ts.getTime() % 1000), 4);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
95 chunk.writeUInt32LE(buf.length, 8);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
96 buf.copy(chunk, 12);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
97 ss.data.push(chunk);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
98 ss.record.write(chunk);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
99 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
100 this.child.stdout.on("data", ttyrec_chunk);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
101 this.child.stderr.on("data", ttyrec_chunk);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
102 this.child.on("exit", function (code, signal) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
103 ss.exitcode = (code != null ? code : 255);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
104 ss.alive = false;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
105 fs.unlink(ss.lock);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
106 /* Wait for all the data to get collected */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
107 setTimeout(ss.cleanup, 1000);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
108 });
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
109 this.write = function (data) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
110 if (this.alive)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
111 this.child.stdin.write(data);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
112 /* Otherwise, throw some kind of exception? */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
113 };
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
114 this.read = function () {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
115 if (this.data.length == 0)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
116 return null;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
117 var pos = 0;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
118 var i = 0;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
119 for (i = 0; i < this.data.length; i++)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
120 pos += this.data[i].length - 12;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
121 var nbuf = new Buffer(pos);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
122 var tptr;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
123 pos = 0;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
124 while (this.data.length > 0) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
125 tptr = this.data.shift();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
126 tptr.copy(nbuf, pos, 12);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
127 pos += tptr.length - 12;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
128 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
129 return nbuf;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
130 };
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
131 this.close = function () {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
132 if (this.alive)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
133 this.child.kill('SIGHUP');
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
134 };
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
135 this.cleanup = function () {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
136 /* Call this when the child is dead. */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
137 if (this.alive)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
138 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
139 ss.record.end();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
140 /* Give the client a chance to read any leftover data. */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
141 if (ss.data.length > 0)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
142 setTimeout(ss.remove, 8000);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
143 else
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
144 ss.remove();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
145 };
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
146 this.remove = function () {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
147 delete sessions[ss.sessid];
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
148 console.log("Session " + this.sessid + " removed.");
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
149 };
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
150 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
151
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
152 /* A few utility functions */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
153 function timestamp() {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
154 dd = new Date();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
155 sd = dd.toISOString();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
156 sd = sd.slice(0, sd.indexOf("."));
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
157 return sd.replace("T", ".");
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
158 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
159
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
160 function randkey() {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
161 rnum = Math.floor(Math.random() * 65536 * 65536);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
162 hexstr = rnum.toString(16);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
163 while (hexstr.length < 8)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
164 hexstr = "0" + hexstr;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
165 return hexstr;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
166 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
167
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
168 /* Returns a list of the cookies in the request, obviously. */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
169 function getCookies(req) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
170 cookies = [];
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
171 if ("cookie" in req.headers) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
172 cookstrs = req.headers["cookie"].split("; ");
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
173 for (var i = 0; i < cookstrs.length; i++) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
174 eqsign = cookstrs[i].indexOf("=");
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
175 if (eqsign > 0) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
176 name = cookstrs[i].slice(0, eqsign).toLowerCase();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
177 val = cookstrs[i].slice(eqsign + 1);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
178 cookies[name] = val;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
179 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
180 else if (eqsign < 0)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
181 cookies[cookstrs[i]] = null;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
182 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
183 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
184 return cookies;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
185 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
186
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
187 function urlDec(encstr) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
188 var decstr = "";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
189 var tnum;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
190 for (var i = 0; i < encstr.length; i++)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
191 {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
192 if (encstr.charAt(i) == "+")
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
193 decstr += " ";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
194 else if (encstr.charAt(i) == "%")
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
195 {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
196 tnum = Number("0x" + encstr.slice(i + 1, 2));
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
197 if (!isNaN(tnum) && tnum >= 0)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
198 decstr += String.fromCharCode(tnum);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
199 i += 2;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
200 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
201 else
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
202 decstr += encstr.charAt(i);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
203 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
204 return decstr;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
205 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
206
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
207 /* Returns the contents of a form */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
208 function getFormValues(formtext) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
209 var pairstrs = formtext.split("&");
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
210 var data = {};
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
211 for (var i = 0; i < pairstrs.length; i++)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
212 {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
213 var eqsign = pairstrs[i].indexOf("=");
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
214 if (eqsign > 0) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
215 rawname = pairstrs[i].slice(0, eqsign);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
216 rawval = pairstrs[i].slice(eqsign + 1);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
217 name = urlDec(rawname);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
218 val = urlDec(rawval);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
219 if (!(name in data))
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
220 data[name] = [];
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
221 data[name].push(val);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
222 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
223 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
224 return data;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
225 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
226
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
227 function getMsg(posttext) {
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
228 var jsonobj;
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
229 if (!posttext)
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
230 return {};
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
231 try {
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
232 jsonobj = JSON.parse(posttext);
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
233 }
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
234 catch (e) {
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
235 if (e instanceof SyntaxError)
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
236 return {};
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
237 }
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
238 if (typeof(jsonobj) != "object")
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
239 return {};
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
240 return jsonobj;
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
241 }
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
242
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
243 function auth(username, password) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
244 // Real authentication not implemented
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
245 return true;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
246 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
247
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
248 function login(req, res, formdata) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
249 if (!("game" in formdata)) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
250 sendError(res, 2, "No game specified.");
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
251 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
252 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
253 else if (!("name" in formdata)) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
254 sendError(res, 2, "Username not given.");
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
255 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
256 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
257 else if (!("pw" in formdata)) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
258 sendError(res, 2, "Password not given.");
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
259 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
260 }
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
261 var username = formdata["name"];
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
262 var password = formdata["pw"];
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
263 var gname = formdata["game"];
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
264 var dims = [formdata["h"], formdata["w"]];
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
265 if (!(gname in games)) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
266 sendError(res, 2, "No such game: " + gname);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
267 console.log("Request for nonexistant game \"" + gname + "\"");
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
268 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
269 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
270 var progressdir = "/dgldir/inprogress-" + games[gname].uname;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
271
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
272 // This sets up the game once starting is approved.
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
273 function startgame() {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
274 var ts = timestamp();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
275 var lockfile = path.join(progressdir, username + ":node:" + ts + ".ttyrec");
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
276 var ttyrec = path.join("/dgldir/ttyrec", username, gname, ts + ".ttyrec");
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
277 var nsession = new TermSession(gname, username, [lockfile, ttyrec], dims);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
278 if (nsession) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
279 /* Technically there's a race condition for the "lock"file, but since
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
280 * it requires the user deliberately starting two games at similar times,
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
281 * it's not too serious. We can't get O_EXCL in Node anyway. */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
282 res.writeHead(200, {'Content-Type': 'text/plain'});
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
283 var reply = {"t": "l", "id": nsession.sessid, "w": nsession.w, "h":
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
284 nsession.h};
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
285 res.write(JSON.stringify(reply));
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
286 res.end();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
287 console.log("%s playing %s (key %s, pid %d)", username, gname,
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
288 nsession.sessid, nsession.child.pid);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
289 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
290 else {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
291 sendError(res, 5, "Failed to open TTY");
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
292 console.log("Unable to allocate TTY for " + gname);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
293 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
294 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
295 function checkit(code, signal) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
296 // check the password
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
297 if (code != 0) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
298 sendError(res, 3);
17
d3e3d6b4016b rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
299 if (code == 1)
d3e3d6b4016b rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
300 console.log("Password check failed for user " + username);
d3e3d6b4016b rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
301 else if (code == 2)
d3e3d6b4016b rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
302 console.log("Attempted login by nonexistent user " + username);
d3e3d6b4016b rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
303 else
d3e3d6b4016b rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
304 console.log("Login failed: sqlickrypt error " + code);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
305 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
306 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
307 // check for an existing game
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
308 fs.readdir(progressdir, function(err, files) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
309 if (!err) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
310 var fre = RegExp("^" + username + ":");
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
311 for (var i = 0; i < files.length; i++) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
312 if (files[i].match(fre)) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
313 sendError(res, 4, null);
17
d3e3d6b4016b rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
314 console.log(username + " is already playing " + gname);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
315 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
316 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
317 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
318 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
319 // If progressdir isn't readable, start a new game anyway.
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
320 startgame();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
321 });
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
322 }
17
d3e3d6b4016b rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
323 /* Launch the sqlickrypt utility to check the password. */
d3e3d6b4016b rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
324 var checker = require('child_process').spawn("/bin/sqlickrypt");
d3e3d6b4016b rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
325 checker.on("exit", checkit);
d3e3d6b4016b rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
326 checker.stdin.end(username + '\n' + password + '\n', "utf8");
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
327 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
328 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
329
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
330 function logout(term, res) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
331 if (!term.alive) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
332 sendError(res, 1, null);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
333 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
334 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
335 cterm.close();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
336 var resheaders = {'Content-Type': 'text/plain'};
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
337 res.writeHead(200, resheaders);
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
338 res.write(JSON.stringify({"t": "q"}));
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
339 res.end();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
340 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
341 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
342
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
343 function findTermSession(formdata) {
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
344 if (typeof(formdata) != "object")
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
345 return null;
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
346 if ("id" in formdata) {
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
347 var sessid = formdata["id"];
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
348 if (sessid in sessions) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
349 return sessions[sessid];
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
350 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
351 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
352 return null;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
353 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
354
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
355 function serveStatic(req, res, fname) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
356 var nname = path.normalize(fname);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
357 if (nname == "" || nname == "/")
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
358 nname = "index.html";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
359 if (nname.match(/\/$/))
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
360 path.join(nname, "index.html"); /* it was a directory */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
361 var realname = path.join(serveStaticRoot, nname);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
362 var extension = path.extname(realname);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
363 path.exists(realname, function (exists) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
364 var resheaders = {};
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
365 if (!exists || !extension || extension == ".html")
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
366 resheaders["Content-Type"] = "text/html";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
367 else if (extension == ".png")
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
368 resheaders["Content-Type"] = "image/png";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
369 else if (extension == ".css")
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
370 resheaders["Content-Type"] = "text/css";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
371 else if (extension == ".js")
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
372 resheaders["Content-Type"] = "text/javascript";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
373 else if (extension == ".svg")
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
374 resheaders["Content-Type"] = "image/svg+xml";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
375 else
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
376 resheaders["Content-Type"] = "application/octet-stream";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
377 if (exists) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
378 fs.readFile(realname, function (error, data) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
379 if (error) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
380 res.writeHead(500, {});
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
381 res.end();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
382 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
383 else {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
384 res.writeHead(200, resheaders);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
385 res.write(data);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
386 res.end();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
387 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
388 });
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
389 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
390 else {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
391 res.writeHead(404, resheaders);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
392 res.write("<html><head><title>" + nname + "</title></head>\n<body><h1>" + nname + " Not Found</h1></body></html>\n");
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
393 res.end();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
394 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
395 });
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
396 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
397 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
398
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
399 function readFeed(res, term) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
400 if (term) {
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
401 var reply = {};
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
402 var result = term.read();
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
403 if (result == null) {
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
404 if (term.alive)
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
405 reply.t = "n";
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
406 else
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
407 reply.t = "q";
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
408 }
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
409 else {
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
410 reply.t = "d";
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
411 reply.n = term.nsend++;
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
412 reply.d = result.toString("hex");
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
413 }
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
414 res.writeHead(200, { "Content-Type": "text/plain" });
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
415 res.write(JSON.stringify(reply));
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
416 res.end();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
417 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
418 else {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
419 sendError(res, 1, null);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
420 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
421 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
422
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
423 var errorcodes = [ "Generic Error", "Not logged in", "Invalid data",
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
424 "Login failed", "Already playing", "Game launch failed" ];
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
425
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
426 function sendError(res, ecode, msg) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
427 res.writeHead(200, { "Content-Type": "text/plain" });
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
428 var edict = {"t": "E"};
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
429 if (!(ecode < errorcodes.length && ecode > 0))
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
430 ecode = 0;
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
431 edict["c"] = ecode;
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
432 edict["s"] = errorcodes[ecode];
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
433 if (msg)
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
434 edict["s"] += ": " + msg;
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
435 res.write(JSON.stringify(edict));
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
436 res.end();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
437 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
438
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
439 function handler(req, res) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
440 /* default headers for the response */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
441 var resheaders = {'Content-Type': 'text/html'};
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
442 /* The request body will be added to this as it arrives. */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
443 var reqbody = "";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
444 var formdata;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
445
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
446 /* Register a listener to get the body. */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
447 function moredata(chunk) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
448 reqbody += chunk;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
449 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
450 req.on('data', moredata);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
451
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
452 /* This will send the response once the whole request is here. */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
453 function respond() {
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
454 //formdata = getFormValues(reqbody);
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
455 formdata = getMsg(reqbody);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
456 var target = url.parse(req.url).pathname;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
457 var cterm = findTermSession(formdata);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
458 /* First figure out if the client is POSTing to a command interface. */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
459 if (req.method == 'POST') {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
460 if (target == '/feed') {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
461 if (!cterm) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
462 sendError(res, 1, null);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
463 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
464 }
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
465 if (formdata.t == "q") {
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
466 /* The client wants to terminate the process. */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
467 logout(cterm, res);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
468 }
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
469 else if (formdata.t == "d" && typeof(formdata.d) == "string") {
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
470 /* process the keys */
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
471 hexstr = formdata.d.replace(/[^0-9a-f]/gi, "");
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
472 if (hexstr.length % 2 != 0) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
473 sendError(res, 2, "incomplete byte");
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
474 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
475 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
476 keybuf = new Buffer(hexstr, "hex");
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
477 /* TODO OoO correction */
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
478 cterm.write(keybuf);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
479 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
480 readFeed(res, cterm);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
481 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
482 else if (target == "/login") {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
483 login(req, res, formdata);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
484 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
485 else {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
486 res.writeHead(405, resheaders);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
487 res.end();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
488 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
489 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
490 else if (req.method == 'GET' || req.method == 'HEAD') {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
491 if (target == '/feed') {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
492 if (!cterm) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
493 sendError(res, 1, null);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
494 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
495 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
496 readFeed(res, cterm);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
497 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
498 /* Default page, create a new term */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
499 /* FIXME New term not created anymore, is a special case still needed? */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
500 else if (target == '/') {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
501 serveStatic(req, res, "/");
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
502 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
503 else /* Go look for it in the filesystem */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
504 serveStatic(req, res, target);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
505 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
506 else { /* Some other method */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
507 res.writeHead(501, resheaders);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
508 res.write("<html><head><title>501</title></head>\n<body><h1>501 Not Implemented</h1></body></html>\n");
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
509 res.end();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
510 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
511 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
512 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
513 req.on('end', respond);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
514
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
515 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
516
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
517 process.on("exit", function () {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
518 for (var sessid in sessions) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
519 if (sessions[sessid].alive)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
520 sessions[sessid].child.kill('SIGHUP');
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
521 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
522 console.log("Quitting...");
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
523 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
524 });
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
525
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
526 /* Initialization STARTS HERE */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
527 process.env["TERM"] = "xterm-256color";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
528
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
529 if (process.getuid() != 0) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
530 console.log("Not running as root, cannot chroot.");
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
531 process.exit(1);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
532 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
533 try {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
534 process.chdir(chrootDir);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
535 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
536 catch (err) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
537 console.log("Cannot enter " + chrootDir + " : " + err);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
538 process.exit(1);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
539 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
540 try {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
541 daemon.chroot(chrootDir);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
542 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
543 catch (err) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
544 console.log("chroot to " + chrootDir + " failed: " + err);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
545 process.exit(1);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
546 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
547 try {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
548 // drop gid first, that requires UID=0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
549 process.setgid(dropToGID);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
550 process.setuid(dropToUID);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
551 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
552 catch (err) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
553 console.log("Could not drop permissions: " + err);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
554 process.exit(1);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
555 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
556
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
557 http.createServer(handler).listen(8080, "127.0.0.1");
8
ad0a31e52007 Call the dgl replacement rlgwebd instead of webttyd.
John "Elwin" Edwards <elwin@sdf.org>
parents: 0
diff changeset
558 console.log('rlgwebd running at http://127.0.0.1:8080/');