Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Battlefield 2 RCon module
var net = require('net');
var crypto = require('crypto');
var util = require('util');
var events = require('events');
function rcon(options) {
$rcon = this;
this.client = net.connect(
{
host: options.host,
port: options.port
}, function() {
console.log("Connected!");
}
);
this.client.on('data', function(data) {
var sent = data.toString();
if(sent.indexOf('### Digest seed: ') != -1) {
var seed = sent.replace("### Digest seed: ", "").trim();
console.log("seed: " + seed);
$rcon.client.write("login " + crypto.createHash('md5').update(seed + options.password).digest('hex') + "\n");
}
if(sent.indexOf('Authentication successful') != -1) {
$rcon.emit('authed');
}
});
}
util.inherits(rcon, events.EventEmitter);
rcon.prototype.send = function(data, callback) {
this.client.write(data + "\n");
this.client.once("data", function(data) {
callback(data);
});
}
exports.rcon = rcon;
var bf2 = require('./bf2rcon');
var options = {
host: "127.0.0.1",
port: 12345,
password: "*password*"
}
var rcon = new bf2.rcon(options);
var usermatch = /Id: \s?([0-9]*) - (.*) is remote ip: ([0-9\.]*):([0-9]*)/g;
var userlist = [];
rcon.on("authed", function() {
userlist = [];
this.send("exec admin.listPlayers", function(data) {
data = data.toString();
match = usermatch.exec(data);
while(match != null) {
userlist.push({
id: match[1],
name: match[2],
ip: match[3],
port: match[4]
});
match = usermatch.exec(data);
}
console.log(userlist);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment