2021-12-05 21:28:02 -05:00
|
|
|
use futures::SinkExt;
|
2021-12-05 17:53:26 -05:00
|
|
|
use futures::StreamExt;
|
|
|
|
use log::*;
|
2021-12-05 19:14:14 -05:00
|
|
|
use nostr_rs_relay::close::Close;
|
2021-12-05 17:53:26 -05:00
|
|
|
use nostr_rs_relay::conn;
|
2021-12-11 16:48:59 -05:00
|
|
|
use nostr_rs_relay::db;
|
2021-12-05 17:53:26 -05:00
|
|
|
use nostr_rs_relay::error::{Error, Result};
|
|
|
|
use nostr_rs_relay::event::Event;
|
|
|
|
use nostr_rs_relay::protostream;
|
|
|
|
use nostr_rs_relay::protostream::NostrMessage::*;
|
2021-12-05 21:28:02 -05:00
|
|
|
use nostr_rs_relay::protostream::NostrResponse::*;
|
2021-12-11 16:48:59 -05:00
|
|
|
use std::collections::HashMap;
|
2021-12-05 17:53:26 -05:00
|
|
|
use std::env;
|
|
|
|
use tokio::net::{TcpListener, TcpStream};
|
|
|
|
use tokio::runtime::Builder;
|
|
|
|
use tokio::sync::broadcast;
|
2021-12-11 16:48:59 -05:00
|
|
|
use tokio::sync::broadcast::{Receiver, Sender};
|
2021-12-05 17:53:26 -05:00
|
|
|
use tokio::sync::mpsc;
|
2021-12-11 16:48:59 -05:00
|
|
|
use tokio::sync::oneshot;
|
2021-12-05 17:53:26 -05:00
|
|
|
|
|
|
|
/// Start running a Nostr relay server.
|
|
|
|
fn main() -> Result<(), Error> {
|
|
|
|
// setup logger
|
|
|
|
let _ = env_logger::try_init();
|
|
|
|
let addr = env::args()
|
|
|
|
.nth(1)
|
2021-12-11 17:57:55 -05:00
|
|
|
.unwrap_or_else(|| "0.0.0.0:8080".to_string());
|
2021-12-05 17:53:26 -05:00
|
|
|
// configure tokio runtime
|
|
|
|
let rt = Builder::new_multi_thread()
|
|
|
|
.enable_all()
|
|
|
|
.thread_name("tokio-ws")
|
|
|
|
.build()
|
|
|
|
.unwrap();
|
|
|
|
// start tokio
|
|
|
|
rt.block_on(async {
|
|
|
|
let listener = TcpListener::bind(&addr).await.expect("Failed to bind");
|
|
|
|
info!("Listening on: {}", addr);
|
|
|
|
// Establish global broadcast channel. This is where all
|
|
|
|
// accepted events will be distributed for other connected clients.
|
2021-12-11 16:48:59 -05:00
|
|
|
|
|
|
|
// this needs to be large enough to accomodate any slow
|
|
|
|
// readers - otherwise messages will be dropped before they
|
|
|
|
// can be processed. Since this is global to all connections,
|
|
|
|
// we can tolerate this being rather large (for 4096, the
|
|
|
|
// buffer itself is about 1MB).
|
|
|
|
let (bcast_tx, _) = broadcast::channel::<Event>(4096);
|
2021-12-05 17:53:26 -05:00
|
|
|
// Establish database writer channel. This needs to be
|
|
|
|
// accessible from sync code, which is why the broadcast
|
|
|
|
// cannot be reused.
|
2021-12-11 16:48:59 -05:00
|
|
|
let (event_tx, event_rx) = mpsc::channel::<Event>(16);
|
2021-12-05 17:53:26 -05:00
|
|
|
// start the database writer.
|
2021-12-11 16:48:59 -05:00
|
|
|
db::db_writer(event_rx).await;
|
|
|
|
// setup a broadcast channel for invoking a process shutdown
|
|
|
|
let (invoke_shutdown, _) = broadcast::channel::<()>(1);
|
|
|
|
let shutdown_handler = invoke_shutdown.clone();
|
|
|
|
// listen for ctrl-c interruupts
|
|
|
|
tokio::spawn(async move {
|
|
|
|
tokio::signal::ctrl_c().await.unwrap();
|
|
|
|
// Your handler here
|
|
|
|
info!("got ctrl-c");
|
|
|
|
shutdown_handler.send(()).ok();
|
|
|
|
});
|
|
|
|
let mut stop_listening = invoke_shutdown.subscribe();
|
|
|
|
// shutdown on Ctrl-C, or accept a new connection
|
|
|
|
loop {
|
|
|
|
tokio::select! {
|
|
|
|
_ = stop_listening.recv() => {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Ok((stream, _)) = listener.accept() => {
|
|
|
|
tokio::spawn(nostr_server(
|
|
|
|
stream,
|
|
|
|
bcast_tx.clone(),
|
|
|
|
event_tx.clone(),
|
|
|
|
invoke_shutdown.subscribe(),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
2021-12-05 17:53:26 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn nostr_server(
|
|
|
|
stream: TcpStream,
|
|
|
|
broadcast: Sender<Event>,
|
2021-12-11 16:48:59 -05:00
|
|
|
event_tx: tokio::sync::mpsc::Sender<Event>,
|
|
|
|
mut shutdown: Receiver<()>,
|
2021-12-05 17:53:26 -05:00
|
|
|
) {
|
|
|
|
// get a broadcast channel for clients to communicate on
|
|
|
|
// wrap the TCP stream in a websocket.
|
2021-12-05 21:28:02 -05:00
|
|
|
let mut bcast_rx = broadcast.subscribe();
|
2021-12-11 16:48:59 -05:00
|
|
|
// upgrade the TCP connection to WebSocket
|
2021-12-05 17:53:26 -05:00
|
|
|
let conn = tokio_tungstenite::accept_async(stream).await;
|
|
|
|
let ws_stream = conn.expect("websocket handshake error");
|
2021-12-11 16:48:59 -05:00
|
|
|
// wrap websocket into a stream & sink of Nostr protocol messages
|
2021-12-05 17:53:26 -05:00
|
|
|
let mut nostr_stream = protostream::wrap_ws_in_nostr(ws_stream);
|
|
|
|
// Track internal client state
|
2021-12-05 19:14:14 -05:00
|
|
|
let mut conn = conn::ClientConn::new();
|
2021-12-11 16:48:59 -05:00
|
|
|
let cid = conn.get_client_prefix();
|
|
|
|
// Create a channel for receiving query results from the database.
|
|
|
|
// we will send out the tx handle to any query we generate.
|
|
|
|
let (query_tx, mut query_rx) = mpsc::channel::<db::QueryResult>(256);
|
|
|
|
// maintain a hashmap of a oneshot channel for active subscriptions.
|
|
|
|
// when these subscriptions are cancelled, make a message
|
|
|
|
// available to the executing query so it knows to stop.
|
|
|
|
//let (abandon_query_tx, _) = oneshot::channel::<()>();
|
|
|
|
let mut running_queries: HashMap<String, oneshot::Sender<()>> = HashMap::new();
|
|
|
|
|
2021-12-05 17:53:26 -05:00
|
|
|
loop {
|
|
|
|
tokio::select! {
|
2021-12-11 16:48:59 -05:00
|
|
|
_ = shutdown.recv() => {
|
|
|
|
// server shutting down, exit loop
|
|
|
|
break;
|
|
|
|
},
|
|
|
|
Some(query_result) = query_rx.recv() => {
|
|
|
|
info!("Got query result");
|
|
|
|
let res = EventRes(query_result.sub_id,query_result.event);
|
|
|
|
nostr_stream.send(res).await.ok();
|
|
|
|
},
|
2021-12-05 21:28:02 -05:00
|
|
|
Ok(global_event) = bcast_rx.recv() => {
|
|
|
|
// ignoring closed broadcast errors, there will always be one sender available.
|
|
|
|
// Is there a subscription for this event?
|
|
|
|
let sub_name_opt = conn.get_matching_subscription(&global_event);
|
2021-12-11 16:48:59 -05:00
|
|
|
if sub_name_opt.is_some() {
|
2021-12-05 21:28:02 -05:00
|
|
|
let sub_name = sub_name_opt.unwrap();
|
|
|
|
let event_str = serde_json::to_string(&global_event);
|
|
|
|
if event_str.is_ok() {
|
|
|
|
info!("sub match: client: {}, sub: {}, event: {}",
|
2021-12-11 16:48:59 -05:00
|
|
|
cid, sub_name,
|
2021-12-05 21:28:02 -05:00
|
|
|
global_event.get_event_id_prefix());
|
|
|
|
// create an event response and send it
|
|
|
|
let res = EventRes(sub_name.to_owned(),event_str.unwrap());
|
|
|
|
nostr_stream.send(res).await.ok();
|
2021-12-11 16:48:59 -05:00
|
|
|
} else {
|
|
|
|
warn!("could not convert event to string");
|
2021-12-05 21:28:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
// check if this client has a subscription
|
2021-12-05 17:53:26 -05:00
|
|
|
proto_next = nostr_stream.next() => {
|
|
|
|
match proto_next {
|
2021-12-05 18:15:50 -05:00
|
|
|
Some(Ok(EventMsg(ec))) => {
|
|
|
|
// An EventCmd needs to be validated to be converted into an Event
|
2021-12-05 17:53:26 -05:00
|
|
|
// handle each type of message
|
2021-12-05 18:15:50 -05:00
|
|
|
let parsed : Result<Event> = Result::<Event>::from(ec);
|
|
|
|
match parsed {
|
2021-12-05 19:14:14 -05:00
|
|
|
Ok(e) => {
|
|
|
|
let id_prefix:String = e.id.chars().take(8).collect();
|
2021-12-11 16:48:59 -05:00
|
|
|
info!("Successfully parsed/validated event: {} from client: {}", id_prefix, cid);
|
|
|
|
// Write this to the database
|
|
|
|
event_tx.send(e.clone()).await.ok();
|
2021-12-05 21:28:02 -05:00
|
|
|
// send this event to everyone listening.
|
|
|
|
let bcast_res = broadcast.send(e);
|
|
|
|
if bcast_res.is_err() {
|
|
|
|
warn!("Could not send broadcast message: {:?}", bcast_res);
|
|
|
|
}
|
|
|
|
},
|
2021-12-11 16:48:59 -05:00
|
|
|
Err(_) => {info!("Client {} sent an invalid event", cid)}
|
2021-12-05 18:15:50 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
Some(Ok(SubMsg(s))) => {
|
2021-12-11 16:48:59 -05:00
|
|
|
info!("Client {} requesting a subscription", cid);
|
|
|
|
|
2021-12-05 19:14:14 -05:00
|
|
|
// subscription handling consists of:
|
2021-12-11 16:48:59 -05:00
|
|
|
// * registering the subscription so future events can be matched
|
|
|
|
// * making a channel to cancel to request later
|
|
|
|
// * sending a request for a SQL query
|
|
|
|
let (abandon_query_tx, abandon_query_rx) = oneshot::channel::<()>();
|
|
|
|
running_queries.insert(s.id.to_owned(), abandon_query_tx);
|
|
|
|
// register this connection
|
|
|
|
conn.subscribe(s.clone()).ok();
|
|
|
|
// start a database query
|
|
|
|
db::db_query(s, query_tx.clone(), abandon_query_rx).await;
|
2021-12-05 18:33:40 -05:00
|
|
|
},
|
2021-12-05 19:14:14 -05:00
|
|
|
Some(Ok(CloseMsg(cc))) => {
|
|
|
|
// closing a request simply removes the subscription.
|
|
|
|
let parsed : Result<Close> = Result::<Close>::from(cc);
|
|
|
|
match parsed {
|
2021-12-11 16:48:59 -05:00
|
|
|
Ok(c) => {
|
|
|
|
let stop_tx = running_queries.remove(&c.id);
|
|
|
|
match stop_tx {
|
|
|
|
Some(tx) => {
|
|
|
|
info!("Removing query, telling DB to abandon query");
|
|
|
|
tx.send(()).ok();
|
|
|
|
},
|
|
|
|
None => {}
|
|
|
|
}
|
|
|
|
conn.unsubscribe(c);
|
|
|
|
},
|
2021-12-05 19:14:14 -05:00
|
|
|
Err(_) => {info!("Invalid command ignored");}
|
|
|
|
}
|
2021-12-05 17:53:26 -05:00
|
|
|
},
|
|
|
|
None => {
|
2021-12-11 16:48:59 -05:00
|
|
|
info!("normal websocket close from client: {}",cid);
|
|
|
|
break;
|
2021-12-05 17:53:26 -05:00
|
|
|
},
|
|
|
|
Some(Err(Error::ConnError)) => {
|
2021-12-11 16:48:59 -05:00
|
|
|
info!("got connection close/error, disconnecting client: {}",cid);
|
|
|
|
break;
|
2021-12-05 17:53:26 -05:00
|
|
|
}
|
|
|
|
Some(Err(e)) => {
|
2021-12-11 16:48:59 -05:00
|
|
|
info!("got non-fatal error from client: {}, error: {:?}", cid, e);
|
2021-12-05 17:53:26 -05:00
|
|
|
},
|
|
|
|
}
|
2021-12-11 16:48:59 -05:00
|
|
|
},
|
2021-12-05 17:53:26 -05:00
|
|
|
}
|
|
|
|
}
|
2021-12-11 16:48:59 -05:00
|
|
|
// connection cleanup - ensure any still running queries are terminated.
|
|
|
|
for (_, stop_tx) in running_queries.into_iter() {
|
|
|
|
stop_tx.send(()).ok();
|
|
|
|
}
|
|
|
|
info!("stopping client connection: {}", cid);
|
2021-12-05 09:42:28 -05:00
|
|
|
}
|