File size: 2,861 Bytes
bd98362
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123

'use strict';

function flush() {
  this.commands.forEach(function(command) {
    this.client.write(Packet.encode(0x02, Protocol.REQ_ID+1, command));
  }, this);
  this.commands = [];
}

function dequeue(response) {
  var callback = this.responseCallbacks.shift();
  if (typeof callback === 'function') {
    callback(response);
  }
}

function connect() {
  this.client = net.connect({
    host: this.host,
    port: this.port
  }, function() {
    this.emit('connected');
    this.client.write(Packet.encode(
      Protocol.SERVERDATA_AUTH,
      Protocol.AUTH_ID,
      this.password
    ));
  }.bind(this));

  this.client.setTimeout(this.timeout);
}

function listen() {
  this.client.on('data', function(data) {
    var res = Packet.decode(data);
    switch(res.type) {
      case Protocol.SERVERDATA_RESPONSE_VALUE:
        if (this.authenticated) {
          dequeue.call(this, res);
        }
        break;
      case Protocol.SERVERDATA_AUTH_RESPONSE:
        if (res.body !== '') {
          return this.emit('error', 'Error: Invalid RCON password');
        }
        this.authenticated = true;
        this.emit('authenticated');
        if (this.execOnAuthenticated) flush.call(this);
        break;
      default:
        this.emit('error', 'Error: Unknown server response');
        break;
    }
  }.bind(this));

  this.client.on('timeout', this.client.end.bind(this.client));
  this.client.on('end', this.emit.bind(this, 'disconnected'));

  this.client.on('error', function(e) {
    this.emit('error', e);
  }.bind(this));
}

var net = require('net'),
    EventEmitter = require('events').EventEmitter,
    inherits = require('util').inherits,
    Packet = require('./packet'),
    Protocol = require('./protocol');

var Rcon = function(options) {
  EventEmitter.call(this);
  options = options || {};
  this.host = options.host || '127.0.0.1';
  this.port = options.port || '27015';
  this.password = options.password;
  this.timeout = options.timeout || 5000;


  //
  // need a more succinct name for this option
  this.execOnAuthenticated = options.execOnAuthenticated || true;


  this.authenticated = false;
  this.responseCallbacks = [];
  this.commands = [];

  if (options.connect) {
    connect.call(this);
    listen.call(this);
  }
};

inherits(Rcon, EventEmitter);

Rcon.prototype.exec = function(command, callback) {
  if (this.authenticated) {
    this.client.write(Packet.encode(0x02, Protocol.REQ_ID, command));
  } else {
    this.commands.push(command);
  }
  this.responseCallbacks.push(callback);
  return this;
};

Rcon.prototype.connect = function() {
  this.emit('connecting');
  if (!this.client) {
    connect.call(this);
    listen.call(this);
  }
  return this;
};

Rcon.prototype.close = function() {
  this.emit('disconnecting');
  if (this.client) this.client.end();
  return this;
};

module.exports = Rcon;