From 31f6d8380ec961be7c889874154a5e9d82bbf8a3 Mon Sep 17 00:00:00 2001 From: roblabla Date: Sun, 22 Feb 2015 22:13:22 +0000 Subject: [PATCH] Publish debugging tool, fix bugs in protojs --- examples/proxy.js | 84 +++++++++++++++++++++++++++++++++++++++++++++++ lib/protocol.js | 3 +- 2 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 examples/proxy.js diff --git a/examples/proxy.js b/examples/proxy.js new file mode 100644 index 0000000..3caff0d --- /dev/null +++ b/examples/proxy.js @@ -0,0 +1,84 @@ +var mc = require('../'); + +var states = mc.protocol.states; +function print_help() { + console.log("usage: node proxy.js []"); +} + +if (process.argv.length < 4) { + console.log("Too few arguments!"); + print_help(); + process.exit(1); +} + +process.argv.forEach(function(val, index, array) { + if (val == "-h") { + print_help(); + process.exit(0); + } +}); + +var host = process.argv[2]; +var port = 25565; +var user = process.argv[3]; +var passwd = process.argv[4]; + +if (host.indexOf(':') != -1) { + port = host.substring(host.indexOf(':')+1); + host = host.substring(0, host.indexOf(':')); +} + +var srv = mc.createServer({ + 'online-mode': false, + port: 25566 +}); +srv.on('login', function (client) { + var addr = client.socket.remoteAddress; + console.log('Incoming connection', '('+addr+')'); + var endedClient = false; + var endedTargetClient = false; + client.on('end', function() { + endedClient = true; + console.log('Connection closed by client', '('+addr+')'); + if (!endedTargetClient) + targetClient.end("End"); + }); + client.on('error', function() { + endedClient = true; + console.log('Connection error by client', '('+addr+')'); + if (!endedTargetClient) + targetClient.end("Error"); + }); + var targetClient = mc.createClient({ + host: host, + port: port, + username: user, + password: passwd + }); + 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)}`); + 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} : ${JSON.stringify(packet)}`); + if (!endedClient) + client.write(packet.id, packet); + } + }); + targetClient.on('end', function() { + endedTargetClient = true; + console.log('Connection closed by server', '('+addr+')'); + if (!endedClient) + client.end("End"); + }); + targetClient.on('error', function() { + endedTargetClient = true; + console.log('Connection error by server', '('+addr+')'); + if (!endedClient) + client.end("Error"); + }); +}); diff --git a/lib/protocol.js b/lib/protocol.js index 2182602..5a9f43a 100644 --- a/lib/protocol.js +++ b/lib/protocol.js @@ -491,7 +491,7 @@ var packets = { { name: "value", type: "ustring" }, { name: "isSigned", type: "bool" }, { name: "signature", type: "ustring", condition: function(field_values) { - return field_values["isSigned"]; + return field_values["this"]["isSigned"]; }} ]}}}, { name: "gamemode", type: "varint", condition: function(field_values) { @@ -668,7 +668,6 @@ var packets = { ]}, position: {id: 0x04, fields: [ { name: "x", type: "double" }, - { name: "stance", type: "double" }, { name: "y", type: "double" }, { name: "z", type: "double" }, { name: "onGround", type: "bool" }