Merge pull request #132 from thejoshwolfe/pull-1

adding command line switches to examples/proxy.js for dumping messages
This commit is contained in:
Robin Lambertz 2015-03-18 15:59:44 +01:00
commit c55d7737d8

View File

@ -1,27 +1,66 @@
var mc = require('../'); var mc = require('../');
var states = mc.protocol.states; var states = mc.protocol.states;
function print_help() { function printHelpAndExit(exitCode) {
console.log("usage: node proxy.js <target_srv> <user> [<password>]"); console.log("usage: node proxy.js [<options>...] <target_srv> <user> [<password>]");
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("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.");
process.exit(exitCode);
} }
if (process.argv.length < 4) { if (process.argv.length < 4) {
console.log("Too few arguments!"); console.log("Too few arguments!");
print_help(); printHelpAndExit(1);
process.exit(1);
} }
process.argv.forEach(function(val, index, array) { process.argv.forEach(function(val, index, array) {
if (val == "-h") { if (val == "-h") {
print_help(); printHelpAndExit(0);
process.exit(0);
} }
}); });
var host = process.argv[2]; var args = process.argv.slice(2);
var host;
var port = 25565; var port = 25565;
var user = process.argv[3]; var user;
var passwd = process.argv[4]; 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 arg = parseInt(args[i]);
if (isNaN(arg)) printHelpAndExit(1);
if (option == "--dump") {
printIdWhitelist[arg] = true;
} else if (option == "-x") {
printIdBlacklist[arg] = true;
} 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) { if (host.indexOf(':') != -1) {
port = host.substring(host.indexOf(':')+1); port = host.substring(host.indexOf(':')+1);
@ -59,7 +98,11 @@ srv.on('login', function (client) {
var brokenPackets = [/*0x04, 0x2f, 0x30*/]; var brokenPackets = [/*0x04, 0x2f, 0x30*/];
client.on('packet', function(packet) { client.on('packet', function(packet) {
if (targetClient.state == states.PLAY && packet.state == states.PLAY) { if (targetClient.state == states.PLAY && packet.state == states.PLAY) {
//console.log(`client->server: ${client.state}.${packet.id} : ${JSON.stringify(packet)}`); if (shouldDump(packet.id)) {
console.log("client->server:",
client.state + ".0x" + packet.id.toString(16) + " :",
JSON.stringify(packet));
}
if (!endedTargetClient) if (!endedTargetClient)
targetClient.write(packet.id, packet); targetClient.write(packet.id, packet);
} }
@ -68,7 +111,11 @@ srv.on('login', function (client) {
if (packet.state == states.PLAY && client.state == states.PLAY && if (packet.state == states.PLAY && client.state == states.PLAY &&
brokenPackets.indexOf(packet.id) === -1) 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)) {
console.log("client<-server:",
targetClient.state + ".0x" + packet.id.toString(16) + " :",
(packet.id != 38 ? JSON.stringify(packet) : "Packet too big"));
}
if (!endedClient) if (!endedClient)
client.write(packet.id, packet); client.write(packet.id, packet);
} }
@ -120,3 +167,10 @@ srv.on('login', function (client) {
client.end("Error"); client.end("Error");
}); });
}); });
function shouldDump(id) {
if (printIdBlacklist[id]) return false;
if (printAllIds) return true;
if (printIdWhitelist[id]) return true;
return false;
}