From c1ced61d477a35fa3193126e42eca27a536b8802 Mon Sep 17 00:00:00 2001 From: roblabla Date: Tue, 3 Mar 2015 12:43:46 +0000 Subject: [PATCH] Make proxy able to spot discrepencies in input/output buffer. --- examples/proxy.js | 46 ++++++++++++++++++++++++++++++++++++++++++---- package.json | 3 ++- 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/examples/proxy.js b/examples/proxy.js index ba8df02..acf4e84 100644 --- a/examples/proxy.js +++ b/examples/proxy.js @@ -53,22 +53,60 @@ srv.on('login', function (client) { host: host, port: port, username: user, - password: passwd + password: passwd, + 'online-mode': passwd != null ? true : false }); + var brokenPackets = [/*0x04, 0x2f, 0x30*/]; client.on('packet', function(packet) { if (targetClient.state == states.PLAY && packet.state == states.PLAY) { - console.log(`client->server: ${client.state}.${packet.id} : ${JSON.stringify(packet)}`); + //console.log(`client->server: ${client.state}.${packet.id} : ${JSON.stringify(packet)}`); if (!endedTargetClient) targetClient.write(packet.id, packet); } }); targetClient.on('packet', function(packet) { - if (packet.state == states.PLAY && client.state == states.PLAY) { - console.log(`client<-server: ${targetClient.state}.${packet.id} : ${packet.id != 38 ? JSON.stringify(packet) : "Packet too big"}`); + if (packet.state == states.PLAY && client.state == states.PLAY && + brokenPackets.indexOf(packet.id) === -1) + { + //console.log(`client<-server: ${targetClient.state}.${packet.id} : ${packet.id != 38 ? JSON.stringify(packet) : "Packet too big"}`); if (!endedClient) client.write(packet.id, packet); } }); + var buffertools = require('buffertools'); + targetClient.on('raw', function(buffer, state) { + if (client.state != states.PLAY || state != states.PLAY) + return; + var packetId = mc.protocol.types.varint[0](buffer, 0); + var packetData = mc.protocol.parsePacketData(buffer, state, false, {"packet": 1}).results; + var packetBuff = mc.protocol.createPacketBuffer(packetData.id, packetData.state, packetData, true); + if (buffertools.compare(buffer, packetBuff) != 0) + { + console.log(`client<-server: Error in packetId ${state}.0x${packetId.value.toString(16)}`); + console.log(buffer.toString('hex')); + console.log(packetBuff.toString('hex')); + } + /*if (client.state == states.PLAY && brokenPackets.indexOf(packetId.value) !== -1) + { + console.log(`client<-server: raw packet`); + console.log(packetData); + if (!endedClient) + client.writeRaw(buffer); + }*/ + }); + client.on('raw', function(buffer, state) { + if (state != states.PLAY || targetClient.state != states.PLAY) + return; + var packetId = mc.protocol.types.varint[0](buffer, 0); + var packetData = mc.protocol.parsePacketData(buffer, state, true, {"packet": 1}).results; + var packetBuff = mc.protocol.createPacketBuffer(packetData.id, packetData.state, packetData, false); + if (buffertools.compare(buffer, packetBuff) != 0) + { + console.log(`client->server: Error in packetId ${state}.0x${packetId.value.toString(16)}`); + console.log(buffer.toString('hex')); + console.log(packetBuff.toString('hex')); + } + }); targetClient.on('end', function() { endedTargetClient = true; console.log('Connection closed by server', '('+addr+')'); diff --git a/package.json b/package.json index e53dbce..c001f00 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,8 @@ "mkdirp": "~0.3.4", "rimraf": "~2.1.1", "zfill": "0.0.1", - "batch": "~0.3.1" + "batch": "~0.3.1", + "buffertools": "^2.1.2" }, "dependencies": { "ansi-color": "0.2.1",