- Joined
- Sep 10, 2007
- Messages
- 970
- Reaction score
- 815
This time in a fun language!
I've worked on this for..30 minutes?
output for lulz
server.js
client.js
packetreader.js
wutdo?
I've worked on this for..30 minutes?
output for lulz
Code:
C:\Users\Jacob\Documents\My Web Sites\Empty Site>node server.js
Connection from: 127.0.0.1
Got packet from client: 3e9
server.js
Code:
var net = require('net');
var client = require('./client');
clients = [];
index = 0;
net.createServer(function(socket) {
socket.addListener("connect", function() {
console.log("Connection from: " + socket.remoteAddress);
clients.push(new client(socket, index++));
});
}).listen(6000);
client.js
Code:
var sys = require('sys');
var net = require('net');
var packetreader = require('./packetreader');
module.exports = Client;
function Client(socket, index) {
this.socket = socket;
this.uid = index;
this.onConnection();
var instance = this;
this.socket.addListener('data', function(data) {
packetreader.decrypt(data, 2, 2, instance.key);
var size = data.readUInt16LE(2);
if (size <= data.length){
var packet = new packetreader(data, instance.key);
console.log("Got packet from client: " + packet.getCommandId().toString(16));
}
});
}
Client.prototype.onConnection = function() {
this.key = new Buffer(32);
var buffer = new Buffer(26);
var iv = new Buffer([0x37, 0x04, 0x5D, 0x2E, 0x43, 0x3A,
0x49, 0x53, 0x50, 0x05, 0x13, 0xC9,
0x28, 0xA4, 0x4D, 0x05]);
var xor = new Buffer([0x57, 0x02, 0x5B, 0x04, 0x34, 0x06, 0x01,
0x08, 0x37, 0x0A, 0x12, 0x69, 0x41, 0x38,
0x0F, 0x78]);
// bulid the key
var ipLong = this.intToNetworkLong(this.socket.remoteAddress);
this.key.writeUInt32LE(ipLong, 0);
this.key.writeUInt32LE(2, 4);
this.key.writeUInt32LE(0, 8);
this.key.writeUInt32LE(this.uid, 12);
iv.copy(this.key, 16, 0, 16);
// build the packet
buffer.writeUInt16LE(10, 0);
buffer.writeUInt32LE(26, 2);
buffer.writeUInt32LE(0, 6);
for(var i = 4; i < 12; i += 4)
buffer.writeUInt32LE(this.key.readUInt32LE(i), 6 + i);
buffer.writeUInt32LE(this.key.readUInt32LE(0), 22);
for(i = 0; i < 4; ++i) {
var a = xor.readUInt32LE(i * 4);
var b = this.key.readUInt32LE(i * 4);
this.key.writeUInt32LE(a ^ b, i * 4);
}
this.sendRawPacket(buffer);
}
Client.prototype.sendRawPacket = function(buffer) {
this.socket.write(buffer);
}
Client.prototype.intToNetworkLong = function(value) {
ip = String(value);
var parts = ip.split(".");
var res = 0;
res += parseInt(parts[0], 10) << 24;
res += parseInt(parts[1], 10) << 16;
res += parseInt(parts[2], 10) << 8;
res += parseInt(parts[3], 10);
return res;
}
packetreader.js
Code:
var sys = require('sys');
module.exports = PacketReader;
function PacketReader(buffer, key)
{
this.data = buffer;
this.key = key;
PacketReader.decrypt(this.data, 6, this.data.length - 6, this.key);
}
PacketReader.prototype.getCommandId = function() {
return this.data.readUInt16LE(8);
}
PacketReader.decrypt = function(data, index, size, key) {
for(var i = 0; i < size; ++i) {
var a = data[index + i];
a ^= 0xf0;
var b = (7 & a);
a = (a >> 3);
b = (b << 5);
b = (a | b);
data[index + i] = (b ^ key[i % 32]);
}
}
wutdo?