Newbie Spellweaver
- Joined
- Jul 13, 2015
- Messages
- 71
- Reaction score
- 31
Introduction
Hi guys! On this amazing and wonderful day, I will explain how you can decode Client -> Server packets using javascript and node. When I was a noob and didn't have knowledge about packets, no one tried to help me so I decided post this tt1:.
Well, first we need understand how the packet structure is formed. A packet always will start with a integer and a short, respectively the length and header of the packet. And too it's extremely important understand the primitive values, their size (in bits) and width (in byte). For example, a short is a signed 16-bit with 2 bytes and integer is a signed 32-bit with 4 bytes.
Length and header
The first packet that you will receive is the ReleaseVersionEvent, so I will use it as buffer examples: Buffer:
With that understood, let's start the game :junglejane:. Obligatorily you have to decode the length before the header, so following this rule, let's go decode the length first. As we already know, the length is a integer, in other words, a signed 32-bit, okay? okay. If you search for a method that reads a 32-bit integer, probably you will find the
Congratulations, you have the packet length and these 4 bytes are no longer useful. So it would be convenient slice them, no? You can use the
Well done. It's time to decode the header. As I had said before, the header is a short or a signed 16-bit integer, you can choose haha. The methods
Packet body
Then we have the length and header of the packet. Next, we will decode the packet body. Note that the body always varies from packet to packet, never will be the same. To decode a string, first you need the length of the string that is a short before the string.
Now you can get the string and slice it.
Thanks for reading tt:
Hi guys! On this amazing and wonderful day, I will explain how you can decode Client -> Server packets using javascript and node. When I was a noob and didn't have knowledge about packets, no one tried to help me so I decided post this tt1:.
Well, first we need understand how the packet structure is formed. A packet always will start with a integer and a short, respectively the length and header of the packet. And too it's extremely important understand the primitive values, their size (in bits) and width (in byte). For example, a short is a signed 16-bit with 2 bytes and integer is a signed 32-bit with 4 bytes.
Length and header
The first packet that you will receive is the ReleaseVersionEvent, so I will use it as buffer examples: Buffer:
Code:
00 00 00 34 0f a0 00 21 50 52 4f 44 55 43 54 49 4f 4e 2d 32 30 31 36 30 31 30 31 32 32 30 35 2d 32 32 36 36 36 37 34 38 36 00 05 46 4c 41 53 48 00 00 ...
With that understood, let's start the game :junglejane:. Obligatorily you have to decode the length before the header, so following this rule, let's go decode the length first. As we already know, the length is a integer, in other words, a signed 32-bit, okay? okay. If you search for a method that reads a 32-bit integer, probably you will find the
You must be registered to see links
methods, and you are correct, you can use them. Eg: Note that offset used is 0 because you must start reading at first byte.
Code:
let length = buffer.readInt32BE(0); // OUTPUT: 52
Congratulations, you have the packet length and these 4 bytes are no longer useful. So it would be convenient slice them, no? You can use the
You must be registered to see links
method to do this.
Code:
buffer = buffer.slice(4); // OUTPUT: 0f a0 00 21 50 52 4f 44 55 43 54 49 4f 4e 2d 32 30 31 36 30 31 30 31 32 32 30 35 2d 32 32 36 36 36 37 34 38 36 00 05 46 4c 41 53 48 00 00 00 01 00 00 ...
Well done. It's time to decode the header. As I had said before, the header is a short or a signed 16-bit integer, you can choose haha. The methods
You must be registered to see links
do what we want, then we can use them. YOU NEVER CAN'T FORGET TO SLICE THE BYTES. Eg:
Code:
let header = buffer.readInt16BE(0); // OUTPUT: 4000
buffer = buffer.slice(2); // OUTPUT: 00 21 50 52 4f 44 55 43 54 49 4f 4e 2d 32 30 31 36 30 31 30 31 32 32 30 35 2d 32 32 36 36 36 37 34 38 36 00 05 46 4c 41 53 48 00 00 00 01 00 00 00 00
Packet body
Then we have the length and header of the packet. Next, we will decode the packet body. Note that the body always varies from packet to packet, never will be the same. To decode a string, first you need the length of the string that is a short before the string.
Code:
let stringLength = buffer.readInt16BE(0);
buffer = buffer.slice(2);
Now you can get the string and slice it.
Code:
let string = buffer.toString('utf8', 0, stringLength);
buffer = buffer.slice(stringLength);
Thanks for reading tt:
Last edited: