Commit a3e235cc authored by nanahira's avatar nanahira

fix zombie client thing

parent 2de215e8
Pipeline #43332 passed with stages
in 1 minute and 4 seconds
......@@ -16,12 +16,25 @@ import {
merge,
map,
take,
startWith,
switchMap,
timer,
} from 'rxjs';
import { YGOProCtosDisconnect } from '../utility/ygopro-ctos-disconnect';
export class ClientHandler {
private static readonly CLIENT_IDLE_TIMEOUT_MS = 5 * 60 * 1000;
constructor(private ctx: Context) {}
private isPreHandshakeMsg(msg: YGOProCtosBase): boolean {
return [
YGOProCtosExternalAddress,
YGOProCtosPlayerInfo,
YGOProCtosJoinGame,
].some((allowed) => (msg instanceof allowed) as boolean);
}
async init() {
this.ctx
.middleware(YGOProCtosExternalAddress, async (msg, client, next) => {
......@@ -63,12 +76,7 @@ export class ClientHandler {
return next();
}
const isPreHandshakeMsg = [
YGOProCtosExternalAddress,
YGOProCtosPlayerInfo,
YGOProCtosJoinGame,
].some((allowed) => (msg instanceof allowed) as boolean);
if (client.established === isPreHandshakeMsg) {
if (client.established === this.isPreHandshakeMsg(msg)) {
// disallow any messages before handshake is complete, except for the ones needed for handshake
return undefined;
}
......@@ -133,6 +141,7 @@ export class ClientHandler {
.then(() => {
this.logger.debug({ client: client.name }, 'Handshake completed');
client.established = true;
this.installIdleDisconnectGuard(client);
return true;
})
.catch((error) => {
......@@ -149,6 +158,24 @@ export class ClientHandler {
return false;
});
}
private installIdleDisconnectGuard(client: Client) {
client.receive$
.pipe(
filter((msg) => !this.isPreHandshakeMsg(msg)),
startWith(undefined),
switchMap(() => timer(ClientHandler.CLIENT_IDLE_TIMEOUT_MS)),
take(1),
takeUntil(client.disconnect$),
)
.subscribe(() => {
this.logger.info(
{ client: client.name || client.loggingIp(), ip: client.loggingIp() },
'Disconnecting idle client due to inactivity timeout',
);
client.disconnect();
});
}
}
declare module 'ygopro-msg-encode' {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment