Okay so, I asked on the Nodejs IRC, and very helpful frenchman helped me out. This code now works:
var net = require('net'),
events = require('events'),
util = require('util');
function Connection(settings) {
events.EventEmitter.call(this);
var self = this;
this.socket = net.createConnection(settings.port, settings.host);
this.socket.setNoDelay(true);
this.status = 'connecting';
this.socket.on('connect', function() {
console.log('connected');
console.log('writing');
var auth = "ds;localhost:25565";
var b = new Buffer(auth.length + 1);
b[0] = 2;
b.write(auth, 1);
self.socket.write(b, function() {
console.log('flushed');
});
self.status = 'send-2';
});
function handleByte(B) {
switch(self.status) {
case 'send-2':
if(b !== 0x2)
return false;
self.socket.write(new Buffer('01', 'hex'), function() {
console.log('flushed');
});
self.status = 'send-1';
return true;
case 'send-1':
if(b !== 0x1)
return false;
self.status = 'recv-0d';
return true;
case 'recv-0d':
if(b !== 0x1)
return false;
self.emit('authenticated');
return true;
default:
return false;
}
}
this.socket.on('data', function(chunk) {
console.log('got data: ' + chunk);
while(chunk.length > 0) {
if(!handleByte(chunk[0])) {
console.error('error in ' + self.status);
process.exit(1);
}
chunk = chunk.slice(1);
}
});
}
util.inherits(events.EventEmitter, Connection);
exports.Connection = Connection;
var server = new Connection({ port: 25565, host: "localhost"});
Now, the problem is, when it connects to the server (minecraft server), I get this in my server console:
ava.io.IOException: Received string length longer than maximum allowed (26217 > 64)
at lx.a(SourceFile:197)
at ny.a(SourceFile:21)
at lx.a(SourceFile:155)
at qq.h(SourceFile:189)
at qq.c(SourceFile:9)
at zt.run(SourceFile:76)
2012-03-29 17:48:00 [INFO] /127.0.0.1:57231 lost connection
Okay. So what do I do? I'm guessing it has to do with this part of my code:
var auth = "ds;localhost:25565";
var b = new Buffer(auth.length + 1);
b[0] = 2;
b.write(auth, 1);
self.socket.write(b, function() {
If I run Wireshark on localhost, and connect using the official client, I should be expecting this:
00000000 02 00 1e 00 66 00 69 00 72 00 65 00 63 00 61 00 ....f.i. r.e.c.a.
00000010 74 00 64 00 65 00 73 00 69 00 67 00 6e 00 73 00 t.d.e.s. i.g.n.s.
00000020 3b 00 6c 00 6f 00 63 00 61 00 6c 00 68 00 6f 00 ;.l.o.c. a.l.h.o.
00000030 73 00 74 00 3a 00 32 00 35 00 35 00 36 00 35 s.t.:.2. 5.5.6.5
However, my code generates this:
00000000 02 66 69 72 65 63 61 74 64 65 73 69 67 6e 73 3b .ds ds;
00000010 6c 6f 63 61 6c 68 6f 73 74 3a 32 35 35 36 35 localhos t:25565
What do I have to do? If you look at the TCP dump, the actual data is the same, but the official client sticks NULL between each character, hence why it's twice as long. (And for some reason, it also adds "30" before starting the string part -> 1e = 30)