Mercurial > hg > rlgwebd
annotate rlgwebd.js @ 20:5f785e1d5cca
RLG-Web: set up user directories on registration
Have RLG-Web create various user directories when a new user registers,
as dgamelaunch does. Also add some debugging messages if registration
fails.
author | John "Elwin" Edwards <elwin@sdf.org> |
---|---|
date | Tue, 22 May 2012 21:44:38 -0700 |
parents | 188bbd857124 |
children | 51d59a0e3b20 |
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. */ |
19
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
324 var checker = child_process.spawn("/bin/sqlickrypt", ["check"]); |
17
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 |
20
5f785e1d5cca
RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
19
diff
changeset
|
330 /* Sets things up for a new user, like dgamelaunch's commands[register] */ |
5f785e1d5cca
RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
19
diff
changeset
|
331 function regsetup(username) { |
5f785e1d5cca
RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
19
diff
changeset
|
332 function regsetup_l2(err) { |
5f785e1d5cca
RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
19
diff
changeset
|
333 for (var g in games) { |
5f785e1d5cca
RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
19
diff
changeset
|
334 fs.mkdir(path.join("/dgldir/ttyrec", username, games[g].uname), 0755); |
5f785e1d5cca
RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
19
diff
changeset
|
335 } |
5f785e1d5cca
RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
19
diff
changeset
|
336 } |
5f785e1d5cca
RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
19
diff
changeset
|
337 fs.mkdir(path.join("/dgldir/userdata", username), 0755); |
5f785e1d5cca
RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
19
diff
changeset
|
338 fs.mkdir(path.join("/dgldir/ttyrec/", username), 0755, regsetup_l2); |
5f785e1d5cca
RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
19
diff
changeset
|
339 } |
5f785e1d5cca
RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
19
diff
changeset
|
340 |
19
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
341 function register(req, res, formdata) { |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
342 var uname, passwd, email; |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
343 if (typeof (formdata.name) != "string" || formdata.name === "") { |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
344 sendError(res, 2, "No name given."); |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
345 return; |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
346 } |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
347 else |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
348 uname = formdata["name"]; |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
349 if (typeof (formdata.pw) != "string" || formdata.pw === "") { |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
350 sendError(res, 2, "No password given."); |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
351 return; |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
352 } |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
353 else |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
354 passwd = formdata["pw"]; |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
355 if (typeof (formdata.email) != "string" || formdata.email === "") { |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
356 /* E-mail is optional */ |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
357 email = "nobody@nowhere.not"; |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
358 } |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
359 else |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
360 email = formdata["email"]; |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
361 function checkreg(code, signal) { |
20
5f785e1d5cca
RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
19
diff
changeset
|
362 if (code == 4) { |
19
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
363 sendError(res, 2, "Invalid characters in name or email."); |
20
5f785e1d5cca
RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
19
diff
changeset
|
364 console.log("Attempted registration: " + uname + " " + email); |
5f785e1d5cca
RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
19
diff
changeset
|
365 } |
5f785e1d5cca
RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
19
diff
changeset
|
366 else if (code == 1) { |
19
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
367 sendError(res, 2, "Username " + uname + " is already being used."); |
20
5f785e1d5cca
RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
19
diff
changeset
|
368 console.log("Attempted duplicate registration: " + uname); |
5f785e1d5cca
RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
19
diff
changeset
|
369 } |
5f785e1d5cca
RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
19
diff
changeset
|
370 else if (code != 0) { |
19
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
371 sendError(res, 0, null); |
20
5f785e1d5cca
RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
19
diff
changeset
|
372 console.log("sqlickrypt register failed with code " + code); |
5f785e1d5cca
RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
19
diff
changeset
|
373 } |
19
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
374 else { |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
375 res.writeHead(200, {'Content-Type': 'text/plain'}); |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
376 var reply = {"t": "r", "d": uname}; |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
377 res.write(JSON.stringify(reply)); |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
378 res.end(); |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
379 console.log("Added new user: " + uname); |
20
5f785e1d5cca
RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
19
diff
changeset
|
380 regsetup(uname); |
19
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
381 } |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
382 } |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
383 var child_adder = child_process.spawn("/bin/sqlickrypt", ["register"]); |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
384 child_adder.on("exit", checkreg); |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
385 child_adder.stdin.end(uname + '\n' + passwd + '\n' + email + '\n', "utf8"); |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
386 return; |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
387 } |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
388 |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
389 function logout(term, res) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
390 if (!term.alive) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
391 sendError(res, 1, null); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
392 return; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
393 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
394 cterm.close(); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
395 var resheaders = {'Content-Type': 'text/plain'}; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
396 res.writeHead(200, resheaders); |
16
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
397 res.write(JSON.stringify({"t": "q"})); |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
398 res.end(); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
399 return; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
400 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
401 |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
402 function findTermSession(formdata) { |
16
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
403 if (typeof(formdata) != "object") |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
404 return null; |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
405 if ("id" in formdata) { |
16
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
406 var sessid = formdata["id"]; |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
407 if (sessid in sessions) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
408 return sessions[sessid]; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
409 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
410 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
411 return null; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
412 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
413 |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
414 function serveStatic(req, res, fname) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
415 var nname = path.normalize(fname); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
416 if (nname == "" || nname == "/") |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
417 nname = "index.html"; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
418 if (nname.match(/\/$/)) |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
419 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
|
420 var realname = path.join(serveStaticRoot, nname); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
421 var extension = path.extname(realname); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
422 path.exists(realname, function (exists) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
423 var resheaders = {}; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
424 if (!exists || !extension || extension == ".html") |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
425 resheaders["Content-Type"] = "text/html"; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
426 else if (extension == ".png") |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
427 resheaders["Content-Type"] = "image/png"; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
428 else if (extension == ".css") |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
429 resheaders["Content-Type"] = "text/css"; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
430 else if (extension == ".js") |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
431 resheaders["Content-Type"] = "text/javascript"; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
432 else if (extension == ".svg") |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
433 resheaders["Content-Type"] = "image/svg+xml"; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
434 else |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
435 resheaders["Content-Type"] = "application/octet-stream"; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
436 if (exists) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
437 fs.readFile(realname, function (error, data) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
438 if (error) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
439 res.writeHead(500, {}); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
440 res.end(); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
441 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
442 else { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
443 res.writeHead(200, resheaders); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
444 res.write(data); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
445 res.end(); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
446 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
447 }); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
448 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
449 else { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
450 res.writeHead(404, resheaders); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
451 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
|
452 res.end(); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
453 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
454 }); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
455 return; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
456 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
457 |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
458 function readFeed(res, term) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
459 if (term) { |
16
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
460 var reply = {}; |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
461 var result = term.read(); |
16
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
462 if (result == null) { |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
463 if (term.alive) |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
464 reply.t = "n"; |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
465 else |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
466 reply.t = "q"; |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
467 } |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
468 else { |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
469 reply.t = "d"; |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
470 reply.n = term.nsend++; |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
471 reply.d = result.toString("hex"); |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
472 } |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
473 res.writeHead(200, { "Content-Type": "text/plain" }); |
16
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
474 res.write(JSON.stringify(reply)); |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
475 res.end(); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
476 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
477 else { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
478 sendError(res, 1, null); |
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 } |
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 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
|
483 "Login failed", "Already playing", "Game launch failed" ]; |
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 function sendError(res, ecode, msg) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
486 res.writeHead(200, { "Content-Type": "text/plain" }); |
16
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
487 var edict = {"t": "E"}; |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
488 if (!(ecode < errorcodes.length && ecode > 0)) |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
489 ecode = 0; |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
490 edict["c"] = ecode; |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
491 edict["s"] = errorcodes[ecode]; |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
492 if (msg) |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
493 edict["s"] += ": " + msg; |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
494 res.write(JSON.stringify(edict)); |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
495 res.end(); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
496 } |
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 function handler(req, res) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
499 /* default headers for the response */ |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
500 var resheaders = {'Content-Type': 'text/html'}; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
501 /* 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
|
502 var reqbody = ""; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
503 var formdata; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
504 |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
505 /* Register a listener to get the body. */ |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
506 function moredata(chunk) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
507 reqbody += chunk; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
508 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
509 req.on('data', moredata); |
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 /* 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
|
512 function respond() { |
16
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
513 //formdata = getFormValues(reqbody); |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
514 formdata = getMsg(reqbody); |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
515 var target = url.parse(req.url).pathname; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
516 var cterm = findTermSession(formdata); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
517 /* 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
|
518 if (req.method == 'POST') { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
519 if (target == '/feed') { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
520 if (!cterm) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
521 sendError(res, 1, null); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
522 return; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
523 } |
16
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
524 if (formdata.t == "q") { |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
525 /* The client wants to terminate the process. */ |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
526 logout(cterm, res); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
527 } |
16
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
528 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
|
529 /* process the keys */ |
16
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
530 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
|
531 if (hexstr.length % 2 != 0) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
532 sendError(res, 2, "incomplete byte"); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
533 return; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
534 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
535 keybuf = new Buffer(hexstr, "hex"); |
16
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
536 /* TODO OoO correction */ |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
537 cterm.write(keybuf); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
538 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
539 readFeed(res, cterm); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
540 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
541 else if (target == "/login") { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
542 login(req, res, formdata); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
543 } |
19
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
544 else if (target == "/addacct") { |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
545 register(req, res, formdata); |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
546 } |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
547 else { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
548 res.writeHead(405, resheaders); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
549 res.end(); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
550 } |
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 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
|
553 if (target == '/feed') { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
554 if (!cterm) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
555 sendError(res, 1, null); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
556 return; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
557 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
558 readFeed(res, cterm); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
559 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
560 /* Default page, create a new term */ |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
561 /* 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
|
562 else if (target == '/') { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
563 serveStatic(req, res, "/"); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
564 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
565 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
|
566 serveStatic(req, res, target); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
567 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
568 else { /* Some other method */ |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
569 res.writeHead(501, resheaders); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
570 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
|
571 res.end(); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
572 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
573 return; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
574 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
575 req.on('end', respond); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
576 |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
577 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
578 |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
579 process.on("exit", function () { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
580 for (var sessid in sessions) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
581 if (sessions[sessid].alive) |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
582 sessions[sessid].child.kill('SIGHUP'); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
583 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
584 console.log("Quitting..."); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
585 return; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
586 }); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
587 |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
588 /* Initialization STARTS HERE */ |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
589 process.env["TERM"] = "xterm-256color"; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
590 |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
591 if (process.getuid() != 0) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
592 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
|
593 process.exit(1); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
594 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
595 try { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
596 process.chdir(chrootDir); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
597 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
598 catch (err) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
599 console.log("Cannot enter " + chrootDir + " : " + err); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
600 process.exit(1); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
601 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
602 try { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
603 daemon.chroot(chrootDir); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
604 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
605 catch (err) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
606 console.log("chroot to " + chrootDir + " failed: " + err); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
607 process.exit(1); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
608 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
609 try { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
610 // drop gid first, that requires UID=0 |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
611 process.setgid(dropToGID); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
612 process.setuid(dropToUID); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
613 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
614 catch (err) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
615 console.log("Could not drop permissions: " + err); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
616 process.exit(1); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
617 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
618 |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
619 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
|
620 console.log('rlgwebd running at http://127.0.0.1:8080/'); |