filter: add whitelist filter
Fixes: https://github.com/damus-io/noteguard/issues/3 Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
parent
bae87107ae
commit
74c90453b8
5 changed files with 51 additions and 16 deletions
19
README.md
19
README.md
|
|
@ -14,12 +14,15 @@ You can add any new filter you want by implementing the `NoteFilter` trait and r
|
||||||
The `pipeline` config specifies the order in which filters are run. When the first `reject` or `shadowReject` action is hit, then the pipeline stops and returns the rejection error.
|
The `pipeline` config specifies the order in which filters are run. When the first `reject` or `shadowReject` action is hit, then the pipeline stops and returns the rejection error.
|
||||||
|
|
||||||
```toml
|
```toml
|
||||||
|
pipeline = ["whitelist", "ratelimit"]
|
||||||
pipeline = ["ratelimit"]
|
|
||||||
|
|
||||||
[filters.ratelimit]
|
[filters.ratelimit]
|
||||||
notes_per_minute = 8
|
posts_per_minute = 8
|
||||||
whitelist = ["127.0.0.1"]
|
whitelist = ["127.0.0.1"]
|
||||||
|
|
||||||
|
[filters.whitelist]
|
||||||
|
#pubkeys = ["32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245"]
|
||||||
|
ips = ["127.0.0.1", "127.0.0.2"]
|
||||||
```
|
```
|
||||||
|
|
||||||
## Filters
|
## Filters
|
||||||
|
|
@ -38,6 +41,16 @@ Settings:
|
||||||
|
|
||||||
- `whitelist`: a list of IP4 or IP6 addresses that are allowed to bypass the ratelimit.
|
- `whitelist`: a list of IP4 or IP6 addresses that are allowed to bypass the ratelimit.
|
||||||
|
|
||||||
|
## Whitelist
|
||||||
|
|
||||||
|
The whitelist filter only allows notes to pass if it matches a particular pubkey or source ip:
|
||||||
|
|
||||||
|
- `pubkeys`: a list of hex public keys to let through
|
||||||
|
|
||||||
|
- `ips`: a list of ip addresses to let through
|
||||||
|
|
||||||
|
Either criteria can match
|
||||||
|
|
||||||
## Testing
|
## Testing
|
||||||
|
|
||||||
You can test your filters like so:
|
You can test your filters like so:
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,10 @@
|
||||||
|
|
||||||
pipeline = ["ratelimit"]
|
pipeline = ["whitelist", "ratelimit"]
|
||||||
|
|
||||||
[filters.ratelimit]
|
[filters.ratelimit]
|
||||||
posts_per_minute = 8
|
posts_per_minute = 8
|
||||||
whitelist = ["127.0.0.1"]
|
whitelist = ["127.0.0.1"]
|
||||||
|
|
||||||
|
[filters.whitelist]
|
||||||
|
pubkeys = ["16c21558762108afc34e4ff19e4ed51d9a48f79e0c34531efc423d21ab435e93"]
|
||||||
|
ips = ["127.0.0.1"]
|
||||||
|
|
|
||||||
|
|
@ -1,23 +1,31 @@
|
||||||
use crate::{Action, InputMessage, NoteFilter, OutputMessage};
|
use crate::{Action, InputMessage, NoteFilter, OutputMessage};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize, Default)]
|
||||||
pub struct Whitelist {
|
pub struct Whitelist {
|
||||||
pub pubkeys: Vec<String>,
|
pub pubkeys: Option<Vec<String>>,
|
||||||
pub ips: Vec<String>,
|
pub ips: Option<Vec<String>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl NoteFilter for Whitelist {
|
impl NoteFilter for Whitelist {
|
||||||
fn filter_note(&mut self, msg: &InputMessage) -> OutputMessage {
|
fn filter_note(&mut self, msg: &InputMessage) -> OutputMessage {
|
||||||
if self.pubkeys.contains(&msg.event.pubkey) || self.ips.contains(&msg.source_info) {
|
if let Some(pubkeys) = &self.pubkeys {
|
||||||
OutputMessage::new(msg.event.id.clone(), Action::Accept, None)
|
if pubkeys.contains(&msg.event.pubkey) {
|
||||||
} else {
|
return OutputMessage::new(msg.event.id.clone(), Action::Accept, None);
|
||||||
OutputMessage::new(
|
}
|
||||||
msg.event.id.clone(),
|
|
||||||
Action::Reject,
|
|
||||||
Some("blocked: pubkey not on the whitelist".to_string()),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let Some(ips) = &self.ips {
|
||||||
|
if ips.contains(&msg.source_info) {
|
||||||
|
return OutputMessage::new(msg.event.id.clone(), Action::Accept, None);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
OutputMessage::new(
|
||||||
|
msg.event.id.clone(),
|
||||||
|
Action::Reject,
|
||||||
|
Some("blocked: pubkey/ip not on the whitelist".to_string()),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn name(&self) -> &'static str {
|
fn name(&self) -> &'static str {
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
use noteguard::filters::RateLimit;
|
use noteguard::filters::{RateLimit, Whitelist};
|
||||||
use noteguard::{Action, InputMessage, NoteFilter, OutputMessage};
|
use noteguard::{Action, InputMessage, NoteFilter, OutputMessage};
|
||||||
use serde::de::DeserializeOwned;
|
use serde::de::DeserializeOwned;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
@ -41,6 +41,7 @@ impl Noteguard {
|
||||||
/// every new instance of [`Noteguard`]
|
/// every new instance of [`Noteguard`]
|
||||||
fn register_builtin_filters(&mut self) {
|
fn register_builtin_filters(&mut self) {
|
||||||
self.register_filter::<RateLimit>();
|
self.register_filter::<RateLimit>();
|
||||||
|
self.register_filter::<Whitelist>();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Run the loaded filters. You must call `load_config` before calling this, otherwise
|
/// Run the loaded filters. You must call `load_config` before calling this, otherwise
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,14 @@
|
||||||
|
{"type": "new","receivedAt":12345,"sourceType":"IP4","sourceInfo": "127.0.0.3","event":{"id": "68421a122cef086512b2c5bd29ca6285ced8bd8e302e347e3c5d90466c860a76","pubkey": "16c21558762108afc34e4ff19e4ed51d9a48f79e0c34531efc423d21ab435e93","created_at": 1720408658,"kind": 1,"tags": [],"content": "hi","sig": "7b76471744ded2b720ca832cdc89e670f6093ce38aeef55a5c6a4e077883d7d80dda1e9051032fb1faa1c3c212c517e93ee42b3ceac8e8e9b04bad46a361de90"}}
|
||||||
|
{"type": "new","receivedAt":12345,"sourceType":"IP4","sourceInfo": "127.0.0.3","event":{"id": "68421a122cef086512b2c5bd29ca6285ced8bd8e302e347e3c5d90466c860a76","pubkey": "16c21558762108afc34e4ff19e4ed51d9a48f79e0c34531efc423d21ab435e93","created_at": 1720408658,"kind": 1,"tags": [],"content": "hi","sig": "7b76471744ded2b720ca832cdc89e670f6093ce38aeef55a5c6a4e077883d7d80dda1e9051032fb1faa1c3c212c517e93ee42b3ceac8e8e9b04bad46a361de90"}}
|
||||||
|
{"type": "new","receivedAt":12345,"sourceType":"IP4","sourceInfo": "127.0.0.3","event":{"id": "68421a122cef086512b2c5bd29ca6285ced8bd8e302e347e3c5d90466c860a76","pubkey": "16c21558762108afc34e4ff19e4ed51d9a48f79e0c34531efc423d21ab435e93","created_at": 1720408658,"kind": 1,"tags": [],"content": "hi","sig": "7b76471744ded2b720ca832cdc89e670f6093ce38aeef55a5c6a4e077883d7d80dda1e9051032fb1faa1c3c212c517e93ee42b3ceac8e8e9b04bad46a361de90"}}
|
||||||
{"type": "new","receivedAt":12345,"sourceType":"IP4","sourceInfo": "127.0.0.1","event":{"id": "68421a122cef086512b2c5bd29ca6285ced8bd8e302e347e3c5d90466c860a76","pubkey": "16c21558762108afc34e4ff19e4ed51d9a48f79e0c34531efc423d21ab435e93","created_at": 1720408658,"kind": 1,"tags": [],"content": "hi","sig": "7b76471744ded2b720ca832cdc89e670f6093ce38aeef55a5c6a4e077883d7d80dda1e9051032fb1faa1c3c212c517e93ee42b3ceac8e8e9b04bad46a361de90"}}
|
{"type": "new","receivedAt":12345,"sourceType":"IP4","sourceInfo": "127.0.0.1","event":{"id": "68421a122cef086512b2c5bd29ca6285ced8bd8e302e347e3c5d90466c860a76","pubkey": "16c21558762108afc34e4ff19e4ed51d9a48f79e0c34531efc423d21ab435e93","created_at": 1720408658,"kind": 1,"tags": [],"content": "hi","sig": "7b76471744ded2b720ca832cdc89e670f6093ce38aeef55a5c6a4e077883d7d80dda1e9051032fb1faa1c3c212c517e93ee42b3ceac8e8e9b04bad46a361de90"}}
|
||||||
{"type": "new","receivedAt":12345,"sourceType":"IP4","sourceInfo": "127.0.0.1","event":{"id": "68421a122cef086512b2c5bd29ca6285ced8bd8e302e347e3c5d90466c860a76","pubkey": "16c21558762108afc34e4ff19e4ed51d9a48f79e0c34531efc423d21ab435e93","created_at": 1720408658,"kind": 1,"tags": [],"content": "hi","sig": "7b76471744ded2b720ca832cdc89e670f6093ce38aeef55a5c6a4e077883d7d80dda1e9051032fb1faa1c3c212c517e93ee42b3ceac8e8e9b04bad46a361de90"}}
|
{"type": "new","receivedAt":12345,"sourceType":"IP4","sourceInfo": "127.0.0.1","event":{"id": "68421a122cef086512b2c5bd29ca6285ced8bd8e302e347e3c5d90466c860a76","pubkey": "16c21558762108afc34e4ff19e4ed51d9a48f79e0c34531efc423d21ab435e93","created_at": 1720408658,"kind": 1,"tags": [],"content": "hi","sig": "7b76471744ded2b720ca832cdc89e670f6093ce38aeef55a5c6a4e077883d7d80dda1e9051032fb1faa1c3c212c517e93ee42b3ceac8e8e9b04bad46a361de90"}}
|
||||||
{"type": "new","receivedAt":12345,"sourceType":"IP4","sourceInfo": "127.0.0.1","event":{"id": "68421a122cef086512b2c5bd29ca6285ced8bd8e302e347e3c5d90466c860a76","pubkey": "16c21558762108afc34e4ff19e4ed51d9a48f79e0c34531efc423d21ab435e93","created_at": 1720408658,"kind": 1,"tags": [],"content": "hi","sig": "7b76471744ded2b720ca832cdc89e670f6093ce38aeef55a5c6a4e077883d7d80dda1e9051032fb1faa1c3c212c517e93ee42b3ceac8e8e9b04bad46a361de90"}}
|
{"type": "new","receivedAt":12345,"sourceType":"IP4","sourceInfo": "127.0.0.1","event":{"id": "68421a122cef086512b2c5bd29ca6285ced8bd8e302e347e3c5d90466c860a76","pubkey": "16c21558762108afc34e4ff19e4ed51d9a48f79e0c34531efc423d21ab435e93","created_at": 1720408658,"kind": 1,"tags": [],"content": "hi","sig": "7b76471744ded2b720ca832cdc89e670f6093ce38aeef55a5c6a4e077883d7d80dda1e9051032fb1faa1c3c212c517e93ee42b3ceac8e8e9b04bad46a361de90"}}
|
||||||
{"type": "new","receivedAt":12345,"sourceType":"IP4","sourceInfo": "127.0.0.2","event":{"id": "68421a122cef086512b2c5bd29ca6285ced8bd8e302e347e3c5d90466c860a76","pubkey": "16c21558762108afc34e4ff19e4ed51d9a48f79e0c34531efc423d21ab435e93","created_at": 1720408658,"kind": 1,"tags": [],"content": "hi","sig": "7b76471744ded2b720ca832cdc89e670f6093ce38aeef55a5c6a4e077883d7d80dda1e9051032fb1faa1c3c212c517e93ee42b3ceac8e8e9b04bad46a361de90"}}
|
{"type": "new","receivedAt":12345,"sourceType":"IP4","sourceInfo": "127.0.0.2","event":{"id": "68421a122cef086512b2c5bd29ca6285ced8bd8e302e347e3c5d90466c860a76","pubkey": "16c21558762108afc34e4ff19e4ed51d9a48f79e0c34531efc423d21ab435e93","created_at": 1720408658,"kind": 1,"tags": [],"content": "hi","sig": "7b76471744ded2b720ca832cdc89e670f6093ce38aeef55a5c6a4e077883d7d80dda1e9051032fb1faa1c3c212c517e93ee42b3ceac8e8e9b04bad46a361de90"}}
|
||||||
{"type": "new","receivedAt":12345,"sourceType":"IP4","sourceInfo": "127.0.0.2","event":{"id": "68421a122cef086512b2c5bd29ca6285ced8bd8e302e347e3c5d90466c860a76","pubkey": "16c21558762108afc34e4ff19e4ed51d9a48f79e0c34531efc423d21ab435e93","created_at": 1720408658,"kind": 1,"tags": [],"content": "hi","sig": "7b76471744ded2b720ca832cdc89e670f6093ce38aeef55a5c6a4e077883d7d80dda1e9051032fb1faa1c3c212c517e93ee42b3ceac8e8e9b04bad46a361de90"}}
|
{"type": "new","receivedAt":12345,"sourceType":"IP4","sourceInfo": "127.0.0.2","event":{"id": "68421a122cef086512b2c5bd29ca6285ced8bd8e302e347e3c5d90466c860a76","pubkey": "16c21558762108afc34e4ff19e4ed51d9a48f79e0c34531efc423d21ab435e93","created_at": 1720408658,"kind": 1,"tags": [],"content": "hi","sig": "7b76471744ded2b720ca832cdc89e670f6093ce38aeef55a5c6a4e077883d7d80dda1e9051032fb1faa1c3c212c517e93ee42b3ceac8e8e9b04bad46a361de90"}}
|
||||||
|
{"type": "new","receivedAt":12345,"sourceType":"IP4","sourceInfo": "127.0.0.2","event":{"id": "68421a122cef086512b2c5bd29ca6285ced8bd8e302e347e3c5d90466c860a76","pubkey": "16c21558762108afc34e4ff19e4ed51d9a48f79e0c34531efc423d21ab435e93","created_at": 1720408658,"kind": 1,"tags": [],"content": "hi","sig": "7b76471744ded2b720ca832cdc89e670f6093ce38aeef55a5c6a4e077883d7d80dda1e9051032fb1faa1c3c212c517e93ee42b3ceac8e8e9b04bad46a361de90"}}
|
||||||
|
{"type": "new","receivedAt":12345,"sourceType":"IP4","sourceInfo": "127.0.0.2","event":{"id": "68421a122cef086512b2c5bd29ca6285ced8bd8e302e347e3c5d90466c860a76","pubkey": "16c21558762108afc34e4ff19e4ed51d9a48f79e0c34531efc423d21ab435e93","created_at": 1720408658,"kind": 1,"tags": [],"content": "hi","sig": "7b76471744ded2b720ca832cdc89e670f6093ce38aeef55a5c6a4e077883d7d80dda1e9051032fb1faa1c3c212c517e93ee42b3ceac8e8e9b04bad46a361de90"}}
|
||||||
|
{"type": "new","receivedAt":12345,"sourceType":"IP4","sourceInfo": "127.0.0.2","event":{"id": "68421a122cef086512b2c5bd29ca6285ced8bd8e302e347e3c5d90466c860a76","pubkey": "16c21558762108afc34e4ff19e4ed51d9a48f79e0c34531efc423d21ab435e93","created_at": 1720408658,"kind": 1,"tags": [],"content": "hi","sig": "7b76471744ded2b720ca832cdc89e670f6093ce38aeef55a5c6a4e077883d7d80dda1e9051032fb1faa1c3c212c517e93ee42b3ceac8e8e9b04bad46a361de90"}}
|
||||||
|
{"type": "new","receivedAt":12345,"sourceType":"IP4","sourceInfo": "127.0.0.2","event":{"id": "68421a122cef086512b2c5bd29ca6285ced8bd8e302e347e3c5d90466c860a76","pubkey": "16c21558762108afc34e4ff19e4ed51d9a48f79e0c34531efc423d21ab435e93","created_at": 1720408658,"kind": 1,"tags": [],"content": "hi","sig": "7b76471744ded2b720ca832cdc89e670f6093ce38aeef55a5c6a4e077883d7d80dda1e9051032fb1faa1c3c212c517e93ee42b3ceac8e8e9b04bad46a361de90"}}
|
||||||
|
{"type": "new","receivedAt":12345,"sourceType":"IP4","sourceInfo": "127.0.0.2","event":{"id": "68421a122cef086512b2c5bd29ca6285ced8bd8e302e347e3c5d90466c860a76","pubkey": "16c21558762108afc34e4ff19e4ed51d9a48f79e0c34531efc423d21ab435e93","created_at": 1720408658,"kind": 1,"tags": [],"content": "hi","sig": "7b76471744ded2b720ca832cdc89e670f6093ce38aeef55a5c6a4e077883d7d80dda1e9051032fb1faa1c3c212c517e93ee42b3ceac8e8e9b04bad46a361de90"}}
|
||||||
|
{"type": "new","receivedAt":12345,"sourceType":"IP4","sourceInfo": "127.0.0.2","event":{"id": "68421a122cef086512b2c5bd29ca6285ced8bd8e302e347e3c5d90466c860a76","pubkey": "16c21558762108afc34e4ff19e4ed51d9a48f79e0c34531efc423d21ab435e93","created_at": 1720408658,"kind": 1,"tags": [],"content": "hi","sig": "7b76471744ded2b720ca832cdc89e670f6093ce38aeef55a5c6a4e077883d7d80dda1e9051032fb1faa1c3c212c517e93ee42b3ceac8e8e9b04bad46a361de90"}}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue