Compare commits

..
5 Commits
Author SHA1 Message Date
Greg Heartsfield 8a8ee5c425 build: bump version to 0.1.5 2021-12-19 16:45:17 -06:00
Greg Heartsfield 55bb6bd440 feat: add resource limits for websocket messages 2021-12-19 16:26:32 -06:00
Greg Heartsfield 7933abaa48 fix: allow unknown fields, like author 2021-12-19 16:18:03 -06:00
Greg Heartsfield 9b959e1012 build: bump version to 0.1.4 2021-12-16 19:00:46 -06:00
Greg Heartsfield 5b6a20dfa6 feat: remove author from subscriptions (NIP-01 Spec)
The `authors` field is sufficient to represent all queries that
`author` could have been used in.  See
https://github.com/fiatjaf/nostr/issues/34 for the discussion leading
to this removal.
2021-12-16 18:53:53 -06:00
6 changed files with 14 additions and 53 deletions
Generated
+1 -1
View File
@@ -435,7 +435,7 @@ dependencies = [
[[package]] [[package]]
name = "nostr-rs-relay" name = "nostr-rs-relay"
version = "0.1.3" version = "0.1.5"
dependencies = [ dependencies = [
"bitcoin_hashes", "bitcoin_hashes",
"env_logger", "env_logger",
+1 -1
View File
@@ -1,6 +1,6 @@
[package] [package]
name = "nostr-rs-relay" name = "nostr-rs-relay"
version = "0.1.3" version = "0.1.5"
edition = "2021" edition = "2021"
[dependencies] [dependencies]
+1 -1
View File
@@ -53,7 +53,7 @@ impl ClientConn {
v.push(id); v.push(id);
} }
} }
return v; v
} }
/// Add a new subscription for this connection. /// Add a new subscription for this connection.
-10
View File
@@ -181,16 +181,6 @@ fn query_from_sub(sub: &Subscription) -> String {
for f in sub.filters.iter() { for f in sub.filters.iter() {
// individual filter components // individual filter components
let mut filter_components: Vec<String> = Vec::new(); let mut filter_components: Vec<String> = Vec::new();
// Query for "author"
// https://github.com/fiatjaf/nostr/issues/34
// I believe the author & authors fields are redundant.
if f.author.is_some() {
let author_str = f.author.as_ref().unwrap();
if is_hex(author_str) {
let author_clause = format!("author = x'{}'", author_str);
filter_components.push(author_clause);
}
}
// Query for "authors" // Query for "authors"
if f.authors.is_some() { if f.authors.is_some() {
let authors_escaped: Vec<String> = f let authors_escaped: Vec<String> = f
+9 -1
View File
@@ -18,6 +18,7 @@ use tokio::sync::broadcast;
use tokio::sync::broadcast::{Receiver, Sender}; use tokio::sync::broadcast::{Receiver, Sender};
use tokio::sync::mpsc; use tokio::sync::mpsc;
use tokio::sync::oneshot; use tokio::sync::oneshot;
use tungstenite::protocol::WebSocketConfig;
/// Start running a Nostr relay server. /// Start running a Nostr relay server.
fn main() -> Result<(), Error> { fn main() -> Result<(), Error> {
@@ -93,8 +94,15 @@ async fn nostr_server(
) { ) {
// get a broadcast channel for clients to communicate on // get a broadcast channel for clients to communicate on
let mut bcast_rx = broadcast.subscribe(); let mut bcast_rx = broadcast.subscribe();
// websocket configuration / limits
let config = WebSocketConfig {
max_send_queue: None,
max_message_size: Some(2 << 19), // 512K
max_frame_size: Some(2 << 19), // 512k
accept_unmasked_frames: false, // follow the spec
};
// upgrade the TCP connection to WebSocket // upgrade the TCP connection to WebSocket
let conn = tokio_tungstenite::accept_async(stream).await; let conn = tokio_tungstenite::accept_async_with_config(stream, Some(config)).await;
let ws_stream = conn.expect("websocket handshake error"); let ws_stream = conn.expect("websocket handshake error");
// wrap websocket into a stream & sink of Nostr protocol messages // wrap websocket into a stream & sink of Nostr protocol messages
let mut nostr_stream = protostream::wrap_ws_in_nostr(ws_stream); let mut nostr_stream = protostream::wrap_ws_in_nostr(ws_stream);
+2 -39
View File
@@ -16,12 +16,9 @@ pub struct Subscription {
/// element can be present if it should be used in filtering, or /// element can be present if it should be used in filtering, or
/// absent ([`None`]) if it should be ignored. /// absent ([`None`]) if it should be ignored.
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)] #[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
#[serde(deny_unknown_fields)]
pub struct ReqFilter { pub struct ReqFilter {
/// Event hash /// Event hash
pub id: Option<String>, pub id: Option<String>,
/// Author public key
pub author: Option<String>,
/// Event kind /// Event kind
pub kind: Option<u64>, pub kind: Option<u64>,
/// Referenced event hash /// Referenced event hash
@@ -114,13 +111,6 @@ impl ReqFilter {
.map(|vs| vs.contains(&event.pubkey.to_owned())) .map(|vs| vs.contains(&event.pubkey.to_owned()))
.unwrap_or(true) .unwrap_or(true)
} }
/// Check for a specific author match
fn author_match(&self, event: &Event) -> bool {
self.author
.as_ref()
.map(|v| v == &event.pubkey)
.unwrap_or(true)
}
/// Check if this filter either matches, or does not care about the event tags. /// Check if this filter either matches, or does not care about the event tags.
fn event_match(&self, event: &Event) -> bool { fn event_match(&self, event: &Event) -> bool {
self.event self.event
@@ -148,7 +138,6 @@ impl ReqFilter {
self.id.as_ref().map(|v| v == &event.id).unwrap_or(true) self.id.as_ref().map(|v| v == &event.id).unwrap_or(true)
&& self.since.map(|t| event.created_at > t).unwrap_or(true) && self.since.map(|t| event.created_at > t).unwrap_or(true)
&& self.kind_match(event.kind) && self.kind_match(event.kind)
&& self.author_match(event)
&& self.authors_match(event) && self.authors_match(event)
&& self.pubkey_match(event) && self.pubkey_match(event)
&& self.event_match(event) && self.event_match(event)
@@ -165,17 +154,7 @@ mod tests {
let s: Subscription = serde_json::from_str(raw_json)?; let s: Subscription = serde_json::from_str(raw_json)?;
assert_eq!(s.id, "some-id"); assert_eq!(s.id, "some-id");
assert_eq!(s.filters.len(), 1); assert_eq!(s.filters.len(), 1);
assert_eq!(s.filters.get(0).unwrap().author, None); assert_eq!(s.filters.get(0).unwrap().authors, None);
Ok(())
}
#[test]
fn multi_empty_request_parse() -> Result<()> {
let raw_json = r#"["REQ","some-id",{}]"#;
let s: Subscription = serde_json::from_str(raw_json)?;
assert_eq!(s.id, "some-id");
assert_eq!(s.filters.len(), 1);
assert_eq!(s.filters.get(0).unwrap().author, None);
Ok(()) Ok(())
} }
@@ -277,23 +256,6 @@ mod tests {
Ok(()) Ok(())
} }
#[test]
fn author_single() -> Result<()> {
// subscription with a filter for ID
let s: Subscription = serde_json::from_str(r#"["REQ","xyz",{"author":"abc"}]"#)?;
let e = Event {
id: "123".to_owned(),
pubkey: "abc".to_owned(),
created_at: 0,
kind: 0,
tags: Vec::new(),
content: "".to_owned(),
sig: "".to_owned(),
};
assert_eq!(s.interested_in_event(&e), true);
Ok(())
}
#[test] #[test]
fn authors_single() -> Result<()> { fn authors_single() -> Result<()> {
// subscription with a filter for ID // subscription with a filter for ID
@@ -311,6 +273,7 @@ mod tests {
Ok(()) Ok(())
} }
#[test] #[test]
fn authors_multi_pubkey() -> Result<()> { fn authors_multi_pubkey() -> Result<()> {
// check for any of a set of authors, against the pubkey // check for any of a set of authors, against the pubkey
let s: Subscription = serde_json::from_str(r#"["REQ","xyz",{"authors":["abc", "bcd"]}]"#)?; let s: Subscription = serde_json::from_str(r#"["REQ","xyz",{"authors":["abc", "bcd"]}]"#)?;