Created variables.js to replace duplicate code. Moved more cache.js logs to debug level. Fixed incorrect internal authentication state on status page. Updated dependencies. Fixed incorrect type check on /api/voucher/:type for HA users

This commit is contained in:
Glenn de Haan
2024-09-05 20:44:38 +02:00
parent 228f512ce8
commit 8492616737
13 changed files with 148 additions and 183 deletions

View File

@@ -6,31 +6,12 @@ const fs = require('fs');
/**
* Import own modules
*/
const variables = require('./variables');
const log = require('./log');
const config = require('./config');
const logo = require('../utils/logo');
const types = require('../utils/types');
const time = require('../utils/time');
/**
* Define global variables
*/
const voucherTypes = types(config('voucher_types') || process.env.VOUCHER_TYPES || '480,1,,,;');
const voucherCustom = config('voucher_custom') !== null ? config('voucher_custom') : process.env.VOUCHER_CUSTOM ? process.env.VOUCHER_CUSTOM !== 'false' : true;
const webService = process.env.SERVICE_WEB ? process.env.SERVICE_WEB !== 'false' : true;
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 info to console
*/
@@ -62,43 +43,43 @@ module.exports = () => {
/**
* Log service status
*/
log.info(`[Service][Web] ${webService ? 'Enabled!' : 'Disabled!'}`);
log.info(`[Service][Api] ${apiService ? 'Enabled!' : 'Disabled!'}`);
log.info(`[Service][Web] ${variables.serviceWeb ? 'Enabled!' : 'Disabled!'}`);
log.info(`[Service][Api] ${variables.serviceApi ? 'Enabled!' : 'Disabled!'}`);
/**
* Log voucher types
*/
log.info('[Voucher] Loaded the following types:');
voucherTypes.forEach((type, key) => {
types(variables.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.info(`[Voucher][Custom] ${variables.voucherCustom ? 'Enabled!' : 'Disabled!'}`);
/**
* Log auth status
*/
log.info(`[Auth] ${authDisabled ? 'Disabled!' : `Enabled! Type: ${(oidcIssuerBaseUrl !== '' || oidcAppBaseUrl !== '' || oidcClientId !== '') ? 'OIDC' : 'Internal'}`}`);
log.info(`[Auth] ${variables.authDisabled ? 'Disabled!' : `Enabled! Type: ${(variables.authOidcIssuerBaseUrl !== '' || variables.authOidcAppBaseUrl !== '' || variables.authOidcClientId !== '') ? 'OIDC' : 'Internal'}`}`);
/**
* Verify OIDC configuration
*/
if(oidcIssuerBaseUrl !== '' && (oidcAppBaseUrl === '' || oidcClientId === '')) {
if(variables.authOidcIssuerBaseUrl !== '' && (variables.authOidcAppBaseUrl === '' || variables.authOidcClientId === '')) {
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 === '') {
if(variables.authOidcIssuerBaseUrl !== '' && variables.authOidcClientType === 'confidential' && variables.authOidcClientSecret === '') {
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.info(`[Printer] ${variables.printerType !== '' ? `Enabled! Type: ${variables.printerType}${variables.printerType === 'escpos' ? `, IP: ${variables.printerIp}` : ''}` : 'Disabled!'}`);
/**
* Log email status
*/
if(smtpFrom !== '' && smtpHost !== '' && smtpPort !== '') {
log.info(`[Email] Enabled! SMTP Server: ${smtpHost}:${smtpPort}`);
if(variables.smtpFrom !== '' && variables.smtpHost !== '' && variables.smtpPort !== '') {
log.info(`[Email] Enabled! SMTP Server: ${variables.smtpHost}:${variables.smtpPort}`);
} else {
log.info(`[Email] Disabled!`);
}
@@ -106,5 +87,5 @@ module.exports = () => {
/**
* 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'})`);
log.info(`[UniFi] Using Controller on: ${variables.unifiIp}:${variables.unifiPort} (Site ID: ${variables.unifiSiteId})`);
};

View File

@@ -6,12 +6,7 @@ const log = require('js-logger');
/**
* Import own modules
*/
const config = require('./config');
/**
* Define global variables
*/
const level = config('log_level') || process.env.LOG_LEVEL || "info";
const variables = require('./variables');
/**
* Setup logger
@@ -59,7 +54,7 @@ log.setHandler((messages, context) => {
/**
* Set log level
*/
log.setLevel(logConvert(level));
log.setLevel(logConvert(variables.logLevel));
/**
* Export the application logger

View File

@@ -8,29 +8,19 @@ const nodemailer = require('nodemailer');
/**
* Import own modules
*/
const config = require('./config');
const variables = require('./variables');
const log = require('./log');
/**
* Define global variables
*/
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 smtpSecure = config('smtp_secure') || process.env.SMTP_SECURE || false;
const smtpUsername = config('smtp_username') || process.env.SMTP_USERNAME || '';
const smtpPassword = config('smtp_password') || process.env.SMTP_PASSWORD || '';
/**
* Create nodemailer transport
*/
const transport = nodemailer.createTransport({
host: smtpHost,
port: parseInt(smtpPort),
secure: (smtpSecure === 'true' || smtpSecure === true),
host: variables.smtpHost,
port: parseInt(variables.smtpPort),
secure: (variables.smtpSecure === 'true' || variables.smtpSecure === true),
auth: {
user: smtpUsername,
pass: smtpPassword
user: variables.smtpUsername,
pass: variables.smtpPassword
}
});
@@ -48,7 +38,7 @@ module.exports = {
send: (to, voucher) => {
return new Promise(async (resolve) => {
await transport.sendMail({
from: smtpFrom,
from: variables.smtpFrom,
to: to,
subject: 'Your WiFi Voucher',
text: `Hi there,\n\nSomeone generated a WiFi Voucher, please use this code when connecting:\n\n${voucher.code.slice(0, 5)}-${voucher.code.slice(5)}`,

View File

@@ -7,6 +7,7 @@ const oidc = require('express-openid-connect');
/**
* Import own modules
*/
const variables = require('./variables');
const log = require('./log');
/**
@@ -15,16 +16,16 @@ const log = require('./log');
* @type {{baseURL: string, idpLogout: boolean, authRequired: boolean, clientID: string, issuerBaseURL: string, clientSecret: string, secret: string, authorizationParams: {scope: string, response_type: (string), response_mode: (string)}}}
*/
const settings = {
issuerBaseURL: process.env.AUTH_OIDC_ISSUER_BASE_URL,
baseURL: process.env.AUTH_OIDC_APP_BASE_URL,
clientID: process.env.AUTH_OIDC_CLIENT_ID,
clientSecret: process.env.AUTH_OIDC_CLIENT_SECRET,
issuerBaseURL: variables.authOidcIssuerBaseUrl,
baseURL: variables.authOidcAppBaseUrl,
clientID: variables.authOidcClientId,
clientSecret: variables.authOidcClientSecret,
secret: '',
idpLogout: true,
authRequired: false,
authorizationParams: {
response_type: (process.env.AUTH_OIDC_CLIENT_TYPE === 'confidential') ? 'code' : 'id_token',
response_mode: (process.env.AUTH_OIDC_CLIENT_TYPE === 'confidential') ? 'query' : 'form_post',
response_type: (variables.authOidcClientType === 'confidential') ? 'code' : 'id_token',
response_mode: (variables.authOidcClientType === 'confidential') ? 'query' : 'form_post',
scope: 'openid profile email'
}
};
@@ -42,6 +43,6 @@ module.exports = {
settings.secret = crypto.randomBytes(20).toString('hex');
log.info(`[OIDC] Set secret: ${settings.secret}`);
app.use(oidc.auth(settings));
log.info(`[OIDC] Issuer: ${settings.issuerBaseURL}, Client: ${settings.clientID}, Type: ${process.env.AUTH_OIDC_CLIENT_TYPE || 'public'}`);
log.info(`[OIDC] Issuer: ${settings.issuerBaseURL}, Client: ${settings.clientID}, Type: ${variables.authOidcClientType}`);
}
};

View File

@@ -8,17 +8,12 @@ const PrinterTypes = require('node-thermal-printer').types;
/**
* Import own modules
*/
const variables = require('./variables');
const log = require('./log');
const config = require('./config');
const time = require('../utils/time');
const bytes = require('../utils/bytes');
const size = require('../utils/size');
/**
* Define global variables
*/
const printerIp = config('printer_ip') || process.env.PRINTER_IP || '192.168.1.1';
/**
* Exports the printer module
*/
@@ -139,7 +134,7 @@ module.exports = {
return new Promise(async (resolve, reject) => {
const printer = new ThermalPrinter({
type: PrinterTypes.EPSON,
interface: `tcp://${printerIp}`
interface: `tcp://${variables.printerIp}`
});
const status = await printer.isPrinterConnected();

View File

@@ -6,18 +6,18 @@ const unifi = require('node-unifi');
/**
* Import own modules
*/
const config = require('./config');
const variables = require('./variables');
const log = require('./log');
/**
* UniFi Settings
*/
const settings = {
ip: config('unifi_ip') || process.env.UNIFI_IP || '192.168.1.1',
port: config('unifi_port') || process.env.UNIFI_PORT || 443,
username: config('unifi_username') || process.env.UNIFI_USERNAME || 'admin',
password: config('unifi_password') || process.env.UNIFI_PASSWORD || 'password',
siteID: config('unifi_site_id') || process.env.UNIFI_SITE_ID || 'default'
ip: variables.unifiIp,
port: variables.unifiPort,
username: variables.unifiUsername,
password: variables.unifiPassword,
siteID: variables.unifiSiteId
};
/**

36
modules/variables.js Normal file
View File

@@ -0,0 +1,36 @@
/**
* Import own modules
*/
const config = require('./config');
/**
* Exports all global variables used within the application
*/
module.exports = {
unifiIp: config('unifi_ip') || process.env.UNIFI_IP || '192.168.1.1',
unifiPort: config('unifi_port') || process.env.UNIFI_PORT || 443,
unifiUsername: config('unifi_username') || process.env.UNIFI_USERNAME || 'admin',
unifiPassword: config('unifi_password') || process.env.UNIFI_PASSWORD || 'password',
unifiSiteId: config('unifi_site_id') || process.env.UNIFI_SITE_ID || 'default',
voucherTypes: config('voucher_types') || process.env.VOUCHER_TYPES || '480,1,,,;',
voucherCustom: config('voucher_custom') !== null ? config('voucher_custom') : process.env.VOUCHER_CUSTOM ? process.env.VOUCHER_CUSTOM !== 'false' : true,
serviceWeb: process.env.SERVICE_WEB ? process.env.SERVICE_WEB !== 'false' : true,
serviceApi: config('service_api') || (process.env.SERVICE_API === 'true') || false,
authPassword: process.env.AUTH_PASSWORD || '0000',
authToken: process.env.AUTH_TOKEN || '0000',
authOidcIssuerBaseUrl: process.env.AUTH_OIDC_ISSUER_BASE_URL || '',
authOidcAppBaseUrl: process.env.AUTH_OIDC_APP_BASE_URL || '',
authOidcClientId: process.env.AUTH_OIDC_CLIENT_ID || '',
authOidcClientType: process.env.AUTH_OIDC_CLIENT_TYPE || 'public',
authOidcClientSecret: process.env.AUTH_OIDC_CLIENT_SECRET || '',
authDisabled: (process.env.AUTH_DISABLE === 'true') || false,
printerType: config('printer_type') || process.env.PRINTER_TYPE || '',
printerIp: config('printer_ip') || process.env.PRINTER_IP || '192.168.1.1',
smtpFrom: config('smtp_from') || process.env.SMTP_FROM || '',
smtpHost: config('smtp_host') || process.env.SMTP_HOST || '',
smtpPort: config('smtp_port') || process.env.SMTP_PORT || 25,
smtpSecure: config('smtp_secure') || process.env.SMTP_SECURE || false,
smtpUsername: config('smtp_username') || process.env.SMTP_USERNAME || '',
smtpPassword: config('smtp_password') || process.env.SMTP_PASSWORD || '',
logLevel: config('log_level') || process.env.LOG_LEVEL || 'info'
};