6 Commits
Author SHA1 Message Date
William Casarin cae0393ed0 filter: add content filter
Signed-off-by: William Casarin <jb55@jb55.com>
2024-07-30 10:52:41 -07:00
William Casarin 8e1bb0363f test: add chatgpt generated tests
because I'm lazy

Signed-off-by: William Casarin <jb55@jb55.com>
2024-07-19 11:13:24 -07:00
William Casarin 471a70bbfa simplify main loop
Signed-off-by: William Casarin <jb55@jb55.com>
2024-07-19 11:13:02 -07:00
William Casarin 399f238865 readme: add forwarder to example config
Signed-off-by: William Casarin <jb55@jb55.com>
2024-07-11 15:02:36 -07:00
William Casarin 4704b3034f mention queue size default
Signed-off-by: William Casarin <jb55@jb55.com>
2024-07-11 14:57:16 -07:00
William Casarin 670b390096 readme: make note that forwarder is compile flag
Signed-off-by: William Casarin <jb55@jb55.com>
2024-07-11 14:56:46 -07:00
7 changed files with 248 additions and 39 deletions
+12 -2
View File
@@ -14,7 +14,7 @@ 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.
```toml
pipeline = ["protected_events", "kinds", "whitelist", "ratelimit"]
pipeline = ["protected_events", "kinds", "whitelist", "ratelimit", "forwarder"]
[filters.ratelimit]
posts_per_minute = 8
@@ -32,6 +32,10 @@ kinds = [30065, 1064]
1064 = "blocked: files on nostr is dumb"
[filters.protected_events]
[filters.forwarder]
relay = "ws://localhost:8080"
queue_size = 2000
```
## Installation
@@ -135,12 +139,18 @@ There are no config options, but an empty config entry is still needed:
* name: `forwarder`
You need to compile with the `forwarder` feature to enable this filter:
```sh
$ cargo build --features forwarder --release
```
The forwarder filter allows you to forward notes to another relay. Notes will
be queued if the connection goes down (up to the `queue_size` buffer limit)
- `relay` - the relay to forward notes to, eg: `ws://localhost:8080`
- `queue_size` *optional* - size of the note queue, this is used to buffer notes if the connection goes down
- `queue_size` *optional* - size of the note queue, this is used to buffer notes if the connection goes down. Default is 1000.
## Testing
+4 -1
View File
@@ -1,10 +1,13 @@
pipeline = ["protected_events", "kinds", "whitelist", "ratelimit"]
pipeline = ["protected_events", "kinds", "content", "whitelist", "ratelimit"]
[filters.ratelimit]
posts_per_minute = 8
whitelist = ["127.0.0.1"]
[filters.content]
filters = ["https://cdn.nostr.build/i/some-spammy-or-abusive-image-image.png"]
[filters.whitelist]
pubkeys = ["16c21558762108afc34e4ff19e4ed51d9a48f79e0c34531efc423d21ab435e93"]
ips = ["127.0.0.1"]
+23
View File
@@ -0,0 +1,23 @@
use crate::{Action, InputMessage, NoteFilter, OutputMessage};
use serde::Deserialize;
#[derive(Deserialize, Default)]
pub struct Content {
filters: Vec<String>,
}
impl NoteFilter for Content {
fn filter_note(&mut self, msg: &InputMessage) -> OutputMessage {
for filter in &self.filters {
if msg.event.content.contains(filter) {
return OutputMessage::new(msg.event.id.clone(), Action::ShadowReject, None);
}
}
OutputMessage::new(msg.event.id.clone(), Action::Accept, None)
}
fn name(&self) -> &'static str {
"content"
}
}
+2
View File
@@ -1,3 +1,4 @@
mod content;
mod kinds;
mod protected_events;
mod ratelimit;
@@ -6,6 +7,7 @@ mod whitelist;
#[cfg(feature = "forwarder")]
mod forwarder;
pub use content::Content;
pub use kinds::Kinds;
pub use protected_events::ProtectedEvents;
pub use ratelimit::RateLimit;
+205 -35
View File
@@ -1,14 +1,14 @@
use noteguard::filters::{Kinds, ProtectedEvents, RateLimit, Whitelist};
use noteguard::filters::{Content, Kinds, ProtectedEvents, RateLimit, Whitelist};
#[cfg(feature = "forwarder")]
use noteguard::filters::Forwarder;
use log::info;
use noteguard::{Action, InputMessage, NoteFilter, OutputMessage};
use serde::de::DeserializeOwned;
use serde::Deserialize;
use std::collections::HashMap;
use std::io::{self, BufRead, Read, Write};
use log::info;
use std::io::{self, Read};
#[derive(Deserialize)]
struct Config {
@@ -49,6 +49,7 @@ impl Noteguard {
self.register_filter::<Whitelist>();
self.register_filter::<ProtectedEvents>();
self.register_filter::<Kinds>();
self.register_filter::<Content>();
#[cfg(feature = "forwarder")]
self.register_filter::<Forwarder>();
@@ -113,6 +114,10 @@ fn main() {
noteguard();
}
fn serialize_output_message(msg: &OutputMessage) -> String {
serde_json::to_string(msg).expect("OutputMessage should always serialize correctly")
}
fn noteguard() {
env_logger::init();
info!("running noteguard");
@@ -133,41 +138,206 @@ fn noteguard() {
.expect("Expected filter config to be loaded ok");
let stdin = io::stdin();
let stdout = io::stdout();
let handle = stdout.lock();
let mut writer = io::BufWriter::new(handle);
for line in stdin.lock().lines() {
match line {
Ok(input) => {
let input_message: InputMessage = match serde_json::from_str(&input) {
Ok(msg) => msg,
Err(e) => {
eprintln!("Failed to parse input: {}", e);
continue;
}
};
if input_message.message_type != "new" {
eprintln!("Unexpected request type");
continue;
}
let output_message = noteguard.run(input_message);
match serde_json::to_string(&output_message) {
Ok(json) => {
writeln!(writer, "{}", json).unwrap();
writer.flush().unwrap();
}
Err(e) => {
eprintln!("Failed to serialize output: {}", e);
}
}
}
for line in stdin.lines() {
let line = match line {
Ok(line) => line,
Err(e) => {
eprintln!("Failed to read line: {}", e);
eprintln!("Failed to get line: {}", e);
continue;
}
};
let input_message: InputMessage = match serde_json::from_str(&line) {
Ok(msg) => msg,
Err(e) => {
eprintln!("Failed to parse input: {}", e);
continue;
}
};
if input_message.message_type != "new" {
let out = OutputMessage::new(
input_message.event.id.clone(),
Action::Reject,
Some("invalid strfry write policy input".to_string()),
);
println!("{}", serialize_output_message(&out));
continue;
}
let out = noteguard.run(input_message);
let json = serialize_output_message(&out);
println!("{}", json);
}
}
#[cfg(test)]
mod tests {
use super::*;
use noteguard::filters::{Kinds, ProtectedEvents, RateLimit, Whitelist};
use noteguard::{Action, Note};
use serde_json::json;
// Helper function to create a mock InputMessage
fn create_mock_input_message(event_id: &str, message_type: &str) -> InputMessage {
InputMessage {
message_type: message_type.to_string(),
event: Note {
id: event_id.to_string(),
pubkey: "mock_pubkey".to_string(),
created_at: 0,
kind: 1,
tags: vec![vec!["-".to_string()]],
content: "mock_content".to_string(),
sig: "mock_signature".to_string(),
},
received_at: 0,
source_type: "mock_source".to_string(),
source_info: "mock_source_info".to_string(),
}
}
// Helper function to create a mock OutputMessage
fn create_mock_output_message(
event_id: &str,
action: Action,
msg: Option<&str>,
) -> OutputMessage {
OutputMessage {
id: event_id.to_string(),
action,
msg: msg.map(|s| s.to_string()),
}
}
#[test]
fn test_register_builtin_filters() {
let noteguard = Noteguard::new();
assert!(noteguard.registered_filters.contains_key("ratelimit"));
assert!(noteguard.registered_filters.contains_key("whitelist"));
assert!(noteguard
.registered_filters
.contains_key("protected_events"));
assert!(noteguard.registered_filters.contains_key("kinds"));
}
#[test]
fn test_load_config() {
let mut noteguard = Noteguard::new();
// Create a mock config with one filter (RateLimit)
let config: Config = toml::from_str(
r#"
pipeline = ["ratelimit"]
[filters.ratelimit]
posts_per_minute = 3
"#,
)
.expect("Failed to parse config");
assert!(noteguard.load_config(&config).is_ok());
assert_eq!(noteguard.loaded_filters.len(), 1);
}
#[test]
fn test_run_filters_accept() {
let mut noteguard = Noteguard::new();
// Create a mock config with one filter (RateLimit)
let config: Config = toml::from_str(
r#"
pipeline = ["ratelimit"]
[filters.ratelimit]
posts_per_minute = 3
"#,
)
.expect("Failed to parse config");
noteguard
.load_config(&config)
.expect("Failed to load config");
let input_message = create_mock_input_message("test_event_1", "new");
let output_message = noteguard.run(input_message);
assert_eq!(output_message.action, Action::Accept);
}
#[test]
fn test_run_filters_shadow_reject() {
let mut noteguard = Noteguard::new();
// Create a mock config with one filter (ProtectedEvents) which will shadow reject the input
let config: Config = toml::from_str(
r#"
pipeline = ["protected_events"]
[filters.protected_events]
"#,
)
.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);
}
#[test]
fn test_whitelist_reject() {
let mut noteguard = Noteguard::new();
// Create a mock config with one filter (Whitelist) which will reject the input
let config: Config = toml::from_str(
r#"
pipeline = ["whitelist"]
[filters.whitelist]
pubkeys = ["something"]
"#,
)
.expect("Failed to parse config");
noteguard
.load_config(&config)
.expect("Failed to load config");
let input_message = create_mock_input_message("test_event_2", "new");
let output_message = noteguard.run(input_message);
assert_eq!(output_message.action, Action::Reject);
}
#[test]
fn test_deserialize_input_message() {
let input_json = r#"
{
"type": "new",
"event": {
"id": "test_event_5",
"pubkey": "mock_pubkey",
"created_at": 0,
"kind": 1,
"tags": [],
"content": "mock_content",
"sig": "mock_signature"
},
"receivedAt": 0,
"sourceType": "mock_source",
"sourceInfo": "mock_source_info"
}
"#;
let input_message: InputMessage =
serde_json::from_str(input_json).expect("Failed to deserialize input message");
assert_eq!(input_message.event.id, "test_event_5");
assert_eq!(input_message.message_type, "new");
}
}
+1 -1
View File
@@ -14,7 +14,7 @@ pub struct InputMessage {
pub source_info: String,
}
#[derive(Serialize, Deserialize)]
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
#[serde(rename_all = "camelCase")]
pub enum Action {
Accept,
+1
View File
@@ -1,3 +1,4 @@
{"type": "new","receivedAt":12345,"sourceType":"IP4","sourceInfo": "127.0.0.3","event":{"id":"eabb9491951fc64d85b6cba28a7d53c333c3f1d59779d63abccf9a8f61a0bdef","pubkey":"5f54041530509de28550475bfe73db709609a4ee1e59281527ba81692923418f","created_at":1722305142,"kind":1,"tags":[["t","lightning"],["t","zapping"],["t","introductions"],["t","freefrom"],["t","help"],["t","nostr"],["t","plebchain"],["t","zapathon"],["t","damus"],["t","bitcoin"],["t","anime"],["t","grownostr"],["t","music"],["t","programming"],["t","cat"],["t","dog"],["t","nsfw"],["t","asknostr"],["t","foodstr"],["t","sports"],["t","acg"],["t","game"],["t","movie"],["t","book"],["t","art"],["t","tech"],["t","science"]],"content":"Freefrom is running out of funds and is about to close down. Please scan the QR code to donate.\n Freefrom 资金不足即将倒闭,请扫描二维码捐款\n https://ice.frostsky.com/2024/07/29/9e447d397cac0c27993f0cbcb6efb235.png\n https://cdn.nostr.build/i/270de92fa4e5118a28352720b7f26c360aa2dbfdb1c12517f3e3117a2f9de47a.png\n ","sig":"6fa13bedffb2250e2437af6898ab3b11444eb3aef5fd4b484b25658632d283774186f98a024c225581cd9bbaccb0966fa1360d13fd5671f97c7f7bf515f94166"}}
{"type": "new","receivedAt":12345,"sourceType":"IP4","sourceInfo": "127.0.0.3","event":{"id": "70651d96a2b6b3431cc06b7543249ccd22ab5c203c6aa590b7688f916f252f8f","pubkey": "879d67486027539073d6531d271e3791b15c3e48becbfe4c3727e93355330cc8","created_at": 1720545068,"kind": 1,"tags": [["-"]],"content": "hello there","sig": "21a901e3663bac846493df588ad2185751a5a2826a64c26afb9edce8f9d9344cf00c1ea43016e7faca69da661eadd2731b457a0c31b207ab6ed509a047bf7845"}}
{"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"}}