Skip to content

Commit

Permalink
Implement filtering lua mesages with regex
Browse files Browse the repository at this point in the history
  • Loading branch information
p2004a committed Jan 12, 2025
1 parent 0bac4bd commit 24b3f77
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 2 deletions.
69 changes: 67 additions & 2 deletions src/engineRunner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { runEngine, EngineRunnerImpl } from './engineRunner.js';
import { chdir } from 'node:process';
import { ChildProcess, spawn, type SpawnOptions } from 'node:child_process';
import { pino } from 'pino';
import { EventType } from './engineAutohostInterface.js';

// Find a free port to use for testing
const tmpSock = dgram.createSocket('udp4').bind(0, '127.0.0.1');
Expand Down Expand Up @@ -156,12 +157,16 @@ suite('engineRunner', () => {
for (const packet of [
Buffer.from('00', 'hex'),
Buffer.from('054f6e6c696e65207761726e696e67206c6f6c', 'hex'),
Buffer.from('14320c000a640000407a683630', 'hex'),
Buffer.from('01', 'hex'),
]) {
await asyncSetImmediate();
s.send(packet);
const msg = (await events.once(s, 'message')) as [Buffer, dgram.RemoteInfo];
assert.equal(msg[0].toString('utf8'), `test${packet[0]}`);
// 0x14 -> luamsg, filtered out by default.
if (packet[0] != 0x14) {
const msg = (await events.once(s, 'message')) as [Buffer, dgram.RemoteInfo];
assert.equal(msg[0].toString('utf8'), `test${packet[0]}`);
}
}

await asyncSetImmediate();
Expand All @@ -178,4 +183,64 @@ suite('engineRunner', () => {
await events.once(er, 'start');
await events.once(er, 'exit');
});

test('emit only luamsg matching regex', async () => {
const er = new EngineRunnerImpl(
getEnv((() => {
const cp = new ChildProcess();
process.nextTick(() => cp.emit('spawn'));
setImmediate(() => simulateEngine(cp));
return cp;
}) as typeof spawn),
);
er._run({
...optsBase,
startRequest: {
...demoStartRequest,
luamsgRegexp: '^id:',
},
});

const { promise: receivedAll, resolve: receivedAllResolve } = Promise.withResolvers();
let expectedPackets = 4;

async function simulateEngine(cp: ChildProcess) {
const s = dgram.createSocket('udp4');
s.connect(testPort);
await events.once(s, 'connect');

for (const packet of [
Buffer.from('00', 'hex'),
Buffer.from('14320c000a64000069643a6173', 'hex'),
Buffer.from('14320c000a64000078783a7878', 'hex'),
Buffer.from('14320c000a64000069643a786e', 'hex'),
Buffer.from('14320c000a64000069643affff', 'hex'),
]) {
s.send(packet);
}
await receivedAll;
cp.emit('exit', 0, 'exit');
s.close();
}

assert.rejects(er.sendPacket(Buffer.from('asd')), /not running/);

const packetsData: Buffer[] = [];
er.on('packet', (packet) => {
if (packet.type == EventType.GAME_LUAMSG) {
packetsData.push(packet.data);
}
if (--expectedPackets == 0) {
receivedAllResolve(undefined);
}
});

await events.once(er, 'exit');

assert.deepEqual(packetsData, [
Buffer.from('id:as'),
Buffer.from('id:xn'),
Buffer.from('69643affff', 'hex'),
]);
});
});
18 changes: 18 additions & 0 deletions src/engineRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export class EngineRunnerImpl extends TypedEmitter<EngineRunnerEvents> implement
private engineSpawned: boolean = false;
private state: State = State.None;
private logger: Env['logger'];
private luamsgRegex: RegExp | null = null;

public constructor(private env: Env) {
super();
Expand All @@ -115,9 +116,18 @@ export class EngineRunnerImpl extends TypedEmitter<EngineRunnerEvents> implement
}
this.state = State.Starting;
const run = async () => {
if (opts.startRequest.luamsgRegexp) {
try {
this.luamsgRegex = new RegExp(opts.startRequest.luamsgRegexp);
} catch (err) {
throw new TachyonError('invalid_request', `Invalid luamsg RegExp: ${err}`);
}
}

const instanceDir = await this.setupInstanceDir(opts);
await this.startUdpServer(opts.autohostPort);
await this.startEngine(instanceDir, opts.startRequest);

// The last part of startup is handled in the packed handler
};
run().catch((err) => this.handleError(err));
Expand Down Expand Up @@ -249,6 +259,14 @@ export class EngineRunnerImpl extends TypedEmitter<EngineRunnerEvents> implement
);
return;
}

// Don't emit luamsg's not matching start script regexp.
if (
packet.type === EventType.GAME_LUAMSG &&
(this.luamsgRegex === null || !this.luamsgRegex.test(packet.data.toString('utf8')))
) {
return;
}
this.emit('packet', packet);
} catch (err) {
// Don't crash the server on packet parsing errors, it might have been
Expand Down

0 comments on commit 24b3f77

Please sign in to comment.