From ea29eb34b0de67b7ee8470898d5cf5049186b89d Mon Sep 17 00:00:00 2001 From: c43721 Date: Sun, 2 Jun 2024 14:24:22 -0500 Subject: [PATCH] chore(docs,example): add example, only publish core files, more docs --- .npmignore | 2 -- README.md | 18 ++++++++++++++---- examples/using-passwords.mjs | 16 ++++++++++++++++ jsr.json | 4 ++-- src/index.ts | 2 +- src/logReceiver.ts | 31 ++++++++++++++++++++++++++++--- 6 files changed, 61 insertions(+), 12 deletions(-) delete mode 100644 .npmignore create mode 100644 examples/using-passwords.mjs diff --git a/.npmignore b/.npmignore deleted file mode 100644 index 04c01ba..0000000 --- a/.npmignore +++ /dev/null @@ -1,2 +0,0 @@ -node_modules/ -dist/ \ No newline at end of file diff --git a/README.md b/README.md index a0f5303..41f70f1 100644 --- a/README.md +++ b/README.md @@ -4,15 +4,15 @@ Wrapper around [Source Dedicated Server](https://developer.valvesoftware.com/wik ## Set Up -Your server must have logs enabled. To do so, run `log on` in your server console or through RCON. In order to receieve messages, you must also add a log address using `logaddress_add` command to add the URL or IP of your server's receiever. To delete a destination, use `logaddress_del`. +Your server must have logs enabled. To do so, run `log on` in your server console or through RCON. In order to receieve messages, you must also add a log address using `logaddress_add` command to add the URL or IP of your server's receiver. To delete a destination, use `logaddress_del`. You can always list existing addresses using `logaddress_list`. -# Installation +## Installation See https://jsr.io/@c43721/srcds-log-receiver for more details. -# Usage +### Usage You can define a receiver and listen to messages published to the UDP stream. @@ -29,8 +29,18 @@ console.log("Log receiver running.. "); receiver.on("event", (message) => console.log(message)); ``` +### Security + +In order to make this as extendable as possible, there is no built in security. However, you can mitigate this by using `sv_logsecret` and checking for the password in the packet. Otherwise, you can use the Socket information to determine what IPs sent the message. + +### Examples + For more examples, see documentation or the examples folder. -# Contributing +## Contributing If there's a feature or bug, please raise a github issue first alongside your PR (if you're kind enough to make a PR.) + +## License + +Distributed under the MIT License. See [LICENSE](LICENSE) for more information. diff --git a/examples/using-passwords.mjs b/examples/using-passwords.mjs new file mode 100644 index 0000000..a228a2f --- /dev/null +++ b/examples/using-passwords.mjs @@ -0,0 +1,16 @@ +import { LogReceiver } from "@c43721/srcds-log-receiver"; + +const receiver = new LogReceiver({ + address: "0.0.0.0", + port: 9871, +}); + +console.log("Log receiver running.. "); + +receiver.on("event", (message) => { + if (message.password === null) { + return; + } else if (message.password === "mysuperdupersecret") { + return console.log(message); + } +}); diff --git a/jsr.json b/jsr.json index 8ae8c9d..ee70907 100644 --- a/jsr.json +++ b/jsr.json @@ -1,8 +1,8 @@ { "name": "@c43721/srcds-log-receiver", - "version": "1.1.2", + "version": "1.1.3", "exports": "./src/index.ts", "publish": { - "exclude": ["./examples", "./.github"] + "include": ["jsr.json", "LICENSE", "README.md", "src/**/*.ts"] } } diff --git a/src/index.ts b/src/index.ts index 7cc54d8..64e852a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,2 +1,2 @@ export { LogReceiver, type LogReceiverOptions } from "./logReceiver"; -export { EventData } from "./types"; +export { type EventData } from "./types"; diff --git a/src/logReceiver.ts b/src/logReceiver.ts index d7066ff..c0b6379 100644 --- a/src/logReceiver.ts +++ b/src/logReceiver.ts @@ -10,14 +10,14 @@ import { EventData } from "./types"; export interface LogReceiverOptions { /** * The address to listen on - * + * * @default "0.0.0.0" */ address?: string; /** * The port to use - * + * * @default 9871 */ port?: number; @@ -71,6 +71,29 @@ export interface LogReceiverOptions { * * While not required, there may be times where you have short-lived receivers. Since each instance creates a socket, closing those sockets becomes a problem. Abort controllers help with that by providing a way to explicitly close the resources for you * + * @example Using the password and IP to verify the source of the data + * ```ts + * import { LogReceiver} from "@c43721/srcds-log-receiver"; + * + * const receiver = new LogReceiver({ + * address: "0.0.0.0", + * port: 9871, + * }); + * + * receiver.on("event", (message) => { + * if (message.password === null) { + * return; + * } + * + * if (message.password === "mysuperdupersecretlogpassword") { + * await doSomethingWithTheMessage(message); + * } + * }); + * + * ``` + * + * For security reasons, you should always use a log secret to prevent evaluation of potentially malicious messages. Do this by looking at the password field. In order to set up the log secret, you can use the `sv_logsecret` command + * */ export class LogReceiver extends EventEmitter { #socket: Socket; @@ -100,7 +123,9 @@ export class LogReceiver extends EventEmitter { this.#socket.on("connect", () => this.emit("connect")); this.#socket.on("error", (error) => this.emit("error", error)); this.#socket.on("listening", () => this.emit("listening")); - this.#socket.on("message", (buffer, serverInfo) => this.#handleMessage(buffer, serverInfo)); + this.#socket.on("message", (buffer, serverInfo) => + this.#handleMessage(buffer, serverInfo) + ); } #handleMessage(buffer: Buffer, serverInfo: RemoteInfo) {