Add TLS feature with Caddy
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
import { execFileSync } from "node:child_process";
|
||||
import grabSystemSetupStatus from "@/src/functions/backend/setup/grab-system-setup-status";
|
||||
|
||||
const CADDY_SERVICE_NAME = "wgui-caddy";
|
||||
|
||||
function run({ command, args }: { command: string; args: string[] }) {
|
||||
try {
|
||||
const output = execFileSync(command, args, {
|
||||
encoding: "utf-8",
|
||||
stdio: "pipe",
|
||||
timeout: 30000,
|
||||
}).trim();
|
||||
|
||||
return {
|
||||
success: true as const,
|
||||
msg: output,
|
||||
};
|
||||
} catch (error: any) {
|
||||
const stdout = error?.stdout ? String(error.stdout).trim() : "";
|
||||
const stderr = error?.stderr ? String(error.stderr).trim() : "";
|
||||
|
||||
return {
|
||||
success: false as const,
|
||||
msg: [stdout, stderr, error.message]
|
||||
.filter(Boolean)
|
||||
.join("\n")
|
||||
.trim(),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
function caddyServiceIsActive({
|
||||
init_system,
|
||||
}: {
|
||||
init_system: ReturnType<typeof grabSystemSetupStatus>["init_system"];
|
||||
}) {
|
||||
if (init_system == "systemd") {
|
||||
const status = run({
|
||||
command: "systemctl",
|
||||
args: ["is-active", CADDY_SERVICE_NAME],
|
||||
});
|
||||
|
||||
return status.success && status.msg == "active";
|
||||
}
|
||||
|
||||
if (init_system == "openrc") {
|
||||
return run({
|
||||
command: "rc-service",
|
||||
args: [CADDY_SERVICE_NAME, "status"],
|
||||
}).success;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
export default function reloadCaddyService() {
|
||||
const setup_status = grabSystemSetupStatus();
|
||||
const service_active = caddyServiceIsActive({
|
||||
init_system: setup_status.init_system,
|
||||
});
|
||||
|
||||
if (setup_status.is_dev && !service_active) {
|
||||
return {
|
||||
success: true as const,
|
||||
msg: `Development mode — Caddy service is not running, reload skipped`,
|
||||
};
|
||||
}
|
||||
|
||||
switch (setup_status.init_system) {
|
||||
case "systemd":
|
||||
return run({
|
||||
command: "systemctl",
|
||||
args: ["reload", CADDY_SERVICE_NAME],
|
||||
});
|
||||
case "openrc":
|
||||
return run({
|
||||
command: "rc-service",
|
||||
args: [CADDY_SERVICE_NAME, "reload"],
|
||||
});
|
||||
default:
|
||||
return {
|
||||
success: false as const,
|
||||
msg: `No supported init system found for wgui-caddy`,
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user