Skip to content

Instantly share code, notes, and snippets.

@ArmedGuy
Created October 21, 2013 12:05
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ArmedGuy/7082803 to your computer and use it in GitHub Desktop.
Save ArmedGuy/7082803 to your computer and use it in GitHub Desktop.
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);
});
});
@Edwin-Sun
Copy link

Nice RCON script. Is there a way to change this script so the RCOn port ( connection ) stays open and the userlist will be retrieved every second?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment