Drop mcpc_buffer, use mc-ping-updated
This commit is contained in:
parent
a21ed3f6fb
commit
f1eb418636
@ -1,81 +0,0 @@
|
||||
function CustomBuffer(existingBuffer) {
|
||||
var buffer = existingBuffer || new Buffer(48);
|
||||
var offset = 0;
|
||||
|
||||
this.writeVarInt = function(val) {
|
||||
while (true) {
|
||||
if ((val & 0xFFFFFF80) == 0) {
|
||||
this.writeUByte(val);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
this.writeUByte(val & 0x7F | 0x80);
|
||||
|
||||
val = val >>> 7;
|
||||
}
|
||||
};
|
||||
|
||||
this.writeString = function(string) {
|
||||
this.writeVarInt(string.length);
|
||||
|
||||
if (offset + string.length >= buffer.length) {
|
||||
Buffer.concat([buffer, new Buffer(string.length)]);
|
||||
}
|
||||
|
||||
buffer.write(string, offset, string.length, "UTF-8");
|
||||
|
||||
offset += string.length;
|
||||
};
|
||||
|
||||
this.writeUShort = function(val) {
|
||||
this.writeUByte(val >> 8);
|
||||
this.writeUByte(val & 0xFF);
|
||||
};
|
||||
|
||||
this.writeUByte = function(val) {
|
||||
if (offset + 1 >= buffer.length) {
|
||||
Buffer.concat([buffer, new Buffer(50)]);
|
||||
}
|
||||
|
||||
buffer.writeUInt8(val, offset++);
|
||||
};
|
||||
|
||||
this.readVarInt = function() {
|
||||
var val = 0;
|
||||
var count = 0;
|
||||
|
||||
while (true) {
|
||||
var i = buffer.readUInt8(offset++);
|
||||
|
||||
val |= (i & 0x7F) << count++ * 7;
|
||||
|
||||
if ((i & 0x80) != 128) {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return val;
|
||||
};
|
||||
|
||||
this.readString = function() {
|
||||
var length = this.readVarInt();
|
||||
var str = buffer.toString("UTF-8", offset, offset + length);
|
||||
|
||||
offset += length;
|
||||
|
||||
return str;
|
||||
};
|
||||
|
||||
this.buffer = function() {
|
||||
return buffer.slice(0, offset);
|
||||
};
|
||||
|
||||
this.offset = function() {
|
||||
return offset;
|
||||
};
|
||||
}
|
||||
|
||||
exports.createBuffer = function(buffer) {
|
||||
return new CustomBuffer(buffer);
|
||||
};
|
108
lib/ping.js
108
lib/ping.js
@ -1,100 +1,24 @@
|
||||
var net = require('net');
|
||||
var mcpe_ping = require('mcpe-ping');
|
||||
|
||||
var mcpc = require('./mcpc_buffer');
|
||||
var mcpc_ping = require('mc-ping-updated');
|
||||
|
||||
function pingMinecraftPC(host, port, timeout, callback) {
|
||||
var client = new net.Socket();
|
||||
var milliseconds = (new Date).getTime();
|
||||
|
||||
client.setTimeout(timeout, function() {
|
||||
client.destroy();
|
||||
|
||||
callback(new Error('timeout'), null);
|
||||
});
|
||||
|
||||
client.connect(port, host, function() {
|
||||
// Write out handshake packet.
|
||||
var handshakeBuffer = mcpc.createBuffer();
|
||||
|
||||
handshakeBuffer.writeVarInt(0);
|
||||
handshakeBuffer.writeVarInt(47);
|
||||
handshakeBuffer.writeString(host);
|
||||
handshakeBuffer.writeUShort(port);
|
||||
handshakeBuffer.writeVarInt(1);
|
||||
|
||||
writePCBuffer(client, handshakeBuffer);
|
||||
|
||||
// Write the set connection state packet, we should get the MOTD after this.
|
||||
var setModeBuffer = mcpc.createBuffer();
|
||||
|
||||
setModeBuffer.writeVarInt(0);
|
||||
|
||||
writePCBuffer(client, setModeBuffer);
|
||||
});
|
||||
|
||||
var readingBuffer = new Buffer(0);
|
||||
|
||||
client.on('data', function(data) {
|
||||
readingBuffer = Buffer.concat([readingBuffer, data]);
|
||||
|
||||
var buffer = mcpc.createBuffer(readingBuffer);
|
||||
var length;
|
||||
|
||||
try {
|
||||
length = buffer.readVarInt();
|
||||
} catch(err) {
|
||||
// The buffer isn't long enough yet, wait for more data!
|
||||
return;
|
||||
}
|
||||
|
||||
// Make sure we have the data we need!
|
||||
if (readingBuffer.length < length - buffer.offset() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Read the packet ID, throw it away.
|
||||
buffer.readVarInt();
|
||||
|
||||
try {
|
||||
var json = JSON.parse(buffer.readString());
|
||||
|
||||
// Remap our JSON into our custom structure.
|
||||
var res = {
|
||||
players: json.players,
|
||||
version: json.version.protocol,
|
||||
latency: (new Date).getTime() - milliseconds
|
||||
};
|
||||
|
||||
if (json.favicon) {
|
||||
res.favicon = json.favicon;
|
||||
}
|
||||
|
||||
// We parsed it, send it along!
|
||||
callback(null, res);
|
||||
} catch (err) {
|
||||
// Our data is corrupt? Fail hard.
|
||||
callback(err, null);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// We're done here.
|
||||
client.destroy();
|
||||
});
|
||||
|
||||
client.on('error', function(err) {
|
||||
callback(err, null);
|
||||
});
|
||||
}
|
||||
|
||||
// Wraps our Buffer into another to fit the Minecraft protocol.
|
||||
function writePCBuffer(client, buffer) {
|
||||
var length = mcpc.createBuffer();
|
||||
|
||||
length.writeVarInt(buffer.buffer().length);
|
||||
|
||||
client.write(Buffer.concat([length.buffer(), buffer.buffer()]));
|
||||
mcpc_ping(host, port, function(err, res) {
|
||||
if (err) {
|
||||
callback(err, null);
|
||||
} else {
|
||||
// Remap our JSON into our custom structure.
|
||||
callback(null, {
|
||||
players: {
|
||||
online: res.players.online,
|
||||
max: res.players.max
|
||||
},
|
||||
version: res.version.protocol,
|
||||
latency: (new Date).getTime() - milliseconds
|
||||
});
|
||||
}
|
||||
}, timeout);
|
||||
}
|
||||
|
||||
// This is a wrapper function for mcpe-ping, mainly used to convert the data structure of the result.
|
||||
|
@ -4,6 +4,7 @@
|
||||
"description": "A Minecraft server tracker that lets you focus on the basics.",
|
||||
"main": "app.js",
|
||||
"dependencies": {
|
||||
"mc-ping-updated": "0.0.6",
|
||||
"mcpe-ping": "0.0.3",
|
||||
"mime": "^1.3.4",
|
||||
"socket.io": "^1.3.7",
|
||||
|
Loading…
Reference in New Issue
Block a user