Merge branch 'master' into feature/download-file-cleanup

This commit is contained in:
Cooldude2606 authored and GitHub committed 2026-07-29 23:13:29 +01:00
commit 3a689b379a
21 files changed
+632 -46

No files matched your search

+2
View File
@@ -43,6 +43,7 @@
"finalhandler": "^1.3.1",
"jsonwebtoken": "catalog:",
"jszip": "catalog:",
"semver": "^7.7.1",
"winston": "catalog:",
"winston-daily-rotate-file": "catalog:",
"ws": "catalog:",
@@ -57,6 +58,7 @@
"@types/finalhandler": "catalog:",
"@types/jsonwebtoken": "catalog:",
"@types/node": "catalog:",
"@types/semver": "^7.5.0",
"@types/ws": "catalog:",
"@types/yargs": "^17.0.33",
"react": "catalog:",
@@ -189,6 +189,15 @@ export default class ControlConnection extends BaseConnection {
if (!this._controller.canRestart) {
throw new lib.RequestError("Cannot restart, controller does not have a process monitor to restart it.");
}
const downgrade = await this._controller.checkRestartDowngrade();
if (downgrade) {
const { installedVersion, runningVersion } = downgrade;
throw new lib.RequestError(
`Cannot restart controller with installed Clusterio version ${installedVersion} because it ` +
`is older than running version ${runningVersion}. ` +
"Stop the controller before starting the older version manually."
);
}
this._controller.shouldRestart = true;
this._controller.stop();
}
+26
View File
@@ -12,6 +12,7 @@ import http from "http";
import https from "https";
import jwt from "jsonwebtoken";
import path from "path";
import semver from "semver";
import stream from "stream";
import * as lib from "@clusterio/lib";
@@ -654,6 +655,31 @@ export default class Controller {
return false;
}
async checkRestartDowngrade() {
try {
const runningVersion = this.config.get("controller.version");
const packageJsonPath = require.resolve("@clusterio/controller/package.json");
const packageJson = JSON.parse(await fs.readFile(packageJsonPath, "utf8"));
const installedVersion = packageJson.version;
if (!semver.valid(runningVersion) || !semver.valid(installedVersion)) {
logger.warn(
`Unable to compare running controller version ${runningVersion} ` +
`with installed version ${installedVersion}.`
);
return null;
}
if (semver.lt(installedVersion, runningVersion)) {
return { installedVersion, runningVersion };
}
} catch (err: any) {
logger.warn(`Failed to check controller version before restart:\n${err.stack ?? err.message}`);
}
return null;
}
onAutosaveIntervalChanged() {
if (this.autosaveInterval) {
clearInterval(this.autosaveInterval);
+3 -4
View File
@@ -118,12 +118,11 @@ async function safeOutputFile(file, data, options={}) {
}
async function execFile(cmd, args) {
const escaped = args.map(escapeArg);
logger.verbose(`executing ${cmd} ${escaped.join(" ")}`);
const command = [cmd, ...args.map(escapeArg)].join(" ");
logger.verbose(`executing ${command}`);
return new Promise((resolve, reject) => {
let child = child_process.execFile(
cmd,
escaped,
command,
{
shell: true,
+34
View File
@@ -2,6 +2,7 @@ import fs from "node:fs/promises";
import path from "path";
import events from "events";
import pidusage from "pidusage";
import semver from "semver";
import setBlocking from "set-blocking";
import stream from "stream";
import util from "util";
@@ -496,6 +497,14 @@ export default class Host extends lib.Link {
if (!this.canRestart) {
throw new lib.RequestError("Cannot restart, host does not have a process monitor to restart it.");
}
const downgrade = await this.checkRestartDowngrade();
if (downgrade) {
const { installedVersion, runningVersion } = downgrade;
throw new lib.RequestError(
`Cannot restart host with installed Clusterio version ${installedVersion} because it is older than ` +
`running version ${runningVersion}. Stop the host before starting the older version manually.`
);
}
process.exitCode = 1;
this.shutdown();
}
@@ -875,6 +884,31 @@ export default class Host extends lib.Link {
return false;
}
async checkRestartDowngrade() {
try {
const runningVersion = this.config.get("host.version");
const packageJsonPath = require.resolve("@clusterio/host/package.json");
const packageJson = JSON.parse(await fs.readFile(packageJsonPath, "utf8"));
const installedVersion = packageJson.version;
if (!semver.valid(runningVersion) || !semver.valid(installedVersion)) {
logger.warn(
`Unable to compare running host version ${runningVersion} ` +
`with installed version ${installedVersion}.`
);
return null;
}
if (semver.lt(installedVersion, runningVersion)) {
return { installedVersion, runningVersion };
}
} catch (err: any) {
logger.warn(`Failed to check host version before restart:\n${err.stack ?? err.message}`);
}
return null;
}
async handleHostMetricsRequest() {
let requests: Promise<InstanceType<typeof lib.InstanceMetricsRequest["Response"]>>[] = [];
for (let instanceConnection of this.instanceConnections.values()) {
+85 -1
View File
@@ -12,7 +12,7 @@ import ModInfo, { ModDependencyUnsatisfiedReason } from "./ModInfo";
import {
PartialVersion, PartialVersionSchema, integerPartialVersion,
SourceVersion, SourceVersionSchema, normaliseFullVersion,
SourceVersion, SourceVersionSchema, normaliseFullVersion, integerSourceVersion,
} from "./version";
@@ -32,6 +32,10 @@ export interface ModSetting {
value: boolean | number | string | ModSettingColor;
}
export type ModRecordAdvisory =
| { type: "recommended_dependency", sourceModName: string }
| { type: "update_available", version: SourceVersion };
const ModSettingJsonSchema = Type.Object({
"value": Type.Union([Type.Boolean(), Type.Number(), Type.String(), ModSettingColor]),
});
@@ -56,6 +60,86 @@ export interface ModRecord {
warning?: ModDependencyUnsatisfiedReason | "wrong_factorio_version",
/** Used inside packages\web_ui\src\components\ModPackViewPage.tsx when there is no error. */
info?: ModInfo,
/** Non-blocking recommendations displayed by the Web UI. */
advisories?: ModRecordAdvisory[],
}
type AnnotatedModRecord = ModRecord & { advisories: ModRecordAdvisory[] };
/**
* Finds newer versions already installed on the controller for mods in a pack.
*/
export function getInstalledModUpdates(mods: ModRecord[], installedMods: Iterable<ModInfo>) {
const updates = new Map<string, SourceVersion>();
for (const mod of mods) {
if (!mod.info) {
continue;
}
for (const installedMod of installedMods) {
if (
installedMod.name !== mod.name
|| installedMod.factorioVersion !== mod.info.factorioVersion
|| installedMod.integerVersion <= integerSourceVersion(mod.version)
) {
continue;
}
const existingUpdate = updates.get(mod.name);
if (!existingUpdate || installedMod.integerVersion > integerSourceVersion(existingUpdate)) {
updates.set(mod.name, installedMod.version);
}
}
}
return updates;
}
export function applyModRecordAdvisories(
mods: ModRecord[],
availableUpdates: ReadonlyMap<string, SourceVersion> = new Map(),
) {
const annotatedMods: AnnotatedModRecord[] = mods.map(mod => ({ ...mod, advisories: [] }));
const modsByName = new Map(annotatedMods.map(mod => [mod.name, mod]));
for (const sourceMod of annotatedMods) {
if (!sourceMod.enabled || !sourceMod.info) {
continue;
}
for (const dependency of sourceMod.info.dependencies) {
if (!dependency.recommended) {
continue;
}
const targetMod = modsByName.get(dependency.name);
if (
!targetMod
|| targetMod.enabled
|| dependency.version && !dependency.version.testVersion(targetMod.version)
) {
continue;
}
targetMod.advisories.push({
type: "recommended_dependency",
sourceModName: sourceMod.name,
});
}
}
for (const mod of annotatedMods) {
const availableVersion = availableUpdates.get(mod.name);
if (availableVersion && integerSourceVersion(availableVersion) > integerSourceVersion(mod.version)) {
mod.advisories.push({ type: "update_available", version: availableVersion });
}
if (mod.advisories.length === 0) {
delete (mod as ModRecord).advisories;
}
}
return annotatedMods;
}
const ModRecordJsonSchema = Type.Object({
+2 -2
View File
@@ -5,8 +5,8 @@
*/
export { default as ExportManifest } from "./ExportManifest";
export { default as ModInfo, ModDependency } from "./ModInfo";
export { ModSettingColor } from "./ModPack";
export type { ModSetting, ModRecord } from "./ModPack";
export { ModSettingColor, getInstalledModUpdates, applyModRecordAdvisories } from "./ModPack";
export type { ModSetting, ModRecord, ModRecordAdvisory } from "./ModPack";
export { default as ModPack } from "./ModPack";
export { default as ModuleInfo } from "./ModuleInfo";
export { default as Permission } from "./Permission";
+18
View File
@@ -114,6 +114,24 @@ export class Address {
return `${this.type}:${this.id}` as const;
}
/**
* Returns this address with any request id removed
*
* A request id identifies one request in flight and is set on the
* source address of a request so that its response can be matched back
* up with it. It is meaningless on anything else, so an address taken
* from a request has to be stripped of it before it is used as the
* destination of an event.
*
* @returns this address without a request id.
*/
withoutRequestId() {
if (this.requestId === undefined) {
return this;
}
return new Address(this.type, this.id);
}
/**
* Returns true if this address targets the given destination
* @param dst - Destination to check
+4 -3
View File
@@ -7,6 +7,8 @@ import fs from "node:fs/promises";
import path from "path";
import stream from "stream";
import { redactUrl } from "./helpers";
/**
* Returns the newest file in a directory
@@ -332,12 +334,11 @@ export async function downloadFile(
}
// Fetch stream bytes. Anything that goes wrong from here until the
// body has been written leaves a temporary file that is of no use to
// anyone, including the request never reaching the server at all.
// body has been written leaves a temporary file that needs to be removed
try {
const response = await fetch(url);
if (!response.ok || !response.body) {
throw new Error(`Failed to download ${url}: ${response.status} ${response.statusText}`);
throw new Error(`Failed to download ${redactUrl(url)}: ${response.status} ${response.statusText}`);
}
const writer = stream.Writable.toWeb(writeStream);
await response.body.pipeTo(writer);
+45
View File
@@ -268,6 +268,51 @@ export function formatBytes(bytes: number, prefixes: "si" | "binary" = "si") {
return significant.toFixed(fractionDigits) + units[exponent];
}
/**
* Query parameters whose values are credentials and must never be logged.
*
* Matched case insensitively against the full parameter name.
*/
const sensitiveSearchParams = new Set([
"access_token",
"api_key",
"apikey",
"password",
"secret",
"token",
]);
/**
* Replace credentials in a URL with a placeholder
*
* Returns the URL as a string with the values of any query parameter
* carrying a credential replaced by `REDACTED`, so that the URL can
* safely be included in log output and error messages. The parameter
* names are kept intact to preserve the diagnostic value of the URL.
*
* Returns the input unchanged if it is a string which does not parse as
* a URL, as there are then no query parameters to redact.
*
* @param url - URL to redact credentials from.
* @returns url with credentials replaced by `REDACTED`.
*/
export function redactUrl(url: URL | string) {
let parsed;
try {
parsed = new URL(url);
} catch {
return String(url);
}
const searchParams = new URLSearchParams();
for (const [name, value] of parsed.searchParams) {
searchParams.append(name, sensitiveSearchParams.has(name.toLowerCase()) ? "REDACTED" : value);
}
parsed.search = searchParams.toString();
return parsed.toString();
}
function skipWhitespace(pos: number, input: string) {
// whitespace = 1*" "
while (pos < input.length && input.charAt(pos) === " ") {
+10 -9
View File
@@ -238,7 +238,7 @@ export class SubscriptionRequest {
type Subscriber = {
link: Link,
dst: Address,
address: Address,
filters: SubscriptionFilters,
}
@@ -296,7 +296,7 @@ export class SubscriptionController {
continue;
}
if (subscriber.filters.intersects(broadcastFilters)) {
subscriber.link.sendTo(subscriber.dst, event);
subscriber.link.sendTo(subscriber.address, event);
}
}
}
@@ -343,8 +343,9 @@ export class SubscriptionController {
if (!eventData) {
throw new Error(`Event ${request.eventName} is not a registered as subscribable`);
}
const addressIndex = src.addressIndex();
const subscriber = eventData.subscriptions.get(addressIndex);
const index = src.addressIndex();
const address = src.withoutRequestId();
const subscriber = eventData.subscriptions.get(index);
switch (request.action) {
case "unsubscribe":
if (!subscriber) {
@@ -352,13 +353,13 @@ export class SubscriptionController {
}
subscriber.filters.subtract(request.filters);
if (subscriber.filters.isEmpty()) {
eventData.subscriptions.delete(addressIndex);
eventData.subscriptions.delete(index);
}
break;
case "subscribe":
if (!subscriber) {
eventData.subscriptions.set(addressIndex, { link: link, dst: src, filters: request.filters });
eventData.subscriptions.set(index, { link, address, filters: request.filters });
} else {
subscriber.filters.union(request.filters);
}
@@ -369,9 +370,9 @@ export class SubscriptionController {
if (!subscriber) {
return false;
}
eventData.subscriptions.delete(addressIndex);
eventData.subscriptions.delete(index);
} else if (!subscriber) {
eventData.subscriptions.set(addressIndex, { link: link, dst: src, filters: request.filters });
eventData.subscriptions.set(index, { link, address, filters: request.filters });
} else {
subscriber.filters = request.filters;
}
@@ -384,7 +385,7 @@ export class SubscriptionController {
if (eventData.subscriptionUpdate) {
const eventReplay = await eventData.subscriptionUpdate(request, src, dst);
if (eventReplay) {
link.sendTo(src, eventReplay);
link.sendTo(address, eventReplay);
return true;
}
}
@@ -11,6 +11,7 @@ import {
import {
ExportOutlined, FileUnknownOutlined, FileExclamationOutlined, FileSyncOutlined,
CloseOutlined, DeleteOutlined, ToolOutlined, PlusOutlined, CloudSyncOutlined, CloudDownloadOutlined,
LikeOutlined,
} from "@ant-design/icons";
import type { SorterResult, FilterValue, TableCurrentDataSource } from "antd/es/table/interface";
@@ -545,7 +546,7 @@ function ModsTable(props: ModsTableProps) {
}
}
const mods = [...props.modPack.mods.values(), ...deletedMods.values()].map(
let mods = [...props.modPack.mods.values(), ...deletedMods.values()].map(
(mod: lib.ModRecord): lib.ModRecord => {
if (props.builtInModNames.includes(mod.name)) {
return {
@@ -590,6 +591,7 @@ function ModsTable(props: ModsTableProps) {
}
}
}
mods = lib.applyModRecordAdvisories(mods, lib.getInstalledModUpdates(mods, modInfos.values()));
async function fixDependencyIssues(mod: lib.ModRecord) {
if (!mod.info) {
@@ -766,27 +768,48 @@ function ModsTable(props: ModsTableProps) {
{
title: "Name",
key: "name",
render: (_, mod: lib.ModRecord) => <>
{mod.error === "missing" && <Tooltip title="Mod is missing from storage.">
<FileUnknownOutlined style={{ color: "#a61d24" }} />{" "}
</Tooltip>}
{mod.error === "bad_checksum" && <Tooltip title="Mod checksum mismatch.">
<FileExclamationOutlined style={{ color: "#a61d24" }} />{" "}
</Tooltip>}
{mod.warning === "incompatible" && <Tooltip title="Mod is incompatible with another.">
<FileExclamationOutlined style={{ color: "#dd5e14" }} />{" "}
</Tooltip>}
{mod.warning === "missing_dependency" && <Tooltip title="Mod is missing a dependency.">
<FileUnknownOutlined style={{ color: "#dd5e14" }} />{" "}
</Tooltip>}
{mod.warning === "wrong_version" && <Tooltip title="Mod has wrong dependency version added.">
<FileSyncOutlined style={{ color: "#dd5e14" }} />{" "}
</Tooltip>}
{mod.warning === "wrong_factorio_version" && <Tooltip title="Mod has wrong factorio version.">
<FileSyncOutlined style={{ color: "#dd5e14" }} />{" "}
</Tooltip>}
{mod.info?.title || mod.name}
</>,
render: (_, mod: lib.ModRecord) => {
const recommendedBy = mod.advisories
?.filter(advisory => advisory.type === "recommended_dependency")
.map(advisory => advisory.sourceModName) ?? [];
const update = mod.advisories
?.find(advisory => advisory.type === "update_available");
return <>
{mod.error === "missing" && <Tooltip title="Mod is missing from storage.">
<FileUnknownOutlined style={{ color: "#a61d24" }} />{" "}
</Tooltip>}
{mod.error === "bad_checksum" && <Tooltip title="Mod checksum mismatch.">
<FileExclamationOutlined style={{ color: "#a61d24" }} />{" "}
</Tooltip>}
{mod.warning === "incompatible" && <Tooltip title="Mod is incompatible with another.">
<FileExclamationOutlined style={{ color: "#dd5e14" }} />{" "}
</Tooltip>}
{mod.warning === "missing_dependency" && <Tooltip title="Mod is missing a dependency.">
<FileUnknownOutlined style={{ color: "#dd5e14" }} />{" "}
</Tooltip>}
{mod.warning === "wrong_version" && <Tooltip
title="Mod has wrong dependency version added."
>
<FileSyncOutlined style={{ color: "#dd5e14" }} />{" "}
</Tooltip>}
{mod.warning === "wrong_factorio_version" && <Tooltip
title="Mod has wrong factorio version."
>
<FileSyncOutlined style={{ color: "#dd5e14" }} />{" "}
</Tooltip>}
{recommendedBy.length > 0 && <Tooltip
title={`Recommended by ${recommendedBy.join(", ")}, but currently disabled.`}
>
<LikeOutlined style={{ color: "#0958d9" }} />{" "}
</Tooltip>}
{update?.type === "update_available" && <Tooltip
title={`A newer compatible version (${update.version}) is available.`}
>
<CloudSyncOutlined style={{ color: "#0958d9" }} />{" "}
</Tooltip>}
{mod.info?.title || mod.name}
</>;
},
defaultSortOrder: "ascend",
sorter: (a, b) => strcmp(a.name, b.name),
},