Skip to content

Commit

Permalink
feat: exception handling for packet parsing errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Cdm2883 committed Jul 19, 2024
1 parent 7abc669 commit 5e30cef
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
17 changes: 13 additions & 4 deletions patches/bedrock-protocol+3.38.0.patch
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,19 @@ index 7018ae3..1b38537 100644
ping,
createServer,
diff --git a/node_modules/bedrock-protocol/src/relay.js b/node_modules/bedrock-protocol/src/relay.js
index 3128a7d..6d32e10 100644
index 3128a7d..251e385 100644
--- a/node_modules/bedrock-protocol/src/relay.js
+++ b/node_modules/bedrock-protocol/src/relay.js
@@ -60,6 +60,8 @@ class RelayPlayer extends Player {
@@ -47,6 +47,8 @@ class RelayPlayer extends Player {
try {
des = this.server.deserializer.parsePacketBuffer(packet)
} catch (e) {
+ if (this.$hook$readUpstream$parsing_error?.(packet, e)) return
+
this.server.deserializer.dumpFailedBuffer(packet, this.connection.address)
console.error(this.connection.address, e)

@@ -60,6 +62,8 @@ class RelayPlayer extends Player {
const params = des.data.params
this.upInLog('->', name, params)

Expand All @@ -32,7 +41,7 @@ index 3128a7d..6d32e10 100644
if (name === 'play_status' && params.status === 'login_success') return // Already sent this, this needs to be sent ASAP or client will disconnect

if (debugging) { // some packet encode/decode testing stuff
@@ -68,6 +70,8 @@ class RelayPlayer extends Player {
@@ -68,6 +72,8 @@ class RelayPlayer extends Player {

this.emit('clientbound', des.data, des)

Expand All @@ -41,7 +50,7 @@ index 3128a7d..6d32e10 100644
if (!des.canceled) {
if (name === 'start_game') {
setTimeout(() => {
@@ -289,4 +293,4 @@ class Relay extends Server {
@@ -289,4 +295,4 @@ class Relay extends Server {
}

// Too many things called 'Proxy' ;)
Expand Down
5 changes: 5 additions & 0 deletions src/bridgeo/relay/relay-player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ export class CommonRelayPlayer extends RelayPlayer {
// @ts-expect-error
super(...args);

if (this.server.options.ignorePacketParseError) this.$hook$readUpstream$parsing_error = buffer => {
this.sendBuffer(buffer);
return true;
};

// TODO fix definitions
// noinspection CommaExpressionJS
this.$hook$readUpstream$parsed = (buffer, { data: { name } }) => (
Expand Down
1 change: 1 addition & 0 deletions src/bridgeo/relay/relay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import CommonRelayPlayer from "@/bridgeo/relay/relay-player";
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface CommonRelayOptions extends RelayOptions {
// reuseUnchangedBuffer?: boolean;
ignorePacketParseError?: boolean;
}

// noinspection JSUnusedGlobalSymbols
Expand Down
1 change: 1 addition & 0 deletions src/bridgeo/utils/def/bedrock-protocol.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ declare module 'bedrock-protocol' {
on(event: 'clientbound', listener: (packet, options: PacketBufferParsed & { canceled: boolean }) => void): this;
on(event: 'serverbound', listener: (packet, options: PacketBufferParsed & { canceled: boolean }) => void): this;

$hook$readUpstream$parsing_error?(buffer: Buffer, e);
$hook$readUpstream$parsed?(buffer: Buffer, parsed: PacketBufferParsed & { canceled: boolean });
$hook$readUpstream$emitted?(buffer: Buffer, parsed: PacketBufferParsed & { canceled: boolean });
}
Expand Down

0 comments on commit 5e30cef

Please sign in to comment.