rlgwebd.js: read commands from the console
The RLG-Web server can now be controlled with commands sent to stdin. Currently, the only one implemented is "quit". Some improvements to the shutdown process were also made.
This commit is contained in:
parent
8599ec081d
commit
d0e3b1da2f
2 changed files with 44 additions and 8 deletions
|
|
@ -132,7 +132,7 @@ function processMsg(msg) {
|
||||||
if (!msgDict.t)
|
if (!msgDict.t)
|
||||||
return null;
|
return null;
|
||||||
else if (msgDict.t == "E") {
|
else if (msgDict.t == "E") {
|
||||||
if (msgDict.c == 1) {
|
if (msgDict.c == 1 || msgDict.c == 6) {
|
||||||
logout();
|
logout();
|
||||||
}
|
}
|
||||||
debug(1, "Server error: " + msgDict.s);
|
debug(1, "Server error: " + msgDict.s);
|
||||||
|
|
|
||||||
50
rlgwebd.js
50
rlgwebd.js
|
|
@ -15,6 +15,7 @@ var dropToGID = 501;
|
||||||
var serveStaticRoot = "/var/www/"; // inside the chroot
|
var serveStaticRoot = "/var/www/"; // inside the chroot
|
||||||
var passwdfile = "/dgldir/dgl-login";
|
var passwdfile = "/dgldir/dgl-login";
|
||||||
var sessions = {};
|
var sessions = {};
|
||||||
|
var allowlogin = true;
|
||||||
|
|
||||||
var games = {
|
var games = {
|
||||||
"rogue3": {
|
"rogue3": {
|
||||||
|
|
@ -198,8 +199,9 @@ function TermSession(game, user, files, dims) {
|
||||||
ss.remove();
|
ss.remove();
|
||||||
};
|
};
|
||||||
this.remove = function () {
|
this.remove = function () {
|
||||||
delete sessions[ss.sessid];
|
var id = ss.sessid;
|
||||||
tslog("Session %s removed.", this.sessid);
|
delete sessions[id];
|
||||||
|
tslog("Session %s removed.", id);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -305,6 +307,10 @@ function auth(username, password) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function login(req, res, formdata) {
|
function login(req, res, formdata) {
|
||||||
|
if (!allowlogin) {
|
||||||
|
sendError(res, 6, null);
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (!("game" in formdata)) {
|
if (!("game" in formdata)) {
|
||||||
sendError(res, 2, "No game specified.");
|
sendError(res, 2, "No game specified.");
|
||||||
return;
|
return;
|
||||||
|
|
@ -521,8 +527,14 @@ function readFeed(res, term) {
|
||||||
if (result == null) {
|
if (result == null) {
|
||||||
if (term.alive)
|
if (term.alive)
|
||||||
reply.t = "n";
|
reply.t = "n";
|
||||||
else
|
else {
|
||||||
reply.t = "q";
|
if (allowlogin)
|
||||||
|
reply.t = "q";
|
||||||
|
else {
|
||||||
|
sendError(res, 6, null);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
reply.t = "d";
|
reply.t = "d";
|
||||||
|
|
@ -539,7 +551,8 @@ function readFeed(res, term) {
|
||||||
}
|
}
|
||||||
|
|
||||||
var errorcodes = [ "Generic Error", "Not logged in", "Invalid data",
|
var errorcodes = [ "Generic Error", "Not logged in", "Invalid data",
|
||||||
"Login failed", "Already playing", "Game launch failed" ];
|
"Login failed", "Already playing", "Game launch failed",
|
||||||
|
"Server shutting down" ];
|
||||||
|
|
||||||
function sendError(res, ecode, msg) {
|
function sendError(res, ecode, msg) {
|
||||||
res.writeHead(200, { "Content-Type": "text/plain" });
|
res.writeHead(200, { "Content-Type": "text/plain" });
|
||||||
|
|
@ -554,7 +567,7 @@ function sendError(res, ecode, msg) {
|
||||||
res.end();
|
res.end();
|
||||||
}
|
}
|
||||||
|
|
||||||
function handler(req, res) {
|
function webHandler(req, res) {
|
||||||
/* default headers for the response */
|
/* default headers for the response */
|
||||||
var resheaders = {'Content-Type': 'text/html'};
|
var resheaders = {'Content-Type': 'text/html'};
|
||||||
/* The request body will be added to this as it arrives. */
|
/* The request body will be added to this as it arrives. */
|
||||||
|
|
@ -635,6 +648,26 @@ function handler(req, res) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function shutdown () {
|
||||||
|
httpServer.close();
|
||||||
|
httpServer.removeAllListeners('request');
|
||||||
|
process.stdin.removeAllListeners('data');
|
||||||
|
tslog("Shutting down...");
|
||||||
|
process.exit();
|
||||||
|
}
|
||||||
|
|
||||||
|
function conHandler(chunk) {
|
||||||
|
var msg = chunk.toString().split('\n')[0];
|
||||||
|
if (msg == "quit") {
|
||||||
|
allowlogin = false;
|
||||||
|
tslog("Disconnecting...");
|
||||||
|
for (var sessid in sessions) {
|
||||||
|
sessions[sessid].close();
|
||||||
|
}
|
||||||
|
setTimeout(shutdown, 10000);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
process.on("exit", function () {
|
process.on("exit", function () {
|
||||||
for (var sessid in sessions) {
|
for (var sessid in sessions) {
|
||||||
if (sessions[sessid].alive)
|
if (sessions[sessid].alive)
|
||||||
|
|
@ -675,5 +708,8 @@ catch (err) {
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
http.createServer(handler).listen(8080, "127.0.0.1");
|
process.stdin.on('data', conHandler);
|
||||||
|
process.stdin.resume();
|
||||||
|
var httpServer = http.createServer(webHandler);
|
||||||
|
httpServer.listen(8080, "127.0.0.1");
|
||||||
tslog('rlgwebd running at http://127.0.0.1:8080/');
|
tslog('rlgwebd running at http://127.0.0.1:8080/');
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue