From 7613a7764babd4c19503326f9fb47ec4a414a7d5 Mon Sep 17 00:00:00 2001 From: Wildan M Date: Tue, 12 Nov 2024 06:03:29 +0700 Subject: [PATCH] Make ssl wildcard easier --- package-lock.json | 4 ++-- package.json | 2 +- src/util.js | 37 ++++++++++++++++++++++++++++--------- 3 files changed, 31 insertions(+), 12 deletions(-) diff --git a/package-lock.json b/package-lock.json index 3ee866a..55d386e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "domcloud-bridge", - "version": "0.68.0", + "version": "0.69.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "domcloud-bridge", - "version": "0.68.0", + "version": "0.69.0", "license": "MIT", "dependencies": { "cli": "^1.0.1", diff --git a/package.json b/package.json index 075d158..7069508 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "domcloud-bridge", - "version": "0.68.0", + "version": "0.69.0", "description": "Deployment runner for DOM Cloud", "main": "app.js", "engines": { diff --git a/src/util.js b/src/util.js index 6bf7c58..41b5792 100644 --- a/src/util.js +++ b/src/util.js @@ -1,8 +1,9 @@ -import path from 'path'; +import path, { dirname } from 'path'; import { spawn } from 'child_process'; import { lock } from 'proper-lockfile'; import fs from 'fs'; import binaries from './binaries/metadata.cjs'; +import { virtualminExec } from './executor/virtualmin'; const { javaVersionsList, javaVersionsMap, @@ -16,7 +17,7 @@ let tokenSecret, allowIps, sudoutil, version, revision; // https://packagist.org/php-statistics let phpVersionsList = []; /** - * @type {Record} + * @type {Record} */ let sslWildcardsMap = {}; @@ -31,13 +32,6 @@ export const initUtils = () => { const rev = cat('.git/HEAD').trim(); revision = rev.indexOf(':') === -1 ? rev : cat('.git/' + rev.substring(5)).trim(); revision = revision.substring(0, 7); - sslWildcardsMap = (process.env.SSL_WILDCARDS || '').split(',').reduce((a, b) => { - var splits = b.split(':', 2); - if (splits.length == 2) { - a[splits[0].toLowerCase()] = splits[1]; - } - return a; - }, {}); try { const phpPath = process.env.PHPFPM_REMILIST || '/etc/opt/remi/'; const phpFiles = fs.readdirSync(phpPath, { withFileTypes: true }); @@ -48,7 +42,32 @@ export const initUtils = () => { } catch (error) { phpVersionsList = []; } + updateWildcardData(); +} +async function updateWildcardData() { + sslWildcardsMap = {}; + var cachepath = path.join(process.cwd(), '/.tmp/wildcardssl.json'); + try { + sslWildcardsMap = JSON.parse(cat(cachepath)); + for (const domain of (process.env.SSL_WILDCARDS || '').split(',')) { + if (!(domain in sslWildcardsMap) || ['id', 'domain', 'path'].every(k => !(k in sslWildcardsMap[domain]))) { + throw new Error(); + } + } + return + } catch (error) { + + } + const domains = (process.env.SSL_WILDCARDS || '').split(',').map(x => x.split(':')[0]); + for (const [domain, d] of Object.entries(await virtualminExec.getDomainInfo(domains, true))) { + sslWildcardsMap[domain] = { + id: d['ID'] + '', + path: dirname(d['SSL key file'] + ''), + domain, + } + } + writeTo(cachepath, JSON.stringify(sslWildcardsMap)) } export const getLtsPhp = (/** @type {string} */ major) => {