-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
check session IDs in all UDP response packets
- Loading branch information
1 parent
2bdf979
commit d3e583b
Showing
4 changed files
with
48 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,37 @@ | ||
import UDPClient from "../UDPSocket/UDPClient.js"; | ||
import UnconnectedPing from "../Packet/BedrockPing/UnconnectedPing.js"; | ||
import UnconnectedPong from "../Packet/BedrockPing/UnconnectedPong.js"; | ||
import * as crypto from "node:crypto"; | ||
|
||
export default class BedrockPing extends UDPClient { | ||
/** @type {BigInt} */ sessionId; | ||
|
||
/** | ||
* @return {Promise<UnconnectedPong>} | ||
*/ | ||
async ping() { | ||
// Normally, the time field is used for the current ms timestamp, but we're using it as a session ID | ||
// to identify which reply belongs to which request. | ||
this.sessionId = crypto.randomBytes(8).readBigInt64BE(); | ||
let startTime = BigInt(Date.now()); | ||
await this.sendPacket(new UnconnectedPing().setTime(startTime).generateClientGUID()); | ||
await this.sendPacket(new UnconnectedPing().setTime(this.sessionId).generateClientGUID()); | ||
this.signal?.throwIfAborted(); | ||
|
||
return new UnconnectedPong().read(await this.readData()); | ||
// The time field in the response contains the session ID, but we replace it with the start time | ||
// in case anyone relies on the time field containing an actual timestamp. | ||
return new UnconnectedPong().read(await this.readData()).setTime(startTime); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
shouldAcceptPacket(packet) { | ||
let data = packet.getData(); | ||
if (data.byteLength < 9) { | ||
return false; | ||
} | ||
|
||
let timestamp = data.readBigInt64BE(1); | ||
return timestamp === this.sessionId; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters