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
|
|
@ -1,23 +1,31 @@
|
|||
use crate::{Action, InputMessage, NoteFilter, OutputMessage};
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[derive(Deserialize, Default)]
|
||||
pub struct Whitelist {
|
||||
pub pubkeys: Vec<String>,
|
||||
pub ips: Vec<String>,
|
||||
pub pubkeys: Option<Vec<String>>,
|
||||
pub ips: Option<Vec<String>>,
|
||||
}
|
||||
|
||||
impl NoteFilter for Whitelist {
|
||||
fn filter_note(&mut self, msg: &InputMessage) -> OutputMessage {
|
||||
if self.pubkeys.contains(&msg.event.pubkey) || self.ips.contains(&msg.source_info) {
|
||||
OutputMessage::new(msg.event.id.clone(), Action::Accept, None)
|
||||
} else {
|
||||
OutputMessage::new(
|
||||
msg.event.id.clone(),
|
||||
Action::Reject,
|
||||
Some("blocked: pubkey not on the whitelist".to_string()),
|
||||
)
|
||||
if let Some(pubkeys) = &self.pubkeys {
|
||||
if pubkeys.contains(&msg.event.pubkey) {
|
||||
return OutputMessage::new(msg.event.id.clone(), Action::Accept, None);
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
use noteguard::filters::RateLimit;
|
||||
use noteguard::filters::{RateLimit, Whitelist};
|
||||
use noteguard::{Action, InputMessage, NoteFilter, OutputMessage};
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde::Deserialize;
|
||||
|
|
@ -41,6 +41,7 @@ impl Noteguard {
|
|||
/// every new instance of [`Noteguard`]
|
||||
fn register_builtin_filters(&mut self) {
|
||||
self.register_filter::<RateLimit>();
|
||||
self.register_filter::<Whitelist>();
|
||||
}
|
||||
|
||||
/// Run the loaded filters. You must call `load_config` before calling this, otherwise
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue