Skip to content

Commit

Permalink
Release: Fixes, 1.21.50
Browse files Browse the repository at this point in the history
  • Loading branch information
AnyBananaGAME committed Dec 10, 2024
1 parent 9edecce commit 87a3396
Show file tree
Hide file tree
Showing 12 changed files with 219 additions and 122 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 🛠️ Minecraft Bedrock Edition Client Library

![Version](https://img.shields.io/badge/Supported%20Version-1.21.30-brightgreen)
![Version](https://img.shields.io/badge/Supported%20Version-1.21.50-brightgreen)
![npm](https://img.shields.io/npm/v/@sanctumterra/client)
![License](https://img.shields.io/badge/License-MIT-blue)

Expand Down Expand Up @@ -30,6 +30,7 @@ npm i @sanctumterra/client
| 2.0.10 | 712 | 1.21.20 |
| 2.1.5 | 729 | 1.21.30 |
| 2.1.12 | 748 | 1.21.40 |
| 2.1.13 | 766 | 1.21.50 |

## 🚀 Usage Example

Expand All @@ -42,7 +43,7 @@ const client = new Client({
offline: true,
username: "SanctumTerra",
tokensFolder: "./cache/tokens",
version: "1.21.30",
version: "1.21.50",
deviceOS: DeviceOS.Android
});

Expand Down Expand Up @@ -75,7 +76,7 @@ client.on("spawn", () => {

### 🎛️ Client Configuration
- **Required Parameters**: `host`, `port`
- **Optional Parameters**: `offline`, `username`, `tokensFolder`, `version`, `deviceOS`
- **Optional Parameters**: `offline`, `username`, `tokensFolder`, `version`, `deviceOS`, `viewDistance`

### 📡 Event Handling
- Events allow you to listen to any implemented packet if it is not implemented you will receive a warning and it should not crash if there is a crash then make an issue on github.
Expand All @@ -87,6 +88,11 @@ client.on("spawn", () => {

## 📜 Changelog

### 2.1.13
- Added support for Minecraft 1.21.50.
- Fixed many bugs.
- Slightly Improved performance.

### 2.1.12
- Added support for Minecraft 1.21.40.
- New Raknet! It now uses NAPI as Rust is faster.
Expand Down
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@sanctumterra/client",
"version": "2.1.12",
"minecraft": "1.21.40",
"version": "2.1.13",
"minecraft": "1.21.50",
"description": "We'll be singing Baraye.",
"main": "./dist/index.js",
"type": "commonjs",
Expand Down Expand Up @@ -35,15 +35,16 @@
"why-is-node-running": "^3.2.0"
},
"dependencies": {
"@sanctumterra/raknet": "^1.3.57",
"@sanctumterra/raknet": "^1.3.60",
"@sanctumterra/rs-rak-client": "^1.0.63",
"@serenityjs/binarystream": "^2.6.6",
"@serenityjs/block": "^0.4.4",
"@serenityjs/network": "^0.4.4",
"@serenityjs/protocol": "^0.6.0",
"@serenityjs/raknet": "^0.4.4",
"@serenityjs/protocol": "^0.6.1-beta-20241119050127",
"@serenityjs/raknet": "^0.6.2-beta-20241128210118",
"@serenityjs/world": "^0.4.4",
"@types/node": "^22.7.5",
"crypto": "^1.0.1",
"fast-json-stable-stringify": "^2.1.0",
"lodash": "^4.17.21",
"prismarine-auth": "github:AnyBananaGAME/prismarine-auth",
Expand Down
42 changes: 34 additions & 8 deletions src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
NetworkItemStackDescriptor,
PlayerActionPacket,
PlayerAuthInputData,
PlayerAuthInputPacket,
PlayMode,
TextPacket,
TextPacketType,
Expand All @@ -33,6 +32,8 @@ import { Inventory } from "./client/inventory/Inventory";
import { Connection } from "./Connection";
import { Logger } from "./vendor/Logger";
import { Queue } from "./vendor/Queue";
import { PlayerAuthInputPacket } from "./vendor/packets/player-auth-input";
import type { PlayerAuthInputPacket as PAIP } from "@serenityjs/protocol";

class Client extends Connection {
private sneaking = false;
Expand Down Expand Up @@ -87,7 +88,7 @@ class Client extends Connection {
packet.playMode = PlayMode.Screen;
packet.interactionMode = InteractionMode.Touch;
packet.interactRotation = new Vector2f(0, 0);
packet.inputTick = BigInt(this.tick);
packet.tick = BigInt(this.tick);
packet.positionDelta = new Vector3f(0, 0, 0);
packet.itemStackRequest = null;
packet.blockActions = null;
Expand All @@ -97,7 +98,7 @@ class Client extends Connection {
const cancel = false;
this.emit("PrePlayerAuthInputPacket", packet, cancel);
if (!cancel) {
this.sendPacket(packet, Priority.Immediate);
// this.sendPacket(packet, Priority.Immediate);
}
}, 100);
}
Expand Down Expand Up @@ -208,7 +209,7 @@ class Client extends Connection {
* @param position The position of the block
* @param ticks The number of ticks to break the block
*/
private async breakBlock(position: Vector3f, ticks = 5): Promise<void> {
public async breakBlock(position: Vector3f, ticks = 5): Promise<void> {
const MAX_DISTANCE = 5;
const TICK_INTERVAL = 100;

Expand All @@ -229,7 +230,8 @@ class Client extends Connection {
return new Promise((resolve) => {
this.once(
"PrePlayerAuthInputPacket",
(packet: PlayerAuthInputPacket) => {
// @ts-expect-error meh
(packet: PlayerAuthInputPacket, _cancel: boolean) => {
modifier(packet);
resolve();
},
Expand Down Expand Up @@ -285,9 +287,33 @@ class Client extends Connection {

// Stop Break
await modifyNextPacket((packet: PlayerAuthInputPacket) => {
// packet.inputData.setFlag(InputDataFlags.BlockAction, true);
// packet.inputData.setFlag(InputDataFlags.ItemInteract, true);
// this.lookAt(position.x, position.y, position.z);
packet.inputData.setFlag(InputData.PerformBlockActions, true);
packet.inputData.setFlag(InputData.StartUsingItem, true);
this.lookAt(position.x, position.y, position.z);
packet.blockActions = new PlayerBlockActions([
new PlayerBlockActionData(
PlayerActionType.StopDestroyBlock,
position,
face,
),
]);
packet.inputTransaction = new InputTransaction(
new LegacyTransaction(0, []),
[],
new ItemUseInventoryTransaction(
ItemUseInventoryTransactionType.Destroy,
TriggerType.Unknown,
position,
face,
0,
new NetworkItemStackDescriptor(0),
this.position,
new Vector3f(0, 0, 0),
0,
false,
),
);

// packet.blockActions = new PlayerBlockActions([
// new PlayerBlockActionData(PlayerActionType.StopDestroyBlock, position, face),
// ]);
Expand Down
10 changes: 9 additions & 1 deletion src/Connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
createPublicKey,
KeyObject,
} from "node:crypto";
import * as crypto from "node:crypto";
import { measureExecutionTime } from "./vendor/debug-tools";

import {
Expand All @@ -61,6 +62,7 @@ class Connection extends Listener {
public position!: Vector3f;
public tick = 0;
public _encryption = false;
public compression = false;
public options: ClientOptions;
public data: ClientData;

Expand Down Expand Up @@ -147,6 +149,7 @@ class Connection extends Listener {
const networkSettingsPacket = new RequestNetworkSettingsPacket();
networkSettingsPacket.protocol = this.protocol;
this.sendPacket(networkSettingsPacket);
this.compression = true;
}

@measureExecutionTime
Expand All @@ -155,6 +158,8 @@ class Connection extends Listener {
let Advertisement_: Advertisement;
this.once("session", async () => {
Advertisement_ = await this.handleSessionStart();
console.timeEnd("RakConnect");
// this.raknet.frameAndSend(Buffer.from([254, 0, 236, 151, 151, 151]))
});
this.once("StartGamePacket", (packet: StartGamePacket) => {
resolve([Advertisement_, packet]);
Expand All @@ -173,6 +178,7 @@ class Connection extends Listener {
if (this.options.debug) Logger.debug("S -> C NetworkSettingsPacket");
this.data.sendDeflated = true;
this.data.compressionThreshold = instance.compressionThreshold;
this.data.compressionMethod = instance.compressionMethod;
this.sendLoginPacket();
}

Expand Down Expand Up @@ -207,6 +213,8 @@ class Connection extends Listener {
pubKeyDer,
);

// this.data.sharedSecret = diffieHellman({ privateKey: this.data.loginData.ecdhKeyPair.privateKey, publicKey: pubKeyDer })

this.setupEncryption(salt);
this.sendClientToServerHandshake();
}
Expand Down Expand Up @@ -283,7 +291,7 @@ class Connection extends Listener {

ecdh.setPrivateKey(Buffer.from(privateKeyJwk.d, "base64"));
const publicKeyBuffer = Buffer.concat([
Buffer.from([0x04]), // Uncompressed point format
Buffer.from([0x04]),
Buffer.from(publicKeyJwk.x, "base64"),
Buffer.from(publicKeyJwk.y, "base64"),
]);
Expand Down
1 change: 1 addition & 0 deletions src/client/ClientData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ class ClientData {
public accessToken!: string[];
public sendDeflated = false;
public compressionThreshold!: number;
public compressionMethod = 0;
public sharedSecret!: Buffer;
public secretKeyBytes!: Buffer;
private defaultPayload: Payload | null = null;
Expand Down
9 changes: 6 additions & 3 deletions src/client/ClientOptions.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const Versions = ["1.21.20", "1.21.30", "1.21.40"] as const;
const Versions = ["1.21.20", "1.21.30", "1.21.40", "1.21.50"] as const;

enum ProtocolList {
"1.21.20" = 712,
"1.21.30" = 729,
"1.21.40" = 748,
"1.21.50" = 766,
}

enum DeviceOS {
Expand Down Expand Up @@ -36,21 +37,23 @@ type ClientOptions = {
port: number;
skinData: object | null;
debug: boolean;
compressionThreshold: number;
tokensFolder: string;
viewDistance: number;
deviceOS: DeviceOS;
};

const defaultOptions: ClientOptions = {
version: "1.21.30",
version: "1.21.50",
offline: false,
username: "defaultUser",
host: "127.0.0.1",
port: 19132,
skinData: null,
debug: false,
compressionThreshold: 512,
tokensFolder: `${process.cwd()}/tokens`,
viewDistance: 6,
viewDistance: 4,
deviceOS: DeviceOS.Win10,
};

Expand Down
13 changes: 10 additions & 3 deletions src/tools/connect.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { TextPacket } from "@serenityjs/protocol";
import "reflect-metadata";
import { Vector3f, type TextPacket } from "@serenityjs/protocol";
import { Client } from "../Client";
// import fs from "fs";
// import path from "path";
Expand Down Expand Up @@ -102,19 +103,25 @@ const client = new Client({
host: "127.0.0.1",
offline: true,
username: "SanctumTerra",
version: "1.21.40",
version: "1.21.50",
port: 19132,
viewDistance: 1,
viewDistance: 4,
// debug: true
});

console.time("Connection");
console.time("RakConnect");
// writeToLog("Starting connection...");

client.connect().then(([ad, packet]) => {
console.timeEnd("Connection");

// console.log(ad);
// writeToLog(`Connected successfully: ${JSON.stringify(ad)}`);
setTimeout(() => {
const vec = new Vector3f(260, 65, 236);
// client.breakBlock(vec);
}, 1000);
});

client.on("DisconnectPacket", (packet) => {
Expand Down
2 changes: 1 addition & 1 deletion src/tools/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const connection = new Connection({
host: "127.0.0.1",
port: 19132,
username: "SanctumTerra",
version: "1.21.30",
version: "1.21.50",
offline: true,
});

Expand Down
22 changes: 9 additions & 13 deletions src/vendor/PacketEncryptor.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { GAME_BYTE } from "@serenityjs/network";
import { CompressionMethod } from "@serenityjs/protocol";
import { Frame, Reliability } from "@serenityjs/raknet";
import * as crypto from "node:crypto";
import * as Zlib from "node:zlib";
import type { Connection } from "src/Connection";
import { Logger } from "./Logger";

class PacketEncryptor {
public secretKeyBytes: Buffer;
Expand All @@ -12,6 +12,7 @@ class PacketEncryptor {
public receiveCounter: bigint;
public cipher: crypto.Cipher | null;
public decipher: crypto.Decipher | null;
public client: Connection;

constructor(client: Connection, secretKey: Buffer, compressionThreshold = 1) {
this.secretKeyBytes = Buffer.from(secretKey);
Expand All @@ -20,6 +21,7 @@ class PacketEncryptor {
this.receiveCounter = 0n;
this.cipher = null;
this.decipher = null;
this.client = client;

this.initializeCipher(client.data.iv);
this.initializeDecipher(client.data.iv);
Expand Down Expand Up @@ -66,11 +68,11 @@ class PacketEncryptor {

encryptPacket(framed: Buffer): Frame {
let deflated: Buffer;

if (framed.byteLength > this.compressionThreshold) {
// framed.byteLength > this.client.data.compressionThreshold
if (framed.byteLength > this.client.data.compressionThreshold) {
deflated = Buffer.from([
CompressionMethod.Zlib,
...Zlib.deflateRawSync(framed),
...Zlib.deflateRawSync(framed, { level: 7 }),
]);
} else {
deflated = Buffer.from([CompressionMethod.None, ...framed]);
Expand Down Expand Up @@ -101,23 +103,17 @@ class PacketEncryptor {
if (!this.decipher) {
throw new Error("Decipher not initialized");
}
const old = this.receiveCounter;
this.receiveCounter++;

const decrypted = this.decipher.update(encryptedPayload);
const packet = decrypted.slice(0, decrypted.length - 8);
const receivedChecksum = decrypted.slice(decrypted.length - 8);

const computedChecksum = this.computeCheckSum(packet, old);
const computedChecksum = this.computeCheckSum(packet, this.receiveCounter);
this.receiveCounter++;

if (!receivedChecksum.equals(computedChecksum)) {
throw new Error(
`Checksum mismatch ${receivedChecksum.toString("hex")} != ${computedChecksum.toString("hex")}`,
);
throw new Error("Checksum mismatch");
}
// else {
// Logger.debug(`Checksum matched ${receivedChecksum.toString('hex')} == ${computedChecksum.toString('hex')}`);
// }

return packet;
}
Expand Down
Loading

0 comments on commit 87a3396

Please sign in to comment.