diff --git a/src/client/chat.js b/src/client/chat.js index a50f4b988ad9fb29d5eb9e1633b498615aa9cd28..b8b819eef0762a77d9db5c0fb06be648303628c7 100644 --- a/src/client/chat.js +++ b/src/client/chat.js @@ -110,7 +110,7 @@ module.exports = function (client, options) { for (const player of packet.data) { if (player.chatSession) { client._players[player.uuid] = { - publicKey: crypto.createPublicKey({ key: player.chatSession.publicKey.keyBytes, format: 'der', type: 'spki' }), + // publicKey: crypto.createPublicKey({ key: player.chatSession.publicKey.keyBytes, format: 'der', type: 'spki' }), publicKeyDER: player.chatSession.publicKey.keyBytes, sessionUuid: player.chatSession.uuid } @@ -120,7 +120,7 @@ module.exports = function (client, options) { if (player.crypto) { client._players[player.uuid] = { - publicKey: crypto.createPublicKey({ key: player.crypto.publicKey, format: 'der', type: 'spki' }), + // publicKey: crypto.createPublicKey({ key: player.crypto.publicKey, format: 'der', type: 'spki' }), publicKeyDER: player.crypto.publicKey, signature: player.crypto.signature, displayName: player.displayName || player.name @@ -190,7 +190,7 @@ module.exports = function (client, options) { if (mcData.supportFeature('useChatSessions')) { const tsDelta = BigInt(Date.now()) - packet.timestamp const expired = !packet.timestamp || tsDelta > messageExpireTime || tsDelta < 0 - const verified = !packet.unsignedChatContent && updateAndValidateSession(packet.senderUuid, packet.plainMessage, packet.signature, packet.index, packet.previousMessages, packet.salt, packet.timestamp) && !expired + const verified = false && !packet.unsignedChatContent && updateAndValidateSession(packet.senderUuid, packet.plainMessage, packet.signature, packet.index, packet.previousMessages, packet.salt, packet.timestamp) && !expired if (verified) client._signatureCache.push(packet.signature) client.emit('playerChat', { globalIndex: packet.globalIndex, @@ -356,7 +356,7 @@ module.exports = function (client, options) { } } - client._signedChat = (message, options = {}) => { + client._signedChat = async (message, options = {}) => { options.timestamp = options.timestamp || BigInt(Date.now()) options.salt = options.salt || 1n @@ -401,7 +401,7 @@ module.exports = function (client, options) { message, timestamp: options.timestamp, salt: options.salt, - signature: (client.profileKeys && client._session) ? client.signMessage(message, options.timestamp, options.salt, undefined, acknowledgements) : undefined, + signature: (client.profileKeys && client._session) ? await client.signMessage(message, options.timestamp, options.salt, undefined, acknowledgements) : undefined, offset: client._lastSeenMessages.pending, checksum: computeChatChecksum(client._lastSeenMessages), // 1.21.5+ acknowledged @@ -416,7 +416,7 @@ module.exports = function (client, options) { message, timestamp: options.timestamp, salt: options.salt, - signature: client.profileKeys ? client.signMessage(message, options.timestamp, options.salt, options.preview) : Buffer.alloc(0), + signature: client.profileKeys ? await client.signMessage(message, options.timestamp, options.salt, options.preview) : Buffer.alloc(0), signedPreview: options.didPreview, previousMessages: client._lastSeenMessages.map((e) => ({ messageSender: e.sender, diff --git a/src/client/encrypt.js b/src/client/encrypt.js index 63cc2bd9615100bd2fd63dfe14c094aa6b8cd1c9..36df57d1196af9761d920fa285ac48f85410eaef 100644 --- a/src/client/encrypt.js +++ b/src/client/encrypt.js @@ -25,7 +25,11 @@ module.exports = function (client, options) { if (packet.serverId !== '-') { debug('This server appears to be an online server and you are providing no password, the authentication will probably fail') } - sendEncryptionKeyResponse() + client.end('This server appears to be an online server and you are providing no authentication. Try authenticating first.') + // sendEncryptionKeyResponse() + // client.once('set_compression', () => { + // clearTimeout(loginTimeout) + // }) } function onJoinServerResponse (err) { diff --git a/src/client.js b/src/client.js index e369e77d055ba919e8f9da7b8e8b5dc879c74cf4..11c6bff299f1186ab1ecb6744f53ff0c648ab192 100644 --- a/src/client.js +++ b/src/client.js @@ -111,7 +111,13 @@ class Client extends EventEmitter { this._hasBundlePacket = false } } else { - emitPacket(parsed) + try { + emitPacket(parsed) + } catch (err) { + console.log('Client incorrectly handled packet ' + parsed.metadata.name) + console.error(err) + // todo investigate why it doesn't close the stream even if unhandled there + } } }) } @@ -169,7 +175,10 @@ class Client extends EventEmitter { } const onFatalError = (err) => { - this.emit('error', err) + // todo find out what is trying to write after client disconnect + if(err.code !== 'ECONNABORTED') { + this.emit('error', err) + } endSocket() } @@ -198,6 +207,8 @@ class Client extends EventEmitter { serializer -> framer -> socket -> splitter -> deserializer */ if (this.serializer) { this.serializer.end() + this.socket?.end() + this.socket?.emit('end') } else { if (this.socket) this.socket.end() } @@ -243,6 +254,7 @@ class Client extends EventEmitter { debug('writing packet ' + this.state + '.' + name) debug(params) } + this.emit('writePacket', name, params) this.serializer.write({ name, params }) } diff --git a/src/client.js.rej b/src/client.js.rej new file mode 100644 index 0000000000000000000000000000000000000000..1101e2477adfdc004381b78e7d70953dacb7b484 --- /dev/null +++ b/src/client.js.rej @@ -0,0 +1,31 @@ +@@ -89,10 +89,12 @@ + parsed.metadata.name = parsed.data.name + parsed.data = parsed.data.params + parsed.metadata.state = state +- debug('read packet ' + state + '.' + parsed.metadata.name) +- if (debug.enabled) { +- const s = JSON.stringify(parsed.data, null, 2) +- debug(s && s.length > 10000 ? parsed.data : s) ++ if (!globalThis.excludeCommunicationDebugEvents?.includes(parsed.metadata.name)) { ++ debug('read packet ' + state + '.' + parsed.metadata.name) ++ if (debug.enabled) { ++ const s = JSON.stringify(parsed.data, null, 2) ++ debug(s && s.length > 10000 ? parsed.data : s) ++ } + } + if (this._hasBundlePacket && parsed.metadata.name === 'bundle_delimiter') { + if (this._mcBundle.length) { // End bundle +@@ -239,8 +252,11 @@ + + write (name, params) { + if (!this.serializer.writable) { return } +- debug('writing packet ' + this.state + '.' + name) +- debug(params) ++ if (!globalThis.excludeCommunicationDebugEvents?.includes(name)) { ++ debug(`[${this.state}] from ${this.isServer ? 'server' : 'client'}: ` + name) ++ debug(params) ++ } ++ this.emit('writePacket', name, params) + this.serializer.write({ name, params }) + } +