mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-12 15:04:52 +09:00
_watchServerLogActions dereferenced two regex results with ! and
threw on null. The name regex /^([^ ]+)/ returns null when a line
classified as an action starts with a space, and the BAN reason
regex /\. Reason: (.+)\.$/ returns null for a [BAN] line with no
". Reason: X." tail. The output listener runs synchronously off the
stdout LineSplitter, so the throw becomes an uncaughtException and
takes the whole host down with every instance on it.
Repro: instance with enable_save_patching off and enable_script_commands
on; a control client sends /sc print("2000-01-01 00:00:00 [BAN] evil").
The raw print reaches stdout, parses as a dated BAN action, and the
reason deref throws.
Skip the line when the name regex misses and default the ban reason
to empty when the tail is absent. Apply the same name guard to
_watchPlayerPromote. The autosave ![1] in server.ts is safe because
its filter regex already guarantees the capture.
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
365 lines
12 KiB
JavaScript
365 lines
12 KiB
JavaScript
import assert from "node:assert/strict";
|
|
import path from "node:path";
|
|
|
|
import * as lib from "@clusterio/lib";
|
|
import { PlayerStats, wait } from "@clusterio/lib";
|
|
import Instance from "@clusterio/host/dist/node/src/Instance.js";
|
|
import { MockConnector, MockServer } from "../mock.js";
|
|
|
|
const addr = lib.Address.fromShorthand;
|
|
|
|
|
|
describe("class Instance", function() {
|
|
let src;
|
|
let dst = addr({ hostId: 1 });
|
|
let instance;
|
|
let connector;
|
|
beforeEach(function() {
|
|
let instanceConfig = new lib.InstanceConfig("host");
|
|
instanceConfig.set("instance.name", "foo");
|
|
src = addr({ instanceId: instanceConfig.get("instance.id") });
|
|
connector = new MockConnector(src, dst);
|
|
instance = new Instance({ assignGamePort: () => 1 }, connector, "dir", "factorioDir", instanceConfig);
|
|
instance.server = new MockServer();
|
|
});
|
|
|
|
describe(".name", function() {
|
|
it("should give the name of the instance", function() {
|
|
assert.equal(instance.name, "foo");
|
|
});
|
|
});
|
|
|
|
describe(".path()", function() {
|
|
it("should give the path when called without arguments", function() {
|
|
assert.equal(instance.path(), "dir");
|
|
});
|
|
it("should join path with arguments", function() {
|
|
assert.equal(instance.path("bar"), path.join("dir", "bar"));
|
|
});
|
|
});
|
|
|
|
describe(".checkModPackVersion()", function() {
|
|
function modPack(factorioVersion) {
|
|
return lib.ModPack.fromJSON({ name: "pack", factorio_version: factorioVersion });
|
|
}
|
|
|
|
it("should accept a mod pack for an older or equal Factorio version", function() {
|
|
instance.server.version = "1.1.91";
|
|
instance.checkModPackVersion(modPack("1.1"));
|
|
instance.checkModPackVersion(modPack("1.1.90"));
|
|
instance.checkModPackVersion(modPack("1.1.91"));
|
|
});
|
|
|
|
it("should reject a mod pack for a newer Factorio version", function() {
|
|
instance.server.version = "1.1.91";
|
|
assert.throws(
|
|
() => instance.checkModPackVersion(modPack("1.1.110")),
|
|
new lib.RequestError(
|
|
"Mod pack pack is for Factorio 1.1.110 which is newer than the Factorio 1.1.91 this instance runs"
|
|
)
|
|
);
|
|
assert.throws(() => instance.checkModPackVersion(modPack("2.0")), lib.RequestError);
|
|
});
|
|
|
|
it("should skip the check when the server version is unknown", function() {
|
|
instance.checkModPackVersion(modPack("2.0"));
|
|
});
|
|
});
|
|
|
|
describe("._recordPlayerJoin()", function() {
|
|
it("should add player to playersOnline", function() {
|
|
instance._recordPlayerJoin("player");
|
|
assert(instance.playersOnline.has("player"), "player was not added");
|
|
});
|
|
|
|
it("should create playerStats entry", function() {
|
|
assert(!instance.playerStats.has("player"));
|
|
instance._recordPlayerJoin("player");
|
|
assert(instance.playerStats.has("player"), "player was not added to stats");
|
|
});
|
|
|
|
|
|
it("should send player_event", function() {
|
|
instance._recordPlayerJoin("player");
|
|
let stats = instance.playerStats.get("player");
|
|
assert.deepEqual(
|
|
connector.sentMessages[0],
|
|
new lib.MessageEvent(
|
|
1, src, addr("controller"), "InstancePlayerUpdateEvent",
|
|
new lib.InstancePlayerUpdateEvent(
|
|
"join",
|
|
"player",
|
|
new PlayerStats({
|
|
join_count: 1,
|
|
last_join_at_ms: stats.lastJoinAt.getTime(),
|
|
})
|
|
)
|
|
),
|
|
);
|
|
});
|
|
|
|
it("should be idempotent", async function() {
|
|
instance._recordPlayerJoin("player");
|
|
await wait(10);
|
|
instance._recordPlayerJoin("player");
|
|
assert(instance.playersOnline.has("player"), "player was not added");
|
|
assert.equal(connector.sentMessages.length, 1);
|
|
});
|
|
});
|
|
|
|
describe("._recordPlayerLeave()", function() {
|
|
it("should remove player to playersOnline", async function() {
|
|
instance._recordPlayerJoin("player");
|
|
await wait(10);
|
|
instance._recordPlayerLeave("player");
|
|
assert(!instance.playersOnline.has("player"), "player was not removed");
|
|
});
|
|
|
|
it("should update playerStats", async function() {
|
|
instance._recordPlayerJoin("player");
|
|
await wait(10);
|
|
instance._recordPlayerLeave("player");
|
|
assert(instance.playerStats.has("player"), "playerStats record missing");
|
|
assert(instance.playerStats.get("player").onlineTimeMs > 0, "no onlineTimeMs recorded");
|
|
});
|
|
|
|
it("should send player_event", async function() {
|
|
instance._recordPlayerJoin("player");
|
|
await wait(10);
|
|
instance._recordPlayerLeave("player", "quit");
|
|
let stats = instance.playerStats.get("player");
|
|
assert.deepEqual(
|
|
connector.sentMessages[1],
|
|
new lib.MessageEvent(
|
|
2, src, addr("controller"), "InstancePlayerUpdateEvent",
|
|
new lib.InstancePlayerUpdateEvent(
|
|
"leave",
|
|
"player",
|
|
new PlayerStats({
|
|
join_count: 1,
|
|
online_time_ms: stats.onlineTimeMs,
|
|
last_join_at_ms: stats.lastJoinAt.getTime(),
|
|
last_leave_at_ms: stats.lastLeaveAt.getTime(),
|
|
last_leave_reason: "quit",
|
|
}),
|
|
"quit",
|
|
)
|
|
),
|
|
);
|
|
});
|
|
|
|
it("should be idempotent", async function() {
|
|
instance._recordPlayerJoin("player");
|
|
await wait(10);
|
|
instance._recordPlayerLeave("player", "quit");
|
|
await wait(10);
|
|
instance._recordPlayerLeave("player", "quit");
|
|
assert(!instance.playersOnline.has("player"), "player was not removed");
|
|
assert.equal(connector.sentMessages.length, 2);
|
|
});
|
|
});
|
|
|
|
describe("._checkOnlinePlayers()", function() {
|
|
it("should do nothing on empty server", async function() {
|
|
await instance._checkOnlinePlayers();
|
|
assert.equal(instance.server.rconCommands.length, 0, "commands were sent");
|
|
assert.equal(connector.sentMessages.length, 0, "messages were sent");
|
|
});
|
|
|
|
it("should do nothing on correct online presence", async function() {
|
|
instance.server.rconCommandResults.set("/players online", "Online Players (1):\n player (online)\n");
|
|
instance.playersOnline.add("player");
|
|
await instance._checkOnlinePlayers();
|
|
assert.equal(connector.sentMessages.length, 0, "messages were sent");
|
|
});
|
|
|
|
it("should add missing players", async function() {
|
|
instance.server.rconCommandResults.set(
|
|
"/players online", "Online Players (2):\n player (online)\n foo (online)\n"
|
|
);
|
|
instance._recordPlayerJoin("player");
|
|
await instance._checkOnlinePlayers();
|
|
let stats = instance.playerStats.get("foo");
|
|
assert.deepEqual(
|
|
connector.sentMessages[1],
|
|
new lib.MessageEvent(
|
|
2, src, addr("controller"), "InstancePlayerUpdateEvent",
|
|
new lib.InstancePlayerUpdateEvent(
|
|
"join",
|
|
"foo",
|
|
new PlayerStats({
|
|
join_count: 1,
|
|
last_join_at_ms: stats.lastJoinAt.getTime(),
|
|
})
|
|
)
|
|
)
|
|
);
|
|
});
|
|
|
|
it("should remove extra players", async function() {
|
|
instance.server.rconCommandResults.set("/players online", "Online Players (0):\n");
|
|
instance._recordPlayerJoin("player");
|
|
await wait(10);
|
|
await instance._checkOnlinePlayers();
|
|
let stats = instance.playerStats.get("player");
|
|
assert.deepEqual(
|
|
connector.sentMessages[1],
|
|
new lib.MessageEvent(
|
|
2, src, addr("controller"), "InstancePlayerUpdateEvent",
|
|
new lib.InstancePlayerUpdateEvent(
|
|
"leave",
|
|
"player",
|
|
new PlayerStats({
|
|
join_count: 1,
|
|
online_time_ms: stats.onlineTimeMs,
|
|
last_join_at_ms: stats.lastJoinAt.getTime(),
|
|
last_leave_at_ms: stats.lastLeaveAt.getTime(),
|
|
last_leave_reason: "quit",
|
|
}),
|
|
"quit",
|
|
)
|
|
)
|
|
);
|
|
});
|
|
});
|
|
|
|
describe("_recordUserUpdate()", function() {
|
|
beforeEach(function() {
|
|
instance.config.set("factorio.sync_banlist", "bidirectional");
|
|
instance.config.set("factorio.sync_adminlist", "bidirectional");
|
|
instance.config.set("factorio.sync_whitelist", "bidirectional");
|
|
});
|
|
it("should send InstanceBanlistUpdateEvent for bans", function() {
|
|
instance._recordUserUpdate("BAN", "player", "reason");
|
|
assert.deepEqual(
|
|
connector.sentMessages[0],
|
|
new lib.MessageEvent(
|
|
1, src, addr("allInstances"), "InstanceBanlistUpdateEvent",
|
|
new lib.InstanceBanlistUpdateEvent(
|
|
"player",
|
|
true,
|
|
"reason",
|
|
)
|
|
),
|
|
);
|
|
});
|
|
it("should send InstanceBanlistUpdateEvent for unbans", function() {
|
|
instance._recordUserUpdate("UNBANNED", "player");
|
|
assert.deepEqual(
|
|
connector.sentMessages[0],
|
|
new lib.MessageEvent(
|
|
1, src, addr("allInstances"), "InstanceBanlistUpdateEvent",
|
|
new lib.InstanceBanlistUpdateEvent(
|
|
"player",
|
|
false,
|
|
"",
|
|
)
|
|
),
|
|
);
|
|
});
|
|
it("should send InstanceAdminlistUpdateEvent for promotes", function() {
|
|
instance._recordUserUpdate("PROMOTE", "player");
|
|
assert.deepEqual(
|
|
connector.sentMessages[0],
|
|
new lib.MessageEvent(
|
|
1, src, addr("allInstances"), "InstanceAdminlistUpdateEvent",
|
|
new lib.InstanceAdminlistUpdateEvent(
|
|
"player",
|
|
true,
|
|
)
|
|
),
|
|
);
|
|
});
|
|
it("should send InstanceAdminlistUpdateEvent for demotes", function() {
|
|
instance._recordUserUpdate("DEMOTE", "player");
|
|
assert.deepEqual(
|
|
connector.sentMessages[0],
|
|
new lib.MessageEvent(
|
|
1, src, addr("allInstances"), "InstanceAdminlistUpdateEvent",
|
|
new lib.InstanceAdminlistUpdateEvent(
|
|
"player",
|
|
false,
|
|
)
|
|
),
|
|
);
|
|
});
|
|
it("should send InstanceWhitelistUpdateEvent for whitelist add", function() {
|
|
instance._recordUserUpdate("WHITELISTED", "player");
|
|
assert.deepEqual(
|
|
connector.sentMessages[0],
|
|
new lib.MessageEvent(
|
|
1, src, addr("allInstances"), "InstanceWhitelistUpdateEvent",
|
|
new lib.InstanceWhitelistUpdateEvent(
|
|
"player",
|
|
true,
|
|
)
|
|
),
|
|
);
|
|
});
|
|
it("should send InstanceWhitelistUpdateEvent for whitelist remove", function() {
|
|
instance._recordUserUpdate("UNWHITELISTED", "player");
|
|
assert.deepEqual(
|
|
connector.sentMessages[0],
|
|
new lib.MessageEvent(
|
|
1, src, addr("allInstances"), "InstanceWhitelistUpdateEvent",
|
|
new lib.InstanceWhitelistUpdateEvent(
|
|
"player",
|
|
false,
|
|
)
|
|
),
|
|
);
|
|
});
|
|
it("should throw for unknown event types", function() {
|
|
assert.throws(() => {
|
|
instance._recordUserUpdate("INVALID TYPE", "player");
|
|
});
|
|
});
|
|
it("should not send events when it is not bidirectional", function() {
|
|
for (const configValue of ["disabled", "enabled"]) { // Excludes Bidirectional
|
|
instance.config.set("factorio.sync_banlist", configValue);
|
|
instance.config.set("factorio.sync_adminlist", configValue);
|
|
instance.config.set("factorio.sync_whitelist", configValue);
|
|
for (const eventType of
|
|
["BAN", "UNBANNED", "PROMOTE", "DEMOTE", "WHITELISTED", "UNWHITELISTED"]
|
|
) {
|
|
instance._recordUserUpdate(eventType, "player");
|
|
assert.equal(connector.sentMessages[0], undefined);
|
|
}
|
|
}
|
|
});
|
|
});
|
|
|
|
describe("_watchServerLogActions()", function() {
|
|
beforeEach(function() {
|
|
instance.config.set("factorio.sync_banlist", "bidirectional");
|
|
instance._watchServerLogActions();
|
|
});
|
|
afterEach(function() {
|
|
clearInterval(instance._playerCheckInterval);
|
|
});
|
|
function emitAction(action, message) {
|
|
instance.server.emit("output", { type: "action", action, message }, "");
|
|
}
|
|
it("should record a normal JOIN action", function() {
|
|
emitAction("JOIN", "player joined the game");
|
|
assert(instance.playersOnline.has("player"), "player was not added");
|
|
});
|
|
it("should parse the reason of a normal BAN action", function() {
|
|
emitAction("BAN", "player was banned by <server>. Reason: griefing.");
|
|
assert.deepEqual(
|
|
connector.sentMessages[0],
|
|
new lib.MessageEvent(
|
|
1, src, addr("allInstances"), "InstanceBanlistUpdateEvent",
|
|
new lib.InstanceBanlistUpdateEvent("player", true, "griefing"),
|
|
),
|
|
);
|
|
});
|
|
it("should not throw on a BAN action without a reason tail", function() {
|
|
assert.doesNotThrow(() => emitAction("BAN", "evil"));
|
|
});
|
|
it("should not throw on an action whose message starts with a space", function() {
|
|
assert.doesNotThrow(() => emitAction("JOIN", " "));
|
|
});
|
|
});
|
|
});
|