Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/js
- var ANSI_COLORS = {
- 'normal': '\x1B[0m',
- 'black': '\x1B[0;30m',
- 'hblack': '\x1B[1;30m',
- 'red': '\x1B[0;31m',
- 'hred': '\x1B[1;31m',
- 'green': '\x1B[0;32m',
- 'hgreen': '\x1B[1;32m',
- 'yellow': '\x1B[0;33m',
- 'hyellow': '\x1B[1;33m',
- 'blue': '\x1B[0;34m',
- 'hblue': '\x1B[1;34m',
- 'magenta': '\x1B[0;35m',
- 'hmagenta': '\x1B[1;35m',
- 'cyan': '\x1B[0;36m',
- 'hcyan': '\x1B[1;36m',
- 'white': '\x1B[0;37m',
- 'hwhite': '\x1B[1;37m',
- };
- function log(msg, color) {
- var date = new Date().toISOString()
- .replace(/T/, ' ').
- .replace(/\..+/, '');
- var text = msg;
- if(color != null && color in ANSI_COLORS) {
- text = ANSI_COLORS[color] + text + ANSI_COLORS['normal'] ;
- }
- process.stdout.write('['+date+'] ' + text + '\n');
- }
- function RconService(rconData) {
- var me = this;
- var WebSocket = require('ws');
- var States = {
- eCLOSED:0,
- eOPENED:1,
- eCONNECTING:2
- };
- this.rconData = rconData;
- this.state = States.eCLOSED;
- this.identifier = 0;
- this.listeners = {};
- this.commandQueue = [];
- this.Connect = function() {
- log("Connecting");
- this.state = States.eCONNECTING;
- this.ws = new WebSocket( "ws://" + this.rconData.addr + ":" + this.rconData.port + "/" + this.rconData.pass );
- this.ws.on('open', function () {
- log("Connected", 'green');
- me.state = States.eOPENED;
- while(command = me.commandQueue.shift()) {
- me.Command(command.msg, command.callBack)
- }
- });
- this.ws.on('close', function () {
- if(me.rconData.noRetry) {
- log("Disconnected", 'red');
- process.exit(1);
- } else {
- log("Disconnected, retrying in 2s", 'red');
- me.state = States.eCLOSED;
- setTimeout(function() { me.Connect() }, 2000);
- }
- });
- this.ws.on('message', function (data, flags) {
- me.OnMessage( JSON.parse( data ) );
- });
- this.ws.on('error', function (err) {
- if(me.rconData.noRetry) {
- log(err, 'hred');
- process.exit(1);
- } else {
- log(err+" retrying in 10s", 'hred');
- me.state = States.eCLOSED;
- setTimeout(function() { me.Connect() }, 10000);
- }
- });
- };
- this.Command = function(msg, callBack) {
- if(this.state == States.eCLOSED)
- this.Connect();
- if(this.state == States.eOPENED) {
- var packet = {
- Identifier: this.identifier,
- Message: msg,
- Type: "3"
- };
- if(callBack != null)
- this.listeners[this.identifier] = callBack
- this.identifier += 1;
- this.ws.send( JSON.stringify( packet ) );
- } else {
- this.commandQueue.push({msg:msg, callBack:callBack});
- }
- };
- this.OnMessage = function(data) {
- if(data.Identifier in me.listeners && me.listeners[data.Identifier] != null) {
- me.listeners[data.Identifier](data.Message);
- me.listeners[data.Identifier] = null;
- } else if(this.defaultListener != null) {
- this.defaultListener(data.Message);
- }
- };
- }
- var rcon = null;
- function RunCommand(command, callback)
- {
- if(rcon == null) {
- rcon = new RconService(rconData);
- }
- if(callback != null)
- rcon.Command(command, callback);
- else
- rcon.Command(command, function(response) {log(response)});
- }
- function RunCommandOnce(command)
- {
- if(rcon == null) {
- rcon = new RconService(rconData);
- }
- rcon.Command(command, function(response) {log(response); process.exit()});
- }
- clientdataRE = new RegExp(/^([0-9.]{7,15})\:([0-9]*)\/([0-9]*)\/(.*)/);
- killedRE = new RegExp(/^(.+?)\[[0-9]+\/[0-9]+\] was killed by (.+?)\[[0-9]+\/[0-9]+\]/);
- killedNpcRE = new RegExp(/^(.+?)\[[0-9]+\/[0-9]+\] was killed by (.+?)\.prefab \((\w+)\)/);
- diedRE = new RegExp(/^(.+?)\[[0-9]+\/[0-9]+\] died \((\w+)\)/);
- diedRE2 = new RegExp(/^(.+?)\[[0-9]+\/[0-9]+\] was killed by (\w+)/);
- function logRconData(msg) {
- if((data = clientdataRE.exec(msg))) {
- var remainder = data[4];
- if(/.* joined \[.*\//.test(remainder)) {
- log(msg, 'hgreen');
- } else if (/.* kicked\: .*/.test(remainder)) {
- log(msg, 'yellow');
- } else if (/.* Rejecting connection \- .*/.test(remainder)) {
- log(msg, 'yellow');
- } else if (/.* disconnecting\: {0,1}.*/.test(remainder)) {
- log(msg, 'green');
- } else {
- log(msg, 'hblue');
- }
- } else if (/^\[CHAT\]/.test(msg)) {
- log(msg, 'hcyan');
- } else if (/^\[event\]/.test(msg)) {
- log(msg, 'magenta');
- } else if((names = killedRE.exec(msg))) {
- log(msg, 'hyellow');
- if(rconData.killFeed) {
- log('ECHOED KILL ' + names[2] + ' a butté ' + names[1], 'yellow');
- rcon.Command('say <color=red>' + names[2] + '</color><color=green> a butté </color><color=red>' + names[1] + '</color> ');
- }
- } else if((names = killedNpcRE.exec(msg))) {
- log(msg, 'yellow');
- if(rconData.killFeed) {
- var entity = names[2];
- var npc = names[3];
- var reason = ' a butté ';
- if(npc == 'wolf') {
- npc = 'Un loup';
- reason = ' a bouffé ';
- } else if(npc == 'bear') {
- npc = 'Un ours';
- reason = ' a bouffé ';
- } else if(npc == 'entity') {
- if(
- entity == 'barricade.wood' ||
- entity == 'barricade.woodwire' ||
- entity == 'wall.external.high.wood' ||
- entity == 'wall.external.high.stone'||
- entity == 'gate.external.high.wood' ||
- entity == 'gate.external.high.stone'
- ) {
- npc = 'Des fils barbelés';
- reason = ' ont déchiré ';
- } else if(entity == 'beartrap') {
- npc = 'Un piège à ours';
- reason = ' a broyé ';
- } else if(entity == 'landmine') {
- npc = 'Une mine';
- reason = ' a explosé '
- } else if(entity == 'spikes.floor') {
- npc = 'Des pieux';
- reason = ' ont empalé ';
- } else if(entity == 'patrolhelicopter') {
- npc = 'L\'hélicoptère';
- reason = ' a éclaté ';
- } else if(entity == 'lock.code') {
- npc = 'Un verrou';
- reason = ' a electrocuté ';
- } else if(entity == 'autoturret') {
- npc = 'Une tourelle';
- reason = ' a mitraillé ';
- } else {
- npc = entity;
- }
- }
- log('ECHOED KILL ' + npc + reason + names[1], 'yellow');
- rcon.Command('say <color=green>' + npc + reason + '</color> <color=red>' + names[1] + '</color>');
- }
- } else if ((names = diedRE.exec(msg)) || (names = diedRE2.exec(msg))) {
- log(msg, 'yellow');
- if(rconData.killFeed) {
- var reason = names[2];
- if(reason == 'Bite') {
- reason = ' s\'est fait bouffé';
- } else if (reason == 'Cold') {
- reason = ' est devenu un glaçon';
- } else if (reason == 'Bullet') {
- reason = ' a pris une balle et en est mort';
- } else if (reason == 'Blunt') {
- reason = ' a explosé';
- } else if (reason == 'Fall') {
- reason = ' a sauté d\'un peu trop haut';
- } else if (reason == 'Drowned') {
- reason = ' s\'est noyé';
- } else if (reason == 'Bleeding') {
- reason = ' s\'est vidé de son sang';
- } else if (reason == 'Poison') {
- reason = ' aurait du passer chez le pharmacien avant de manger';
- } else if (reason == 'Thirst' || reason == 'Hunger') {
- reason = ' a oublié de manger';
- } else if (reason == 'Radiation') {
- reason = ' a pris trop de Becquerel';
- } else if (reason == 'Suicide') {
- return;
- } else {
- reason = ' et mort de ' + names[2];
- }
- log('ECHOED KILL ' + names[1] + reason, 'yellow');
- rcon.Command('say <color=red>' + names[1] + '</color><color=green>' + reason + '</color>');
- }
- } else {
- log(msg);
- }
- }
- function autoCompleterQuery(line, callback) {
- autoCompleterCurrentLinePartial = line;
- autoCompleterCurrentCallBack = callback;
- var findItem = null;
- var findFunction = null;
- if(/^\s*$/.test(line)) {
- autoCompleterCurrentRegExp = '\\n ((?:[\\w]+\\.)?'+autoCompleterCurrentLinePartial+'[\\w\\.]*)';
- findFunction = 'find ';
- findItem = '.';
- } else if(/^[\w\.]+$/.test(line)) {
- autoCompleterCurrentRegExp = '\\n ((?:[\\w]+\\.)?'+autoCompleterCurrentLinePartial+'[\\w\\.]*)';
- findFunction = 'find ';
- findItem = line;
- } else if(matches = /^([\w\.]+)\s*("\.*)$/.exec(line)) {
- autoCompleterCurrentRegExp = '\\n\\d+ ('+matches[2]+'\\.*")';
- autoCompleterCurrentLinePartial = matches[2];
- findFunction = 'status';
- findItem = '';
- } else if(matches = /^([\w\.]+)\s*(\d*)$/.exec(line)) {
- autoCompleterCurrentRegExp = '\\n('+matches[2]+'\\d*)\\s';
- autoCompleterCurrentLinePartial = matches[2];
- findFunction = 'status';
- findItem = '';
- }
- if(findFunction)
- RunCommand(findFunction + findItem, autoCompleterQueryCallback);
- else
- callback(null, [[], line]);
- }
- function autoCompleterQueryCallback(msg) {
- var re = new RegExp(autoCompleterCurrentRegExp, 'gi');
- var result = msg.match(re);
- autoCompleterCurrentCallBack(null, [result, autoCompleterCurrentLinePartial]);
- }
- var program = require('commander');
- program
- .usage('[command]')
- .option('-a, --address <address>', 'The the RCON server address')
- .option('-p, --port <port>', 'The the RCON server port')
- .option('-P, --password <password>', 'The RCON server password')
- .option('-k, --kill-feed', 'Kill Feed')
- .option('-x, --no-retry', 'Disable retry')
- .parse(process.argv);
- if (!program.address || !program.port || !program.password) {
- program.outputHelp();
- process.exit(1);
- }
- rconData = {};
- rconData.addr = program.address;
- rconData.port = program.port;
- rconData.pass = program.password;
- rconData.noRetry = program.noRetry;
- rconData.killFeed = program.killFeed;
- if(program.args && program.args != "") {
- log("Sending command: " + program.args.join(' '));
- RunCommandOnce(program.args.join(' '));
- } else {
- rcon = new RconService(rconData);
- rcon.defaultListener = function(msg) {logRconData(msg)};
- rcon.Connect();
- var readline = require('readline');
- var rl = readline.createInterface({
- input: process.stdin,
- output: process.stdout,
- completer: autoCompleterQuery,
- terminal: true
- });
- rl.on('line', function(l) {
- if(l != "") {
- log("Sending command: " + l, 'hwhite');
- RunCommand(l)
- }
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment