From 762212747ee88882ed0abb780bdefb9158c2d401 Mon Sep 17 00:00:00 2001 From: Cooldude2606 <25043174+Cooldude2606@users.noreply.github.com> Date: Fri, 1 Nov 2024 17:42:33 +0000 Subject: [PATCH] Added factorio.enable_script_commands --- packages/host/src/Instance.ts | 16 ++++++++++++++++ packages/lib/src/config/definitions.ts | 10 ++++++++++ plugins/global_chat/instance.ts | 4 ++++ plugins/inventory_sync/instance.ts | 6 ++++++ plugins/research_sync/instance.ts | 3 +++ plugins/statistics_exporter/instance.ts | 3 +++ plugins/subspace_storage/instance.ts | 4 ++++ 7 files changed, 46 insertions(+) diff --git a/packages/host/src/Instance.ts b/packages/host/src/Instance.ts index c7208f0e..4101a7c1 100644 --- a/packages/host/src/Instance.ts +++ b/packages/host/src/Instance.ts @@ -14,6 +14,12 @@ import { exportData } from "./export"; import type Host from "./Host"; import BaseInstancePlugin from "./BaseInstancePlugin"; +const scriptCommands = [ + "/cheat", "/editor", + "/command", "/c", + "/measured-command", "/mc", + "/silent-command", "/sc" +]; const instanceRconCommandDuration = new lib.Histogram( "clusterio_instance_rcon_command_duration_seconds", @@ -371,6 +377,16 @@ rcon.print(game.table_to_json(players))`.replace(/\r?\n/g, " "); } async sendRcon(message: string, expectEmpty = false, plugin = "") { + if ( + !this.config.get("factorio.enable_script_commands") + && scriptCommands.find(cmd => message.startsWith(cmd)) + ) { + throw new Error( + "Attempted to use script command while disabled. See config factorio.enable_script_commands.\n" + + `Command: ${message}` + ); + } + let instanceId = String(this.id); let observeDuration = instanceRconCommandDuration.labels(instanceId).startTimer(); try { diff --git a/packages/lib/src/config/definitions.ts b/packages/lib/src/config/definitions.ts index 0584f4c3..db0b6da8 100644 --- a/packages/lib/src/config/definitions.ts +++ b/packages/lib/src/config/definitions.ts @@ -319,6 +319,7 @@ export interface InstanceConfigFields { "factorio.player_online_autosave_slots": number; "factorio.mod_pack_id": number | null; "factorio.enable_save_patching": boolean; + "factorio.enable_script_commands": boolean; "factorio.enable_whitelist": boolean; "factorio.enable_authserver_bans": boolean; "factorio.settings": Record; @@ -427,6 +428,15 @@ export class InstanceConfig extends classes.Config { type: "boolean", initialValue: true, }, + "factorio.enable_script_commands": { + description: + "Allows achievement breaking commands to be executed over rcon. " + + "Required for Clusterio integrations and most plugins. " + + "This does not prevent players using script commands.", + restartRequired: true, + type: "boolean", + initialValue: true, + }, "factorio.enable_whitelist": { description: "Turn on whitelist for joining the server.", type: "boolean", diff --git a/plugins/global_chat/instance.ts b/plugins/global_chat/instance.ts index 2cfb2c6e..10c0401c 100644 --- a/plugins/global_chat/instance.ts +++ b/plugins/global_chat/instance.ts @@ -16,6 +16,10 @@ export class InstancePlugin extends BaseInstancePlugin { messageQueue: string[] = []; async init() { + if (!this.instance.config.get("factorio.enable_script_commands")) { + throw new Error("global_chat plugin requires script commands."); + } + this.instance.handle(ChatEvent, this.handleChatEvent.bind(this)); } diff --git a/plugins/inventory_sync/instance.ts b/plugins/inventory_sync/instance.ts index c831e8fd..275d033e 100644 --- a/plugins/inventory_sync/instance.ts +++ b/plugins/inventory_sync/instance.ts @@ -31,6 +31,12 @@ export class InstancePlugin extends BaseInstancePlugin { disconnecting!: boolean; async init() { + if (!this.instance.config.get("factorio.enable_save_patching")) { + throw new Error("inventory_sync plugin requires save patching."); + } + if (!this.instance.config.get("factorio.enable_script_commands")) { + throw new Error("inventory_sync plugin requires script commands."); + } this.playersToRelease = new Set(); this.disconnecting = false; diff --git a/plugins/research_sync/instance.ts b/plugins/research_sync/instance.ts index 0b47531f..c905ff34 100644 --- a/plugins/research_sync/instance.ts +++ b/plugins/research_sync/instance.ts @@ -30,6 +30,9 @@ export class InstancePlugin extends BaseInstancePlugin { if (!this.instance.config.get("factorio.enable_save_patching")) { throw new Error("research_sync plugin requires save patching."); } + if (!this.instance.config.get("factorio.enable_script_commands")) { + throw new Error("research_sync plugin requires script commands."); + } this.instance.server.on("ipc-research_sync:contribution", (tech: IpcContribution) => { this.researchContribution(tech).catch(err => this.unexpectedError(err)); diff --git a/plugins/statistics_exporter/instance.ts b/plugins/statistics_exporter/instance.ts index 76502173..b824992d 100644 --- a/plugins/statistics_exporter/instance.ts +++ b/plugins/statistics_exporter/instance.ts @@ -66,6 +66,9 @@ export class InstancePlugin extends BaseInstancePlugin { if (!this.instance.config.get("factorio.enable_save_patching")) { throw new Error("statistics_exporter plugin requires save patching."); } + if (!this.instance.config.get("factorio.enable_script_commands")) { + throw new Error("statistics_exporter plugin requires script commands."); + } } async gatherMetrics() { diff --git a/plugins/subspace_storage/instance.ts b/plugins/subspace_storage/instance.ts index 9771eb26..cc9be259 100644 --- a/plugins/subspace_storage/instance.ts +++ b/plugins/subspace_storage/instance.ts @@ -20,6 +20,10 @@ export class InstancePlugin extends BaseInstancePlugin { } async init() { + if (!this.instance.config.get("factorio.enable_script_commands")) { + throw new Error("subspace_storage plugin requires script commands."); + } + this.pendingTasks = new Set(); this.instance.server.on("ipc-subspace_storage:output", (output: IpcItems) => { this.provideItems(output).catch(err => this.unexpectedError(err));