From 60c4cf691e684ac1f3ed1137f3a2f14d40d9c52b Mon Sep 17 00:00:00 2001 From: Patrick Meade Date: Mon, 15 Jul 2024 09:04:17 -0500 Subject: [PATCH 1/2] Toward git builds and test merges --- .gitignore | 3 +++ README.md | 2 +- package.json | 8 ++++++-- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 05abcda..1e62d2e 100644 --- a/.gitignore +++ b/.gitignore @@ -131,3 +131,6 @@ dist # don't add logs to version control /log + +# don't check in local developer cruft +/dev-local diff --git a/README.md b/README.md index 067f3e1..774cfac 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ Web interface to control STARFLY-13 server uptime ## License -Uptime +Uptime Copyright 2024 Patrick Meade. This program is free software: you can redistribute it and/or modify diff --git a/package.json b/package.json index 8a46613..321a7c1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "uptime", - "version": "0.1.0", + "version": "0.2.0", "description": "Web interface to control STARFLY-13 server uptime", "main": "index.js", "scripts": { @@ -12,11 +12,15 @@ }, "keywords": [ "discord", + "git", + "merge", "server", "shiptest", "space station 13", "ss13", - "starfly-13" + "starfly-13", + "test", + "test merge" ], "author": "Patrick Meade ", "license": "AGPL-3.0", From 322cf207773bc997567409b7243c8e6f08251f07 Mon Sep 17 00:00:00 2001 From: Patrick Meade Date: Tue, 16 Jul 2024 05:46:28 -0500 Subject: [PATCH 2/2] Add support for external SSL; retire an admin --- admins.json | 8 ++++---- index.js | 52 ++++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 42 insertions(+), 18 deletions(-) diff --git a/admins.json b/admins.json index 5854a8d..dd4c083 100644 --- a/admins.json +++ b/admins.json @@ -1,9 +1,5 @@ { "admins": [ - { - "id": "188132867778281472", - "global_name": "Baredolf" - }, { "id": "384483884915490826", "global_name": "blinkdog" @@ -22,5 +18,9 @@ } ], "retired": [ + { + "id": "188132867778281472", + "global_name": "Baredolf" + } ] } diff --git a/index.js b/index.js index 447261c..3de5832 100644 --- a/index.js +++ b/index.js @@ -18,12 +18,11 @@ const { exec } = require('child_process'); const cookieParser = require('cookie-parser'); const express = require('express'); // const helmet = require('helmet') -const https = require('https'); const { request } = require('undici'); const util = require('util'); const { v4: uuidv4 } = require('uuid'); -const { admins: ADMINS } = require("./admins.json") +const execAsync = util.promisify(exec); const { BASE_URL, CLIENT_ID, CLIENT_SECRET, DISCORD_REDIRECT_URI, @@ -34,6 +33,9 @@ const { var lastDate = Date.now(); const sessionStore = {}; +//--------------------------------------------------------------------------------------------------------------------- +// create the app and define the routes +//--------------------------------------------------------------------------------------------------------------------- const app = express(); app.set('view engine', 'hbs'); @@ -189,6 +191,15 @@ const checkAuth = (req, res, next) => { // annotate the request with the user object const { user } = sessionStore[sessionId]; req.user = user; + // reload our admin credentials (yes, every time) + ADMINS = [] + try { + ADMINS = JSON.parse(fs.readFileSync('admins.json'))['admins']; + } catch (error) { + // if we couldn't read it, or parse it or something + console.log('Unable to read admin data from admins.json; NO admins FOR YOU!'); + console.error(error); + } // check our list of admins for (admin of ADMINS) { // if this user is on the list @@ -278,20 +289,9 @@ app.post('/server', checkAuth, async (req, res) => { return res.redirect('/server'); }); - -// start listening for incoming connections to the uptime service -const credentials = { cert: TLS_CERT, key: TLS_KEY }; -const httpsServer = https.createServer(credentials, app); -httpsServer.listen(PORT, '0.0.0.0', () => { - console.log(`Uptime is running on ${BASE_URL}`); -}); - - //--------------------------------------------------------------------------------------------------------------------- -// utility functions below... +// utility functions //--------------------------------------------------------------------------------------------------------------------- -const execAsync = util.promisify(exec); - // get the duration between the last event and now function getHumanReadableDuration(lastEvent) { @@ -465,3 +465,27 @@ async function stopDockerContainer(containerName) { console.error('Error executing docker stop command:', error); } } + +//--------------------------------------------------------------------------------------------------------------------- +// start listening for incoming connections to the uptime service +//--------------------------------------------------------------------------------------------------------------------- + +// if we were provided SSL/TLS credentials directly +if(TLS_CERT && TLS_KEY) { + // start an httpsServer, handling SSL/TLS ourselves directly + const https = require('https'); + const credentials = { cert: TLS_CERT, key: TLS_KEY }; + const httpsServer = https.createServer(credentials, app); + httpsServer.listen(PORT, '0.0.0.0', () => { + console.log(`Uptime is running on ${BASE_URL}`); + }); +} +// otherwise, we don't have SSL/TLS credentials +else { + // start an httpServer and hope that nginx is handling SSL/TLS + const http = require('http'); + const httpServer = http.createServer(app); + httpServer.listen(PORT, '0.0.0.0', () => { + console.log(`Uptime is running on ${BASE_URL}`); + }); +}