-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
75 lines (66 loc) · 2.68 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
var protocol = require('minecraft-protocol').protocol;
var packetNames = require('./packet-names.js').packets;
var colors = require('colors');
var EventEmitter = require('events').EventEmitter;
console.log('Welcome to mincraft-sniffer, the protocol inspector');
console.log('Start your server on port 25565, and connect your client to 25566');
console.log('NOTE: this does not work with encryption. Therefore, offline mode must be enabled.');
console.log('server packets are blue'.blue + ', ' + 'client packets are green'.green);
console.log('');
var proxy = require('./proxy.js').start({
proxyPort : 25566,
serviceHost : 'localhost',
servicePort : 25565
});
proxy.on('connect', function(client) {
client.incomingBuffer = new Buffer(0);
client.outgoingBuffer = new Buffer(0);
client.state = 'handshaking';
console.log('accepted connection');
});
proxy.on('clientToServer', function(client, data) {
client.incomingBuffer = Buffer.concat([ client.incomingBuffer, data ]);
client.incomingBuffer = handleData(client, true /* toServer */, client.incomingBuffer);
});
proxy.on('serverToClient', function(client, data) {
client.outgoingBuffer = Buffer.concat([ client.incomingBuffer, data ]);
client.outgoingBuffer = handleData(client, false /* to client */, client.outgoingBuffer);
});
var packets = new EventEmitter();
packets.on('handshaking:toServer:handshake', function(client, packet) {
var oldState = client.state;
client.state = ['handshaking', 'status', 'login', 'play'][packet.nextState];
console.log('state changed from ' + oldState + ' to ' + client.state);
});
packets.on('login:toClient:loginSuccess', function(client, packet) {
var oldState = client.state;
client.state = 'play';
console.log('state changed from ' + oldState + ' to ' + client.state);
});
function handleData(client, isToServer, buffer) {
var source = isToServer ? 'client' : 'server';
var dest = isToServer ? 'toServer' : 'toClient';
while (true) {
parsed = protocol.parsePacket(buffer, client.state, isToServer, {packet: 1 });
if (!parsed)
break;
if (parsed.error) {
console.error('parse error from ' + source + ': ' + parsed.error);
} else {
packet = parsed.results;
var name = packetNames[client.state][dest][packet.id];
if (typeof name == 'undefined')
console.error('unknown packet id ' + packet.id + ' from ' + source);
else {
delete packet.id;
if (['mapChunkBulk', 'mapChunk'].indexOf(name) >= 0)
packet.compressedChunkData = '(hidden)';
var output = (name + ': ').bold + JSON.stringify(packet);
console.log(output[isToServer ? 'green' : 'blue']);
packets.emit(client.state+':'+dest+':'+name, client, packet);
}
}
buffer = buffer.slice(parsed.size);
}
return buffer;
}