view rlgwebd @ 203:5491ca3a335b

Fail cleanly if necessary files can't be opened.
author John "Elwin" Edwards
date Wed, 04 Jan 2017 21:25:43 -0500
parents 7f25bb89b59c
children a200b313870d
line wrap: on
line source

#!/usr/bin/env node

var http = require('http');
var https = require('https');
var net = require('net');
var url = require('url');
var path = require('path');
var fs = require('fs');
var events = require('events');
var child_process = require('child_process');
// Dependencies
var posix = require("posix");
var pty = require("pty.js");
var WebSocketServer = require("websocket").server;

/* Default options */
var rlgwebd_options = { 
  control_socket: "/var/run/rlgwebd.sock",
  http_port: 8080,
  https_port: 8081,
  chrootDir: "/var/dgl/",
  username: "rodney",
  static_root: "/var/www/"
};

/* Read configuration from a file */
var config_file = "/etc/rlgwebd.conf";
var config_lines = read_or_die(config_file, "Configuration file").toString().split('\n');

for (var i = 0; i < config_lines.length; i++) {
  if (config_lines[i].length > 0 && config_lines[i][0] != '#') {
    var config_fields = config_lines[i].split('=');
    if (config_fields.length < 2)
      continue;
    var option_name = config_fields[0].trim();
    // This can't handle values containing '=' or whitespace at the end
    var option_value = config_fields[1].trim();
    rlgwebd_options[option_name] = option_value;
  }
}

/* Should HTTPS be enabled? */
if ("domain_name" in rlgwebd_options && "keyfile" in rlgwebd_options &&
    "certfile" in rlgwebd_options)
  rlgwebd_options["use_https"] = true;

var clearbufs = [
  new Buffer([27, 91, 72, 27, 91, 50, 74]), // xterm: CSI H CSI 2J
  new Buffer([27, 91, 72, 27, 91, 74]) // screen: CSI H CSI J
];

/* Data on the games available. */
var games = {
  "rogue3": {
    "name": "Rogue V3",
    "uname": "rogue3",
    "suffix": ".r3sav",
    "path": "/usr/bin/rogue3"
  },
  "rogue4": {
    "name": "Rogue V4",
    "uname": "rogue4",
    "suffix": ".r4sav",
    "path": "/usr/bin/rogue4"
  },
  "rogue5": {
    "name": "Rogue V5",
    "uname": "rogue5",
    "suffix": ".r5sav",
    "path": "/usr/bin/rogue5"
  },
  "srogue": {
    "name": "Super-Rogue",
    "uname": "srogue",
    "suffix": ".srsav",
    "path": "/usr/bin/srogue"
  },
  "arogue5": {
    "name": "Advanced Rogue 5",
    "uname": "arogue5",
    "suffix": ".ar5sav",
    "path": "/usr/bin/arogue5"
  },
  "arogue7": {
    "name": "Advanced Rogue 7",
    "uname": "arogue7",
    "suffix": ".ar7sav",
    "path": "/usr/bin/arogue7"
  },
  "xrogue": {
    "name": "XRogue",
    "uname": "xrogue",
    "suffix": ".xrsav",
    "path": "/usr/bin/xrogue"
  }
};

/* Global state */
var logins = {};
var sessions = {};
var dglgames = {};
var allowlogin = true;
var gamemux = new events.EventEmitter();

/* A base class.  TermSession and DglSession inherit from it. */
function BaseGame() {
  /* Games subclass EventEmitter, though there are few listeners. */
  events.EventEmitter.call(this);
  /* Array of watching WebSockets. */
  this.watchers = [];
  /* replaybuf holds the output since the last screen clear, so watchers can
   * begin with a complete screen. replaylen is the number of bytes stored. */
  this.replaybuf = new Buffer(1024);
  this.replaylen = 0;
  /* Time of last activity. */
  this.lasttime = new Date();
}
BaseGame.prototype = new events.EventEmitter();

BaseGame.prototype.tag = function () {
  if (this.pname === undefined || this.gname === undefined)
    return "";
  return this.gname + "/" + this.pname;
};

BaseGame.prototype.framepush = function(chunk) {
  /* If this chunk resets the screen, discard what preceded it. */
  if (isclear(chunk)) {
    this.replaybuf = new Buffer(1024);
    this.replaylen = 0;
  }
  /* Make sure there's space. */
  while (this.replaybuf.length < chunk.length + this.replaylen) {
    var nbuf = new Buffer(this.replaybuf.length * 2);
    this.replaybuf.copy(nbuf, 0, 0, this.replaylen);
    this.replaybuf = nbuf;
    if (this.replaybuf.length > 65536) {
      tslog("Warning: %s frame buffer at %d bytes", this.tag(), 
              this.replaybuf.length);
    }
  }
  chunk.copy(this.replaybuf, this.replaylen);
  this.replaylen += chunk.length;
};

/* Adds a watcher. */
BaseGame.prototype.attach = function (wsReq) {
  var conn = wsReq.accept(null, wsReq.origin);
  conn.sendUTF(JSON.stringify({
          "t": "w", "w": this.w, "h": this.h, "p": this.pname, "g": this.gname
  }));
  conn.sendUTF(JSON.stringify({"t": "d",
      "d": this.replaybuf.toString("hex", 0, this.replaylen)}));
  conn.on('close', this.detach.bind(this, conn));
  this.watchers.push(conn);
};

BaseGame.prototype.detach = function (socket) {
  var n = this.watchers.indexOf(socket);
  if (n >= 0) {
    this.watchers.splice(n, 1);
    tslog("A WebSocket watcher has left game %s", this.tag());