Prevent replay

This commit is contained in:
jude 2023-03-07 15:43:47 +00:00
parent c0f2c4bbef
commit 4a55f5c11f
3 changed files with 10 additions and 0 deletions

View File

@ -49,6 +49,14 @@ document.addEventListener("DOMContentLoaded", () => {
window.console.error(`Signature invalid! Ignoring packet ${data.id}.`);
return;
}
// check if the packet is replayed.
if (data.timestamp <= sender.lastPacket) {
window.console.error(`Replay detected! Ignoring packet ${data.id}.`);
return;
}
sender.lastPacket = data.timestamp;
}
switch (data.type) {

View File

@ -5,6 +5,7 @@ export class Packet {
return {
type: name,
id: window.crypto.randomUUID(),
timestamp: Date.now(),
author: ID,
};
}

View File

@ -19,6 +19,7 @@ export class Player {
this.id = id;
this.ready = false;
this.rsaPubKey = RsaPubKey.fromJSON(pubkey);
this.lastPacket = 0;
// Data which is reset every turn
this.isPlaying = false;