Skip to content

Commit

Permalink
Add option to skip token validation
Browse files Browse the repository at this point in the history
Signed-off-by: Jens Reidel <[email protected]>
  • Loading branch information
Gelbpunkt committed Sep 27, 2023
1 parent e41d6a4 commit d8257cd
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 48 deletions.
90 changes: 45 additions & 45 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Create a file `config.json` and fill in these fields as you wish:
},
"status": "idle",
"backpressure": 100,
"validate_token": true,
"externally_accessible_url": "ws://localhost:7878",
"cache": {
"channels": false,
Expand All @@ -50,7 +51,7 @@ Create a file `config.json` and fill in these fields as you wish:
}
```

You can omit the `token` key entirely and set the `TOKEN` environment variable when running to avoid putting credentials in the configuration file.
You can omit the `token` key entirely and set the `TOKEN` environment variable when running to avoid putting credentials in the configuration file. Client tokens will be validated to match the one configured unless `validate_token` is set to `false`.

By default, the total shard count will be calculated using the `/api/gateway/bot` endpoint. If you want to change this, set `shards` to the amount of shards. It will also launch all shards by default, you can customize this to launch only a range of shards using `shard_start` and `shard_end` (start inclusive, end exclusive).

Expand Down
6 changes: 6 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ pub struct Config {
pub status: Status,
#[serde(default = "default_backpressure")]
pub backpressure: usize,
#[serde(default = "default_validate_token")]
pub validate_token: bool,
#[serde(default)]
pub twilight_http_proxy: Option<String>,
pub externally_accessible_url: String,
Expand Down Expand Up @@ -199,6 +201,10 @@ const fn default_backpressure() -> usize {
100
}

const fn default_validate_token() -> bool {
true
}

pub enum Error {
InvalidConfig(JsonError),
NotFound(String),
Expand Down
8 changes: 6 additions & 2 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,9 @@ pub async fn handle_client<S: 'static + AsyncRead + AsyncWrite + Unpin + Send>(
}

// Discord tokens may be prefixed by 'Bot ' in IDENTIFY
if identify.d.token.split_whitespace().last() != Some(&CONFIG.token) {
if CONFIG.validate_token
&& identify.d.token.split_whitespace().last() != Some(&CONFIG.token)
{
warn!("[{addr}] Token from client mismatched, disconnecting");
break;
}
Expand Down Expand Up @@ -305,7 +307,9 @@ pub async fn handle_client<S: 'static + AsyncRead + AsyncWrite + Unpin + Send>(
};

// Discord tokens may be prefixed by 'Bot ' in RESUME
if resume.d.token.split_whitespace().last() != Some(&CONFIG.token) {
if CONFIG.validate_token
&& resume.d.token.split_whitespace().last() != Some(&CONFIG.token)
{
warn!("[{addr}] Token from client mismatched, disconnecting");
break;
}
Expand Down

0 comments on commit d8257cd

Please sign in to comment.