1 const express = require("express");
2 const cors = require("cors");
3 const body_parser = require("body-parser");
4 const session = require("express-session");
5 const bcrypt = require("bcrypt");
6 const path = require("path");
9 var lmno_config = require("./lmno-config.json");
15 function config_usage() {
16 console.log(`Error: Refusing to run without configuration.
18 Please create a file named lmno-config.json that looks as follows:
21 "session_secret": "<this should be a long string of true-random characters>",
23 "username": "<username>",
24 "password_hash_bcrypt": "<password_hash_made_by_bcrypt>"
28 Note: Of course, change all of <these-parts> to actual values desired.
30 The "node lmno-passwd.js" command can help generate password hashes.`);
33 const app = express();
35 app.use(body_parser.urlencoded({ extended: false }));
36 app.use(body_parser.json());
38 secret: lmno_config.session_secret,
40 saveUninitialized: false
43 /* Load each of our game mini-apps. */
44 var empires = require("./empires");
52 return [null,null,null,null].map(() => LMNO.letters.charAt(Math.floor(Math.random() * LMNO.letters.length))).join('');
57 var id = this.generate_id();
58 } while (id in this.ids);
60 const game = new empires.Game();
72 /* Some letters we don't use in our IDs:
74 * 1. Vowels (AEIOU) to avoid accidentally spelling an unfortunate word
75 * 2. Lowercase letters (replace with corresponding capital on input)
76 * 3. N (replace with M on input)
77 * 4. P (replace with B on input)
78 * 5. S (replace with F on input)
80 LMNO.letters = "BCDFGHJKLMQRTVWXYZ";
82 const lmno = new LMNO();
84 /* Force a game ID into a canonical form as described above. */
85 function lmno_canonize(id) {
87 id = id.toUpperCase();
89 /* Replace unused letters with nearest phonetic match. */
90 id = id.replace(/N/g, 'M');
91 id = id.replace(/P/g, 'B');
92 id = id.replace(/S/g, 'F');
94 /* Replace unused numbers nearest visual match. */
95 id = id.replace(/0/g, 'O');
96 id = id.replace(/1/g, 'I');
97 id = id.replace(/5/g, 'S');
102 app.post('/new/:game_engine', (request, response) => {
103 const game_engine = request.params.game_engine;
104 const game_id = lmno.create_game(game_engine);
105 response.send(JSON.stringify(game_id));
108 /* Redirect any requests to a game ID at the top-level.
110 * Specifically, after obtaining the game ID (from the path) we simply
111 * lookup the game engine for the corresponding game and then redirect
112 * to the engine- and game-specific path.
114 app.get('/[a-zA-Z0-9]{4}', (request, response) => {
115 const game_id = request.path.replace(/\//g, "");
116 const canon_id = lmno_canonize(game_id);
118 /* Redirect user to page with the canonical ID in it. */
119 if (game_id !== canon_id) {
120 response.redirect(301, `/${canon_id}/`);
124 const game = lmno.ids[game_id];
125 if (game === undefined) {
126 response.sendStatus(404);
129 response.redirect(301, `/${game.engine}/${game.id}/`);
132 /* LMNO middleware to lookup the game. */
133 app.use('/empires/:game_id([a-zA-Z0-9]{4})', (request, response, next) => {
134 const game_id = request.params.game_id;
135 const canon_id = lmno_canonize(game_id);
137 /* Redirect user to page with the canonical ID in it. */
138 if (game_id !== canon_id) {
139 const new_url = request.originalUrl.replace("/empires/" + game_id,
140 "/empires/" + canon_id);
141 response.redirect(301, new_url);
145 /* See if there is any game with this ID. */
146 const game = lmno.ids[game_id];
147 if (game === undefined) {
148 response.sendStatus(404);
152 /* Stash the game onto the request to be used by the game-specific code. */
153 request.game = game.game;
157 function auth_admin(request, response, next) {
158 /* If there is no user associated with this session, redirect to the login
159 * page (and set a "next" query parameter so we can come back here).
161 if (! request.session.user) {
162 response.redirect(302, "/login?next=" + request.path);
166 /* If the user is logged in but not authorized to view the page then
167 * we return that error. */
168 if (request.session.user.role !== "admin") {
169 response.status(401).send("Unauthorized");
175 app.get('/logout', (request, response) => {
176 request.session.user = undefined;
178 response.send("You are now logged out.");
181 app.get('/login', (request, response) => {
182 if (request.session.user) {
183 response.send("Welcome, " + request.session.user + ".");
187 response.sendFile(path.join(__dirname, './login.html'));
190 app.post('/login', async (request, response) => {
191 const username = request.body.username;
192 const password = request.body.password;
193 const user = lmno_config.users[username];
195 response.sendStatus(404);
198 const match = await bcrypt.compare(password, user.password_hash_bcrypt);
200 response.sendStatus(404);
203 request.session.user = { username: user.username, role: user.role };
204 response.sendStatus(200);
208 /* A stats page (only available to admin users) */
209 app.get('/stats/', auth_admin, (request, response) => {
213 for (let id in lmno.ids) {
214 if (lmno.ids[id].game.clients.length)
219 response.send(`<html><body>Active games: ${active}.<br>
220 Idle games: ${idle}</body></html>`);
224 /* Mount sub apps. only _after_ we have done all the middleware we need. */
225 app.use('/empires/[a-zA-Z0-9]{4}/', empires.app);
227 app.listen(4000, function () {
228 console.log('LMNO server listening on localhost:4000');