Mercurial > hg > rlgwebd
annotate rlgwebd.js @ 34:57f4b36ef06b
rlgwebd.js: handle HTTP HEAD properly.
Prevent a message body from being sent in response to HTTP HEAD.
author | John "Elwin" Edwards <elwin@sdf.org> |
---|---|
date | Tue, 05 Jun 2012 20:13:47 -0700 |
parents | c75fc4b1d13d |
children | a0387f112bcf |
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'); |
28
2ad2b6491aa9
rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents:
27
diff
changeset
|
6 var net = require('net'); |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
7 var url = require('url'); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
8 var path = require('path'); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
9 var fs = require('fs'); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
10 var child_process = require('child_process'); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
11 var daemon = require(path.join(localModules, "daemon")); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
12 |
30
b5570a594266
rlgwebd.js: listen on any address
John "Elwin" Edwards <elwin@sdf.org>
parents:
29
diff
changeset
|
13 /* Configuration variables */ |
28
2ad2b6491aa9
rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents:
27
diff
changeset
|
14 // These first two files are NOT in the chroot. |
2ad2b6491aa9
rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents:
27
diff
changeset
|
15 var ctlsocket = "/var/local/rlgwebd/ctl"; |
2ad2b6491aa9
rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents:
27
diff
changeset
|
16 var logfile = "/var/local/rlgwebd/log"; |
30
b5570a594266
rlgwebd.js: listen on any address
John "Elwin" Edwards <elwin@sdf.org>
parents:
29
diff
changeset
|
17 var httpPort = 8080; |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
18 var chrootDir = "/var/dgl/"; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
19 var dropToUID = 501; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
20 var dropToGID = 501; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
21 var serveStaticRoot = "/var/www/"; // inside the chroot |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
22 var passwdfile = "/dgldir/dgl-login"; |
30
b5570a594266
rlgwebd.js: listen on any address
John "Elwin" Edwards <elwin@sdf.org>
parents:
29
diff
changeset
|
23 |
b5570a594266
rlgwebd.js: listen on any address
John "Elwin" Edwards <elwin@sdf.org>
parents:
29
diff
changeset
|
24 /* Global state */ |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
25 var sessions = {}; |
27
83f9a799a374
rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents:
26
diff
changeset
|
26 var allowlogin = true; |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
27 |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
28 var games = { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
29 "rogue3": { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
30 "name": "Rogue V3", |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
31 "uname": "rogue3", |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
32 "path": "/bin/rogue3" |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
33 }, |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
34 "rogue4": { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
35 "name": "Rogue V4", |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
36 "uname": "rogue4", |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
37 "path": "/bin/rogue4" |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
38 }, |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
39 "rogue5": { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
40 "name": "Rogue V5", |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
41 "uname": "rogue5", |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
42 "path": "/bin/rogue5" |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
43 }, |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
44 "srogue": { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
45 "name": "Super-Rogue", |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
46 "uname": "srogue", |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
47 "path": "/bin/srogue" |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
48 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
49 }; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
50 |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
51 /* 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
|
52 * 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
|
53 * been authenticated. |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
54 */ |
16
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
55 function TermSession(game, user, files, dims) { |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
56 /* First make sure starting the game will work. */ |
32
c75fc4b1d13d
rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
31
diff
changeset
|
57 if (game in games) { |
c75fc4b1d13d
rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
31
diff
changeset
|
58 this.game = games[game]; |
c75fc4b1d13d
rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
31
diff
changeset
|
59 } |
c75fc4b1d13d
rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
31
diff
changeset
|
60 else { |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
61 // TODO: throw an exception instead |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
62 return null; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
63 } |
32
c75fc4b1d13d
rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
31
diff
changeset
|
64 this.player = user; |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
65 /* 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
|
66 this.alive = false; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
67 this.sessid = randkey(); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
68 while (this.sessid in sessions) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
69 this.sessid = randkey(); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
70 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
71 /* Grab a spot in the sessions table. */ |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
72 sessions[this.sessid] = this; |
16
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
73 /* State for messaging. */ |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
74 this.nsend = 0; |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
75 this.nrecv = 0; |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
76 this.msgQ = [] |
23
21de24c08aed
Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents:
22
diff
changeset
|
77 this.Qtimeout = null; |
16
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
78 /* Set up the sizes. */ |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
79 this.w = Math.floor(Number(dims[1])); |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
80 if (!(this.w > 0 && this.w < 256)) |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
81 this.w = 80; |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
82 this.h = Math.floor(Number(dims[0])); |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
83 if (!(this.h > 0 && this.h < 256)) |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
84 this.h = 24; |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
85 /* Environment. */ |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
86 var childenv = {}; |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
87 for (var key in process.env) { |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
88 childenv[key] = process.env[key]; |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
89 } |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
90 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
|
91 /* TODO handle tty-opening errors */ |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
92 /* TODO make argument-finding into a method */ |
32
c75fc4b1d13d
rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
31
diff
changeset
|
93 args = [this.game.path, "-n", user.toString()]; |
16
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
94 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
|
95 var ss = this; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
96 this.alive = true; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
97 this.data = []; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
98 this.lock = files[0]; |
16
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
99 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
|
100 this.h + '\n', "utf8"); |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
101 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
|
102 /* END setup */ |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
103 function ttyrec_chunk(buf) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
104 var ts = new Date(); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
105 var chunk = new Buffer(buf.length + 12); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
106 /* TTYREC headers */ |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
107 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
|
108 chunk.writeUInt32LE(1000 * (ts.getTime() % 1000), 4); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
109 chunk.writeUInt32LE(buf.length, 8); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
110 buf.copy(chunk, 12); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
111 ss.data.push(chunk); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
112 ss.record.write(chunk); |
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.child.stdout.on("data", ttyrec_chunk); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
115 this.child.stderr.on("data", ttyrec_chunk); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
116 this.child.on("exit", function (code, signal) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
117 ss.exitcode = (code != null ? code : 255); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
118 ss.alive = false; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
119 fs.unlink(ss.lock); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
120 /* 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
|
121 setTimeout(ss.cleanup, 1000); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
122 }); |
23
21de24c08aed
Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents:
22
diff
changeset
|
123 this.write = function (data, n) { |
21de24c08aed
Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents:
22
diff
changeset
|
124 if (!this.alive || typeof (n) != "number") { |
21de24c08aed
Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents:
22
diff
changeset
|
125 return; |
21de24c08aed
Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents:
22
diff
changeset
|
126 } |
21de24c08aed
Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents:
22
diff
changeset
|
127 //console.log("Got message " + n); |
21de24c08aed
Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents:
22
diff
changeset
|
128 var oindex = n - this.nrecv; |
21de24c08aed
Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents:
22
diff
changeset
|
129 if (oindex === 0) { |
21de24c08aed
Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents:
22
diff
changeset
|
130 //console.log("Writing message " + n); |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
131 this.child.stdin.write(data); |
23
21de24c08aed
Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents:
22
diff
changeset
|
132 this.nrecv++; |
21de24c08aed
Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents:
22
diff
changeset
|
133 var next; |
21de24c08aed
Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents:
22
diff
changeset
|
134 while ((next = this.msgQ.shift()) !== undefined) { |
21de24c08aed
Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents:
22
diff
changeset
|
135 //console.log("Writing message " + this.nrecv); |
21de24c08aed
Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents:
22
diff
changeset
|
136 this.child.stdin.write(next); |
21de24c08aed
Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents:
22
diff
changeset
|
137 this.nrecv++; |
21de24c08aed
Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents:
22
diff
changeset
|
138 } |
21de24c08aed
Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents:
22
diff
changeset
|
139 if (this.msgQ.length == 0 && this.Qtimeout) { |
21de24c08aed
Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents:
22
diff
changeset
|
140 clearTimeout(this.Qtimeout); |
21de24c08aed
Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents:
22
diff
changeset
|
141 this.Qtimeout = null; |
21de24c08aed
Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents:
22
diff
changeset
|
142 } |
21de24c08aed
Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents:
22
diff
changeset
|
143 } |
21de24c08aed
Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents:
22
diff
changeset
|
144 else if (oindex > 0 && oindex <= 1024) { |
26
9b58f8d3ea70
rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents:
23
diff
changeset
|
145 tslog("Stashing message %d at %d", n, oindex - 1); |
23
21de24c08aed
Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents:
22
diff
changeset
|
146 this.msgQ[oindex - 1] = data; |
21de24c08aed
Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents:
22
diff
changeset
|
147 if (!this.Qtimeout) { |
21de24c08aed
Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents:
22
diff
changeset
|
148 var nextn = this.nrecv + this.msgQ.length + 1; |
21de24c08aed
Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents:
22
diff
changeset
|
149 this.Qtimeout = setTimeout(this.flushQ, 30000, this, nextn); |
21de24c08aed
Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents:
22
diff
changeset
|
150 } |
21de24c08aed
Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents:
22
diff
changeset
|
151 } |
21de24c08aed
Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents:
22
diff
changeset
|
152 /* Otherwise, discard it */ |
21de24c08aed
Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents:
22
diff
changeset
|
153 return; |
21de24c08aed
Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents:
22
diff
changeset
|
154 }; |
21de24c08aed
Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents:
22
diff
changeset
|
155 this.flushQ = function (session, n) { |
21de24c08aed
Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents:
22
diff
changeset
|
156 /* Callback for when an unreceived message times out. |
21de24c08aed
Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents:
22
diff
changeset
|
157 * n is the first empty space that will not be given up on. */ |
21de24c08aed
Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents:
22
diff
changeset
|
158 if (!session.alive) |
21de24c08aed
Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents:
22
diff
changeset
|
159 return; |
21de24c08aed
Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents:
22
diff
changeset
|
160 session.nrecv++; |
21de24c08aed
Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents:
22
diff
changeset
|
161 var next; |
21de24c08aed
Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents:
22
diff
changeset
|
162 /* Clear the queue up to n */ |
21de24c08aed
Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents:
22
diff
changeset
|
163 while (session.nrecv < n) { |
21de24c08aed
Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents:
22
diff
changeset
|
164 next = session.msgQ.shift(); |
21de24c08aed
Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents:
22
diff
changeset
|
165 if (next !== undefined) |
21de24c08aed
Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents:
22
diff
changeset
|
166 session.child.stdin.write(next); |
21de24c08aed
Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents:
22
diff
changeset
|
167 session.nrecv++; |
21de24c08aed
Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents:
22
diff
changeset
|
168 } |
21de24c08aed
Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents:
22
diff
changeset
|
169 /* Clear out anything that's ready. */ |
21de24c08aed
Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents:
22
diff
changeset
|
170 while ((next = session.msgQ.shift()) !== undefined) { |
21de24c08aed
Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents:
22
diff
changeset
|
171 session.child.stdin.write(next); |
21de24c08aed
Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents:
22
diff
changeset
|
172 session.nrecv++; |
21de24c08aed
Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents:
22
diff
changeset
|
173 } |
21de24c08aed
Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents:
22
diff
changeset
|
174 /* Now set another timeout if necessary. */ |
21de24c08aed
Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents:
22
diff
changeset
|
175 if (session.msgQ.length != 0) { |
21de24c08aed
Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents:
22
diff
changeset
|
176 var nextn = session.nrecv + session.msgQ.length + 1; |
21de24c08aed
Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents:
22
diff
changeset
|
177 session.Qtimeout = setTimeout(session.flushQ, 30000, session, nextn); |
21de24c08aed
Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents:
22
diff
changeset
|
178 } |
26
9b58f8d3ea70
rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents:
23
diff
changeset
|
179 tslog("Flushing queue for session %s", session.sessid); |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
180 }; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
181 this.read = function () { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
182 if (this.data.length == 0) |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
183 return null; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
184 var pos = 0; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
185 var i = 0; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
186 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
|
187 pos += this.data[i].length - 12; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
188 var nbuf = new Buffer(pos); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
189 var tptr; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
190 pos = 0; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
191 while (this.data.length > 0) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
192 tptr = this.data.shift(); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
193 tptr.copy(nbuf, pos, 12); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
194 pos += tptr.length - 12; |
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 return nbuf; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
197 }; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
198 this.close = function () { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
199 if (this.alive) |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
200 this.child.kill('SIGHUP'); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
201 }; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
202 this.cleanup = function () { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
203 /* Call this when the child is dead. */ |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
204 if (this.alive) |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
205 return; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
206 ss.record.end(); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
207 /* 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
|
208 if (ss.data.length > 0) |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
209 setTimeout(ss.remove, 8000); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
210 else |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
211 ss.remove(); |
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 this.remove = function () { |
27
83f9a799a374
rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents:
26
diff
changeset
|
214 var id = ss.sessid; |
83f9a799a374
rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents:
26
diff
changeset
|
215 delete sessions[id]; |
83f9a799a374
rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents:
26
diff
changeset
|
216 tslog("Session %s removed.", id); |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
217 }; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
218 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
219 |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
220 /* A few utility functions */ |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
221 function timestamp() { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
222 dd = new Date(); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
223 sd = dd.toISOString(); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
224 sd = sd.slice(0, sd.indexOf(".")); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
225 return sd.replace("T", "."); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
226 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
227 |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
228 function randkey() { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
229 rnum = Math.floor(Math.random() * 65536 * 65536); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
230 hexstr = rnum.toString(16); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
231 while (hexstr.length < 8) |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
232 hexstr = "0" + hexstr; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
233 return hexstr; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
234 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
235 |
26
9b58f8d3ea70
rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents:
23
diff
changeset
|
236 function tslog() { |
9b58f8d3ea70
rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents:
23
diff
changeset
|
237 arguments[0] = new Date().toISOString() + ": " + String(arguments[0]); |
9b58f8d3ea70
rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents:
23
diff
changeset
|
238 console.log.apply(console, arguments); |
9b58f8d3ea70
rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents:
23
diff
changeset
|
239 } |
9b58f8d3ea70
rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents:
23
diff
changeset
|
240 |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
241 /* 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
|
242 function getCookies(req) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
243 cookies = []; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
244 if ("cookie" in req.headers) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
245 cookstrs = req.headers["cookie"].split("; "); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
246 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
|
247 eqsign = cookstrs[i].indexOf("="); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
248 if (eqsign > 0) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
249 name = cookstrs[i].slice(0, eqsign).toLowerCase(); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
250 val = cookstrs[i].slice(eqsign + 1); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
251 cookies[name] = val; |
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 (eqsign < 0) |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
254 cookies[cookstrs[i]] = null; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
255 } |
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 return cookies; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
258 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
259 |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
260 function urlDec(encstr) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
261 var decstr = ""; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
262 var tnum; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
263 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
|
264 { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
265 if (encstr.charAt(i) == "+") |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
266 decstr += " "; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
267 else if (encstr.charAt(i) == "%") |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
268 { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
269 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
|
270 if (!isNaN(tnum) && tnum >= 0) |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
271 decstr += String.fromCharCode(tnum); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
272 i += 2; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
273 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
274 else |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
275 decstr += encstr.charAt(i); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
276 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
277 return decstr; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
278 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
279 |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
280 /* Returns the contents of a form */ |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
281 function getFormValues(formtext) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
282 var pairstrs = formtext.split("&"); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
283 var data = {}; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
284 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
|
285 { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
286 var eqsign = pairstrs[i].indexOf("="); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
287 if (eqsign > 0) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
288 rawname = pairstrs[i].slice(0, eqsign); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
289 rawval = pairstrs[i].slice(eqsign + 1); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
290 name = urlDec(rawname); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
291 val = urlDec(rawval); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
292 if (!(name in data)) |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
293 data[name] = []; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
294 data[name].push(val); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
295 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
296 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
297 return data; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
298 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
299 |
16
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
300 function getMsg(posttext) { |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
301 var jsonobj; |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
302 if (!posttext) |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
303 return {}; |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
304 try { |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
305 jsonobj = JSON.parse(posttext); |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
306 } |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
307 catch (e) { |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
308 if (e instanceof SyntaxError) |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
309 return {}; |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
310 } |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
311 if (typeof(jsonobj) != "object") |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
312 return {}; |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
313 return jsonobj; |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
314 } |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
315 |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
316 function auth(username, password) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
317 // Real authentication not implemented |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
318 return true; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
319 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
320 |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
321 function login(req, res, formdata) { |
27
83f9a799a374
rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents:
26
diff
changeset
|
322 if (!allowlogin) { |
83f9a799a374
rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents:
26
diff
changeset
|
323 sendError(res, 6, null); |
83f9a799a374
rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents:
26
diff
changeset
|
324 return; |
83f9a799a374
rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents:
26
diff
changeset
|
325 } |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
326 if (!("game" in formdata)) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
327 sendError(res, 2, "No game specified."); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
328 return; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
329 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
330 else if (!("name" in formdata)) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
331 sendError(res, 2, "Username not given."); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
332 return; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
333 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
334 else if (!("pw" in formdata)) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
335 sendError(res, 2, "Password not given."); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
336 return; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
337 } |
16
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
338 var username = formdata["name"]; |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
339 var password = formdata["pw"]; |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
340 var gname = formdata["game"]; |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
341 var dims = [formdata["h"], formdata["w"]]; |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
342 if (!(gname in games)) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
343 sendError(res, 2, "No such game: " + gname); |
26
9b58f8d3ea70
rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents:
23
diff
changeset
|
344 tslog("Request for nonexistant game \"%s\"", gname); |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
345 return; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
346 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
347 var progressdir = "/dgldir/inprogress-" + games[gname].uname; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
348 |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
349 // 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
|
350 function startgame() { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
351 var ts = timestamp(); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
352 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
|
353 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
|
354 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
|
355 if (nsession) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
356 /* 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
|
357 * 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
|
358 * it's not too serious. We can't get O_EXCL in Node anyway. */ |
31
7dd6becf9ce9
rlgwebd.js: improve MIME types.
John "Elwin" Edwards <elwin@sdf.org>
parents:
30
diff
changeset
|
359 res.writeHead(200, {'Content-Type': 'application/json'}); |
16
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
360 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
|
361 nsession.h}; |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
362 res.write(JSON.stringify(reply)); |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
363 res.end(); |
26
9b58f8d3ea70
rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents:
23
diff
changeset
|
364 tslog("%s playing %s (key %s, pid %d)", username, gname, |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
365 nsession.sessid, nsession.child.pid); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
366 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
367 else { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
368 sendError(res, 5, "Failed to open TTY"); |
26
9b58f8d3ea70
rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents:
23
diff
changeset
|
369 tslog("Unable to allocate TTY for %s", gname); |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
370 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
371 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
372 function checkit(code, signal) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
373 // check the password |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
374 if (code != 0) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
375 sendError(res, 3); |
17
d3e3d6b4016b
rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
16
diff
changeset
|
376 if (code == 1) |
26
9b58f8d3ea70
rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents:
23
diff
changeset
|
377 tslog("Password check failed for user %s", username); |
17
d3e3d6b4016b
rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
16
diff
changeset
|
378 else if (code == 2) |
26
9b58f8d3ea70
rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents:
23
diff
changeset
|
379 tslog("Attempted login by nonexistent user %s", username); |
17
d3e3d6b4016b
rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
16
diff
changeset
|
380 else |
26
9b58f8d3ea70
rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents:
23
diff
changeset
|
381 tslog("Login failed: sqlickrypt error %d", code); |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
382 return; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
383 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
384 // check for an existing game |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
385 fs.readdir(progressdir, function(err, files) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
386 if (!err) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
387 var fre = RegExp("^" + username + ":"); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
388 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
|
389 if (files[i].match(fre)) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
390 sendError(res, 4, null); |
26
9b58f8d3ea70
rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents:
23
diff
changeset
|
391 tslog("%s is already playing %s", username, gname); |
0
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 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
395 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
396 // 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
|
397 startgame(); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
398 }); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
399 } |
17
d3e3d6b4016b
rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
16
diff
changeset
|
400 /* 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
|
401 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
|
402 checker.on("exit", checkit); |
d3e3d6b4016b
rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
16
diff
changeset
|
403 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
|
404 return; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
405 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
406 |
20
5f785e1d5cca
RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
19
diff
changeset
|
407 /* 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
|
408 function regsetup(username) { |
5f785e1d5cca
RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
19
diff
changeset
|
409 function regsetup_l2(err) { |
5f785e1d5cca
RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
19
diff
changeset
|
410 for (var g in games) { |
5f785e1d5cca
RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
19
diff
changeset
|
411 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
|
412 } |
5f785e1d5cca
RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
19
diff
changeset
|
413 } |
5f785e1d5cca
RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
19
diff
changeset
|
414 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
|
415 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
|
416 } |
5f785e1d5cca
RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
19
diff
changeset
|
417 |
19
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
418 function register(req, res, formdata) { |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
419 var uname, passwd, email; |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
420 if (typeof (formdata.name) != "string" || formdata.name === "") { |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
421 sendError(res, 2, "No name given."); |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
422 return; |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
423 } |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
424 else |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
425 uname = formdata["name"]; |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
426 if (typeof (formdata.pw) != "string" || formdata.pw === "") { |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
427 sendError(res, 2, "No password given."); |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
428 return; |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
429 } |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
430 else |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
431 passwd = formdata["pw"]; |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
432 if (typeof (formdata.email) != "string" || formdata.email === "") { |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
433 /* E-mail is optional */ |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
434 email = "nobody@nowhere.not"; |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
435 } |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
436 else |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
437 email = formdata["email"]; |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
438 function checkreg(code, signal) { |
20
5f785e1d5cca
RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
19
diff
changeset
|
439 if (code == 4) { |
19
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
440 sendError(res, 2, "Invalid characters in name or email."); |
26
9b58f8d3ea70
rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents:
23
diff
changeset
|
441 tslog("Attempted registration: %s %s", uname, email); |
20
5f785e1d5cca
RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
19
diff
changeset
|
442 } |
5f785e1d5cca
RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
19
diff
changeset
|
443 else if (code == 1) { |
19
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
444 sendError(res, 2, "Username " + uname + " is already being used."); |
26
9b58f8d3ea70
rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents:
23
diff
changeset
|
445 tslog("Attempted duplicate registration: %s", uname); |
20
5f785e1d5cca
RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
19
diff
changeset
|
446 } |
5f785e1d5cca
RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
19
diff
changeset
|
447 else if (code != 0) { |
19
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
448 sendError(res, 0, null); |
26
9b58f8d3ea70
rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents:
23
diff
changeset
|
449 tslog("sqlickrypt register failed with code %d", code); |
20
5f785e1d5cca
RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
19
diff
changeset
|
450 } |
19
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
451 else { |
31
7dd6becf9ce9
rlgwebd.js: improve MIME types.
John "Elwin" Edwards <elwin@sdf.org>
parents:
30
diff
changeset
|
452 res.writeHead(200, {'Content-Type': 'application/json'}); |
19
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
453 var reply = {"t": "r", "d": uname}; |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
454 res.write(JSON.stringify(reply)); |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
455 res.end(); |
26
9b58f8d3ea70
rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents:
23
diff
changeset
|
456 tslog("Added new user: %s", uname); |
20
5f785e1d5cca
RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
19
diff
changeset
|
457 regsetup(uname); |
19
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
458 } |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
459 } |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
460 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
|
461 child_adder.on("exit", checkreg); |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
462 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
|
463 return; |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
464 } |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
465 |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
466 function logout(term, res) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
467 if (!term.alive) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
468 sendError(res, 1, null); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
469 return; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
470 } |
22 | 471 term.close(); |
31
7dd6becf9ce9
rlgwebd.js: improve MIME types.
John "Elwin" Edwards <elwin@sdf.org>
parents:
30
diff
changeset
|
472 var resheaders = {'Content-Type': 'application/json'}; |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
473 res.writeHead(200, resheaders); |
16
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
474 res.write(JSON.stringify({"t": "q"})); |
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 return; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
477 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
478 |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
479 function findTermSession(formdata) { |
16
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
480 if (typeof(formdata) != "object") |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
481 return null; |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
482 if ("id" in formdata) { |
16
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
483 var sessid = formdata["id"]; |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
484 if (sessid in sessions) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
485 return sessions[sessid]; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
486 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
487 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
488 return null; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
489 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
490 |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
491 function serveStatic(req, res, fname) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
492 var nname = path.normalize(fname); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
493 if (nname == "" || nname == "/") |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
494 nname = "index.html"; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
495 if (nname.match(/\/$/)) |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
496 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
|
497 var realname = path.join(serveStaticRoot, nname); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
498 var extension = path.extname(realname); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
499 path.exists(realname, function (exists) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
500 var resheaders = {}; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
501 if (!exists || !extension || extension == ".html") |
31
7dd6becf9ce9
rlgwebd.js: improve MIME types.
John "Elwin" Edwards <elwin@sdf.org>
parents:
30
diff
changeset
|
502 resheaders["Content-Type"] = "text/html; charset=utf-8"; |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
503 else if (extension == ".png") |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
504 resheaders["Content-Type"] = "image/png"; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
505 else if (extension == ".css") |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
506 resheaders["Content-Type"] = "text/css"; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
507 else if (extension == ".js") |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
508 resheaders["Content-Type"] = "text/javascript"; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
509 else if (extension == ".svg") |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
510 resheaders["Content-Type"] = "image/svg+xml"; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
511 else |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
512 resheaders["Content-Type"] = "application/octet-stream"; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
513 if (exists) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
514 fs.readFile(realname, function (error, data) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
515 if (error) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
516 res.writeHead(500, {}); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
517 res.end(); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
518 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
519 else { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
520 res.writeHead(200, resheaders); |
34
57f4b36ef06b
rlgwebd.js: handle HTTP HEAD properly.
John "Elwin" Edwards <elwin@sdf.org>
parents:
32
diff
changeset
|
521 if (req.method != 'HEAD') |
57f4b36ef06b
rlgwebd.js: handle HTTP HEAD properly.
John "Elwin" Edwards <elwin@sdf.org>
parents:
32
diff
changeset
|
522 res.write(data); |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
523 res.end(); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
524 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
525 }); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
526 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
527 else { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
528 res.writeHead(404, resheaders); |
34
57f4b36ef06b
rlgwebd.js: handle HTTP HEAD properly.
John "Elwin" Edwards <elwin@sdf.org>
parents:
32
diff
changeset
|
529 if (req.method != 'HEAD') { |
57f4b36ef06b
rlgwebd.js: handle HTTP HEAD properly.
John "Elwin" Edwards <elwin@sdf.org>
parents:
32
diff
changeset
|
530 res.write("<html><head><title>" + nname + "</title></head>\n<body><h1>" |
57f4b36ef06b
rlgwebd.js: handle HTTP HEAD properly.
John "Elwin" Edwards <elwin@sdf.org>
parents:
32
diff
changeset
|
531 + nname + " Not Found</h1></body></html>\n"); |
57f4b36ef06b
rlgwebd.js: handle HTTP HEAD properly.
John "Elwin" Edwards <elwin@sdf.org>
parents:
32
diff
changeset
|
532 } |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
533 res.end(); |
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 }); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
536 return; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
537 } |
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 function readFeed(res, term) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
540 if (term) { |
16
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
541 var reply = {}; |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
542 var result = term.read(); |
16
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
543 if (result == null) { |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
544 if (term.alive) |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
545 reply.t = "n"; |
27
83f9a799a374
rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents:
26
diff
changeset
|
546 else { |
83f9a799a374
rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents:
26
diff
changeset
|
547 if (allowlogin) |
83f9a799a374
rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents:
26
diff
changeset
|
548 reply.t = "q"; |
83f9a799a374
rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents:
26
diff
changeset
|
549 else { |
83f9a799a374
rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents:
26
diff
changeset
|
550 sendError(res, 6, null); |
83f9a799a374
rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents:
26
diff
changeset
|
551 return; |
83f9a799a374
rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents:
26
diff
changeset
|
552 } |
83f9a799a374
rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents:
26
diff
changeset
|
553 } |
16
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
554 } |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
555 else { |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
556 reply.t = "d"; |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
557 reply.n = term.nsend++; |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
558 reply.d = result.toString("hex"); |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
559 } |
31
7dd6becf9ce9
rlgwebd.js: improve MIME types.
John "Elwin" Edwards <elwin@sdf.org>
parents:
30
diff
changeset
|
560 res.writeHead(200, { "Content-Type": "application/json" }); |
16
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
561 res.write(JSON.stringify(reply)); |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
562 res.end(); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
563 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
564 else { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
565 sendError(res, 1, null); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
566 } |
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 |
32
c75fc4b1d13d
rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
31
diff
changeset
|
569 function statusmsg(req, res) { |
c75fc4b1d13d
rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
31
diff
changeset
|
570 var reply = {"s": allowlogin, "g": []}; |
c75fc4b1d13d
rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
31
diff
changeset
|
571 for (var sessid in sessions) { |
c75fc4b1d13d
rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
31
diff
changeset
|
572 if (sessions[sessid].alive) { |
c75fc4b1d13d
rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
31
diff
changeset
|
573 var gamedesc = {}; |
c75fc4b1d13d
rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
31
diff
changeset
|
574 gamedesc["p"] = sessions[sessid].player; |
c75fc4b1d13d
rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
31
diff
changeset
|
575 gamedesc["g"] = sessions[sessid].game.name; |
c75fc4b1d13d
rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
31
diff
changeset
|
576 reply["g"].push(gamedesc); |
c75fc4b1d13d
rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
31
diff
changeset
|
577 } |
c75fc4b1d13d
rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
31
diff
changeset
|
578 } |
c75fc4b1d13d
rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
31
diff
changeset
|
579 res.writeHead(200, { "Content-Type": "application/json" }); |
34
57f4b36ef06b
rlgwebd.js: handle HTTP HEAD properly.
John "Elwin" Edwards <elwin@sdf.org>
parents:
32
diff
changeset
|
580 if (req.method != 'HEAD') |
57f4b36ef06b
rlgwebd.js: handle HTTP HEAD properly.
John "Elwin" Edwards <elwin@sdf.org>
parents:
32
diff
changeset
|
581 res.write(JSON.stringify(reply)); |
32
c75fc4b1d13d
rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
31
diff
changeset
|
582 res.end(); |
c75fc4b1d13d
rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
31
diff
changeset
|
583 } |
c75fc4b1d13d
rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
31
diff
changeset
|
584 |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
585 var errorcodes = [ "Generic Error", "Not logged in", "Invalid data", |
27
83f9a799a374
rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents:
26
diff
changeset
|
586 "Login failed", "Already playing", "Game launch failed", |
83f9a799a374
rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents:
26
diff
changeset
|
587 "Server shutting down" ]; |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
588 |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
589 function sendError(res, ecode, msg) { |
31
7dd6becf9ce9
rlgwebd.js: improve MIME types.
John "Elwin" Edwards <elwin@sdf.org>
parents:
30
diff
changeset
|
590 res.writeHead(200, { "Content-Type": "application/json" }); |
16
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
591 var edict = {"t": "E"}; |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
592 if (!(ecode < errorcodes.length && ecode > 0)) |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
593 ecode = 0; |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
594 edict["c"] = ecode; |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
595 edict["s"] = errorcodes[ecode]; |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
596 if (msg) |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
597 edict["s"] += ": " + msg; |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
598 res.write(JSON.stringify(edict)); |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
599 res.end(); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
600 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
601 |
27
83f9a799a374
rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents:
26
diff
changeset
|
602 function webHandler(req, res) { |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
603 /* default headers for the response */ |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
604 var resheaders = {'Content-Type': 'text/html'}; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
605 /* 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
|
606 var reqbody = ""; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
607 var formdata; |
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 /* Register a listener to get the body. */ |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
610 function moredata(chunk) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
611 reqbody += chunk; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
612 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
613 req.on('data', moredata); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
614 |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
615 /* 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
|
616 function respond() { |
16
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
617 formdata = getMsg(reqbody); |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
618 var target = url.parse(req.url).pathname; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
619 var cterm = findTermSession(formdata); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
620 /* 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
|
621 if (req.method == 'POST') { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
622 if (target == '/feed') { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
623 if (!cterm) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
624 sendError(res, 1, null); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
625 return; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
626 } |
16
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
627 if (formdata.t == "q") { |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
628 /* The client wants to terminate the process. */ |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
629 logout(cterm, res); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
630 } |
16
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
631 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
|
632 /* process the keys */ |
16
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
633 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
|
634 if (hexstr.length % 2 != 0) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
635 sendError(res, 2, "incomplete byte"); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
636 return; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
637 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
638 keybuf = new Buffer(hexstr, "hex"); |
16
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
639 /* TODO OoO correction */ |
23
21de24c08aed
Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents:
22
diff
changeset
|
640 cterm.write(keybuf, formdata.n); |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
641 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
642 readFeed(res, cterm); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
643 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
644 else if (target == "/login") { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
645 login(req, res, formdata); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
646 } |
19
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
647 else if (target == "/addacct") { |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
648 register(req, res, formdata); |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
649 } |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
650 else { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
651 res.writeHead(405, resheaders); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
652 res.end(); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
653 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
654 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
655 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
|
656 if (target == '/feed') { |
34
57f4b36ef06b
rlgwebd.js: handle HTTP HEAD properly.
John "Elwin" Edwards <elwin@sdf.org>
parents:
32
diff
changeset
|
657 if (req.method == 'HEAD') { |
57f4b36ef06b
rlgwebd.js: handle HTTP HEAD properly.
John "Elwin" Edwards <elwin@sdf.org>
parents:
32
diff
changeset
|
658 res.writeHead(200, {"Content-Type": "application/json"}); |
57f4b36ef06b
rlgwebd.js: handle HTTP HEAD properly.
John "Elwin" Edwards <elwin@sdf.org>
parents:
32
diff
changeset
|
659 res.end(); |
57f4b36ef06b
rlgwebd.js: handle HTTP HEAD properly.
John "Elwin" Edwards <elwin@sdf.org>
parents:
32
diff
changeset
|
660 return; |
57f4b36ef06b
rlgwebd.js: handle HTTP HEAD properly.
John "Elwin" Edwards <elwin@sdf.org>
parents:
32
diff
changeset
|
661 } |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
662 if (!cterm) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
663 sendError(res, 1, null); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
664 return; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
665 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
666 readFeed(res, cterm); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
667 } |
32
c75fc4b1d13d
rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
31
diff
changeset
|
668 else if (target == '/status') { |
c75fc4b1d13d
rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
31
diff
changeset
|
669 statusmsg(req, res); |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
670 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
671 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
|
672 serveStatic(req, res, target); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
673 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
674 else { /* Some other method */ |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
675 res.writeHead(501, resheaders); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
676 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
|
677 res.end(); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
678 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
679 return; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
680 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
681 req.on('end', respond); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
682 |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
683 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
684 |
27
83f9a799a374
rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents:
26
diff
changeset
|
685 function shutdown () { |
83f9a799a374
rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents:
26
diff
changeset
|
686 httpServer.close(); |
83f9a799a374
rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents:
26
diff
changeset
|
687 httpServer.removeAllListeners('request'); |
28
2ad2b6491aa9
rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents:
27
diff
changeset
|
688 ctlServer.close(); |
27
83f9a799a374
rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents:
26
diff
changeset
|
689 tslog("Shutting down..."); |
83f9a799a374
rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents:
26
diff
changeset
|
690 process.exit(); |
83f9a799a374
rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents:
26
diff
changeset
|
691 } |
83f9a799a374
rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents:
26
diff
changeset
|
692 |
83f9a799a374
rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents:
26
diff
changeset
|
693 function conHandler(chunk) { |
83f9a799a374
rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents:
26
diff
changeset
|
694 var msg = chunk.toString().split('\n')[0]; |
83f9a799a374
rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents:
26
diff
changeset
|
695 if (msg == "quit") { |
83f9a799a374
rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents:
26
diff
changeset
|
696 allowlogin = false; |
83f9a799a374
rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents:
26
diff
changeset
|
697 tslog("Disconnecting..."); |
83f9a799a374
rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents:
26
diff
changeset
|
698 for (var sessid in sessions) { |
83f9a799a374
rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents:
26
diff
changeset
|
699 sessions[sessid].close(); |
83f9a799a374
rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents:
26
diff
changeset
|
700 } |
83f9a799a374
rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents:
26
diff
changeset
|
701 setTimeout(shutdown, 10000); |
83f9a799a374
rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents:
26
diff
changeset
|
702 } |
83f9a799a374
rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents:
26
diff
changeset
|
703 } |
83f9a799a374
rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents:
26
diff
changeset
|
704 |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
705 process.on("exit", function () { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
706 for (var sessid in sessions) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
707 if (sessions[sessid].alive) |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
708 sessions[sessid].child.kill('SIGHUP'); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
709 } |
26
9b58f8d3ea70
rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents:
23
diff
changeset
|
710 tslog("Quitting..."); |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
711 return; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
712 }); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
713 |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
714 /* Initialization STARTS HERE */ |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
715 process.env["TERM"] = "xterm-256color"; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
716 |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
717 if (process.getuid() != 0) { |
26
9b58f8d3ea70
rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents:
23
diff
changeset
|
718 tslog("Not running as root, cannot chroot."); |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
719 process.exit(1); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
720 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
721 |
29
cf9d294bc52f
rlgwebd.js: fix reference error
John "Elwin" Edwards <elwin@sdf.org>
parents:
28
diff
changeset
|
722 var httpServer; // declare here so shutdown() can find it |
cf9d294bc52f
rlgwebd.js: fix reference error
John "Elwin" Edwards <elwin@sdf.org>
parents:
28
diff
changeset
|
723 |
28
2ad2b6491aa9
rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents:
27
diff
changeset
|
724 /* Open the control socket before chrooting where it can't be found */ |
2ad2b6491aa9
rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents:
27
diff
changeset
|
725 var ctlServer = net.createServer(function (sock) { |
2ad2b6491aa9
rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents:
27
diff
changeset
|
726 sock.on('data', conHandler); |
2ad2b6491aa9
rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents:
27
diff
changeset
|
727 }); |
2ad2b6491aa9
rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents:
27
diff
changeset
|
728 ctlServer.listen(ctlsocket, function () { |
2ad2b6491aa9
rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents:
27
diff
changeset
|
729 /* fork off and die */ |
2ad2b6491aa9
rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents:
27
diff
changeset
|
730 try { |
2ad2b6491aa9
rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents:
27
diff
changeset
|
731 daemon.start(logfile); |
2ad2b6491aa9
rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents:
27
diff
changeset
|
732 } |
2ad2b6491aa9
rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents:
27
diff
changeset
|
733 catch (err) { |
2ad2b6491aa9
rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents:
27
diff
changeset
|
734 tslog("Daemonization failed: %s", err); |
2ad2b6491aa9
rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents:
27
diff
changeset
|
735 process.exit(1); |
2ad2b6491aa9
rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents:
27
diff
changeset
|
736 } |
2ad2b6491aa9
rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents:
27
diff
changeset
|
737 /* chroot and drop permissions. daemon.chroot() does chdir() itself. */ |
2ad2b6491aa9
rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents:
27
diff
changeset
|
738 try { |
2ad2b6491aa9
rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents:
27
diff
changeset
|
739 daemon.chroot(chrootDir); |
2ad2b6491aa9
rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents:
27
diff
changeset
|
740 } |
2ad2b6491aa9
rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents:
27
diff
changeset
|
741 catch (err) { |
2ad2b6491aa9
rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents:
27
diff
changeset
|
742 tslog("chroot to %s failed: %s", chrootDir, err); |
2ad2b6491aa9
rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents:
27
diff
changeset
|
743 process.exit(1); |
2ad2b6491aa9
rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents:
27
diff
changeset
|
744 } |
2ad2b6491aa9
rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents:
27
diff
changeset
|
745 try { |
2ad2b6491aa9
rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents:
27
diff
changeset
|
746 // drop gid first, that requires UID=0 |
2ad2b6491aa9
rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents:
27
diff
changeset
|
747 process.setgid(dropToGID); |
2ad2b6491aa9
rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents:
27
diff
changeset
|
748 process.setuid(dropToUID); |
2ad2b6491aa9
rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents:
27
diff
changeset
|
749 } |
2ad2b6491aa9
rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents:
27
diff
changeset
|
750 catch (err) { |
2ad2b6491aa9
rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents:
27
diff
changeset
|
751 tslog("Could not drop permissions: %s", err); |
2ad2b6491aa9
rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents:
27
diff
changeset
|
752 process.exit(1); |
2ad2b6491aa9
rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents:
27
diff
changeset
|
753 } |
29
cf9d294bc52f
rlgwebd.js: fix reference error
John "Elwin" Edwards <elwin@sdf.org>
parents:
28
diff
changeset
|
754 httpServer = http.createServer(webHandler); |
30
b5570a594266
rlgwebd.js: listen on any address
John "Elwin" Edwards <elwin@sdf.org>
parents:
29
diff
changeset
|
755 httpServer.listen(httpPort); |
b5570a594266
rlgwebd.js: listen on any address
John "Elwin" Edwards <elwin@sdf.org>
parents:
29
diff
changeset
|
756 tslog('rlgwebd running on port %d', httpPort); |
28
2ad2b6491aa9
rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents:
27
diff
changeset
|
757 }); |
2ad2b6491aa9
rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents:
27
diff
changeset
|
758 |