I'm using a simple socket script to establish an rcon connection to my Battlefield heroes server. I need to grab the seed from the returned data then concatenate it with the password to use in creating the login hash I need. But the strings won't concatenate with the normal string methods. If I output the strings separately they display fine, but they simply won't combine so I can hash them.
var net = require('net');
var crypto = require('crypto');
var md5sum = crypto.createHash('md5');
var HOST = '<ip address>', PORT = <port>,
PASSWORD = '<password>';
var client = new net.Socket();
client.connect(PORT, HOST, function() {
console.log('CONNECTED TO: ' + HOST + ':' + PORT);
});
client.on('data', function(data) {
console.log(data.toString());
response = data.toString();
if (data.toString().slice(0,10) == "### Digest") {
var seed = response.slice(17);
auth = seed.concat(PASSWORD);
console.log('auth: '+auth);
hash = require('crypto').createHash('md5').update(auth).digest("hex");
console.log(hash);
//client.write('login '+hash+' \n');
}
//client.destroy();
});
client.on('close', function() {
//do something on close
});
client.on('error', function(err) {
console.log(err);
});
I found a solution that works for me. Not sure if I'm over doing it but I decided to pass both strings to new buffers. Then I passed them into an array to combine them. (shrug) I think it'll do. Any better solutions?
var net = require('net');
var crypto = require('crypto');
var md5sum = crypto.createHash('md5');
var HOST = '<ip address>', PORT = <port>,
PASSWORD = '<password>';
var client = new net.Socket();
client.connect(PORT, HOST, function() {
console.log('CONNECTED TO: ' + HOST + ':' + PORT);
});
client.on('data', function(data) {
console.log(data.toString());
response = data.toString();
if (data.toString().slice(0,10) == "### Digest") {
var a = new Buffer(response.slice(17));
var b = new Buffer(PASSWORD);
var auth = new Array();
auth += a.toString().replace(/\n/g,'');
auth += b.toString();
hash = require('crypto').createHash('md5').update(auth).digest("hex");
client.write('login '+hash+' \n');
}
//client.destroy();
});
client.on('close', function() {
//do something on close
});
client.on('error', function(err) {
console.log(err);
});