150 lines
4.4 KiB
JavaScript
150 lines
4.4 KiB
JavaScript
"use strict";
|
|
|
|
function cleanString(value) {
|
|
return String(value ?? "").trim();
|
|
}
|
|
|
|
function cleanScript(value, maxLength = 262144) {
|
|
return String(value ?? "")
|
|
.replace(/\0/g, "")
|
|
.replace(/\r\n?/g, "\n")
|
|
.trim()
|
|
.slice(0, maxLength);
|
|
}
|
|
|
|
function toFiniteNumber(value) {
|
|
const numeric = Number(value);
|
|
return Number.isFinite(numeric) ? numeric : null;
|
|
}
|
|
|
|
function cleanAbsoluteContainerPath(value, fallback = "") {
|
|
let raw = cleanString(value).replace(/\0/g, "").replace(/\\/g, "/");
|
|
if (!raw) return fallback;
|
|
if (!raw.startsWith("/")) return fallback;
|
|
raw = raw.replace(/\/+/g, "/");
|
|
if (raw.length > 1) raw = raw.replace(/\/+$/, "");
|
|
return raw || fallback;
|
|
}
|
|
|
|
function sanitizeEnvPayload(env) {
|
|
if (!env || typeof env !== "object" || Array.isArray(env)) return null;
|
|
const out = {};
|
|
for (const [key, value] of Object.entries(env)) {
|
|
const envKey = cleanString(key);
|
|
if (!envKey) continue;
|
|
out[envKey] = String(value ?? "");
|
|
}
|
|
return Object.keys(out).length ? out : null;
|
|
}
|
|
|
|
function sanitizeDockerInstallPayload(install) {
|
|
if (!install || typeof install !== "object" || Array.isArray(install)) return null;
|
|
|
|
const script = cleanScript(install.script || install.installScript || install.install_script);
|
|
if (!script) return null;
|
|
|
|
const image = cleanString(install.image || install.container) || "ghcr.io/pterodactyl/installers:debian";
|
|
const entrypoint = cleanString(install.entrypoint) || "bash";
|
|
const mountPath = cleanAbsoluteContainerPath(
|
|
install.mountPath || install.containerPath || install.container_path || install.serverPath,
|
|
"/mnt/server"
|
|
);
|
|
const runtimePath = cleanAbsoluteContainerPath(
|
|
install.runtimePath || install.runtime_path || install.homePath || install.home_path,
|
|
"/home/container"
|
|
);
|
|
|
|
const timeoutRaw = toFiniteNumber(install.timeoutSeconds || install.timeout_seconds || install.timeout);
|
|
const timeoutSeconds = timeoutRaw && timeoutRaw > 0
|
|
? Math.max(60, Math.min(Math.trunc(timeoutRaw), 7200))
|
|
: 1800;
|
|
const minimumStorageRaw = toFiniteNumber(install.minimumStorageMb || install.minimum_storage_mb);
|
|
const minimumStorageMb = minimumStorageRaw && minimumStorageRaw > 0
|
|
? Math.max(1, Math.min(Math.trunc(minimumStorageRaw), 1024 * 1024))
|
|
: 0;
|
|
|
|
const payload = {
|
|
image,
|
|
script,
|
|
entrypoint,
|
|
mountPath,
|
|
runtimePath,
|
|
timeoutSeconds,
|
|
};
|
|
if (minimumStorageMb > 0) payload.minimumStorageMb = minimumStorageMb;
|
|
|
|
const env = sanitizeEnvPayload(install.env);
|
|
if (env) payload.env = env;
|
|
|
|
return payload;
|
|
}
|
|
|
|
function sanitizeDockerTemplatePayload(docker) {
|
|
if (!docker || typeof docker !== "object") return null;
|
|
|
|
const image = cleanString(docker.image);
|
|
if (!image) return null;
|
|
|
|
const payload = {
|
|
image,
|
|
};
|
|
|
|
const tag = cleanString(docker.tag) || "latest";
|
|
if (tag) payload.tag = tag;
|
|
|
|
const command = cleanString(docker.command);
|
|
if (command) payload.command = command;
|
|
|
|
const workdir = cleanString(docker.workdir || docker.workingDir || docker.working_dir);
|
|
if (workdir) payload.workdir = workdir;
|
|
|
|
const restart = cleanString(docker.restart);
|
|
if (restart) payload.restart = restart;
|
|
|
|
const restartPolicy = cleanString(docker.restartPolicy);
|
|
if (restartPolicy) payload.restartPolicy = restartPolicy;
|
|
|
|
const ports = Array.isArray(docker.ports)
|
|
? docker.ports
|
|
.map((entry) => {
|
|
const port = toFiniteNumber(entry);
|
|
return Number.isInteger(port) && port > 0 ? port : null;
|
|
})
|
|
.filter((entry) => entry != null)
|
|
: [];
|
|
if (ports.length) payload.ports = ports;
|
|
|
|
const volumes = Array.isArray(docker.volumes)
|
|
? docker.volumes.map((entry) => cleanString(entry)).filter(Boolean)
|
|
: [];
|
|
if (volumes.length) payload.volumes = volumes;
|
|
|
|
const env = sanitizeEnvPayload(docker.env);
|
|
if (env && Object.keys(env).length) payload.env = env;
|
|
|
|
const adpanelStartup = cleanScript(
|
|
docker.adpanelStartup ||
|
|
docker.adpanel_startup ||
|
|
docker.pterodactylStartup ||
|
|
docker.pterodactyl_startup ||
|
|
docker.startup,
|
|
32768
|
|
);
|
|
if (adpanelStartup) payload.adpanelStartup = adpanelStartup;
|
|
|
|
const install = sanitizeDockerInstallPayload(docker.install || docker.installation);
|
|
if (install) payload.install = install;
|
|
|
|
if (docker.console !== undefined) {
|
|
payload.console = docker.console;
|
|
}
|
|
|
|
return payload;
|
|
}
|
|
|
|
module.exports = {
|
|
sanitizeDockerTemplatePayload,
|
|
sanitizeDockerInstallPayload,
|
|
sanitizeEnvPayload,
|
|
};
|