Add hideErrors option

fix #431
This commit is contained in:
Romain Beaumont 2018-08-03 21:11:33 +02:00
parent 62d595937b
commit 9cb5810597
No known key found for this signature in database
GPG key ID: DB60E388B3BCF286
6 changed files with 24 additions and 15 deletions

View file

@ -24,6 +24,7 @@ automatically logged in and validated against mojang's auth.
* stream : a stream to use as connection * stream : a stream to use as connection
* connect : a function taking the client as parameter and that should client.setSocket(socket) * 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) 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]) ## 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. * 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 * 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 * 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]) ## mc.Client(isServer,version,[customPackets])

View file

@ -11,7 +11,7 @@ const createSerializer = require('./transforms/serializer').createSerializer
const createDeserializer = require('./transforms/serializer').createDeserializer const createDeserializer = require('./transforms/serializer').createDeserializer
class Client extends EventEmitter { class Client extends EventEmitter {
constructor (isServer, version, customPackets) { constructor (isServer, version, customPackets, hideErrors = false) {
super() super()
this.customPackets = customPackets this.customPackets = customPackets
this.version = version this.version = version
@ -25,6 +25,7 @@ class Client extends EventEmitter {
this.decompressor = null this.decompressor = null
this.ended = true this.ended = true
this.latency = 0 this.latency = 0
this.hideErrors = hideErrors
this.state = states.HANDSHAKING this.state = states.HANDSHAKING
} }
@ -183,7 +184,7 @@ class Client extends EventEmitter {
this.compressor.on('error', (err) => this.emit('error', err)) this.compressor.on('error', (err) => this.emit('error', err))
this.serializer.unpipe(this.framer) this.serializer.unpipe(this.framer)
this.serializer.pipe(this.compressor).pipe(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.decompressor.on('error', (err) => this.emit('error', err))
this.splitter.unpipe(this.deserializer) this.splitter.unpipe(this.deserializer)
this.splitter.pipe(this.decompressor).pipe(this.deserializer) this.splitter.pipe(this.decompressor).pipe(this.deserializer)

View file

@ -28,8 +28,9 @@ function createClient (options) {
const version = mcData.version const version = mcData.version
options.majorVersion = version.majorVersion options.majorVersion = version.majorVersion
options.protocolVersion = version.version 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) tcpDns(client, options)
auth(client, options) auth(client, options)

View file

@ -27,8 +27,9 @@ function createServer (options = {}) {
const mcData = require('minecraft-data')(optVersion) const mcData = require('minecraft-data')(optVersion)
const mcversion = mcData.version 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.mcversion = mcversion
server.motd = motd server.motd = motd
server.maxPlayers = maxPlayers server.maxPlayers = maxPlayers

View file

@ -6,7 +6,7 @@ const Client = require('./client')
const states = require('./states') const states = require('./states')
class Server extends EventEmitter { class Server extends EventEmitter {
constructor (version, customPackets) { constructor (version, customPackets, hideErrors = false) {
super() super()
this.version = version this.version = version
this.socketServer = null this.socketServer = null
@ -14,6 +14,7 @@ class Server extends EventEmitter {
this.decipher = null this.decipher = null
this.clients = {} this.clients = {}
this.customPackets = customPackets this.customPackets = customPackets
this.hideErrors = hideErrors
} }
listen (port, host) { listen (port, host) {
@ -21,7 +22,7 @@ class Server extends EventEmitter {
let nextId = 0 let nextId = 0
self.socketServer = net.createServer() self.socketServer = net.createServer()
self.socketServer.on('connection', socket => { 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 = client.end
client.end = function end (endReason) { client.end = function end (endReason) {
endReason = '{"text":"' + endReason + '"}' endReason = '{"text":"' + endReason + '"}'

View file

@ -8,8 +8,8 @@ module.exports.createCompressor = function (threshold) {
return new Compressor(threshold) return new Compressor(threshold)
} }
module.exports.createDecompressor = function (threshold) { module.exports.createDecompressor = function (threshold, hideErrors) {
return new Decompressor(threshold) return new Decompressor(threshold, hideErrors)
} }
class Compressor extends Transform { class Compressor extends Transform {
@ -39,9 +39,10 @@ class Compressor extends Transform {
} }
class Decompressor extends Transform { class Decompressor extends Transform {
constructor (compressionThreshold = -1) { constructor (compressionThreshold = -1, hideErrors = false) {
super() super()
this.compressionThreshold = compressionThreshold this.compressionThreshold = compressionThreshold
this.hideErrors = hideErrors
} }
_transform (chunk, enc, cb) { _transform (chunk, enc, cb) {
@ -53,14 +54,16 @@ class Decompressor extends Transform {
} else { } else {
zlib.inflate(chunk.slice(size), (err, newBuf) => { zlib.inflate(chunk.slice(size), (err, newBuf) => {
if (err) { if (err) {
console.error('problem inflating chunk') if (!this.hideErrors) {
console.error('uncompressed length ' + value) console.error('problem inflating chunk')
console.error('compressed length ' + chunk.length) console.error('uncompressed length ' + value)
console.error('hex ' + chunk.toString('hex')) console.error('compressed length ' + chunk.length)
console.log(err) console.error('hex ' + chunk.toString('hex'))
console.log(err)
}
return cb() return cb()
} }
if (newBuf.length !== value) { if (newBuf.length !== value && !this.hideErrors) {
console.error('uncompressed length should be ' + value + ' but is ' + newBuf.length) console.error('uncompressed length should be ' + value + ' but is ' + newBuf.length)
} }
this.push(newBuf) this.push(newBuf)