diff --git a/src/client.js b/src/client.js index c89375e3..b3d50435 100644 --- a/src/client.js +++ b/src/client.js @@ -13,7 +13,7 @@ const createDecipher = require('./transforms/encryption').createDecipher const closeTimeout = 30 * 1000 class Client extends EventEmitter { - constructor (isServer, version, customPackets, hideErrors = false) { + constructor (isServer, version, customPackets, hideErrors = false, fullParser = true) { super() this.customPackets = customPackets this.version = version @@ -28,6 +28,7 @@ class Client extends EventEmitter { this.ended = true this.latency = 0 this.hideErrors = hideErrors + this.fullParser = fullParser this.closeTimer = null const mcData = require('minecraft-data')(version) this.state = states.HANDSHAKING @@ -47,7 +48,8 @@ class Client extends EventEmitter { packetsToParse: this.packetsToParse, customPackets: this.customPackets, - noErrorLogging: this.hideErrors + noErrorLogging: this.hideErrors, + fullParser: this.fullParser }) this.splitter.recognizeLegacyPing = state === states.HANDSHAKING diff --git a/src/createClient.js b/src/createClient.js index 10cacc07..252a70d9 100644 --- a/src/createClient.js +++ b/src/createClient.js @@ -34,7 +34,7 @@ function createClient (options) { const hideErrors = options.hideErrors || false const Client = options.Client || DefaultClientImpl - const client = new Client(false, version.minecraftVersion, options.customPackets, hideErrors) + const client = new Client(false, version.minecraftVersion, options.customPackets, hideErrors, options.fullParser ?? true) tcpDns(client, options) if (options.auth instanceof Function) { diff --git a/src/createServer.js b/src/createServer.js index 4a56c0ad..1c4846a9 100644 --- a/src/createServer.js +++ b/src/createServer.js @@ -25,7 +25,8 @@ function createServer (options = {}) { favicon, customPackets, motdMsg, // This is when you want to send formated motd's from ChatMessage instances - socketType = 'tcp' + socketType = 'tcp', + fullParser = true } = options const maxPlayers = options['max-players'] !== undefined ? maxPlayersOld : maxPlayersNew @@ -37,7 +38,7 @@ function createServer (options = {}) { const mcversion = mcData.version const hideErrors = options.hideErrors || false - const server = new Server(mcversion.minecraftVersion, customPackets, hideErrors) + const server = new Server(mcversion.minecraftVersion, customPackets, hideErrors, fullParser) server.mcversion = mcversion server.motd = motd server.motdMsg = motdMsg diff --git a/src/index.d.ts b/src/index.d.ts index 0a5821c3..186fb2a4 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -12,7 +12,7 @@ type PromiseLike = Promise | void declare module 'minecraft-protocol' { export class Client extends EventEmitter { - constructor(isServer: boolean, version: string, customPackets?: any) + constructor(isServer: boolean, version: string, customPackets?: any, hideErrors?: boolean | undefined, fullParser?: boolean | undefined) state: States isServer: boolean socket: Socket @@ -141,10 +141,12 @@ declare module 'minecraft-protocol' { disableChatSigning?: boolean /** Pass custom client implementation if needed. */ Client?: Client + // Enable bounds checking + fullParser?: boolean } export class Server extends EventEmitter { - constructor(version: string, customPackets?: any) + constructor(version: string, customPackets?: any, hideErrors?: boolean | undefined, fullParser?: boolean | undefined) writeToClients(clients: Client[], name: string, params: any): void onlineModeExceptions: object clients: { [key: number]: ServerClient } @@ -200,6 +202,8 @@ declare module 'minecraft-protocol' { enableChatPreview?: boolean socketType?: 'tcp' | 'ipc' Server?: Server + // Enable bounds checking + fullParser?: boolean } export interface SerializerOptions { @@ -207,6 +211,7 @@ declare module 'minecraft-protocol' { isServer?: boolean state?: States version: string + fullParser?: boolean } export interface MicrosoftDeviceAuthorizationResponse { @@ -284,7 +289,7 @@ declare module 'minecraft-protocol' { // TODO: Create typings on protodef to define here export function createSerializer({ state, isServer, version, customPackets }: SerializerOptions): any - export function createDeserializer({ state, isServer, version, customPackets }: SerializerOptions): any + export function createDeserializer({ state, isServer, version, customPackets, fullParser }: SerializerOptions): any export function ping(options: PingOptions, callback?: (error: Error, result: OldPingResult | NewPingResult) => void): Promise } diff --git a/src/server.js b/src/server.js index 99cfcbac..2cdf4058 100644 --- a/src/server.js +++ b/src/server.js @@ -7,7 +7,7 @@ const states = require('./states') const { createSerializer } = require('./transforms/serializer') class Server extends EventEmitter { - constructor (version, customPackets, hideErrors = false) { + constructor (version, customPackets, hideErrors = false, fullParser = true) { super() this.version = version this.socketServer = null @@ -16,6 +16,7 @@ class Server extends EventEmitter { this.clients = {} this.customPackets = customPackets this.hideErrors = hideErrors + this.fullParser = fullParser this.serializer = createSerializer({ state: 'play', isServer: true, version, customPackets }) } @@ -24,7 +25,7 @@ class Server extends EventEmitter { let nextId = 0 self.socketServer = net.createServer() self.socketServer.on('connection', socket => { - const client = new Client(true, this.version, this.customPackets, this.hideErrors) + const client = new Client(true, this.version, this.customPackets, this.hideErrors, this.fullParser) client._end = client.end client.end = function end (endReason, fullReason = JSON.stringify({ text: endReason })) { if (client.state === states.PLAY) { diff --git a/src/transforms/serializer.js b/src/transforms/serializer.js index 7cc127e5..8a216cb9 100644 --- a/src/transforms/serializer.js +++ b/src/transforms/serializer.js @@ -1,9 +1,6 @@ 'use strict' -const ProtoDef = require('protodef').ProtoDef -const Serializer = require('protodef').Serializer -const Parser = require('protodef').FullPacketParser -const { ProtoDefCompiler } = require('protodef').Compiler +const { ProtoDef, Serializer, Parser, FullPacketParser, Compiler: { ProtoDefCompiler } } = require('protodef') const nbt = require('prismarine-nbt') const minecraft = require('../datatypes/minecraft') @@ -49,8 +46,10 @@ function createSerializer ({ state = states.HANDSHAKING, isServer = false, versi return new Serializer(createProtocol(state, !isServer ? 'toServer' : 'toClient', version, customPackets, compiled), 'packet') } -function createDeserializer ({ state = states.HANDSHAKING, isServer = false, version, customPackets, compiled = true, noErrorLogging = false } = {}) { - return new Parser(createProtocol(state, isServer ? 'toServer' : 'toClient', version, customPackets, compiled), 'packet', noErrorLogging) +function createDeserializer ({ state = states.HANDSHAKING, isServer = false, version, customPackets, compiled = true, noErrorLogging = false, fullParser = true } = {}) { + return fullParser + ? new FullPacketParser(createProtocol(state, isServer ? 'toServer' : 'toClient', version, customPackets, compiled), 'packet', noErrorLogging) + : new Parser(createProtocol(state, isServer ? 'toServer' : 'toClient', version, customPackets, compiled), 'packet', noErrorLogging) } module.exports = {