diff --git a/backend/internal/dead-host.js b/backend/internal/dead-host.js index 21b12012..34c94fda 100644 --- a/backend/internal/dead-host.js +++ b/backend/internal/dead-host.js @@ -194,7 +194,7 @@ const internalDeadHost = { .query() .where("is_deleted", 0) .andWhere("id", data.id) - .allowGraph("[owner,certificate]") + .allowGraph(deadHostModel.defaultAllowGraph) .first(); if (accessData.permission_visibility !== "all") { @@ -347,7 +347,7 @@ const internalDeadHost = { .query() .where("is_deleted", 0) .groupBy("id") - .allowGraph("[owner,certificate]") + .allowGraph(deadHostModel.defaultAllowGraph) .orderBy(castJsonIfNeed("domain_names"), "ASC"); if (accessData.permission_visibility !== "all") { diff --git a/backend/internal/proxy-host.js b/backend/internal/proxy-host.js index 3299012a..34475c99 100644 --- a/backend/internal/proxy-host.js +++ b/backend/internal/proxy-host.js @@ -115,9 +115,9 @@ const internalProxyHost = { */ update: (access, data) => { let thisData = data; - const create_certificate = thisData.certificate_id === "new"; + const createCertificate = thisData.certificate_id === "new"; - if (create_certificate) { + if (createCertificate) { delete thisData.certificate_id; } @@ -155,7 +155,7 @@ const internalProxyHost = { ); } - if (create_certificate) { + if (createCertificate) { return internalCertificate .createQuickCertificate(access, { domain_names: thisData.domain_names || row.domain_names, @@ -232,7 +232,6 @@ const internalProxyHost = { */ get: (access, data) => { const thisData = data || {}; - return access .can("proxy_hosts:get", thisData.id) .then((access_data) => { @@ -240,7 +239,7 @@ const internalProxyHost = { .query() .where("is_deleted", 0) .andWhere("id", thisData.id) - .allowGraph("[owner,access_list.[clients,items],certificate]") + .allowGraph(proxyHostModel.defaultAllowGraph) .first(); if (access_data.permission_visibility !== "all") { @@ -422,11 +421,12 @@ const internalProxyHost = { */ getAll: async (access, expand, searchQuery) => { const accessData = await access.can("proxy_hosts:list"); + const query = proxyHostModel .query() .where("is_deleted", 0) .groupBy("id") - .allowGraph("[owner,access_list,certificate]") + .allowGraph(proxyHostModel.defaultAllowGraph) .orderBy(castJsonIfNeed("domain_names"), "ASC"); if (accessData.permission_visibility !== "all") { diff --git a/backend/internal/redirection-host.js b/backend/internal/redirection-host.js index 159ffd8b..5237859c 100644 --- a/backend/internal/redirection-host.js +++ b/backend/internal/redirection-host.js @@ -229,7 +229,6 @@ const internalRedirectionHost = { */ get: (access, data) => { const thisData = data || {}; - return access .can("redirection_hosts:get", thisData.id) .then((access_data) => { @@ -237,7 +236,7 @@ const internalRedirectionHost = { .query() .where("is_deleted", 0) .andWhere("id", thisData.id) - .allowGraph("[owner,certificate]") + .allowGraph(redirectionHostModel.defaultAllowGraph) .first(); if (access_data.permission_visibility !== "all") { @@ -426,7 +425,7 @@ const internalRedirectionHost = { .query() .where("is_deleted", 0) .groupBy("id") - .allowGraph("[owner,certificate]") + .allowGraph(redirectionHostModel.defaultAllowGraph) .orderBy(castJsonIfNeed("domain_names"), "ASC"); if (access_data.permission_visibility !== "all") { diff --git a/backend/internal/stream.js b/backend/internal/stream.js index 805b6652..909c92f8 100644 --- a/backend/internal/stream.js +++ b/backend/internal/stream.js @@ -178,7 +178,6 @@ const internalStream = { */ get: (access, data) => { const thisData = data || {}; - return access .can("streams:get", thisData.id) .then((access_data) => { @@ -186,7 +185,7 @@ const internalStream = { .query() .where("is_deleted", 0) .andWhere("id", thisData.id) - .allowGraph("[owner,certificate]") + .allowGraph(streamModel.defaultAllowGraph) .first(); if (access_data.permission_visibility !== "all") { @@ -375,7 +374,7 @@ const internalStream = { .query() .where("is_deleted", 0) .groupBy("id") - .allowGraph("[owner,certificate]") + .allowGraph(streamModel.defaultAllowGraph) .orderBy("incoming_port", "ASC"); if (access_data.permission_visibility !== "all") { diff --git a/backend/models/dead_host.js b/backend/models/dead_host.js index 0acf7ca7..dc7c775f 100644 --- a/backend/models/dead_host.js +++ b/backend/models/dead_host.js @@ -3,7 +3,7 @@ import { Model } from "objection"; import db from "../db.js"; -import { convertBoolFieldsToInt, convertIntFieldsToBool } from "../lib/helpers.js"; +import { castJsonIfNeed, convertBoolFieldsToInt, convertIntFieldsToBool } from "../lib/helpers.js"; import Certificate from "./certificate.js"; import now from "./now_helper.js"; import User from "./user.js"; @@ -61,6 +61,18 @@ class DeadHost extends Model { return ["domain_names", "meta"]; } + static get defaultAllowGraph() { + return "[owner,certificate]"; + } + + static get defaultExpand() { + return ["certificate", "owner"]; + } + + static get defaultOrder() { + return [castJsonIfNeed("domain_names"), "ASC"]; + } + static get relationMappings() { return { owner: { diff --git a/backend/models/proxy_host.js b/backend/models/proxy_host.js index e8f447c8..acb8da93 100644 --- a/backend/models/proxy_host.js +++ b/backend/models/proxy_host.js @@ -3,7 +3,7 @@ import { Model } from "objection"; import db from "../db.js"; -import { convertBoolFieldsToInt, convertIntFieldsToBool } from "../lib/helpers.js"; +import { castJsonIfNeed, convertBoolFieldsToInt, convertIntFieldsToBool } from "../lib/helpers.js"; import AccessList from "./access_list.js"; import Certificate from "./certificate.js"; import now from "./now_helper.js"; @@ -73,6 +73,18 @@ class ProxyHost extends Model { return ["domain_names", "meta", "locations"]; } + static get defaultAllowGraph() { + return "[owner,access_list.[clients,items],certificate]"; + } + + static get defaultExpand() { + return ["owner", "certificate", "access_list.[clients,items]"]; + } + + static get defaultOrder() { + return [castJsonIfNeed("domain_names"), "ASC"]; + } + static get relationMappings() { return { owner: { diff --git a/backend/models/redirection_host.js b/backend/models/redirection_host.js index 46c73017..0c47de62 100644 --- a/backend/models/redirection_host.js +++ b/backend/models/redirection_host.js @@ -3,7 +3,7 @@ import { Model } from "objection"; import db from "../db.js"; -import { convertBoolFieldsToInt, convertIntFieldsToBool } from "../lib/helpers.js"; +import { castJsonIfNeed, convertBoolFieldsToInt, convertIntFieldsToBool } from "../lib/helpers.js"; import Certificate from "./certificate.js"; import now from "./now_helper.js"; import User from "./user.js"; @@ -70,6 +70,18 @@ class RedirectionHost extends Model { return ["domain_names", "meta"]; } + static get defaultAllowGraph() { + return "[owner,certificate]"; + } + + static get defaultExpand() { + return ["certificate", "owner"]; + } + + static get defaultOrder() { + return [castJsonIfNeed("domain_names"), "ASC"]; + } + static get relationMappings() { return { owner: { diff --git a/backend/models/stream.js b/backend/models/stream.js index 5f61945a..20a23a26 100644 --- a/backend/models/stream.js +++ b/backend/models/stream.js @@ -1,6 +1,6 @@ import { Model } from "objection"; import db from "../db.js"; -import { convertBoolFieldsToInt, convertIntFieldsToBool } from "../lib/helpers.js"; +import { castJsonIfNeed, convertBoolFieldsToInt, convertIntFieldsToBool } from "../lib/helpers.js"; import Certificate from "./certificate.js"; import now from "./now_helper.js"; import User from "./user.js"; @@ -46,6 +46,18 @@ class Stream extends Model { return ["meta"]; } + static get defaultAllowGraph() { + return "[owner,certificate]"; + } + + static get defaultExpand() { + return ["certificate", "owner"]; + } + + static get defaultOrder() { + return [castJsonIfNeed("incoming_port"), "ASC"]; + } + static get relationMappings() { return { owner: { diff --git a/backend/scripts/regenerate-config b/backend/scripts/regenerate-config index 1254ded8..00f84113 100755 --- a/backend/scripts/regenerate-config +++ b/backend/scripts/regenerate-config @@ -2,7 +2,6 @@ import * as process from "node:process"; // Use the node: protocol for built-ins import internalNginx from "../internal/nginx.js"; -import { castJsonIfNeed } from "../lib/helpers.js"; import { global as logger } from "../logger.js"; import deadHostModel from "../models/dead_host.js"; import proxyHostModel from "../models/proxy_host.js"; @@ -11,12 +10,16 @@ import streamModel from "../models/stream.js"; const args = process.argv.slice(2); const UNATTENDED = args.includes("-y") || args.includes("--yes"); -if (args.includes("--help")) { - console.log("Usage: ./regenerate-config [--help] [-y|--yes]"); +const DRY_RUN = args.includes("--dry-run"); + +if (args.includes("--help") || args.includes("-h")) { + console.log("\nThis will iterate over all Hosts and regnerate their Nginx configs.\n") + console.log("Usage: ./regenerate-config [-h|--help] [-y|--yes] [--dry-run]\n"); + process.exit(0); } // ask for the user to confirm the action if not in unattended mode -if (!UNATTENDED) { +if (!UNATTENDED && !DRY_RUN) { const readline = await import("node:readline"); const rl = readline.createInterface({ input: process.stdin, @@ -37,68 +40,37 @@ if (!UNATTENDED) { } } +const logIt = (msg, type = "info") => logger[type]( + `${DRY_RUN ? '[DRY RUN] ' : ''}${msg}`, +); + // Let's do it. -// Proxy hosts -const proxyRows = await proxyHostModel - .query() - .where("is_deleted", 0) - .andWhere("enabled", 1) - .groupBy("id") - .allowGraph("[owner,access_list,certificate]") - .orderBy(castJsonIfNeed("domain_names"), "ASC"); +const processItems = async (model, type) => { + const rows = await model + .query() + .where("is_deleted", 0) + .andWhere("enabled", 1) + .groupBy("id") + .allowGraph(model.defaultAllowGraph) + .withGraphFetched(`[${model.defaultExpand.join(", ")}]`) + .orderBy(...model.defaultOrder); -for (const row of proxyRows) { - logger.info( - `Regenerating config for Proxy Host #${row.id}: ${row.domain_names.join(", ")}`, - ); - await internalNginx.configure(proxyHostModel, "proxy_host", row); -} + logIt(`[${type}] Found ${rows.length} rows to process...`); + for (const row of rows) { + if (!DRY_RUN) { + logIt(`[${type}] Regenerating config #${row.id}: ${row.domain_names ? row.domain_names.join(", ") : 'port ' + row.incoming_port}`); + await internalNginx.configure(proxyHostModel, "proxy_host", row); + } else { + logIt(`[${type}] Skipping generation of config #${row.id}: ${row.domain_names ? row.domain_names.join(", ") : 'port ' + row.incoming_port}`); + } + } +}; -// Redirection hosts -const redirectionRows = await redirectionHostModel - .query() - .where("is_deleted", 0) - .andWhere("enabled", 1) - .groupBy("id") - .allowGraph("[owner,access_list,certificate]") - .orderBy(castJsonIfNeed("domain_names"), "ASC"); +await processItems(proxyHostModel, "Proxy Host"); +await processItems(redirectionHostModel, "Redirection Host"); +await processItems(deadHostModel, "404 Host"); +await processItems(streamModel, "Stream"); -for (const row of redirectionRows) { - logger.info( - `Regenerating config for Redirection Host #${row.id}: ${row.domain_names.join(", ")}`, - ); - await internalNginx.configure(redirectionHostModel, "redirection_host", row); -} - -// 404 hosts -const deadRows = await deadHostModel - .query() - .where("is_deleted", 0) - .andWhere("enabled", 1) - .groupBy("id") - .allowGraph("[owner,access_list,certificate]") - .orderBy(castJsonIfNeed("domain_names"), "ASC"); - -for (const row of deadRows) { - logger.info( - `Regenerating config for 404 Host #${row.id}: ${row.domain_names.join(", ")}`, - ); - await internalNginx.configure(deadHostModel, "dead_host", row); -} - -// Streams -const streamRows = await streamModel - .query() - .where("is_deleted", 0) - .andWhere("enabled", 1) - .groupBy("id") - .allowGraph("[owner,access_list,certificate]"); - -for (const row of streamRows) { - logger.info(`Regenerating config for Stream #${row.id}: ${row.incoming_port} -> ${row.forwarding_host}:${row.forwarding_port}`); - await internalNginx.configure(deadHostModel, "stream", row); -} - -logger.success("Completed"); +logIt("Completed", "success"); process.exit(0);