Regenerate configs improvements
All checks were successful
Close stale issues and PRs / stale (push) Successful in 33s

- Fix certificates
- Adds dry run
- code cleanup
This commit is contained in:
Jamie Curnow
2026-03-03 08:44:42 +10:00
parent f1e95c9c52
commit eb67b3bfb6
9 changed files with 99 additions and 81 deletions

View File

@@ -194,7 +194,7 @@ const internalDeadHost = {
.query() .query()
.where("is_deleted", 0) .where("is_deleted", 0)
.andWhere("id", data.id) .andWhere("id", data.id)
.allowGraph("[owner,certificate]") .allowGraph(deadHostModel.defaultAllowGraph)
.first(); .first();
if (accessData.permission_visibility !== "all") { if (accessData.permission_visibility !== "all") {
@@ -347,7 +347,7 @@ const internalDeadHost = {
.query() .query()
.where("is_deleted", 0) .where("is_deleted", 0)
.groupBy("id") .groupBy("id")
.allowGraph("[owner,certificate]") .allowGraph(deadHostModel.defaultAllowGraph)
.orderBy(castJsonIfNeed("domain_names"), "ASC"); .orderBy(castJsonIfNeed("domain_names"), "ASC");
if (accessData.permission_visibility !== "all") { if (accessData.permission_visibility !== "all") {

View File

@@ -115,9 +115,9 @@ const internalProxyHost = {
*/ */
update: (access, data) => { update: (access, data) => {
let thisData = 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; delete thisData.certificate_id;
} }
@@ -155,7 +155,7 @@ const internalProxyHost = {
); );
} }
if (create_certificate) { if (createCertificate) {
return internalCertificate return internalCertificate
.createQuickCertificate(access, { .createQuickCertificate(access, {
domain_names: thisData.domain_names || row.domain_names, domain_names: thisData.domain_names || row.domain_names,
@@ -232,7 +232,6 @@ const internalProxyHost = {
*/ */
get: (access, data) => { get: (access, data) => {
const thisData = data || {}; const thisData = data || {};
return access return access
.can("proxy_hosts:get", thisData.id) .can("proxy_hosts:get", thisData.id)
.then((access_data) => { .then((access_data) => {
@@ -240,7 +239,7 @@ const internalProxyHost = {
.query() .query()
.where("is_deleted", 0) .where("is_deleted", 0)
.andWhere("id", thisData.id) .andWhere("id", thisData.id)
.allowGraph("[owner,access_list.[clients,items],certificate]") .allowGraph(proxyHostModel.defaultAllowGraph)
.first(); .first();
if (access_data.permission_visibility !== "all") { if (access_data.permission_visibility !== "all") {
@@ -422,11 +421,12 @@ const internalProxyHost = {
*/ */
getAll: async (access, expand, searchQuery) => { getAll: async (access, expand, searchQuery) => {
const accessData = await access.can("proxy_hosts:list"); const accessData = await access.can("proxy_hosts:list");
const query = proxyHostModel const query = proxyHostModel
.query() .query()
.where("is_deleted", 0) .where("is_deleted", 0)
.groupBy("id") .groupBy("id")
.allowGraph("[owner,access_list,certificate]") .allowGraph(proxyHostModel.defaultAllowGraph)
.orderBy(castJsonIfNeed("domain_names"), "ASC"); .orderBy(castJsonIfNeed("domain_names"), "ASC");
if (accessData.permission_visibility !== "all") { if (accessData.permission_visibility !== "all") {

View File

@@ -229,7 +229,6 @@ const internalRedirectionHost = {
*/ */
get: (access, data) => { get: (access, data) => {
const thisData = data || {}; const thisData = data || {};
return access return access
.can("redirection_hosts:get", thisData.id) .can("redirection_hosts:get", thisData.id)
.then((access_data) => { .then((access_data) => {
@@ -237,7 +236,7 @@ const internalRedirectionHost = {
.query() .query()
.where("is_deleted", 0) .where("is_deleted", 0)
.andWhere("id", thisData.id) .andWhere("id", thisData.id)
.allowGraph("[owner,certificate]") .allowGraph(redirectionHostModel.defaultAllowGraph)
.first(); .first();
if (access_data.permission_visibility !== "all") { if (access_data.permission_visibility !== "all") {
@@ -426,7 +425,7 @@ const internalRedirectionHost = {
.query() .query()
.where("is_deleted", 0) .where("is_deleted", 0)
.groupBy("id") .groupBy("id")
.allowGraph("[owner,certificate]") .allowGraph(redirectionHostModel.defaultAllowGraph)
.orderBy(castJsonIfNeed("domain_names"), "ASC"); .orderBy(castJsonIfNeed("domain_names"), "ASC");
if (access_data.permission_visibility !== "all") { if (access_data.permission_visibility !== "all") {

View File

@@ -178,7 +178,6 @@ const internalStream = {
*/ */
get: (access, data) => { get: (access, data) => {
const thisData = data || {}; const thisData = data || {};
return access return access
.can("streams:get", thisData.id) .can("streams:get", thisData.id)
.then((access_data) => { .then((access_data) => {
@@ -186,7 +185,7 @@ const internalStream = {
.query() .query()
.where("is_deleted", 0) .where("is_deleted", 0)
.andWhere("id", thisData.id) .andWhere("id", thisData.id)
.allowGraph("[owner,certificate]") .allowGraph(streamModel.defaultAllowGraph)
.first(); .first();
if (access_data.permission_visibility !== "all") { if (access_data.permission_visibility !== "all") {
@@ -375,7 +374,7 @@ const internalStream = {
.query() .query()
.where("is_deleted", 0) .where("is_deleted", 0)
.groupBy("id") .groupBy("id")
.allowGraph("[owner,certificate]") .allowGraph(streamModel.defaultAllowGraph)
.orderBy("incoming_port", "ASC"); .orderBy("incoming_port", "ASC");
if (access_data.permission_visibility !== "all") { if (access_data.permission_visibility !== "all") {

View File

@@ -3,7 +3,7 @@
import { Model } from "objection"; import { Model } from "objection";
import db from "../db.js"; 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 Certificate from "./certificate.js";
import now from "./now_helper.js"; import now from "./now_helper.js";
import User from "./user.js"; import User from "./user.js";
@@ -61,6 +61,18 @@ class DeadHost extends Model {
return ["domain_names", "meta"]; 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() { static get relationMappings() {
return { return {
owner: { owner: {

View File

@@ -3,7 +3,7 @@
import { Model } from "objection"; import { Model } from "objection";
import db from "../db.js"; 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 AccessList from "./access_list.js";
import Certificate from "./certificate.js"; import Certificate from "./certificate.js";
import now from "./now_helper.js"; import now from "./now_helper.js";
@@ -73,6 +73,18 @@ class ProxyHost extends Model {
return ["domain_names", "meta", "locations"]; 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() { static get relationMappings() {
return { return {
owner: { owner: {

View File

@@ -3,7 +3,7 @@
import { Model } from "objection"; import { Model } from "objection";
import db from "../db.js"; 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 Certificate from "./certificate.js";
import now from "./now_helper.js"; import now from "./now_helper.js";
import User from "./user.js"; import User from "./user.js";
@@ -70,6 +70,18 @@ class RedirectionHost extends Model {
return ["domain_names", "meta"]; 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() { static get relationMappings() {
return { return {
owner: { owner: {

View File

@@ -1,6 +1,6 @@
import { Model } from "objection"; import { Model } from "objection";
import db from "../db.js"; 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 Certificate from "./certificate.js";
import now from "./now_helper.js"; import now from "./now_helper.js";
import User from "./user.js"; import User from "./user.js";
@@ -46,6 +46,18 @@ class Stream extends Model {
return ["meta"]; 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() { static get relationMappings() {
return { return {
owner: { owner: {

View File

@@ -2,7 +2,6 @@
import * as process from "node:process"; // Use the node: protocol for built-ins import * as process from "node:process"; // Use the node: protocol for built-ins
import internalNginx from "../internal/nginx.js"; import internalNginx from "../internal/nginx.js";
import { castJsonIfNeed } from "../lib/helpers.js";
import { global as logger } from "../logger.js"; import { global as logger } from "../logger.js";
import deadHostModel from "../models/dead_host.js"; import deadHostModel from "../models/dead_host.js";
import proxyHostModel from "../models/proxy_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 args = process.argv.slice(2);
const UNATTENDED = args.includes("-y") || args.includes("--yes"); const UNATTENDED = args.includes("-y") || args.includes("--yes");
if (args.includes("--help")) { const DRY_RUN = args.includes("--dry-run");
console.log("Usage: ./regenerate-config [--help] [-y|--yes]");
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 // 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 readline = await import("node:readline");
const rl = readline.createInterface({ const rl = readline.createInterface({
input: process.stdin, 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. // Let's do it.
// Proxy hosts const processItems = async (model, type) => {
const proxyRows = await proxyHostModel const rows = await model
.query() .query()
.where("is_deleted", 0) .where("is_deleted", 0)
.andWhere("enabled", 1) .andWhere("enabled", 1)
.groupBy("id") .groupBy("id")
.allowGraph("[owner,access_list,certificate]") .allowGraph(model.defaultAllowGraph)
.orderBy(castJsonIfNeed("domain_names"), "ASC"); .withGraphFetched(`[${model.defaultExpand.join(", ")}]`)
.orderBy(...model.defaultOrder);
for (const row of proxyRows) { logIt(`[${type}] Found ${rows.length} rows to process...`);
logger.info( for (const row of rows) {
`Regenerating config for Proxy Host #${row.id}: ${row.domain_names.join(", ")}`, 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); 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");
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 await processItems(proxyHostModel, "Proxy Host");
const deadRows = await deadHostModel await processItems(redirectionHostModel, "Redirection Host");
.query() await processItems(deadHostModel, "404 Host");
.where("is_deleted", 0) await processItems(streamModel, "Stream");
.andWhere("enabled", 1)
.groupBy("id")
.allowGraph("[owner,access_list,certificate]")
.orderBy(castJsonIfNeed("domain_names"), "ASC");
for (const row of deadRows) { logIt("Completed", "success");
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");
process.exit(0); process.exit(0);