You've already forked nginx-proxy-manager
							
							
				mirror of
				https://github.com/NginxProxyManager/nginx-proxy-manager.git
				synced 2025-10-30 18:05:34 +03:00 
			
		
		
		
	Convert backend to ESM
- About 5 years overdue - Remove eslint, use bomejs instead
This commit is contained in:
		| @@ -1,6 +1,6 @@ | ||||
| const fs      = require('fs'); | ||||
| const NodeRSA = require('node-rsa'); | ||||
| const logger  = require('../logger').global; | ||||
| import fs from "node:fs"; | ||||
| import NodeRSA from "node-rsa"; | ||||
| import { global as logger } from "../logger.js"; | ||||
|  | ||||
| const keysFile         = '/data/keys.json'; | ||||
| const mysqlEngine      = 'mysql2'; | ||||
| @@ -12,18 +12,20 @@ let instance = null; | ||||
| // 1. Load from config file first (not recommended anymore) | ||||
| // 2. Use config env variables next | ||||
| const configure = () => { | ||||
| 	const filename = (process.env.NODE_CONFIG_DIR || './config') + '/' + (process.env.NODE_ENV || 'default') + '.json'; | ||||
| 	const filename = `${process.env.NODE_CONFIG_DIR || "./config"}/${process.env.NODE_ENV || "default"}.json`; | ||||
| 	if (fs.existsSync(filename)) { | ||||
| 		let configData; | ||||
| 		try { | ||||
| 			configData = require(filename); | ||||
| 			// Load this json  synchronously | ||||
| 			const rawData = fs.readFileSync(filename); | ||||
| 			configData = JSON.parse(rawData); | ||||
| 		} catch (_) { | ||||
| 			// do nothing | ||||
| 		} | ||||
|  | ||||
| 		if (configData && configData.database) { | ||||
| 		if (configData?.database) { | ||||
| 			logger.info(`Using configuration from file: ${filename}`); | ||||
| 			instance      = configData; | ||||
| 			instance = configData; | ||||
| 			instance.keys = getKeys(); | ||||
| 			return; | ||||
| 		} | ||||
| @@ -34,15 +36,15 @@ const configure = () => { | ||||
| 	const envMysqlName = process.env.DB_MYSQL_NAME || null; | ||||
| 	if (envMysqlHost && envMysqlUser && envMysqlName) { | ||||
| 		// we have enough mysql creds to go with mysql | ||||
| 		logger.info('Using MySQL configuration'); | ||||
| 		logger.info("Using MySQL configuration"); | ||||
| 		instance = { | ||||
| 			database: { | ||||
| 				engine:   mysqlEngine, | ||||
| 				host:     envMysqlHost, | ||||
| 				port:     process.env.DB_MYSQL_PORT || 3306, | ||||
| 				user:     envMysqlUser, | ||||
| 				engine: mysqlEngine, | ||||
| 				host: envMysqlHost, | ||||
| 				port: process.env.DB_MYSQL_PORT || 3306, | ||||
| 				user: envMysqlUser, | ||||
| 				password: process.env.DB_MYSQL_PASSWORD, | ||||
| 				name:     envMysqlName, | ||||
| 				name: envMysqlName, | ||||
| 			}, | ||||
| 			keys: getKeys(), | ||||
| 		}; | ||||
| @@ -54,33 +56,33 @@ const configure = () => { | ||||
| 	const envPostgresName = process.env.DB_POSTGRES_NAME || null; | ||||
| 	if (envPostgresHost && envPostgresUser && envPostgresName) { | ||||
| 		// we have enough postgres creds to go with postgres | ||||
| 		logger.info('Using Postgres configuration'); | ||||
| 		logger.info("Using Postgres configuration"); | ||||
| 		instance = { | ||||
| 			database: { | ||||
| 				engine:   postgresEngine, | ||||
| 				host:     envPostgresHost, | ||||
| 				port:     process.env.DB_POSTGRES_PORT || 5432, | ||||
| 				user:     envPostgresUser, | ||||
| 				engine: postgresEngine, | ||||
| 				host: envPostgresHost, | ||||
| 				port: process.env.DB_POSTGRES_PORT || 5432, | ||||
| 				user: envPostgresUser, | ||||
| 				password: process.env.DB_POSTGRES_PASSWORD, | ||||
| 				name:     envPostgresName, | ||||
| 				name: envPostgresName, | ||||
| 			}, | ||||
| 			keys: getKeys(), | ||||
| 		}; | ||||
| 		return; | ||||
| 	} | ||||
|  | ||||
| 	const envSqliteFile = process.env.DB_SQLITE_FILE || '/data/database.sqlite'; | ||||
| 	const envSqliteFile = process.env.DB_SQLITE_FILE || "/data/database.sqlite"; | ||||
| 	logger.info(`Using Sqlite: ${envSqliteFile}`); | ||||
| 	instance = { | ||||
| 		database: { | ||||
| 			engine: 'knex-native', | ||||
| 			knex:   { | ||||
| 				client:     sqliteClientName, | ||||
| 			engine: "knex-native", | ||||
| 			knex: { | ||||
| 				client: sqliteClientName, | ||||
| 				connection: { | ||||
| 					filename: envSqliteFile | ||||
| 					filename: envSqliteFile, | ||||
| 				}, | ||||
| 				useNullAsDefault: true | ||||
| 			} | ||||
| 				useNullAsDefault: true, | ||||
| 			}, | ||||
| 		}, | ||||
| 		keys: getKeys(), | ||||
| 	}; | ||||
| @@ -88,150 +90,148 @@ const configure = () => { | ||||
|  | ||||
| const getKeys = () => { | ||||
| 	// Get keys from file | ||||
| 	logger.debug("Cheecking for keys file:", keysFile); | ||||
| 	if (!fs.existsSync(keysFile)) { | ||||
| 		generateKeys(); | ||||
| 	} else if (process.env.DEBUG) { | ||||
| 		logger.info('Keys file exists OK'); | ||||
| 		logger.info("Keys file exists OK"); | ||||
| 	} | ||||
| 	try { | ||||
| 		return require(keysFile); | ||||
| 		// Load this json keysFile synchronously and return the json object | ||||
| 		const rawData = fs.readFileSync(keysFile); | ||||
| 		return JSON.parse(rawData); | ||||
| 	} catch (err) { | ||||
| 		logger.error('Could not read JWT key pair from config file: ' + keysFile, err); | ||||
| 		logger.error(`Could not read JWT key pair from config file: ${keysFile}`, err); | ||||
| 		process.exit(1); | ||||
| 	} | ||||
| }; | ||||
|  | ||||
| const generateKeys = () => { | ||||
| 	logger.info('Creating a new JWT key pair...'); | ||||
| 	logger.info("Creating a new JWT key pair..."); | ||||
| 	// Now create the keys and save them in the config. | ||||
| 	const key = new NodeRSA({ b: 2048 }); | ||||
| 	key.generateKeyPair(); | ||||
|  | ||||
| 	const keys = { | ||||
| 		key: key.exportKey('private').toString(), | ||||
| 		pub: key.exportKey('public').toString(), | ||||
| 		key: key.exportKey("private").toString(), | ||||
| 		pub: key.exportKey("public").toString(), | ||||
| 	}; | ||||
|  | ||||
| 	// Write keys config | ||||
| 	try { | ||||
| 		fs.writeFileSync(keysFile, JSON.stringify(keys, null, 2)); | ||||
| 	} catch (err) { | ||||
| 		logger.error('Could not write JWT key pair to config file: ' + keysFile + ': ' + err.message); | ||||
| 		logger.error(`Could not write JWT key pair to config file: ${keysFile}: ${err.message}`); | ||||
| 		process.exit(1); | ||||
| 	} | ||||
| 	logger.info('Wrote JWT key pair to config file: ' + keysFile); | ||||
| 	logger.info(`Wrote JWT key pair to config file: ${keysFile}`); | ||||
| }; | ||||
|  | ||||
| module.exports = { | ||||
|  | ||||
| 	/** | ||||
| 	 * | ||||
| 	 * @param   {string}  key   ie: 'database' or 'database.engine' | ||||
| 	 * @returns {boolean} | ||||
| 	 */ | ||||
| 	has: function(key) { | ||||
| 		instance === null && configure(); | ||||
| 		const keys = key.split('.'); | ||||
| 		let level  = instance; | ||||
| 		let has    = true; | ||||
| 		keys.forEach((keyItem) =>{ | ||||
| 			if (typeof level[keyItem] === 'undefined') { | ||||
| 				has = false; | ||||
| 			} else { | ||||
| 				level = level[keyItem]; | ||||
| 			} | ||||
| 		}); | ||||
|  | ||||
| 		return has; | ||||
| 	}, | ||||
|  | ||||
| 	/** | ||||
| 	 * Gets a specific key from the top level | ||||
| 	 * | ||||
| 	 * @param {string} key | ||||
| 	 * @returns {*} | ||||
| 	 */ | ||||
| 	get: function (key) { | ||||
| 		instance === null && configure(); | ||||
| 		if (key && typeof instance[key] !== 'undefined') { | ||||
| 			return instance[key]; | ||||
| /** | ||||
|  * | ||||
|  * @param   {string}  key   ie: 'database' or 'database.engine' | ||||
|  * @returns {boolean} | ||||
|  */ | ||||
| const configHas = (key) => { | ||||
| 	instance === null && configure(); | ||||
| 	const keys = key.split("."); | ||||
| 	let level = instance; | ||||
| 	let has = true; | ||||
| 	keys.forEach((keyItem) => { | ||||
| 		if (typeof level[keyItem] === "undefined") { | ||||
| 			has = false; | ||||
| 		} else { | ||||
| 			level = level[keyItem]; | ||||
| 		} | ||||
| 		return instance; | ||||
| 	}, | ||||
| 	}); | ||||
|  | ||||
| 	/** | ||||
| 	 * Is this a sqlite configuration? | ||||
| 	 * | ||||
| 	 * @returns {boolean} | ||||
| 	 */ | ||||
| 	isSqlite: function () { | ||||
| 		instance === null && configure(); | ||||
| 		return instance.database.knex && instance.database.knex.client === sqliteClientName; | ||||
| 	}, | ||||
| 	return has; | ||||
| }; | ||||
|  | ||||
| 	/** | ||||
| 	 * Is this a mysql configuration? | ||||
| 	 * | ||||
| 	 * @returns {boolean} | ||||
| 	 */ | ||||
| 	isMysql: function () { | ||||
| 		instance === null && configure(); | ||||
| 		return instance.database.engine === mysqlEngine; | ||||
| 	}, | ||||
| 	 | ||||
| 	/** | ||||
| 		 * Is this a postgres configuration? | ||||
| 		 * | ||||
| 		 * @returns {boolean} | ||||
| 		 */ | ||||
| 	isPostgres: function () { | ||||
| 		instance === null && configure(); | ||||
| 		return instance.database.engine === postgresEngine; | ||||
| 	}, | ||||
|  | ||||
| 	/** | ||||
| 	 * Are we running in debug mdoe? | ||||
| 	 * | ||||
| 	 * @returns {boolean} | ||||
| 	 */ | ||||
| 	debug: function () { | ||||
| 		return !!process.env.DEBUG; | ||||
| 	}, | ||||
|  | ||||
| 	/** | ||||
| 	 * Returns a public key | ||||
| 	 * | ||||
| 	 * @returns {string} | ||||
| 	 */ | ||||
| 	getPublicKey: function () { | ||||
| 		instance === null && configure(); | ||||
| 		return instance.keys.pub; | ||||
| 	}, | ||||
|  | ||||
| 	/** | ||||
| 	 * Returns a private key | ||||
| 	 * | ||||
| 	 * @returns {string} | ||||
| 	 */ | ||||
| 	getPrivateKey: function () { | ||||
| 		instance === null && configure(); | ||||
| 		return instance.keys.key; | ||||
| 	}, | ||||
|  | ||||
| 	/** | ||||
| 	 * @returns {boolean} | ||||
| 	 */ | ||||
| 	useLetsencryptStaging: function () { | ||||
| 		return !!process.env.LE_STAGING; | ||||
| 	}, | ||||
|  | ||||
| 	/** | ||||
| 	 * @returns {string|null} | ||||
| 	 */ | ||||
| 	useLetsencryptServer: function () { | ||||
| 		if (process.env.LE_SERVER) { | ||||
| 			return process.env.LE_SERVER; | ||||
| 		} | ||||
| 		return null; | ||||
| /** | ||||
|  * Gets a specific key from the top level | ||||
|  * | ||||
|  * @param {string} key | ||||
|  * @returns {*} | ||||
|  */ | ||||
| const configGet = (key) => { | ||||
| 	instance === null && configure(); | ||||
| 	if (key && typeof instance[key] !== "undefined") { | ||||
| 		return instance[key]; | ||||
| 	} | ||||
| 	return instance; | ||||
| }; | ||||
|  | ||||
| /** | ||||
|  * Is this a sqlite configuration? | ||||
|  * | ||||
|  * @returns {boolean} | ||||
|  */ | ||||
| const isSqlite = () => { | ||||
| 	instance === null && configure(); | ||||
| 	return instance.database.knex && instance.database.knex.client === sqliteClientName; | ||||
| }; | ||||
|  | ||||
| /** | ||||
|  * Is this a mysql configuration? | ||||
|  * | ||||
|  * @returns {boolean} | ||||
|  */ | ||||
| const isMysql = () => { | ||||
| 	instance === null && configure(); | ||||
| 	return instance.database.engine === mysqlEngine; | ||||
| }; | ||||
|  | ||||
| /** | ||||
|  * Is this a postgres configuration? | ||||
|  * | ||||
|  * @returns {boolean} | ||||
|  */ | ||||
| const isPostgres = () => { | ||||
| 	instance === null && configure(); | ||||
| 	return instance.database.engine === postgresEngine; | ||||
| }; | ||||
|  | ||||
| /** | ||||
|  * Are we running in debug mdoe? | ||||
|  * | ||||
|  * @returns {boolean} | ||||
|  */ | ||||
| const isDebugMode = () => !!process.env.DEBUG; | ||||
|  | ||||
| /** | ||||
|  * Returns a public key | ||||
|  * | ||||
|  * @returns {string} | ||||
|  */ | ||||
| const getPublicKey = () => { | ||||
| 	instance === null && configure(); | ||||
| 	return instance.keys.pub; | ||||
| }; | ||||
|  | ||||
| /** | ||||
|  * Returns a private key | ||||
|  * | ||||
|  * @returns {string} | ||||
|  */ | ||||
| const getPrivateKey = () => { | ||||
| 	instance === null && configure(); | ||||
| 	return instance.keys.key; | ||||
| }; | ||||
|  | ||||
| /** | ||||
|  * @returns {boolean} | ||||
|  */ | ||||
| const useLetsencryptStaging = () => !!process.env.LE_STAGING; | ||||
|  | ||||
| /** | ||||
|  * @returns {string|null} | ||||
|  */ | ||||
| const useLetsencryptServer = () => { | ||||
| 	if (process.env.LE_SERVER) { | ||||
| 		return process.env.LE_SERVER; | ||||
| 	} | ||||
| 	return null; | ||||
| }; | ||||
|  | ||||
| export { configHas, configGet, isSqlite, isMysql, isPostgres, isDebugMode, getPrivateKey, getPublicKey, useLetsencryptStaging, useLetsencryptServer }; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user