@hidev/udp-rcon
Version:
This node module lets you communicate with a server through the UDP RCON protocol.
112 lines (111 loc) • 4.51 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.UDP_EVENTS_MANAGER = exports.UDP_INSTANCE = exports.UDP_RCON = void 0;
var dgram = require("dgram");
var UDP_RCON = /** @class */ (function () {
function UDP_RCON(_ip, _port, _password) {
this.ip = _ip;
this.port = _port;
this.password = _password;
}
UDP_RCON.prototype.send = function (cmd, success_cb, error_cb) {
// Set a new events instance
var udp_instance = new UDP_INSTANCE();
success_cb && udp_instance.client.setEvent("message", success_cb);
error_cb && udp_instance.client.setEvent("error", error_cb);
// Create RCON buffer
var buffer = this.initBuffer(cmd);
return this.sendUDPSocket(buffer, udp_instance);
};
UDP_RCON.prototype.initBuffer = function (data) {
// TODO: Challenge token
var socketData = "rcon " + this.password + " " + data;
var dataLength = Buffer.byteLength(socketData);
// Set and allocate buffer size
var bufferSize = dataLength + 0x06; // 32 bits length + 16 bits empty string => 6 bytes
var buffer = Buffer.alloc(bufferSize);
// Write buffer
// Endianness not important here
buffer.writeUInt32LE(0xffffffff, 0x00);
buffer.write(socketData, 0x04);
buffer.writeInt16LE(0x00, bufferSize - 0x02);
// console.log(buffer);
// <Buffer ff ff ff ff 72 63 6f 6e 20 70 61 73 73 77 72 64 20 73 61 79 20 42 6f 6e 6a 6f 75 72 21 00 00>
return buffer;
};
UDP_RCON.prototype.sendUDPSocket = function (buffer, udp_instance) {
var clientSocket = dgram.createSocket("udp4");
clientSocket.on("error", function (err) {
clientSocket.close();
udp_instance.client.callEvent("error", err);
});
clientSocket.on("message", function (msg) {
var slicedMsg = msg.slice(4); // Remove first 4 bytes
clientSocket.close();
udp_instance.client.callEvent("message", slicedMsg.toString());
});
clientSocket.on("listening", function () {
var address = clientSocket.address();
udp_instance.client.callEvent("listening", address);
});
// Send to RCON
clientSocket.send(buffer, this.port, this.ip, function (err) {
udp_instance.sender.callEvent("connect");
udp_instance.sender.callEvent("sended", buffer);
err && udp_instance.sender.callEvent("error", err);
});
return udp_instance;
};
return UDP_RCON;
}());
exports.UDP_RCON = UDP_RCON;
var UDP_INSTANCE = /** @class */ (function () {
function UDP_INSTANCE() {
this.sender = new UDP_SENDER();
this.client = new UDP_CLIENT();
}
return UDP_INSTANCE;
}());
exports.UDP_INSTANCE = UDP_INSTANCE;
var UDP_EVENTS_MANAGER = /** @class */ (function () {
function UDP_EVENTS_MANAGER() {
this.events = {};
}
UDP_EVENTS_MANAGER.prototype.setEvent = function (name, event) {
this.events[name] = event;
};
UDP_EVENTS_MANAGER.prototype.callEvent = function (name, args) {
this.events[name] && this.events[name](args);
};
return UDP_EVENTS_MANAGER;
}());
exports.UDP_EVENTS_MANAGER = UDP_EVENTS_MANAGER;
var UDP_SENDER = /** @class */ (function (_super) {
__extends(UDP_SENDER, _super);
function UDP_SENDER() {
return _super.call(this) || this;
}
return UDP_SENDER;
}(UDP_EVENTS_MANAGER));
var UDP_CLIENT = /** @class */ (function (_super) {
__extends(UDP_CLIENT, _super);
function UDP_CLIENT() {
return _super.call(this) || this;
}
return UDP_CLIENT;
}(UDP_EVENTS_MANAGER));