diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..be85d62 --- /dev/null +++ b/.npmignore @@ -0,0 +1,3 @@ +node_modules +test/npm-debug.log +test/server diff --git a/README.md b/README.md index 4a62ee9..8887694 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,8 @@ [![Join the chat at https://gitter.im/PrismarineJS/node-minecraft-protocol](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/PrismarineJS/node-minecraft-protocol?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) +[![Build Status](https://circleci.com/gh/PrismarineJS/node-minecraft-protocol.png?style=shield)](https://circleci.com/gh/PrismarineJS/node-minecraft-protocol) + Parse and serialize minecraft packets, plus authentication and encryption. ## Features @@ -379,6 +381,16 @@ NODE_DEBUG="minecraft-protocol" node [...] ## History +### 0.13.2 + + * Fixed particle packet. + * Fixed release. 0.13.1 release was missing an entire folder. + +### 0.13.1 + + * Externalized rsa-wrap library to its own npm module, named ursa-native + * Fixed broken bed-related packets (thanks [rom1504](https://github.com/rom1504)) + ### 0.13.0 * Updated protocol version to support 1.8.1 (thanks [wtfaremyinitials](https://github.com/wtfaremyinitials)) diff --git a/circle.yml b/circle.yml new file mode 100644 index 0000000..46fcd27 --- /dev/null +++ b/circle.yml @@ -0,0 +1,10 @@ +machine: + node: + version: 0.10.28 +dependencies: + override: + - npm install --dev +test: + override: + - node_modules/.bin/mocha -g "packets" + - node_modules/.bin/mocha -g "mc-server" diff --git a/examples/proxy/proxy.js b/examples/proxy/proxy.js index 37c729f..a6ab416 100644 --- a/examples/proxy/proxy.js +++ b/examples/proxy/proxy.js @@ -1,27 +1,74 @@ var mc = require('../'); var states = mc.protocol.states; -function print_help() { - console.log("usage: node proxy.js []"); +function printHelpAndExit(exitCode) { + console.log("usage: node proxy.js [...] []"); + console.log("options:"); + console.log(" --dump ID"); + console.log(" print to stdout messages with the specified ID."); + console.log(" --dump-all"); + console.log(" print to stdout all messages, except those specified with -x."); + console.log(" -x ID"); + console.log(" do not print messages with this ID."); + console.log(" ID"); + console.log(" an integer in decimal or hex (given to JavaScript's parseInt())"); + console.log(" optionally prefixed with o for client->server or i for client<-server."); + console.log("examples:"); + console.log(" node proxy.js --dump-all -x 0x0 -x 0x3 -x 0x12 -x 0x015 -x 0x16 -x 0x17 -x 0x18 -x 0x19 localhost Player"); + console.log(" print all messages except for some of the most prolific."); + console.log(" node examples/proxy.js --dump i0x2d --dump i0x2e --dump i0x2f dump i0x30 --dump i0x31 --dump i0x32 --dump o0x0d --dump o0x0e --dump o0x0f --dump o0x10 --dump o0x11 localhost Player"); + console.log(" print messages relating to inventory management."); + + process.exit(exitCode); } if (process.argv.length < 4) { console.log("Too few arguments!"); - print_help(); - process.exit(1); + printHelpAndExit(1); } process.argv.forEach(function(val, index, array) { if (val == "-h") { - print_help(); - process.exit(0); + printHelpAndExit(0); } }); -var host = process.argv[2]; +var args = process.argv.slice(2); +var host; var port = 25565; -var user = process.argv[3]; -var passwd = process.argv[4]; +var user; +var passwd; + +var printAllIds = false; +var printIdWhitelist = {}; +var printIdBlacklist = {}; +(function() { + for (var i = 0; i < args.length; i++) { + var option = args[i]; + if (!/^-/.test(option)) break; + if (option == "--dump-all") { + printAllIds = true; + continue; + } + i++; + var match = /^([io]?)(.*)/.exec(args[i]); + var prefix = match[1]; + if (prefix === "") prefix = "io"; + var number = parseInt(match[2]); + if (isNaN(number)) printHelpAndExit(1); + if (option == "--dump") { + printIdWhitelist[number] = prefix; + } else if (option == "-x") { + printIdBlacklist[number] = prefix; + } else { + printHelpAndExit(1); + } + } + if (!(i + 2 <= args.length && args.length <= i + 3)) printHelpAndExit(1); + host = args[i++]; + user = args[i++]; + passwd = args[i++]; +})(); if (host.indexOf(':') != -1) { port = host.substring(host.indexOf(':')+1); @@ -59,7 +106,11 @@ srv.on('login', function (client) { 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)}`); + if (shouldDump(packet.id, "o")) { + console.log("client->server:", + client.state + ".0x" + packet.id.toString(16) + " :", + JSON.stringify(packet)); + } if (!endedTargetClient) targetClient.write(packet.id, packet); } @@ -68,7 +119,11 @@ srv.on('login', function (client) { 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 (shouldDump(packet.id, "i")) { + console.log("client<-server:", + targetClient.state + ".0x" + packet.id.toString(16) + " :", + (packet.id != 38 ? JSON.stringify(packet) : "Packet too big")); + } if (!endedClient) client.write(packet.id, packet); } @@ -120,3 +175,13 @@ srv.on('login', function (client) { client.end("Error"); }); }); + +function shouldDump(id, direction) { + if (matches(printIdBlacklist[id])) return false; + if (printAllIds) return true; + if (matches(printIdWhitelist[id])) return true; + return false; + function matches(result) { + return result != null && result.indexOf(direction) !== -1; + } +} diff --git a/package.json b/package.json index 55c7a92..f630ea3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "minecraft-protocol", - "version": "0.13.0", + "version": "0.13.2", "description": "Parse and serialize minecraft packets, plus authentication and encryption.", "main": "index.js", "repository": { diff --git a/src/index.js b/src/index.js index 0201dd6..3889c43 100644 --- a/src/index.js +++ b/src/index.js @@ -320,7 +320,7 @@ function createClient(options) { hash.update(packet.publicKey); var digest = mcHexDigest(hash); - joinServer(this.username, digest, accessToken, client.session.selectedProfile.id, cb); + joinServer(client.username, digest, accessToken, client.session.selectedProfile.id, cb); } function sendEncryptionKeyResponse() { diff --git a/src/protocol.js b/src/protocol.js index eb9b2f4..2729ecc 100644 --- a/src/protocol.js +++ b/src/protocol.js @@ -131,12 +131,12 @@ var packets = { { name: "slot", type: "byte" } ]}, bed: {id: 0x0a, fields: [ - { name: "entityId", type: "int" }, + { name: "entityId", type: "varint" }, { name: "location", type: "position" } ]}, animation: {id: 0x0b, fields: [ { name: "entityId", type: "varint" }, - { name: "animation", type: "byte" } + { name: "animation", type: "ubyte" } ]}, named_entity_spawn: {id: 0x0c, fields: [ { name: "entityId", type: "varint" }, @@ -379,8 +379,15 @@ var packets = { { name: "offsetY", type: "float" }, { name: "offsetZ", type: "float" }, { name: "particleData", type: "float" }, - { name: "particles", type: "count", typeArgs: { countFor: "data", type: "int" } }, - { name: "data", type: "array", typeArgs: { count: "particles", type: "varint" } } + { name: "particles", type: "int" }, + { name: "data", type: "array", typeArgs: { count: function(fields) { + if (fields.particleId === 36) + return 2; + else if (fields.particleId === 37 || fields.particleId === 38) + return 1; + else + return 0; + }, type: "varint" } } ]}, game_state_change: {id: 0x2b, fields: [ { name: "reason", type: "ubyte" }, @@ -1090,8 +1097,11 @@ function readBool(buffer, offset) { function readPosition(buffer, offset) { var longVal = readLong(buffer, offset).value; // I wish I could do destructuring... var x = longVal[0] >> 6; + if(x>33554432) x-=67108864; var y = ((longVal[0] & 0x3F) << 6) | ((longVal[1] >> 26) & 0x3f); + if(y>2048) y-=4096; var z = longVal[1] & 0x3FFFFFF; + if(z>33554432) z-=67108864; return { value: { x: x, y: y, z: z }, size: 8 @@ -1348,7 +1358,11 @@ function readArray(buffer, offset, typeArgs, rootNode) { value: [], size: 0 } - var count = getField(typeArgs.count, rootNode); + var count; + if (typeof typeArgs.count === "function") + count = typeArgs.count(rootNode); + else + count = getField(typeArgs.count, rootNode); for (var i = 0; i < count; i++) { var readResults = read(buffer, offset, { type: typeArgs.type, typeArgs: typeArgs.typeArgs }, rootNode); results.size += readResults.size;