From 9cb5810597fed419f65d7a86e7f18c350aabab27 Mon Sep 17 00:00:00 2001 From: Romain Beaumont Date: Fri, 3 Aug 2018 21:11:33 +0200 Subject: [PATCH] Add hideErrors option fix #431 --- doc/README.md | 2 ++ src/client.js | 5 +++-- src/createClient.js | 3 ++- src/createServer.js | 3 ++- src/server.js | 5 +++-- src/transforms/compression.js | 21 ++++++++++++--------- 6 files changed, 24 insertions(+), 15 deletions(-) diff --git a/doc/README.md b/doc/README.md index 77c37bd..c506600 100644 --- a/doc/README.md +++ b/doc/README.md @@ -24,6 +24,7 @@ automatically logged in and validated against mojang's auth. * stream : a stream to use as connection * connect : a function taking the client as parameter and that should client.setSocket(socket) and client.emit('connect') when appropriate (see the proxy examples for an example of use) + * hideErrors : do not display errors, default to false ## mc.Server(version,[customPackets]) @@ -82,6 +83,7 @@ Returns a `Client` instance and perform login. * checkTimeoutInterval : default to `30*1000` (30s), check if keepalive received at that period, disconnect otherwise. * version : 1.8 or 1.9 or false (to auto-negotiate): default to 1.8 * customPackets (optional) : an object index by version/state/direction/name, see client_custom_packet for an example + * hideErrors : do not display errors, default to false ## mc.Client(isServer,version,[customPackets]) diff --git a/src/client.js b/src/client.js index f973b04..ef36b2f 100644 --- a/src/client.js +++ b/src/client.js @@ -11,7 +11,7 @@ const createSerializer = require('./transforms/serializer').createSerializer const createDeserializer = require('./transforms/serializer').createDeserializer class Client extends EventEmitter { - constructor (isServer, version, customPackets) { + constructor (isServer, version, customPackets, hideErrors = false) { super() this.customPackets = customPackets this.version = version @@ -25,6 +25,7 @@ class Client extends EventEmitter { this.decompressor = null this.ended = true this.latency = 0 + this.hideErrors = hideErrors this.state = states.HANDSHAKING } @@ -183,7 +184,7 @@ class Client extends EventEmitter { this.compressor.on('error', (err) => this.emit('error', err)) this.serializer.unpipe(this.framer) this.serializer.pipe(this.compressor).pipe(this.framer) - this.decompressor = compression.createDecompressor(threshold) + this.decompressor = compression.createDecompressor(threshold, this.hideErrors) this.decompressor.on('error', (err) => this.emit('error', err)) this.splitter.unpipe(this.deserializer) this.splitter.pipe(this.decompressor).pipe(this.deserializer) diff --git a/src/createClient.js b/src/createClient.js index 90d090e..f4e7d86 100644 --- a/src/createClient.js +++ b/src/createClient.js @@ -28,8 +28,9 @@ function createClient (options) { const version = mcData.version options.majorVersion = version.majorVersion options.protocolVersion = version.version + const hideErrors = options.hideErrors || false - const client = new Client(false, version.minecraftVersion, options.customPackets) + const client = new Client(false, version.minecraftVersion, options.customPackets, hideErrors) tcpDns(client, options) auth(client, options) diff --git a/src/createServer.js b/src/createServer.js index 8e7559e..2efbb20 100644 --- a/src/createServer.js +++ b/src/createServer.js @@ -27,8 +27,9 @@ function createServer (options = {}) { const mcData = require('minecraft-data')(optVersion) const mcversion = mcData.version + const hideErrors = options.hideErrors || false - const server = new Server(mcversion.minecraftVersion, customPackets) + const server = new Server(mcversion.minecraftVersion, customPackets, hideErrors) server.mcversion = mcversion server.motd = motd server.maxPlayers = maxPlayers diff --git a/src/server.js b/src/server.js index 510ec41..1fd7b16 100644 --- a/src/server.js +++ b/src/server.js @@ -6,7 +6,7 @@ const Client = require('./client') const states = require('./states') class Server extends EventEmitter { - constructor (version, customPackets) { + constructor (version, customPackets, hideErrors = false) { super() this.version = version this.socketServer = null @@ -14,6 +14,7 @@ class Server extends EventEmitter { this.decipher = null this.clients = {} this.customPackets = customPackets + this.hideErrors = hideErrors } listen (port, host) { @@ -21,7 +22,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) + const client = new Client(true, this.version, this.customPackets, this.hideErrors) client._end = client.end client.end = function end (endReason) { endReason = '{"text":"' + endReason + '"}' diff --git a/src/transforms/compression.js b/src/transforms/compression.js index 09375f5..9d53b33 100644 --- a/src/transforms/compression.js +++ b/src/transforms/compression.js @@ -8,8 +8,8 @@ module.exports.createCompressor = function (threshold) { return new Compressor(threshold) } -module.exports.createDecompressor = function (threshold) { - return new Decompressor(threshold) +module.exports.createDecompressor = function (threshold, hideErrors) { + return new Decompressor(threshold, hideErrors) } class Compressor extends Transform { @@ -39,9 +39,10 @@ class Compressor extends Transform { } class Decompressor extends Transform { - constructor (compressionThreshold = -1) { + constructor (compressionThreshold = -1, hideErrors = false) { super() this.compressionThreshold = compressionThreshold + this.hideErrors = hideErrors } _transform (chunk, enc, cb) { @@ -53,14 +54,16 @@ class Decompressor extends Transform { } else { zlib.inflate(chunk.slice(size), (err, newBuf) => { if (err) { - console.error('problem inflating chunk') - console.error('uncompressed length ' + value) - console.error('compressed length ' + chunk.length) - console.error('hex ' + chunk.toString('hex')) - console.log(err) + if (!this.hideErrors) { + console.error('problem inflating chunk') + console.error('uncompressed length ' + value) + console.error('compressed length ' + chunk.length) + console.error('hex ' + chunk.toString('hex')) + console.log(err) + } return cb() } - if (newBuf.length !== value) { + if (newBuf.length !== value && !this.hideErrors) { console.error('uncompressed length should be ' + value + ' but is ' + newBuf.length) } this.push(newBuf)