Skip to content

Commit

Permalink
feat: add timeout to requests
Browse files Browse the repository at this point in the history
  • Loading branch information
c43721 committed Nov 13, 2024
1 parent 326822b commit 1958178
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 54 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.vscode
.vscode
deno.lock
1 change: 1 addition & 0 deletions cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ if (!args.password || !args.ip || !args.command) {
using rcon = new Rcon({
host: args.ip,
port,
timeout: 5_000,
});

const didAuthenticate = await rcon.authenticate(args.password!);
Expand Down
4 changes: 2 additions & 2 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"imports": {
"@std/bytes": "jsr:@std/bytes@^1.0.2",
"@std/cli": "jsr:@std/cli@^1.0.6",
"@std/io": "jsr:@std/io@^0.225.0"
"@std/async": "jsr:@std/async@^1.0.8"
},
"version": "0.0.6",
"version": "0.0.7",
"exports": "./mod.ts",
"publish": {
"include": ["README.md", "mod.ts", "src/"]
Expand Down
41 changes: 0 additions & 41 deletions deno.lock

This file was deleted.

24 changes: 14 additions & 10 deletions src/rcon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { protocol } from "./protocol.ts";
import { concat } from "@std/bytes";
import { createConnection, type Socket } from "node:net";
import { encode, decode } from "./packet.ts";
import { abortable } from "@std/async";
import {
NotAuthenticatedException,
NotConnectedException,
Expand Down Expand Up @@ -34,6 +35,8 @@ import type { RconOptions } from "./types.ts";
export class Rcon {
#host: string;
#port: number;
#timeout: number;

#connection?: Socket;
#connected = false;
#authenticated = false;
Expand All @@ -44,10 +47,11 @@ export class Rcon {
* @param {RconOptions} options The connection options
*/
constructor(options: RconOptions) {
const { host, port = 27015 } = options;
const { host, port = 27015, timeout = 30_000 } = options;

this.#host = host;
this.#port = port;
this.#timeout = timeout;
}

/**
Expand All @@ -74,18 +78,17 @@ export class Rcon {
/**
* Authenticates the connection
* @param password The RCON password
*
*
* @returns {Promise<boolean>} The result of the authentication
*/
public async authenticate(password: string): Promise<boolean> {
if (!this.#connected) {
this.#connect();
}

const response = await this.#send(
protocol.SERVERDATA_AUTH,
protocol.ID_AUTH,
password
const response = await abortable(
this.#send(protocol.SERVERDATA_AUTH, protocol.ID_AUTH, password),
AbortSignal.timeout(this.#timeout)
);

if (response === "true") {
Expand All @@ -100,7 +103,7 @@ export class Rcon {
/**
* Executes a command on the server
* @param command The command to execute
*
*
* @returns {Promise<string>} The result of the execution
*/
public async execute(command: string): Promise<string> {
Expand All @@ -114,7 +117,10 @@ export class Rcon {

const packetId = Math.floor(Math.random() * (256 - 1) + 1);

return await this.#send(protocol.SERVERDATA_EXECCOMMAND, packetId, command);
return await abortable(
this.#send(protocol.SERVERDATA_EXECCOMMAND, packetId, command),
AbortSignal.timeout(this.#timeout)
);
}

/**
Expand Down Expand Up @@ -185,7 +191,6 @@ export class Rcon {
(decodedPacket.type === protocol.SERVERDATA_RESPONSE_VALUE ||
decodedPacket.id === protocol.ID_TERM)
) {
// concat the response- even if it's not a multipacket response
if (decodedPacket.id != protocol.ID_TERM) {
potentialMultiPacketResponse = concat([
potentialMultiPacketResponse,
Expand All @@ -204,7 +209,6 @@ export class Rcon {

this.#connection!.write(encodedTerminationPacket);
} else if (decodedPacket.size <= 3700) {
// no need to check for ID_TERM here, since this packet will always be < 3700
return new TextDecoder().decode(potentialMultiPacketResponse);
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@ export interface RconOptions {
* (Optional- Default "27017") The port to connect to
*/
port?: number;

/**
* (Optional- Default "30000") The timeout in milliseconds before a request is aborted
*/
timeout?: number;
}

0 comments on commit 1958178

Please sign in to comment.