Add UDP communication support for instances

Add opt-in UDP messaging between the host and the Factorio server using
the --enable-lua-udp option of Factorio 2.0.59+.  When
factorio.enable_lua_udp is set the instance binds a localhost UDP socket,
starts Factorio with --enable-lua-udp on factorio.lua_udp_port (random
if null, like the RCON port) and tells the clusterio module the host port
via clusterio_private.update_instance.

Data from Factorio is sent with clusterio_api.send_udp(channel, data) in
the same IPC format as send_json, so plugins receive it through the
existing ipc-<channel> events.  Packets not sent from the Lua UDP port
are ignored, as that is the only unauthenticated way into the IPC
handler.  Data to Factorio is sent with the new sendUdp method on
instances and instance plugins, and is dispatched in-game as
on_udp_packet_received events by polling helpers.recv_udp every tick
from the clusterio module.

Plugins requiring UDP declare the new LuaUdp feature flag, which like
SavePatching and ScriptCommands refuses to load the plugin on instances
without the option enabled.

Verified against a headless 2.1.17 server: send_udp and recv_udp are
silent no-ops when the option is off, so a stale port in a save that is
moved to an instance without UDP does no harm.  The integration test
matrix now starts the instance with UDP enabled on Factorio 2.0.59+ and
round trips a player event over it.

Closes #969

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
bbassieandClaude Fable 5.1 committed 2026-10-15 16:38:28 +00:00
1 parent ec51adc71c
commit 788ae17657
15 files changed
+394 -17

No files matched your search

+20 -6
View File
@@ -38,17 +38,20 @@ function api.get_instance_id()
end
local function escape_channel(channel)
-- Escape bad characters. The question mark is used for separating the
-- channel name from the payload.
return channel:gsub("([\x00-\x1f?\\])", function(match)
return "\\x" .. string.format("%02x", match:byte())
end)
end
function api.send_json(channel, data)
if not remote.interfaces.clusterio_api then
return
end
-- Escape bad characters. The question mark is used for separating the
-- channel name from the payload.
channel = channel:gsub("([\x00-\x1f?\\])", function(match)
return "\\x" .. string.format("%02x", match:byte())
end)
channel = escape_channel(channel)
data = compat.table_to_json(data)
-- If there's more than about 4kB of data users running with the Windows
@@ -64,5 +67,16 @@ function api.send_json(channel, data)
end
end
function api.send_udp(channel, data)
local port = call_api("get_host_udp_port")
if not port then
return -- Lua UDP is not enabled for this instance
end
channel = escape_channel(channel)
data = compat.table_to_json(data)
helpers.send_udp(port, "\f$ipc:" .. channel .. "?j" .. data, 0)
end
return api
+16 -3
View File
@@ -17,14 +17,16 @@ function api.get_instance_id()
return compat.script_data.clusterio.instance_id
end
function api.send_json(channel, data)
local function escape_channel(channel)
-- Escape bad characters. The question mark is used for separating the
-- channel name from the payload.
channel = channel:gsub("([\x00-\x1f?\\])", function(match)
return channel:gsub("([\x00-\x1f?\\])", function(match)
return "\\x" .. string.format("%02x", match:byte())
end)
end
function api.send_json(channel, data)
channel = escape_channel(channel)
data = compat.table_to_json(data)
-- If there's more than about 4kB of data users running with the Windows
@@ -41,5 +43,16 @@ function api.send_json(channel, data)
end
end
function api.send_udp(channel, data)
local port = compat.script_data.clusterio.host_udp_port
if not port then
return -- Lua UDP is not enabled for this instance
end
channel = escape_channel(channel)
data = compat.table_to_json(data)
helpers.send_udp(port, "\f$ipc:" .. channel .. "?j" .. data, 0)
end
return api
+15 -2
View File
@@ -15,13 +15,21 @@ end
local impl = {}
impl.events = {}
impl.events[defines.events.on_tick] = check_patch
impl.events[defines.events.on_tick] = function()
check_patch()
-- Dispatch on_udp_packet_received for packets sent by the host
local clusterio = compat.script_data.clusterio
if clusterio and clusterio.host_udp_port then
helpers.recv_udp(0)
end
end
impl.events[api.events.on_server_startup] = function()
if not compat.script_data.clusterio then
compat.script_data.clusterio = {
instance_id = nil,
instance_name = nil,
host_udp_port = nil,
}
end
@@ -74,11 +82,12 @@ end
-- Internal API
clusterio_private = {}
function clusterio_private.update_instance(new_id, new_name)
function clusterio_private.update_instance(new_id, new_name, host_udp_port)
check_patch()
local script_data = compat.script_data
script_data.clusterio.instance_id = new_id
script_data.clusterio.instance_name = new_name
script_data.clusterio.host_udp_port = host_udp_port
script.raise_event(api.events.on_instance_updated, {
instance_id = new_id,
instance_name = new_name,
@@ -105,6 +114,10 @@ remote.add_interface('clusterio_api', {
return compat.script_data.clusterio.instance_name
end,
get_host_udp_port = function()
return compat.script_data.clusterio.host_udp_port
end,
get_file_no = function()
local script_data = compat.script_data
script_data.clusterio_file_no = (script_data.clusterio_file_no or 0) + 1
+16
View File
@@ -259,6 +259,22 @@ export class BaseInstancePlugin {
return await this.instance.sendRcon(message, expectEmpty, this.info.name);
}
/**
* Send UDP message to instance
*
* Send a UDP packet to the Factorio server, which is delivered in-game
* as an on_udp_packet_received event. Delivery is not guaranteed and
* the payload should be kept well below 64 kB. Requires the LuaUdp
* plugin feature.
*
* This should not be called before onStart or after onStop.
*
* @param message - payload of the packet to send.
*/
async sendUdp(message: string | Buffer): Promise<void> {
await this.instance.sendUdp(message);
}
/**
* Send serially ordered RCON message to instance
*
+21 -1
View File
@@ -232,6 +232,8 @@ export default class Instance extends lib.Link {
gamePort: this.config.get("factorio.game_port") ?? host.assignGamePort(this.id),
rconPort: this.config.get("factorio.rcon_port") ?? undefined,
rconPassword: this.config.get("factorio.rcon_password") ?? undefined,
enableLuaUdp: this.config.get("factorio.enable_lua_udp"),
luaUdpPort: this.config.get("factorio.lua_udp_port") ?? undefined,
enableWhitelist: this.config.get("factorio.enable_whitelist"),
enableAuthserverBans: this.config.get("factorio.enable_authserver_bans"),
verboseLogging: this.config.get("factorio.verbose_logging"),
@@ -505,6 +507,23 @@ end`.replace(/\r?\n/g, " ");
}
}
/**
* Send UDP message to the Factorio server
*
* Delivered in-game as an on_udp_packet_received event. Requires
* factorio.enable_lua_udp to be enabled on the instance.
*
* @param message - payload of the packet to send.
*/
async sendUdp(message: string | Buffer) {
if (!this.config.get("factorio.enable_lua_udp")) {
throw new Error(
"Attempted to send UDP message while Lua UDP is disabled. See config factorio.enable_lua_udp."
);
}
await this.server.sendUdp(message);
}
static async listSaves(instanceId: number, savesDir: string, loadedSave: string | null) {
let defaultSave = null;
if (loadedSave === null) {
@@ -1089,7 +1108,8 @@ end`.replace(/\r?\n/g, " ");
*/
async updateInstanceData() {
let name = lib.escapeString(this.name);
await this.sendRcon(`/sc clusterio_private.update_instance(${this.id}, "${name}")`, true);
let hostUdpPort = this.server.hostUdpPort ?? "nil";
await this.sendRcon(`/sc clusterio_private.update_instance(${this.id}, "${name}", ${hostUdpPort})`, true);
}
async updateFactorioSettings(current: Record<string, unknown>, previous: Record<string, unknown>) {
+97
View File
@@ -2,6 +2,7 @@
import fs from "node:fs/promises";
import { type FSWatcher, watch as fsWatch, writeFileSync } from "node:fs";
import child_process from "child_process";
import dgram from "dgram";
import path from "path";
import JSZip from "jszip";
import events from "events";
@@ -542,6 +543,10 @@ export interface FactorioServerOptions {
rconPort?: number,
/** Password use for RCON. */
rconPassword?: string,
/** Enable UDP messaging with the Factorio server. */
enableLuaUdp?: boolean,
/** UDP port the Factorio server receives Lua UDP messages on. */
luaUdpPort?: number,
/** Turn on whitelisting. */
enableWhitelist?: boolean,
/** Enable Factorio.com based multiplayer bans. */
@@ -607,6 +612,8 @@ type FactorioServerEvents = {
* - autosave-fnished - invoked when the autosave finished
* - save-finished - invoked when the server has finished a manual save
* - exit - invoked when the sterver has exited
* - ipc-<channel> - invoked with data sent by the clusterio module in-game,
* either via stdout or via UDP when Lua UDP is enabled
* @extends events.EventEmitter
*/
export class FactorioServer extends events.EventEmitter<FactorioServerEvents> {
@@ -618,6 +625,10 @@ export class FactorioServer extends events.EventEmitter<FactorioServerEvents> {
rconPort: number;
/** Password used for RCON on the Factorio game server */
rconPassword: string;
/** Enable UDP messaging with the Factorio game server */
enableLuaUdp: boolean;
/** UDP port the Factorio game server receives Lua UDP messages on */
luaUdpPort: number;
/** Enable player whitelist */
enableWhitelist: boolean;
/** Enable Factorio.com based multiplayer bans **/
@@ -647,6 +658,7 @@ export class FactorioServer extends events.EventEmitter<FactorioServerEvents> {
_server: child_process.ChildProcessWithoutNullStreams | null = null;
_rconClient: Rcon | null= null;
_rconReady = false;
_udpSocket: dgram.Socket | null = null;
_gameReady = false;
_stripRegExp?: RegExp;
_maxConcurrentCommands = 5;
@@ -679,6 +691,10 @@ export class FactorioServer extends events.EventEmitter<FactorioServerEvents> {
this.rconPort = options.rconPort || randomDynamicPort();
/** Password used for RCON on the Factorio game server */
this.rconPassword = options.rconPassword as string; // init will generate one if not there
/** Enable UDP messaging with the Factorio game server */
this.enableLuaUdp = options.enableLuaUdp || false;
/** UDP port the Factorio game server receives Lua UDP messages on */
this.luaUdpPort = options.luaUdpPort || randomDynamicPort();
/** Enable player whitelist */
this.enableWhitelist = options.enableWhitelist || false;
/** Enable Factorio.com based multiplayer bans **/
@@ -791,6 +807,57 @@ export class FactorioServer extends events.EventEmitter<FactorioServerEvents> {
}
}
_handleUdpMessage(message: Buffer, rinfo: dgram.RemoteInfo) {
// Factorio sends from the socket bound to the Lua UDP port, anything
// else is some other local process.
if (rinfo.port !== this.luaUdpPort) {
this._logger.warn(`Ignoring UDP packet from unexpected source ${rinfo.address}:${rinfo.port}`);
return;
}
if (message.subarray(0, 6).equals(Buffer.from("\f$ipc:"))) {
this._handleIpc(message).catch(err => this.emit("error", err));
return;
}
this._logger.warn(`Ignoring UDP packet not in IPC format: ${message.subarray(0, 100).toString("utf-8")}`);
}
async _bindUdpSocket() {
const socket = dgram.createSocket("udp4");
socket.on("message", (message, rinfo) => this._handleUdpMessage(message, rinfo));
try {
await new Promise<void>((resolve, reject) => {
socket.once("error", reject);
socket.bind(0, "127.0.0.1", () => {
socket.off("error", reject);
resolve();
});
});
} catch (err) {
socket.close();
throw err;
}
socket.on("error", err => this.emit("error", err));
this._udpSocket = socket;
}
_closeUdpSocket() {
if (this._udpSocket) {
this._udpSocket.close();
this._udpSocket = null;
}
}
/**
* UDP port on localhost the host receives Lua UDP messages on
*
* Only available while the server is running with Lua UDP enabled.
*/
get hostUdpPort(): number | undefined {
return this._udpSocket?.address().port;
}
_handleOutput(rawLine: Buffer, source: "stdout" | "stderr") {
if (rawLine.subarray(0, 6).equals(Buffer.from("\f$ipc:"))) {
this._handleIpc(rawLine).catch(err => this.emit("error", err));
@@ -1063,6 +1130,7 @@ export class FactorioServer extends events.EventEmitter<FactorioServerEvents> {
this._server = null;
this._rconClient = null;
this._rconReady = false;
this._closeUdpSocket();
this._gameReady = false;
this._unexpected = [];
this._killed = false;
@@ -1224,6 +1292,9 @@ export class FactorioServer extends events.EventEmitter<FactorioServerEvents> {
*/
async start(save: string) {
this._check(["init"]);
if (this.enableLuaUdp) {
await this._bindUdpSocket();
}
this._state = "running";
try {
@@ -1239,6 +1310,7 @@ export class FactorioServer extends events.EventEmitter<FactorioServerEvents> {
"--port", String(this.gamePort),
"--rcon-port", String(this.rconPort),
"--rcon-password", this.rconPassword,
...(this.enableLuaUdp ? ["--enable-lua-udp", String(this.luaUdpPort)] : []),
...(this.enableWhitelist ? ["--use-server-whitelist"] : []),
...(this.enableAuthserverBans ? ["--use-authserver-bans"] : []),
...(this.verboseLogging ? ["--verbose"] : []),
@@ -1276,6 +1348,9 @@ export class FactorioServer extends events.EventEmitter<FactorioServerEvents> {
*/
async startScenario(scenario: string, seed?: number, mapGenSettings?: object, mapSettings?: object) {
this._check(["init"]);
if (this.enableLuaUdp) {
await this._bindUdpSocket();
}
this._state = "running";
try {
@@ -1297,6 +1372,7 @@ export class FactorioServer extends events.EventEmitter<FactorioServerEvents> {
"--port", String(this.gamePort),
"--rcon-port", String(this.rconPort),
"--rcon-password", this.rconPassword,
...(this.enableLuaUdp ? ["--enable-lua-udp", String(this.luaUdpPort)] : []),
...(this.enableWhitelist ? ["--use-server-whitelist"] : []),
...(this.enableAuthserverBans ? ["--use-authserver-bans"] : []),
...(this.verboseLogging ? ["--verbose"] : []),
@@ -1377,6 +1453,27 @@ export class FactorioServer extends events.EventEmitter<FactorioServerEvents> {
return response;
}
/**
* Send UDP message to the server
*
* Sends a UDP packet to the Lua UDP port of the Factorio server, which
* is delivered in-game as an on_udp_packet_received event. Delivery is
* not guaranteed. Requires Lua UDP to be enabled.
*
* @param message - payload of the packet to send.
*/
async sendUdp(message: string | Buffer) {
this._check(["running", "stopping"]);
if (!this._udpSocket) {
throw new Error("Lua UDP is not enabled");
}
const socket = this._udpSocket;
await new Promise<void>((resolve, reject) => {
socket.send(message, this.luaUdpPort, "127.0.0.1", err => (err ? reject(err) : resolve()));
});
}
/**
* Timeout in ms to wait after a shutdown is requested before killing the
* process. Defaults to 0 meaning no timeout
+26 -1
View File
@@ -482,6 +482,8 @@ export interface InstanceConfigFields {
"factorio.host_assigned_game_port": number | null;
"factorio.rcon_port": number | null;
"factorio.rcon_password": string | null;
"factorio.enable_lua_udp": boolean;
"factorio.lua_udp_port": number | null;
"factorio.player_online_autosave_slots": number;
"factorio.mod_pack_id": number | null;
"factorio.enable_save_patching": boolean;
@@ -606,6 +608,21 @@ export class InstanceConfig extends classes.Config<InstanceConfigFields> {
type: "string",
optional: true,
},
"factorio.enable_lua_udp": {
description:
"Enable UDP messaging between the Factorio server and the host. " +
"Required by plugins using UDP. Requires Factorio 2.0.59 or later.",
restartRequired: true, // Because of the cli option "--enable-lua-udp"
type: "boolean",
initialValue: false,
},
"factorio.lua_udp_port": {
description: "UDP port for the Factorio server to receive Lua UDP messages on, uses a random port if null",
restartRequired: true,
type: "number",
optional: true,
validator: validators.optional(validators.greaterThanEqualZero),
},
"factorio.player_online_autosave_slots": {
description:
"Rename autosaves where players have been online since the previous autosave into a separate " +
@@ -795,7 +812,12 @@ export function addPluginFieldDefinitions(
fieldDef.dependsOn.push("factorio.enable_script_commands");
}
if (scriptCommands || savePatching) {
const luaUdp = pluginInfo.features.includes("LuaUdp");
if (luaUdp) {
fieldDef.dependsOn.push("factorio.enable_lua_udp");
}
if (scriptCommands || savePatching || luaUdp) {
fieldDef.validator = (enabled, config) => {
if (!enabled) {
return;
@@ -806,6 +828,9 @@ export function addPluginFieldDefinitions(
if (scriptCommands && !config.get("factorio.enable_script_commands")) {
throw new Error(`Plugin ${pluginInfo.name} requires script commands`);
}
if (luaUdp && !config.get("factorio.enable_lua_udp")) {
throw new Error(`Plugin ${pluginInfo.name} requires Lua UDP`);
}
};
}
}
+2
View File
@@ -14,6 +14,8 @@ export const PluginFeatureFlags = [
"SavePatching",
/** The plugin requires access to script commands over rcon */
"ScriptCommands",
/** The plugin requires UDP messaging with the Factorio server */
"LuaUdp",
] as const;
/** Used to define the plugin export in plugins */