Mercurial > hg > rlgwebd
annotate rlgwebd.js @ 26:9b58f8d3ea70
rlgwebd.js: add timestamps to log messages.
Wrap console.log() with the tslog() function, which prepends timestamps.
Clean up some of the messages as well.
| author | John "Elwin" Edwards <elwin@sdf.org> | 
|---|---|
| date | Mon, 04 Jun 2012 10:19:36 -0700 | 
| parents | 21de24c08aed | 
| children | 83f9a799a374 | 
| rev | line source | 
|---|---|
| 0 
bd412f63ce0d
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 1 #!/usr/bin/env node | 
| 
bd412f63ce0d
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 2 | 
| 
bd412f63ce0d
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 3 // If you can't quite trust node to find it on its own | 
| 
bd412f63ce0d
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 4 var localModules = '/usr/local/lib/node_modules/'; | 
| 
bd412f63ce0d
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 5 var http = require('http'); | 
| 
bd412f63ce0d
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 6 var url = require('url'); | 
| 
bd412f63ce0d
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 7 var path = require('path'); | 
| 
bd412f63ce0d
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 8 var fs = require('fs'); | 
| 
bd412f63ce0d
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 9 var child_process = require('child_process'); | 
| 
bd412f63ce0d
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 10 var daemon = require(path.join(localModules, "daemon")); | 
| 
bd412f63ce0d
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 11 | 
| 
bd412f63ce0d
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 12 var chrootDir = "/var/dgl/"; | 
| 
bd412f63ce0d
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 13 var dropToUID = 501; | 
| 
bd412f63ce0d
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 14 var dropToGID = 501; | 
| 
bd412f63ce0d
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 15 var serveStaticRoot = "/var/www/"; // inside the chroot | 
| 
bd412f63ce0d
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 16 var passwdfile = "/dgldir/dgl-login"; | 
| 
bd412f63ce0d
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 17 var sessions = {}; | 
| 
bd412f63ce0d
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 18 | 
| 
bd412f63ce0d
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 19 var games = { | 
| 
bd412f63ce0d
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 20 "rogue3": { | 
| 
bd412f63ce0d
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 21 "name": "Rogue V3", | 
| 
bd412f63ce0d
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 22 "uname": "rogue3", | 
| 
bd412f63ce0d
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 23 "path": "/bin/rogue3" | 
| 
bd412f63ce0d
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 24 }, | 
| 
bd412f63ce0d
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 25 "rogue4": { | 
| 
bd412f63ce0d
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 26 "name": "Rogue V4", | 
| 
bd412f63ce0d
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 27 "uname": "rogue4", | 
| 
bd412f63ce0d
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 28 "path": "/bin/rogue4" | 
| 
bd412f63ce0d
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 29 }, | 
| 
bd412f63ce0d
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 30 "rogue5": { | 
| 
bd412f63ce0d
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 31 "name": "Rogue V5", | 
| 
bd412f63ce0d
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 32 "uname": "rogue5", | 
| 
bd412f63ce0d
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 33 "path": "/bin/rogue5" | 
| 
bd412f63ce0d
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 34 }, | 
| 
bd412f63ce0d
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 35 "srogue": { | 
| 
bd412f63ce0d
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 36 "name": "Super-Rogue", | 
| 
bd412f63ce0d
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 37 "uname": "srogue", | 
| 
bd412f63ce0d
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 38 "path": "/bin/srogue" | 
| 
bd412f63ce0d
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 39 } | 
| 
bd412f63ce0d
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 40 }; | 
| 
bd412f63ce0d
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 41 | 
| 
bd412f63ce0d
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 42 /* Constructor for TermSessions. Note that it opens the terminal and | 
| 
bd412f63ce0d
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 43 * adds itself to the sessions dict. It currently assumes the user has | 
| 
bd412f63ce0d
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 44 * been authenticated. | 
| 
bd412f63ce0d
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 45 */ | 
| 16 
ef6127ed6da3
RLGWeb: switch to JSON protocol.
 John "Elwin" Edwards <elwin@sdf.org> parents: 
8diff
changeset | 46 function TermSession(game, user, files, dims) { | 
| 0 
bd412f63ce0d
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 47 /* First make sure starting the game will work. */ | 
| 
bd412f63ce0d
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 48 if (!(game in games)) { | 
| 
bd412f63ce0d
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 49 // TODO: throw an exception instead | 
| 
bd412f63ce0d
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 50 return null; | 
| 
bd412f63ce0d
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 51 } | 
| 
bd412f63ce0d
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 52 /* This order seems to best avoid race conditions... */ | 
| 
bd412f63ce0d
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 53 this.alive = false; | 
| 
bd412f63ce0d
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 54 this.sessid = randkey(); | 
| 
bd412f63ce0d
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 55 while (this.sessid in sessions) { | 
| 
bd412f63ce0d
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 56 this.sessid = randkey(); | 
| 
bd412f63ce0d
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 57 } | 
| 
bd412f63ce0d
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 58 /* Grab a spot in the sessions table. */ | 
| 
bd412f63ce0d
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 59 sessions[this.sessid] = this; | 
| 16 
ef6127ed6da3
RLGWeb: switch to JSON protocol.
 John "Elwin" Edwards <elwin@sdf.org> parents: 
8diff
changeset | 60 /* State for messaging. */ | 
| 
ef6127ed6da3
RLGWeb: switch to JSON protocol.
 John "Elwin" Edwards <elwin@sdf.org> parents: 
8diff
changeset | 61 this.nsend = 0; | 
| 
ef6127ed6da3
RLGWeb: switch to JSON protocol.
 John "Elwin" Edwards <elwin@sdf.org> parents: 
8diff
changeset | 62 this.nrecv = 0; | 
| 
ef6127ed6da3
RLGWeb: switch to JSON protocol.
 John "Elwin" Edwards <elwin@sdf.org> parents: 
8diff
changeset | 63 this.msgQ = [] | 
| 23 
21de24c08aed
Implement message order correction on the server side.
 John "Elwin" Edwards <elwin@sdf.org> parents: 
22diff
changeset | 64 this.Qtimeout = null; | 
| 16 
ef6127ed6da3
RLGWeb: switch to JSON protocol.
 John "Elwin" Edwards <elwin@sdf.org> parents: 
8diff
changeset | 65 /* Set up the sizes. */ | 
| 
ef6127ed6da3
RLGWeb: switch to JSON protocol.
 John "Elwin" Edwards <elwin@sdf.org> parents: 
8diff
changeset | 66 this.w = Math.floor(Number(dims[1])); | 
| 
ef6127ed6da3
RLGWeb: switch to JSON protocol.
 John "Elwin" Edwards <elwin@sdf.org> parents: 
8diff
changeset | 67 if (!(this.w > 0 && this.w < 256)) | 
| 
ef6127ed6da3
RLGWeb: switch to JSON protocol.
 John "Elwin" Edwards <elwin@sdf.org> parents: 
8diff
changeset | 68 this.w = 80; | 
| 
ef6127ed6da3
RLGWeb: switch to JSON protocol.
 John "Elwin" Edwards <elwin@sdf.org> parents: 
8diff
changeset | 69 this.h = Math.floor(Number(dims[0])); | 
| 
ef6127ed6da3
RLGWeb: switch to JSON protocol.
 John "Elwin" Edwards <elwin@sdf.org> parents: 
8diff
changeset | 70 if (!(this.h > 0 && this.h < 256)) | 
| 
ef6127ed6da3
RLGWeb: switch to JSON protocol.
 John "Elwin" Edwards <elwin@sdf.org> parents: 
8diff
changeset | 71 this.h = 24; | 
| 
ef6127ed6da3
RLGWeb: switch to JSON protocol.
 John "Elwin" Edwards <elwin@sdf.org> parents: 
8diff
changeset | 72 /* Environment. */ | 
| 
ef6127ed6da3
RLGWeb: switch to JSON protocol.
 John "Elwin" Edwards <elwin@sdf.org> parents: 
8diff
changeset | 73 var childenv = {}; | 
| 
ef6127ed6da3
RLGWeb: switch to JSON protocol.
 John "Elwin" Edwards <elwin@sdf.org> parents: 
8diff
changeset | 74 for (var key in process.env) { | 
| 
ef6127ed6da3
RLGWeb: switch to JSON protocol.
 John "Elwin" Edwards <elwin@sdf.org> parents: 
8diff
changeset | 75 childenv[key] = process.env[key]; | 
| 
ef6127ed6da3
RLGWeb: switch to JSON protocol.
 John "Elwin" Edwards <elwin@sdf.org> parents: 
8diff
changeset | 76 } | 
| 
ef6127ed6da3
RLGWeb: switch to JSON protocol.
 John "Elwin" Edwards <elwin@sdf.org> parents: 
8diff
changeset | 77 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 | 78 /* TODO handle tty-opening errors */ | 
| 
bd412f63ce0d
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 79 /* TODO make argument-finding into a method */ | 
| 
bd412f63ce0d
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 80 args = [games[game].path, "-n", user.toString()]; | 
| 16 
ef6127ed6da3
RLGWeb: switch to JSON protocol.
 John "Elwin" Edwards <elwin@sdf.org> parents: 
8diff
changeset | 81 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 | 82 var ss = this; | 
| 
bd412f63ce0d
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 83 this.alive = true; | 
| 
bd412f63ce0d
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 84 this.data = []; | 
| 
bd412f63ce0d
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 85 this.lock = files[0]; | 
| 16 | 
