From 156b98395dd72055575d4bf9959c30f8c94f3c87 Mon Sep 17 00:00:00 2001 From: SweetieRick <65025304+SweetieRick@users.noreply.github.com> Date: Sun, 17 Apr 2022 13:15:59 +0200 Subject: [PATCH 1/4] Added code documentation for Rcon functions --- .gitignore | 1 + node-rcon.js | 52 ++++++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 43 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index 3c3629e..e867ec6 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules +service \ No newline at end of file diff --git a/node-rcon.js b/node-rcon.js index b122b87..a85ac4d 100644 --- a/node-rcon.js +++ b/node-rcon.js @@ -19,10 +19,15 @@ var PacketType = { }; /** - * options: - * tcp - true for TCP, false for UDP (optional, default true) - * challenge - if using UDP, whether to use the challenge protocol (optional, default true) - * id - RCON id to use (optional) + * Rcon, opens a RCON connection used by Valve products and GoldSrc, also implemented into the Minecraft Protocol + * @param {string} host - A String representing the IP address of the server you are trying to connect to + * @param {number} port - The RCON port of the server, configurable in the server.properties on the server + * @param {string} password - The RCON password to authenticate into the server. Please avoid leaving this empty + * @param {{tcp: boolean, challenge: boolean, id: any}} options - Connection options; + * `tcp` - true for TCP, false for UDP (optional, default true) + * `challenge` - if using UDP, whether to use the challenge protocol (optional, default true) + * `id` - RCON id to use (optional) + * @returns a connection buffer */ function Rcon(host, port, password, options) { if (!(this instanceof Rcon)) return new Rcon(host, port, password, options); @@ -40,8 +45,15 @@ function Rcon(host, port, password, options) { events.EventEmitter.call(this); }; -util.inherits(Rcon, events.EventEmitter); +util.extends(Rcon, events.EventEmitter); +/** + * Sends a data packet to the server with the RCON connection + * @param {any} data - The data packet sent to the server (optional) + * @param {string} cmd - The command to execute on the server. Note: for Minecraft, commands don't need to be prefixed by a slash (/) + * @param {any} id - RCON id to use (optional) + * @returns + */ Rcon.prototype.send = function(data, cmd, id) { var sendBuf; if (this.tcp) { @@ -71,14 +83,25 @@ Rcon.prototype.send = function(data, cmd, id) { this._sendSocket(sendBuf); }; +/** + * Sends and establishes a socket connection with the server, either on TCP or UDP connections + * @param {*} buf - The buffer inherited from the connection + */ Rcon.prototype._sendSocket = function(buf) { - if (this._tcpSocket) { - this._tcpSocket.write(buf.toString('binary'), 'binary'); - } else if (this._udpSocket) { - this._udpSocket.send(buf, 0, buf.length, this.port, this.host); - } + if (this._udpSocket) + return this._udpSocket.send(buf, 0, buf.length, this.port, this.host); + this._tcpSocket.write(buf.toString('binary'), 'binary'); }; +/** + * Connects to the server. It will return immediately, and if you try to send a command here, it will fail since the connection isn't authenticated yet. Wait for the 'auth' event. + * @example + * var conn = new Rcon('localhost', 1234, 'password'); + * conn.on("auth", function() { + * ... + * }) + * rcon.connect() + */ Rcon.prototype.connect = function() { var self = this; @@ -98,11 +121,20 @@ Rcon.prototype.connect = function() { } }; +/** + * Disconnects from the server. + */ Rcon.prototype.disconnect = function() { if (this._tcpSocket) this._tcpSocket.end(); if (this._udpSocket) this._udpSocket.close(); }; +/** + * Sets a timeout for the connection if it fails to reach the server + * @param {number} timeout - The time frame that if it is exceeded, the connection will drop + * @param {() => {}} callback - A function to execute as a callback + * @returns a function callback + */ Rcon.prototype.setTimeout = function(timeout, callback) { if (!this._tcpSocket) return; From 8193f2576e17d0f0c5b5ddbdfb3409fa37375e90 Mon Sep 17 00:00:00 2001 From: SweetieRick <65025304+SweetieRick@users.noreply.github.com> Date: Sun, 17 Apr 2022 19:25:46 +0200 Subject: [PATCH 2/4] Re-instantiated inherit func + reordered send args --- node-rcon.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/node-rcon.js b/node-rcon.js index a85ac4d..0c1450f 100644 --- a/node-rcon.js +++ b/node-rcon.js @@ -45,16 +45,16 @@ function Rcon(host, port, password, options) { events.EventEmitter.call(this); }; -util.extends(Rcon, events.EventEmitter); +util.inherits(Rcon, events.EventEmitter); /** * Sends a data packet to the server with the RCON connection - * @param {any} data - The data packet sent to the server (optional) * @param {string} cmd - The command to execute on the server. Note: for Minecraft, commands don't need to be prefixed by a slash (/) + * @param {any} data - The data packet sent to the server (optional) * @param {any} id - RCON id to use (optional) * @returns */ -Rcon.prototype.send = function(data, cmd, id) { +Rcon.prototype.send = function(cmd, data, id) { var sendBuf; if (this.tcp) { cmd = cmd || PacketType.COMMAND; @@ -248,4 +248,4 @@ Rcon.prototype.socketOnEnd = function() { this.hasAuthed = false; }; -module.exports = Rcon; +module.exports = Rcon; \ No newline at end of file From f6bd01309e9f55d63e1c0610109713c6f4732645 Mon Sep 17 00:00:00 2001 From: SweetieRick <65025304+SweetieRick@users.noreply.github.com> Date: Sun, 17 Apr 2022 19:30:42 +0200 Subject: [PATCH 3/4] Added type annotations documentation for node-rcon --- node-rcon.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/node-rcon.js b/node-rcon.js index 0c1450f..9226048 100644 --- a/node-rcon.js +++ b/node-rcon.js @@ -54,7 +54,7 @@ util.inherits(Rcon, events.EventEmitter); * @param {any} id - RCON id to use (optional) * @returns */ -Rcon.prototype.send = function(cmd, data, id) { +Rcon.prototype.send = function(data, cmd, id) { var sendBuf; if (this.tcp) { cmd = cmd || PacketType.COMMAND; From b416f2d19f3b91ca546b95fa1f920edc8138dbcc Mon Sep 17 00:00:00 2001 From: SweetieRick <65025304+SweetieRick@users.noreply.github.com> Date: Wed, 20 Apr 2022 22:19:37 +0200 Subject: [PATCH 4/4] Updated to match the request of the Owner Issue #1 and Issue #3 have been addressed and improved upon accordingly, waiting for `_sendSocket` issue to resolve --- node-rcon.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/node-rcon.js b/node-rcon.js index 9226048..77a5012 100644 --- a/node-rcon.js +++ b/node-rcon.js @@ -21,7 +21,7 @@ var PacketType = { /** * Rcon, opens a RCON connection used by Valve products and GoldSrc, also implemented into the Minecraft Protocol * @param {string} host - A String representing the IP address of the server you are trying to connect to - * @param {number} port - The RCON port of the server, configurable in the server.properties on the server + * @param {number} port - The RCON port of the server. Note: For Minecraft, this is specified in the server.properties file under the "rcon.port=..." entry * @param {string} password - The RCON password to authenticate into the server. Please avoid leaving this empty * @param {{tcp: boolean, challenge: boolean, id: any}} options - Connection options; * `tcp` - true for TCP, false for UDP (optional, default true) @@ -83,10 +83,6 @@ Rcon.prototype.send = function(data, cmd, id) { this._sendSocket(sendBuf); }; -/** - * Sends and establishes a socket connection with the server, either on TCP or UDP connections - * @param {*} buf - The buffer inherited from the connection - */ Rcon.prototype._sendSocket = function(buf) { if (this._udpSocket) return this._udpSocket.send(buf, 0, buf.length, this.port, this.host); @@ -248,4 +244,4 @@ Rcon.prototype.socketOnEnd = function() { this.hasAuthed = false; }; -module.exports = Rcon; \ No newline at end of file +module.exports = Rcon;