Update Reverse proxy and TLS configuration on Caddy
This commit is contained in:
@@ -4,6 +4,7 @@ import type {
|
||||
} from "@/db/types/db";
|
||||
import checkUserAccess from "@/src/functions/backend/auth/check-user-access";
|
||||
import decorateClientRoute from "@/src/functions/backend/acme/decorate-client-route";
|
||||
import grabCaddySettings from "@/src/functions/backend/caddy/grab-caddy-settings";
|
||||
import { grabManualDnsChallenge } from "@/src/functions/backend/acme/manual-dns-challenges";
|
||||
import type { AdminCrudAPIParams, TableType } from "@/src/types";
|
||||
import BunSQLite from "@moduletrace/bun-sqlite";
|
||||
@@ -51,10 +52,13 @@ export default async function (
|
||||
]),
|
||||
);
|
||||
|
||||
const settings = await grabCaddySettings();
|
||||
|
||||
const decorate = (route: BUN_SQLITE_WGUI_CLIENT_ROUTES) => {
|
||||
const view = decorateClientRoute({
|
||||
route,
|
||||
certificate: certificates_by_hostname.get(route.hostname || ""),
|
||||
https_port: settings.https_port,
|
||||
});
|
||||
|
||||
if (
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
import adminApiGuard from "@/src/functions/backend/auth/admin-api-guard";
|
||||
import grabCaddySettings from "@/src/functions/backend/caddy/grab-caddy-settings";
|
||||
import syncCaddyIngress from "@/src/functions/backend/caddy/sync-caddy-ingress";
|
||||
import BunSQLite from "@moduletrace/bun-sqlite";
|
||||
import type {
|
||||
APIResponseObject,
|
||||
BunextAPIRouteHandler,
|
||||
} from "@moduletrace/bunext/types";
|
||||
import type { BUN_SQLITE_WGUI_VARIABLES } from "@/db/types/db";
|
||||
import type { ApiReqParams, TableType } from "@/src/types";
|
||||
|
||||
type SettingsBody = {
|
||||
https_port?: number;
|
||||
redirect?: boolean;
|
||||
};
|
||||
|
||||
export const handler: BunextAPIRouteHandler<APIResponseObject> = async ({
|
||||
req,
|
||||
body,
|
||||
}) => {
|
||||
const unauthorized = await adminApiGuard({ req });
|
||||
|
||||
if (unauthorized) {
|
||||
return unauthorized;
|
||||
}
|
||||
|
||||
if (req.method === "GET") {
|
||||
const settings = await grabCaddySettings();
|
||||
|
||||
return {
|
||||
success: true,
|
||||
singleRes: settings,
|
||||
payload: [settings],
|
||||
};
|
||||
}
|
||||
|
||||
if (req.method !== "POST") {
|
||||
return { success: false, msg: `Method not allowed` };
|
||||
}
|
||||
|
||||
const input = (body || {}) as ApiReqParams & SettingsBody;
|
||||
const updates: { key: string; value: string }[] = [];
|
||||
|
||||
if (input.https_port !== undefined) {
|
||||
const port = Number(input.https_port);
|
||||
|
||||
if (
|
||||
!Number.isInteger(port) ||
|
||||
port < 1 ||
|
||||
port > 65535 ||
|
||||
port == 80 ||
|
||||
port == 443
|
||||
) {
|
||||
return {
|
||||
success: false,
|
||||
msg: `HTTPS port must be between 1 and 65535 and cannot be 80 or 443`,
|
||||
};
|
||||
}
|
||||
|
||||
updates.push({ key: "caddy_https_port", value: String(port) });
|
||||
}
|
||||
|
||||
if (input.redirect !== undefined) {
|
||||
updates.push({
|
||||
key: "caddy_https_redirect",
|
||||
value: input.redirect ? "1" : "0",
|
||||
});
|
||||
}
|
||||
|
||||
if (!updates.length) {
|
||||
return { success: false, msg: `No settings to update` };
|
||||
}
|
||||
|
||||
for (const update of updates) {
|
||||
const existing = await BunSQLite.select<
|
||||
BUN_SQLITE_WGUI_VARIABLES,
|
||||
TableType
|
||||
>({
|
||||
table: "variables",
|
||||
});
|
||||
|
||||
const row = existing.payload?.find(
|
||||
(variable) => variable.key === update.key,
|
||||
);
|
||||
|
||||
if (row) {
|
||||
await BunSQLite.update<BUN_SQLITE_WGUI_VARIABLES, TableType>({
|
||||
table: "variables",
|
||||
targetId: row.id,
|
||||
data: { value: update.value },
|
||||
});
|
||||
} else {
|
||||
await BunSQLite.insert({
|
||||
table: "variables",
|
||||
data: [{ key: update.key, value: update.value }],
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const sync = await syncCaddyIngress({ reason: "settings updated" });
|
||||
|
||||
return {
|
||||
success: sync.success,
|
||||
msg: sync.msg,
|
||||
debug: { warnings: sync.warnings },
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user