Skip to content

Commit

Permalink
chore: jsr
Browse files Browse the repository at this point in the history
  • Loading branch information
c43721 committed Oct 21, 2024
1 parent 5f0e676 commit 66d7558
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 25 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Publish to JSR
on:
push:
branches:
- main

jobs:
publish:
runs-on: ubuntu-latest

permissions:
contents: read
id-token: write

steps:
- uses: actions/checkout@v4

- name: Publish package
run: npx jsr publish
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2021 rcon.tf
Copyright (c) 2024 rcon.tf

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# deno-rcon-srcds
rcon-srcds, but in deno
# rcon


5 changes: 5 additions & 0 deletions deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"imports": {
"@std/io": "jsr:@std/io@^0.225.0"
}
}
46 changes: 46 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions jsr.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "@c43721/rcon",
"version": "0.0.1-alpha1",
"exports": "./mod.ts"
}
2 changes: 1 addition & 1 deletion src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class UnableToAuthenicateException extends Error {
export class NotAuthorizedException extends Error {
constructor() {
super();
this.message = "Not authorized. Please authenticate first.";
this.message = "Not authenticated";
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/packet.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Buffer from "https://deno.land/[email protected]/node/buffer.ts";
import { Buffer } from "node:buffer";

/**
* Encode data to packet buffer
Expand Down
3 changes: 3 additions & 0 deletions src/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ const protocol = {
SERVERDATA_RESPONSE_VALUE: 0x00,

ID_AUTH: 0x999,

ID_REQUEST: 0x123,

ID_TERM: 0x777,
} as const;

export default protocol;
49 changes: 29 additions & 20 deletions src/rcon.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import protocol from "./protocol.ts";
import Buffer from "https://deno.land/[email protected]/node/buffer.ts";
import { readAll } from "https://deno.land/std/streams/conversion.ts";
import { Buffer } from "node:buffer";
import { readAll } from "@std/io";
import { encode, decode } from "./packet.ts";
import {
AlreadyAuthenicatedException,
Expand Down Expand Up @@ -44,9 +44,9 @@ export default class Rcon {
* Authenticates the connection
* @param password Password string
*/
async authenticate(password: string): Promise<boolean> {
public async authenticate(password: string): Promise<boolean> {
if (!this.connected) {
await this.connect();
await this.#connect();
}

return new Promise((resolve, reject) => {
Expand All @@ -55,7 +55,7 @@ export default class Rcon {
return;
}

this.write(protocol.SERVERDATA_AUTH, protocol.ID_AUTH, password)
this.#write(protocol.SERVERDATA_AUTH, protocol.ID_AUTH, password)
.then((data) => {
if (data === true) {
this.authenticated = true;
Expand All @@ -73,7 +73,7 @@ export default class Rcon {
* Executes command on the server
* @param command Command to execute
*/
execute(command: string): Promise<string | boolean> {
public execute(command: string): Promise<string | boolean> {
return new Promise((resolve, reject) => {
if (!this.connected) {
reject(new NotAuthorizedException());
Expand All @@ -86,7 +86,7 @@ export default class Rcon {
return;
}

this.write(protocol.SERVERDATA_EXECCOMMAND, packetId, command)
this.#write(protocol.SERVERDATA_EXECCOMMAND, packetId, command)
.then(resolve)
.catch(reject);
});
Expand All @@ -95,7 +95,7 @@ export default class Rcon {
/**
* Creates a connection to the socket
*/
private async connect(): Promise<void> {
async #connect(): Promise<void> {
this.connection = await Deno.connect({
hostname: this.host,
port: this.port,
Expand All @@ -105,7 +105,7 @@ export default class Rcon {
/**
* Destroys the socket connection
*/
disconnect() {
public disconnect() {
this.authenticated = false;
this.connected = false;
this.connection.close();
Expand All @@ -125,11 +125,7 @@ export default class Rcon {
* @param id Packet ID
* @param body Packet payload
*/
private write(
type: number,
id: number,
body: string
): Promise<string | boolean> {
#write(type: number, id: number, body: string): Promise<string | boolean> {
// deno-lint-ignore no-async-promise-executor
return new Promise(async (resolve, reject) => {
let response = "";
Expand Down Expand Up @@ -163,13 +159,26 @@ export default class Rcon {
} else {
resolve(false);
}
} else if (id === decodedPacket.id) {
// remove last line break
response = response.concat(decodedPacket.body.replace(/\n$/, "\n"));
} else if (
id === decodedPacket.id ||
decodedPacket.id === protocol.ID_TERM
) {
if (decodedPacket.id != protocol.ID_TERM) {
response = response.concat(decodedPacket.body.replace(/\n$/, "\n")); // remove last line break
}

// Check the response if it's defined rather than if it contains 'command ${body}'
// Reason for this is because we no longer need to check if it starts with 'command', testing shows it never will
if (response) {
// Hack to cope with multipacket responses
// see https://developer.valvesoftware.com/wiki/Talk:Source_RCON_Protocol#How_to_receive_split_response?
if (decodedPacket.size > 3700) {
const encodedTerminationPacket = encode(
protocol.SERVERDATA_RESPONSE_VALUE,
protocol.ID_TERM,
""
);

this.connection.write(encodedTerminationPacket);
} else if (decodedPacket.size <= 3700) {
// no need to check for ID_TERM here, since this packet will always be < 3700
resolve(response);
}
}
Expand Down

0 comments on commit 66d7558

Please sign in to comment.