Created
October 21, 2013 12:05
-
-
Save ArmedGuy/7082803 to your computer and use it in GitHub Desktop.
Battlefield 2 RCon module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
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?