Moved application startup logs to dedicated info.js module. Lowered log output from unifi.js. Created tag.ejs partial. Implemented dedicated application status page. Updated voucher.ejs to implement status link. Lowered log output from cache.js. General cleanup of server.js. Implemented 501 Not Implemented responses when features are not enabled or configured properly

This commit is contained in:
Glenn de Haan
2024-09-04 18:01:41 +02:00
parent fefdab9b79
commit 86ad4457de
8 changed files with 562 additions and 81 deletions

106
server.js
View File

@@ -1,7 +1,6 @@
/**
* Import base packages
*/
const fs = require('fs');
const os = require('os');
const express = require('express');
const multer = require('multer');
@@ -14,10 +13,7 @@ const config = require('./modules/config');
const log = require('./modules/log');
const cache = require('./modules/cache');
const jwt = require('./modules/jwt');
const logo = require('./utils/logo');
const types = require('./utils/types');
const time = require('./utils/time');
const bytes = require('./utils/bytes');
const info = require('./modules/info');
const unifi = require('./modules/unifi');
const print = require('./modules/print');
const mail = require('./modules/mail');
@@ -33,6 +29,10 @@ const flashMessage = require('./middlewares/flashMessage');
* Import own utils
*/
const {updateCache} = require('./utils/cache');
const types = require('./utils/types');
const time = require('./utils/time');
const bytes = require('./utils/bytes');
const status = require('./utils/status');
/**
* Setup Express app
@@ -48,83 +48,17 @@ const webService = process.env.SERVICE_WEB ? process.env.SERVICE_WEB !== 'false'
const apiService = config('service_api') || (process.env.SERVICE_API === 'true') || false;
const authDisabled = (process.env.AUTH_DISABLE === 'true') || false;
const printerType = config('printer_type') || process.env.PRINTER_TYPE || '';
const printerIp = config('printer_ip') || process.env.PRINTER_IP || '192.168.1.1';
const smtpFrom = config('smtp_from') || process.env.SMTP_FROM || '';
const smtpHost = config('smtp_host') || process.env.SMTP_HOST || '';
const smtpPort = config('smtp_port') || process.env.SMTP_PORT || 25;
const oidcIssuerBaseUrl = process.env.AUTH_OIDC_ISSUER_BASE_URL || '';
const oidcAppBaseUrl = process.env.AUTH_OIDC_APP_BASE_URL || '';
const oidcClientId = process.env.AUTH_OIDC_CLIENT_ID || '';
const oidcClientType = process.env.AUTH_OIDC_CLIENT_TYPE || 'public';
const oidcClientSecret = process.env.AUTH_OIDC_CLIENT_SECRET || '';
/**
* Output logo
* Output info
*/
logo();
/**
* Output build version
*/
if(fs.existsSync('/etc/unifi_voucher_site_build')) {
log.info(`[Version] ${fs.readFileSync('/etc/unifi_voucher_site_build', 'utf-8')}`);
} else {
log.info(`[Version] **DEVELOPMENT**`);
}
/**
* Log external config
*/
if (fs.existsSync('/data/options.json')) {
log.info('[Options] Found at /data/options.json');
}
if (fs.existsSync(`${__dirname}/.options.json`)) {
log.info(`[Options] Found at ${__dirname}/.options.json`);
}
/**
* Log service status
*/
log.info(`[Service][Web] ${webService ? 'Enabled!' : 'Disabled!'}`);
log.info(`[Service][Api] ${apiService ? 'Enabled!' : 'Disabled!'}`);
/**
* Log voucher types
*/
log.info('[Voucher] Loaded the following types:');
voucherTypes.forEach((type, key) => {
log.info(`[Voucher][Type][${key}] ${time(type.expiration)}, ${type.usage === '1' ? 'single-use' : 'multi-use'}${typeof type.upload === "undefined" && typeof type.download === "undefined" && typeof type.megabytes === "undefined" ? ', no limits' : `${typeof type.upload !== "undefined" ? `, upload bandwidth limit: ${type.upload} kb/s` : ''}${typeof type.download !== "undefined" ? `, download bandwidth limit: ${type.download} kb/s` : ''}${typeof type.megabytes !== "undefined" ? `, quota limit: ${type.megabytes} mb` : ''}`}`);
});
log.info(`[Voucher][Custom] ${voucherCustom ? 'Enabled!' : 'Disabled!'}`);
/**
* Log auth status
*/
log.info(`[Auth] ${authDisabled ? 'Disabled!' : `Enabled! Type: ${(oidcIssuerBaseUrl !== '' || oidcAppBaseUrl !== '' || oidcClientId !== '') ? 'OIDC' : 'Internal'}`}`);
/**
* Verify OIDC configuration
*/
if(oidcIssuerBaseUrl !== '' && (oidcAppBaseUrl === '' || oidcClientId === '')) {
log.error(`[OIDC] Incorrect Configuration Detected!. Verify 'AUTH_OIDC_ISSUER_BASE_URL', 'AUTH_OIDC_APP_BASE_URL' and 'AUTH_OIDC_CLIENT_ID' are set! Authentication will be unstable or disabled until issue is resolved!`);
}
if(oidcIssuerBaseUrl !== '' && oidcClientType === 'confidential' && oidcClientSecret === '') {
log.error(`[OIDC] Incorrect Configuration Detected!. Verify 'AUTH_OIDC_CLIENT_SECRET' is set! Authentication will be unstable or disabled until issue is resolved!`);
}
/**
* Log printer status
*/
log.info(`[Printer] ${printerType !== '' ? `Enabled! Type: ${printerType}${printerType === 'escpos' ? `, IP: ${printerIp}` : ''}` : 'Disabled!'}`);
/**
* Log email status
*/
if(smtpFrom !== '' && smtpHost !== '' && smtpPort !== '') {
log.info(`[Email] Enabled! SMTP Server: ${smtpHost}:${smtpPort}`);
} else {
log.info(`[Email] Disabled!`);
}
info();
/**
* Initialize JWT
@@ -133,11 +67,6 @@ if(!authDisabled && (oidcIssuerBaseUrl === '' && oidcAppBaseUrl === '' && oidcCl
jwt.init();
}
/**
* Log controller
*/
log.info(`[UniFi] Using Controller on: ${config('unifi_ip') || process.env.UNIFI_IP || '192.168.1.1'}:${config('unifi_port') || process.env.UNIFI_PORT || 443} (Site ID: ${config('unifi_site_id') || process.env.UNIFI_SITE_ID || 'default'})`);
/**
* Trust proxy
*/
@@ -305,6 +234,11 @@ if(webService) {
}
});
app.get('/voucher/:id/print', [authorization.web], async (req, res) => {
if(printerType === '') {
res.status(501).send();
return;
}
const voucher = cache.vouchers.find((e) => {
return e._id === req.params.id;
});
@@ -337,6 +271,11 @@ if(webService) {
}
});
app.get('/voucher/:id/email', [authorization.web], async (req, res) => {
if(smtpFrom === '' || smtpHost === '' || smtpPort === '') {
res.status(501).send();
return;
}
const voucher = cache.vouchers.find((e) => {
return e._id === req.params.id;
});
@@ -357,6 +296,11 @@ if(webService) {
}
});
app.post('/voucher/:id/email', [authorization.web], async (req, res) => {
if(smtpFrom === '' || smtpHost === '' || smtpPort === '') {
res.status(501).send();
return;
}
if (typeof req.body === "undefined") {
res.status(400).send();
return;
@@ -445,6 +389,12 @@ if(webService) {
});
}
});
app.get('/status', [authorization.web], (req, res) => {
res.render('status', {
baseUrl: req.headers['x-ingress-path'] ? req.headers['x-ingress-path'] : '',
status: status()
});
});
}
if(apiService) {