Enable the player proxy by default and give lobbies an empty save

The player proxy now listens on 34197 by default. New hosts default to
the port range 34200-34299 so they don't hand that port to instances,
and the controller logs an error and runs without the proxy if the port
is taken, as hosts with the old range only skip it when the controller
already holds it.

Lobby instances start the save lobby.zip, created on first start as a
one tile map with no autoplace and with the freeplay scenario replaced by
an empty one. This uses the new chooseSave instance hook, which lets
plugins pick or create the save to start, and setSaveScenario from
@clusterio/host, which sets the scenario the save patcher keeps loading.
Map gen settings can't use a width and height of 0 for this, as Factorio
takes 0 to mean an unlimited map.

The address players reach the proxy on moves from lobby.public_address
to controller.player_proxy_public_address. When it's set the player_auth
login page lists it once for the instances behind the proxy instead of
their own addresses, and lobbies with player_auth loaded get a button
that opens the web interface login dialog.
This commit is contained in:
Danielv123 committed 2026-10-29 20:54:11 +01:00
1 parent 3ae486143d
commit a5bd43637e
23 files changed
+391 -121

No files matched your search

+9 -14
View File
@@ -7,6 +7,7 @@ Works together with the [player proxy](../../docs/player-proxy.md) on the contro
The lobby shows a list of the instances in the cluster, picking one moves the player there with `LuaPlayer.connect_to_server`.
- Picking a stopped instance starts it, the player waits in the lobby and is moved once it's running.
- Other instances with the plugin loaded get a Servers button that opens the same list.
- If the [Player Auth](../player_auth/README.md) plugin is loaded the list has a button for logging into the web interface, which does the same as the `/web-login` command.
- The cluster can be listed in the Factorio server browser as one game per Factorio version a lobby runs, showing the players online in the whole cluster.
- Instances can also be listed in the server browser as their own game on a dedicated proxy port, including while they are stopped.
@@ -25,12 +26,15 @@ Substitute clusteriocontroller with clusteriohost or clusterioctl if this a dedi
## Setting up a lobby
1. Enable the player proxy by setting `controller.player_proxy_port` and restart the controller.
2. Set `lobby.public_address` to the address and port players reach the proxy on, for example `play.example.com:34197`.
This is the address players are sent to when they pick an instance.
1. Make sure the player proxy is enabled, it listens on `controller.player_proxy_port` which defaults to 34197.
2. Set `controller.player_proxy_public_address` to the address and port players reach the proxy on, for example `play.example.com:34197`.
This is the address players are sent to when they pick an instance, and what's published in the server browser.
If it's not set the host name of `controller.public_url` and `controller.player_proxy_port` is used.
3. Create an instance to act as the lobby, set `lobby.is_lobby` to true on it and start it.
Players are made spectators in the lobby, so any map works.
Create one lobby for each Factorio version you want to accept players on.
Lobbies always start the save `lobby.zip`, which is created the first time as an empty one tile map without the freeplay scenario, and players are made spectators in it.
To use a different map, replace `lobby.zip` with a save of your own.
Starting a specific save from the web interface or clusterioctl still works.
Create one lobby for each Factorio version you want to accept players on, by setting `factorio.version` on it.
4. Optionally set `lobby.listed` to false on instances that should not be shown in the list.
Players who connect to the proxy now end up in the lobby for their version.
@@ -50,15 +54,6 @@ Players joining it while it's stopped wait in a lobby running their version unti
## Controller Configuration
### lobby.public_address
Address and port players reach the player proxy on, for example `play.example.com:34197`.
Players are sent here when they pick an instance in a lobby, and the host name is resolved to the address published in the server browser.
If null the host name of `controller.public_url` and `controller.player_proxy_port` is used.
If the player proxy is disabled players are sent straight to the address of the host of the instance instead.
Defaults to null.
### lobby.server_browser_listing
List the cluster in the Factorio server browser as one game for each Factorio version a lobby is running.
+7 -16
View File
@@ -145,15 +145,7 @@ export class LobbyController {
/** Address players use to connect to an instance, through the proxy if it's enabled */
connectAddress(instance: Readonly<InstanceRecord>) {
if (this.proxy) {
const configured = this.controller.config.get("lobby.public_address");
if (configured) {
return configured;
}
const publicUrl = this.controller.config.get("controller.public_url");
if (publicUrl) {
return `${new URL(publicUrl).hostname}:${this.proxy.port}`;
}
return undefined;
return this.controller.getPlayerProxyAddress();
}
const hostId = instance.config.get("instance.assigned_host");
@@ -328,15 +320,14 @@ export class LobbyController {
return listings;
}
const configured = this.controller.config.get("lobby.public_address");
const publicUrl = this.controller.config.get("controller.public_url");
const { host, port } = configured
? parseAddress(configured)
: { host: publicUrl ? new URL(publicUrl).hostname : "", port: undefined };
if (!host) {
this._warnListing("lobby.public_address or controller.public_url must be set to list servers");
const address = this.controller.getPlayerProxyAddress();
if (!address) {
this._warnListing(
"controller.player_proxy_public_address or controller.public_url must be set to list servers"
);
return listings;
}
const { host, port } = parseAddress(address);
let hostAddress = host;
if (!isIP(host)) {
try {
-10
View File
@@ -4,7 +4,6 @@ import * as messages from "./messages.js";
declare module "@clusterio/lib" {
export interface ControllerConfigFields {
"lobby.public_address": string | null;
"lobby.server_browser_listing": boolean;
"lobby.server_browser_name": string | null;
"lobby.server_browser_description": string;
@@ -28,15 +27,6 @@ export const plugin: lib.PluginDeclaration = {
instanceEntrypoint: "dist/node/instance.js",
controllerConfigFields: {
"lobby.public_address": {
title: "Public Address",
description:
"Address and port players reach the player proxy on, for example play.example.com:34197. " +
"Players are sent here when they pick an instance in a lobby. If null the host name of " +
"controller.public_url and controller.player_proxy_port is used.",
type: "string",
optional: true,
},
"lobby.server_browser_listing": {
title: "Server Browser Listing",
description:
+46 -2
View File
@@ -1,10 +1,36 @@
import fs from "node:fs/promises";
import * as lib from "@clusterio/lib";
import type { InstancePluginContext } from "@clusterio/host";
import { type InstancePluginContext, setSaveScenario } from "@clusterio/host";
import { ServerListRequest, TransferRequest, TransferStatus, TransferStatusEvent } from "./messages.js";
const SERVER_LIST_INTERVAL_MS = 5_000;
/** Save lobby instances start, created if it doesn't exist */
export const LOBBY_SAVE = "lobby.zip";
/**
* Map gen settings for an empty world
*
* A width or height of 0 means unlimited to Factorio, so the map is made
* a single tile with everything outside it out of map.
*/
export const lobbyMapGenSettings = {
width: 1,
height: 1,
starting_points: [{ x: 0, y: 0 }],
peaceful_mode: true,
no_enemies_mode: true,
default_enable_all_autoplace_controls: false,
autoplace_controls: {},
autoplace_settings: {
entity: { treat_missing_as_default: false, settings: {} },
decorative: { treat_missing_as_default: false, settings: {} },
},
cliff_settings: { name: "cliff", richness: 0 },
};
// ./module/lobby.lua
type IpcConnect = {
player: string,
@@ -30,7 +56,9 @@ export default async function loadInstancePlugin(context: InstancePluginContext)
async function configure() {
const isLobby = instance.config.get("lobby.is_lobby");
await sendOrderedRcon(`/sc lobby.configure(${isLobby}, ${instance.id})`);
// The player_auth module opens its login dialog from the same IPC message as its /web-login command
const webLogin = [...instance.loadedPlugins].some(info => info.name === "player_auth");
await sendOrderedRcon(`/sc lobby.configure(${isLobby}, ${instance.id}, ${webLogin})`);
}
async function updateServerList() {
@@ -64,6 +92,22 @@ export default async function loadInstancePlugin(context: InstancePluginContext)
await sendOrderedRcon(`/sc lobby.set_player_status("${name}", "${lib.escapeString(text)}")`);
}
instance.hooks.chooseSave.attach(plugin.name, async () => {
if (!instance.config.get("lobby.is_lobby")) {
return undefined;
}
const savePath = instance.path("saves", LOBBY_SAVE);
try {
await fs.access(savePath);
} catch {
logger.info("Creating empty lobby save");
await instance.server.create(LOBBY_SAVE, undefined, lobbyMapGenSettings);
// Saves are created with the freeplay scenario, replace it with one that does nothing
await setSaveScenario(savePath, new lib.ModuleInfo("lobby", "0.0.0"));
}
return LOBBY_SAVE;
});
instance.hooks.start.attach(plugin.name, async () => {
await configure();
lastServerListJson = "";
+17 -9
View File
@@ -12,6 +12,7 @@ local mod_gui = require("mod-gui")
--- @class LobbyScriptData
--- @field is_lobby boolean
--- @field instance_id integer?
--- @field web_login boolean Show button for logging into the web interface with the player_auth plugin
--- @field servers LobbyServer[]
--- @field status table<string, string> Status text by player name
@@ -106,11 +107,15 @@ local function open_browser(player)
local inner = frame.add { type = "frame", name = "content", direction = "vertical",
style = "inside_shallow_frame_with_padding" }
inner.style.horizontally_stretchable = true
local buttons = frame.add { type = "flow", name = "buttons", direction = "horizontal" }
buttons.style.top_margin = 8
if data.web_login then
buttons.add { type = "button", name = "lobby_web_login", caption = "Log in to web interface",
tooltip = "Same as the /web-login command" }
end
local filler = buttons.add { type = "empty-widget" }
filler.style.horizontally_stretchable = true
if not data.is_lobby then
local buttons = frame.add { type = "flow", direction = "horizontal" }
buttons.style.top_margin = 8
local filler = buttons.add { type = "empty-widget" }
filler.style.horizontally_stretchable = true
buttons.add { type = "button", name = "lobby_close", caption = "Close" }
end
end
@@ -194,6 +199,8 @@ lobby_module.events[defines.events.on_gui_click] = function(event)
end
elseif element.name == "lobby_close" then
close_browser(player)
elseif element.name == "lobby_web_login" then
clusterio_api.send_json("player_auth", { type = "open_dialog", player = player.name })
elseif element.tags and element.tags.lobby_instance_id then
local data = get_script_data()
local instance_id = element.tags.lobby_instance_id
@@ -214,14 +221,15 @@ lobby = {}
--- @param is_lobby boolean
--- @param instance_id integer
function lobby.configure(is_lobby, instance_id)
--- @param web_login boolean?
function lobby.configure(is_lobby, instance_id, web_login)
local data = get_script_data()
data.is_lobby = is_lobby
data.instance_id = instance_id
if not is_lobby then
for _, player in pairs(game.players) do
close_browser(player)
end
data.web_login = web_login == true
-- Rebuilt so the frame matches the new configuration
for _, player in pairs(game.players) do
close_browser(player)
end
refresh_all()
end
+8 -2
View File
@@ -300,7 +300,7 @@ describe("lobby plugin", function () {
it("should build server browser listings", async function () {
controller.config.set("lobby.server_browser_listing", true);
controller.config.set("lobby.public_address", "203.0.113.5:40000");
controller.config.set("controller.player_proxy_public_address", "203.0.113.5:40000");
controller.instances.getMutable(4).config.set("lobby.server_browser_listing", true);
proxy.serverVersions.set(1, v2_1_19);
proxy.serverVersions.set(2, v2_0_72);
@@ -353,11 +353,17 @@ describe("lobby plugin", function () {
it("should configure the module and send the server list on start", async function () {
await instance.hooks.start.invoke();
const commands = instance.server.rconCommands;
assert.equal(commands[0], "/sc lobby.configure(true, 7357)");
assert.equal(commands[0], "/sc lobby.configure(true, 7357, false)");
assert(commands[1].startsWith("/sc lobby.set_servers(\""), commands[1]);
assert(commands[1].includes("Instance \\\\\\\"3\\\\\\\""), commands[1]);
});
it("should enable the web login button when player_auth is loaded", async function () {
instance.loadedPlugins.add({ name: "player_auth" });
await instance.hooks.start.invoke();
assert.equal(instance.server.rconCommands[0], "/sc lobby.configure(true, 7357, true)");
});
it("should move players once their transfer is ready", async function () {
instance.server.emit("ipc-lobby:connect", { player: "Player", instance_id: 3 });
await lib.wait(10);
+1
View File
@@ -34,5 +34,6 @@ Defaults to `120`.
Show the address to connect to for each server in the server list on the login page of the web interface, made up of host.public_address and the game port of the instance.
This list is visible to anyone who can reach the web interface, so leave it off if you don't want unauthenticated users to see where the servers are.
If `controller.player_proxy_public_address` is set the list shows that address once for all instances without an `instance.player_proxy_port`, as they are reached through the [player proxy](../../docs/player-proxy.md), and the proxy address with the dedicated port for the others.
Defaults to `false`.
+30 -5
View File
@@ -46,6 +46,13 @@ export default async function loadControllerPlugin(context: ControllerPluginCont
controller.app.get("/api/player_auth/servers", (req: Request, res: Response) => {
const servers: Static<typeof PlayerAuthServer.jsonSchema>[] = [];
const includeAddress = controller.config.get("player_auth.show_connect_address");
// When players are meant to connect through the player proxy, instances without a dedicated proxy
// port are only reachable through its main port, which is listed once for all of them.
const proxyAddress = controller.config.get("controller.player_proxy_public_address")
? controller.getPlayerProxyAddress()
: undefined;
const proxiedVersions = new Set<string | undefined>();
for (const instance of controller.instances.values()) {
const pluginLoaded = instance.config.get("player_auth.load_plugin");
@@ -59,16 +66,34 @@ export default async function loadControllerPlugin(context: ControllerPluginCont
continue;
}
const address = instance.gamePort !== undefined
? `${host.publicAddress}:${instance.gamePort}`
: host.publicAddress;
let address: string | undefined;
if (proxyAddress) {
const proxyPort = instance.config.get("instance.player_proxy_port");
if (proxyPort === null) {
proxiedVersions.add(instance.factorioVersion);
continue;
}
address = controller.getPlayerProxyAddress(proxyPort);
} else if (host.publicAddress !== "") {
address = instance.gamePort !== undefined
? `${host.publicAddress}:${instance.gamePort}`
: host.publicAddress;
}
const settings = instance.config.get("factorio.settings");
const includeAddress = controller.config.get("player_auth.show_connect_address");
servers.push({
name: settings["name"] as string || "unnamed server",
factorioVersion: instance.factorioVersion,
address: (includeAddress && host.publicAddress !== "") ? address : undefined,
address: includeAddress ? address : undefined,
});
}
if (proxiedVersions.size) {
const [version] = proxiedVersions;
servers.unshift({
name: controller.config.get("controller.name"),
factorioVersion: proxiedVersions.size === 1 ? version : undefined,
address: includeAddress ? proxyAddress : undefined,
});
}
+52 -24
View File
@@ -138,32 +138,35 @@ describe("player_auth", function() {
});
describe("/api/player_auth/servers", function() {
it("should return a list of running servers with player_auth loaded", async function() {
function addInstance(id, status, load, name, version, gamePort, host=1) {
mockController.instances.records.set(id, {
config: {
get(field) {
if (field === "player_auth.load_plugin") {
return load;
}
if (field === "factorio.settings") {
return { name };
}
if (field === "instance.id") {
return id;
}
if (field === "instance.assigned_host") {
return host;
}
throw new Error(`field ${field} not implemented`);
},
function addInstance(id, status, load, name, version, gamePort, host=1, proxyPort=null) {
mockController.instances.records.set(id, {
config: {
get(field) {
if (field === "player_auth.load_plugin") {
return load;
}
if (field === "factorio.settings") {
return { name };
}
if (field === "instance.id") {
return id;
}
if (field === "instance.assigned_host") {
return host;
}
if (field === "instance.player_proxy_port") {
return proxyPort;
}
throw new Error(`field ${field} not implemented`);
},
status,
gamePort,
factorioVersion: version,
});
}
},
status,
gamePort,
factorioVersion: version,
});
}
it("should return a list of running servers with player_auth loaded", async function() {
addInstance(1, "running", true, "running loaded", "1.1.1", 34197);
addInstance(2, "stopped", true, "stopped loaded", "1.1.2", 34197);
addInstance(3, "running", false, "running unloaded", "1.1.3", 34197);
@@ -186,6 +189,31 @@ describe("player_auth", function() {
{ name: "no version", address: "127.0.0.1:34197" },
]);
});
it("should list the player proxy once for instances behind it", async function() {
for (const id of [...mockController.instances.records.keys()]) {
mockController.instances.records.delete(id);
}
mockController.mockConfigEntries.set("controller.name", "Cluster");
mockController.mockConfigEntries.set(
"controller.player_proxy_public_address", "play.example:34197"
);
mockController.getPlayerProxyAddress = port => `play.example:${port ?? 34197}`;
try {
addInstance(1, "running", true, "behind proxy", "2.1.19", 34201);
addInstance(2, "running", true, "also behind proxy", "2.1.19", 34202);
addInstance(3, "running", true, "dedicated port", "2.1.19", 34203, 1, 34300);
addInstance(4, "stopped", true, "stopped", "2.1.19", 34204, 1, 34301);
const result = await fetch(`${controllerUrl}/api/player_auth/servers`);
assert.deepEqual(await result.json(), [
{ name: "Cluster", address: "play.example:34197", factorioVersion: "2.1.19" },
{ name: "dedicated port", address: "play.example:34300", factorioVersion: "2.1.19" },
]);
} finally {
mockController.mockConfigEntries.set("controller.player_proxy_public_address", null);
}
});
});
describe("/api/player_auth/player_code", function() {