Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add blacklist filter #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion noteguard.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

pipeline = ["protected_events", "kinds", "content", "whitelist", "ratelimit"]

[filters.ratelimit]
Expand Down
39 changes: 39 additions & 0 deletions src/filters/blacklist.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use crate::{Action, InputMessage, NoteFilter, OutputMessage};
use serde::Deserialize;

#[derive(Deserialize, Default)]
pub struct Blacklist {
pub pubkeys: Option<Vec<String>>,
pub ips: Option<Vec<String>>,
}

impl NoteFilter for Blacklist {
fn filter_note(&mut self, msg: &InputMessage) -> OutputMessage {
let reject_message = "blocked: pubkey/ip is blacklisted".to_string();
if let Some(pubkeys) = &self.pubkeys {
if pubkeys.contains(&msg.event.pubkey) {
return OutputMessage::new(
msg.event.id.clone(),
Action::Reject,
Some(reject_message),
);
}
}

if let Some(ips) = &self.ips {
if ips.contains(&msg.source_info) {
return OutputMessage::new(
msg.event.id.clone(),
Action::Reject,
Some(reject_message),
);
}
}

OutputMessage::new(msg.event.id.clone(), Action::Accept, None)
}

fn name(&self) -> &'static str {
"blacklist"
}
}
2 changes: 2 additions & 0 deletions src/filters/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod blacklist;
mod content;
mod kinds;
mod protected_events;
Expand All @@ -7,6 +8,7 @@ mod whitelist;
#[cfg(feature = "forwarder")]
mod forwarder;

pub use blacklist::Blacklist;
pub use content::Content;
pub use kinds::Kinds;
pub use protected_events::ProtectedEvents;
Expand Down
54 changes: 53 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use noteguard::filters::{Content, Kinds, ProtectedEvents, RateLimit, Whitelist};
use noteguard::filters::{Blacklist, Content, Kinds, ProtectedEvents, RateLimit, Whitelist};

#[cfg(feature = "forwarder")]
use noteguard::filters::Forwarder;
Expand Down Expand Up @@ -47,6 +47,7 @@ impl Noteguard {
fn register_builtin_filters(&mut self) {
self.register_filter::<RateLimit>();
self.register_filter::<Whitelist>();
self.register_filter::<Blacklist>();
self.register_filter::<ProtectedEvents>();
self.register_filter::<Kinds>();
self.register_filter::<Content>();
Expand Down Expand Up @@ -217,6 +218,7 @@ mod tests {
let noteguard = Noteguard::new();
assert!(noteguard.registered_filters.contains_key("ratelimit"));
assert!(noteguard.registered_filters.contains_key("whitelist"));
assert!(noteguard.registered_filters.contains_key("blacklist"));
assert!(noteguard
.registered_filters
.contains_key("protected_events"));
Expand Down Expand Up @@ -315,6 +317,56 @@ mod tests {
assert_eq!(output_message.action, Action::Reject);
}

#[test]
fn test_blacklist_reject() {
let mut noteguard = Noteguard::new();

let config: Config = toml::from_str(
r#"
pipeline = ["blacklist"]
[filters.blacklist]
pubkeys = ["mock_pubkey"]
"#,
)
.expect("Failed to parse config");

noteguard
.load_config(&config)
.expect("Failed to load config");

let input_message = create_mock_input_message("test_event_3", "new");
let output_message = noteguard.run(input_message);

assert_eq!(output_message.action, Action::Reject);
assert_eq!(
output_message.msg.expect("Failed to get message"),
"blocked: pubkey/ip is blacklisted".to_string()
);
}

#[test]
fn test_blacklist_accept() {
let mut noteguard = Noteguard::new();

let config: Config = toml::from_str(
r#"
pipeline = ["blacklist"]
[filters.blacklist]
pubkeys = ["not_blacklisted"]
"#,
)
.expect("Failed to parse config");

noteguard
.load_config(&config)
.expect("Failed to load config");

let input_message = create_mock_input_message("test_event_4", "new");
let output_message = noteguard.run(input_message);

assert_eq!(output_message.action, Action::Accept);
}

#[test]
fn test_deserialize_input_message() {
let input_json = r#"
Expand Down