Skip to content

Commit

Permalink
chore(docs,example): add example, only publish core files, more docs
Browse files Browse the repository at this point in the history
  • Loading branch information
c43721 committed Jun 2, 2024
1 parent 197693e commit ea29eb3
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 12 deletions.
2 changes: 0 additions & 2 deletions .npmignore

This file was deleted.

18 changes: 14 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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.
16 changes: 16 additions & 0 deletions examples/using-passwords.mjs
Original file line number Diff line number Diff line change
@@ -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);
}
});
4 changes: 2 additions & 2 deletions jsr.json
Original file line number Diff line number Diff line change
@@ -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"]
}
}
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { LogReceiver, type LogReceiverOptions } from "./logReceiver";
export { EventData } from "./types";
export { type EventData } from "./types";
31 changes: 28 additions & 3 deletions src/logReceiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit ea29eb3

Please sign in to comment.