2021-12-11 22:43:41 -05:00
|
|
|
//! Server process
|
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-29 23:13:02 -05:00
|
|
|
use nostr_rs_relay::config;
|
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-31 12:51:57 -05:00
|
|
|
use std::env;
|
|
|
|
use std::path::Path;
|
2021-12-05 17:53:26 -05:00
|
|
|
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-19 17:26:32 -05:00
|
|
|
use tungstenite::protocol::WebSocketConfig;
|
2021-12-05 17:53:26 -05:00
|
|
|
|
2021-12-31 12:51:57 -05:00
|
|
|
fn db_from_args(args: Vec<String>) -> Option<String> {
|
2021-12-31 16:19:35 -05:00
|
|
|
if args.len() == 3 && args.get(1) == Some(&"--db".to_owned()) {
|
|
|
|
return args.get(2).map(|x| x.to_owned());
|
2021-12-31 12:51:57 -05:00
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2021-12-05 17:53:26 -05:00
|
|
|
/// Start running a Nostr relay server.
|
|
|
|
fn main() -> Result<(), Error> {
|
2021-12-29 23:13:02 -05:00
|
|
|
// setup logger
|
2021-12-05 17:53:26 -05:00
|
|
|
let _ = env_logger::try_init();
|
2021-12-31 12:51:57 -05:00
|
|
|
// get database directory from args
|
|
|
|
let args: Vec<String> = env::args().collect();
|
|
|
|
let db_dir: Option<String> = db_from_args(args);
|
2021-12-29 23:13:02 -05:00
|
|
|
{
|
2021-12-29 23:47:31 -05:00
|
|
|
let mut settings = config::SETTINGS.write().unwrap();
|
2021-12-29 23:13:02 -05:00
|
|
|
// replace default settings with those read from config.toml
|
2021-12-31 12:51:57 -05:00
|
|
|
let mut c = config::Settings::new();
|
|
|
|
// update with database location
|
|
|
|
if let Some(db) = db_dir {
|
2021-12-31 16:19:35 -05:00
|
|
|
c.database.data_directory = db;
|
2021-12-31 12:51:57 -05:00
|
|
|
}
|
2021-12-29 23:13:02 -05:00
|
|
|
*settings = c;
|
|
|
|
}
|
2021-12-29 23:47:31 -05:00
|
|
|
let config = config::SETTINGS.read().unwrap();
|
2021-12-31 12:51:57 -05:00
|
|
|
// do some config validation.
|
|
|
|
if !Path::new(&config.database.data_directory).is_dir() {
|
|
|
|
error!("Database directory does not exist");
|
|
|
|
return Err(Error::DatabaseDirError);
|
|
|
|
}
|
2021-12-30 22:07:21 -05:00
|
|
|
debug!("config: {:?}", config);
|
2021-12-29 23:13:02 -05:00
|
|
|
let addr = format!("{}:{}", config.network.address.trim(), config.network.port);
|
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 {
|
2021-12-29 23:47:31 -05:00
|
|
|
let settings = config::SETTINGS.read().unwrap();
|
2021-12-05 17:53:26 -05:00
|
|
|
let listener = TcpListener::bind(&addr).await.expect("Failed to bind");
|
2021-12-12 11:03:28 -05:00
|
|
|
info!("listening on: {}", addr);
|
2021-12-11 22:43:41 -05:00
|
|
|
// all client-submitted valid events are broadcast to every
|
|
|
|
// other client on this channel. This should be large enough
|
|
|
|
// to accomodate slower readers (messages are dropped if
|
|
|
|
// clients can not keep up).
|
2021-12-29 23:13:02 -05:00
|
|
|
let (bcast_tx, _) = broadcast::channel::<Event>(settings.limits.broadcast_buffer);
|
2021-12-11 22:43:41 -05:00
|
|
|
// validated events that need to be persisted are sent to the
|
|
|
|
// database on via this channel.
|
2021-12-29 23:13:02 -05:00
|
|
|
let (event_tx, event_rx) = mpsc::channel::<Event>(settings.limits.event_persist_buffer);
|
2021-12-11 22:43:41 -05:00
|
|
|
// establish a channel for letting all threads now about a
|
|
|
|
// requested server shutdown.
|
2021-12-11 16:48:59 -05:00
|
|
|
let (invoke_shutdown, _) = broadcast::channel::<()>(1);
|
2021-12-11 22:43:41 -05:00
|
|
|
let ctrl_c_shutdown = invoke_shutdown.clone();
|
2021-12-11 16:48:59 -05:00
|
|
|
// listen for ctrl-c interruupts
|
|
|
|
tokio::spawn(async move {
|
|
|
|
tokio::signal::ctrl_c().await.unwrap();
|
2021-12-12 11:03:28 -05:00
|
|
|
info!("shutting down due to SIGINT");
|
2021-12-11 22:43:41 -05:00
|
|
|
ctrl_c_shutdown.send(()).ok();
|
2021-12-11 16:48:59 -05:00
|
|
|
});
|
2021-12-30 22:07:21 -05:00
|
|
|
// start the database writer thread. Give it a channel for
|
|
|
|
// writing events, and for publishing events that have been
|
|
|
|
// written (to all connected clients).
|
|
|
|
db::db_writer(event_rx, bcast_tx.clone(), invoke_shutdown.subscribe()).await;
|
|
|
|
|
2021-12-12 11:58:00 -05:00
|
|
|
// track unique client connection count
|
|
|
|
let mut client_accept_count: usize = 0;
|
2021-12-11 16:48:59 -05:00
|
|
|
let mut stop_listening = invoke_shutdown.subscribe();
|
2021-12-11 22:43:41 -05:00
|
|
|
// handle new client connection requests, or SIGINT signals.
|
2021-12-11 16:48:59 -05:00
|
|
|
loop {
|
|
|
|
tokio::select! {
|
|
|
|
_ = stop_listening.recv() => {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Ok((stream, _)) = listener.accept() => {
|
2021-12-12 11:58:00 -05:00
|
|
|
client_accept_count += 1;
|
|
|
|
info!("creating new connection for client #{}",client_accept_count);
|
2021-12-11 16:48:59 -05:00
|
|
|
tokio::spawn(nostr_server(
|
|
|
|
stream,
|
|
|
|
bcast_tx.clone(),
|
|
|
|
event_tx.clone(),
|
|
|
|
invoke_shutdown.subscribe(),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
2021-12-05 17:53:26 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-12-11 22:43:41 -05:00
|
|
|
/// Handle new client connections. This runs through an event loop
|
|
|
|
/// for all client communication.
|
2021-12-05 17:53:26 -05:00
|
|
|
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
|
2021-12-05 21:28:02 -05:00
|
|
|
let mut bcast_rx = broadcast.subscribe();
|
2021-12-29 23:13:02 -05:00
|
|
|
let mut config = WebSocketConfig::default();
|
|
|
|
{
|
2021-12-29 23:47:31 -05:00
|
|
|
let settings = config::SETTINGS.read().unwrap();
|
2021-12-29 23:13:02 -05:00
|
|
|
config.max_message_size = settings.limits.max_ws_message_bytes;
|
|
|
|
config.max_frame_size = settings.limits.max_ws_frame_bytes;
|
|
|
|
}
|
2021-12-11 16:48:59 -05:00
|
|
|
// upgrade the TCP connection to WebSocket
|
2021-12-19 17:26:32 -05:00
|
|
|
let conn = tokio_tungstenite::accept_async_with_config(stream, Some(config)).await;
|
2021-12-05 17:53:26 -05:00
|
|
|
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-12 11:03:28 -05:00
|
|
|
// for stats, keep track of how many events the client published,
|
|
|
|
// and how many it received from queries.
|
|
|
|
let mut client_published_event_count: usize = 0;
|
|
|
|
let mut client_received_event_count: usize = 0;
|
|
|
|
info!("new connection for client: {}", cid);
|
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() => {
|
2021-12-12 11:03:28 -05:00
|
|
|
// database informed us of a query result we asked for
|
2021-12-11 16:48:59 -05:00
|
|
|
let res = EventRes(query_result.sub_id,query_result.event);
|
2021-12-12 11:03:28 -05:00
|
|
|
client_received_event_count += 1;
|
2021-12-11 16:48:59 -05:00
|
|
|
nostr_stream.send(res).await.ok();
|
|
|
|
},
|
2021-12-05 21:28:02 -05:00
|
|
|
Ok(global_event) = bcast_rx.recv() => {
|
2021-12-12 11:03:28 -05:00
|
|
|
// an event has been broadcast to all clients
|
|
|
|
// first check if there is a subscription for this event.
|
2021-12-14 22:38:26 -05:00
|
|
|
let matching_subs = conn.get_matching_subscriptions(&global_event);
|
|
|
|
for s in matching_subs {
|
2021-12-12 11:03:28 -05:00
|
|
|
// TODO: serialize at broadcast time, instead of
|
|
|
|
// once for each consumer.
|
|
|
|
if let Ok(event_str) = serde_json::to_string(&global_event) {
|
|
|
|
debug!("sub match: client: {}, sub: {}, event: {}",
|
2021-12-14 22:38:26 -05:00
|
|
|
cid, s,
|
2021-12-12 11:03:28 -05:00
|
|
|
global_event.get_event_id_prefix());
|
2021-12-05 21:28:02 -05:00
|
|
|
// create an event response and send it
|
2021-12-14 22:38:26 -05:00
|
|
|
let res = EventRes(s.to_owned(),event_str);
|
2021-12-05 21:28:02 -05:00
|
|
|
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-12 11:03:28 -05:00
|
|
|
debug!("successfully parsed/validated event: {} from client: {}", id_prefix, cid);
|
2021-12-11 16:48:59 -05:00
|
|
|
// Write this to the database
|
|
|
|
event_tx.send(e.clone()).await.ok();
|
2021-12-12 11:03:28 -05:00
|
|
|
client_published_event_count += 1;
|
2021-12-05 21:28:02 -05:00
|
|
|
},
|
2021-12-12 11:50:16 -05:00
|
|
|
Err(_) => {
|
|
|
|
info!("client {} sent an invalid event", cid);
|
|
|
|
nostr_stream.send(NoticeRes("event was invalid".to_owned())).await.ok();
|
|
|
|
}
|
2021-12-05 18:15:50 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
Some(Ok(SubMsg(s))) => {
|
2021-12-12 11:03:28 -05:00
|
|
|
debug!("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::<()>();
|
2021-12-12 11:50:16 -05:00
|
|
|
match conn.subscribe(s.clone()) {
|
|
|
|
Ok(()) => {
|
|
|
|
running_queries.insert(s.id.to_owned(), abandon_query_tx);
|
|
|
|
// start a database query
|
|
|
|
db::db_query(s, query_tx.clone(), abandon_query_rx).await;
|
|
|
|
},
|
|
|
|
Err(e) => {
|
|
|
|
info!("Subscription error: {}", e);
|
|
|
|
nostr_stream.send(NoticeRes(format!("{}",e))).await.ok();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
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) => {
|
2021-12-12 11:03:28 -05:00
|
|
|
// check if a query is currently
|
|
|
|
// running, and remove it if so.
|
2021-12-11 16:48:59 -05:00
|
|
|
let stop_tx = running_queries.remove(&c.id);
|
2021-12-11 22:56:52 -05:00
|
|
|
if let Some(tx) = stop_tx {
|
|
|
|
tx.send(()).ok();
|
2021-12-11 16:48:59 -05:00
|
|
|
}
|
2021-12-12 11:03:28 -05:00
|
|
|
// stop checking new events against
|
|
|
|
// the subscription
|
2021-12-11 16:48:59 -05:00
|
|
|
conn.unsubscribe(c);
|
|
|
|
},
|
2021-12-12 11:50:16 -05:00
|
|
|
Err(_) => {
|
|
|
|
info!("invalid command ignored");
|
|
|
|
|
|
|
|
}
|
2021-12-05 19:14:14 -05:00
|
|
|
}
|
2021-12-05 17:53:26 -05:00
|
|
|
},
|
|
|
|
None => {
|
2021-12-12 11:03:28 -05:00
|
|
|
debug!("normal websocket close from client: {}",cid);
|
2021-12-11 16:48:59 -05:00
|
|
|
break;
|
2021-12-05 17:53:26 -05:00
|
|
|
},
|
|
|
|
Some(Err(Error::ConnError)) => {
|
2021-12-12 11:03:28 -05:00
|
|
|
debug!("got connection close/error, disconnecting client: {}",cid);
|
2021-12-11 16:48:59 -05:00
|
|
|
break;
|
2021-12-05 17:53:26 -05:00
|
|
|
}
|
2021-12-31 16:19:35 -05:00
|
|
|
Some(Err(Error::EventMaxLengthError(s))) => {
|
|
|
|
info!("client {} sent event larger ({} bytes) than max size", cid, s);
|
|
|
|
nostr_stream.send(NoticeRes("event exceeded max size".to_owned())).await.ok();
|
|
|
|
},
|
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();
|
|
|
|
}
|
2021-12-12 11:03:28 -05:00
|
|
|
info!(
|
|
|
|
"stopping connection for client: {} (client sent {} event(s), received {})",
|
|
|
|
cid, client_published_event_count, client_received_event_count
|
|
|
|
);
|
2021-12-05 09:42:28 -05:00
|
|
|
}
|