Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,6 @@ dist

# don't add logs to version control
/log

# don't check in local developer cruft
/dev-local
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions admins.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
{
"admins": [
{
"id": "188132867778281472",
"global_name": "Baredolf"
},
{
"id": "384483884915490826",
"global_name": "blinkdog"
Expand All @@ -22,5 +18,9 @@
}
],
"retired": [
{
"id": "188132867778281472",
"global_name": "Baredolf"
}
]
}
52 changes: 38 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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');
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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}`);
});
}
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand All @@ -12,11 +12,15 @@
},
"keywords": [
"discord",
"git",
"merge",
"server",
"shiptest",
"space station 13",
"ss13",
"starfly-13"
"starfly-13",
"test",
"test merge"
],
"author": "Patrick Meade <blinkdog@protonmail.com>",
"license": "AGPL-3.0",
Expand Down