diff --git a/Cargo.lock b/Cargo.lock index 9533514..e017cb9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -54,15 +54,6 @@ dependencies = [ "libc", ] -[[package]] -name = "ansi_term" -version = "0.12.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2" -dependencies = [ - "winapi", -] - [[package]] name = "anyhow" version = "1.0.69" @@ -420,7 +411,7 @@ dependencies = [ "js-sys", "num-integer", "num-traits", - "time", + "time 0.1.45", "wasm-bindgen", "winapi", ] @@ -565,7 +556,7 @@ dependencies = [ "tonic", "tracing", "tracing-core", - "tracing-subscriber 0.3.16", + "tracing-subscriber", ] [[package]] @@ -1511,15 +1502,6 @@ dependencies = [ "libc", ] -[[package]] -name = "matchers" -version = "0.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f099785f7595cc4b4553a174ce30dd7589ef93391ff414dbb67f62392b9e0ce1" -dependencies = [ - "regex-automata", -] - [[package]] name = "matchers" version = "0.1.0" @@ -1695,12 +1677,23 @@ dependencies = [ "tonic", "tonic-build", "tracing", - "tracing-subscriber 0.2.25", + "tracing-appender", + "tracing-subscriber", "tungstenite", "url", "uuid", ] +[[package]] +name = "nu-ansi-term" +version = "0.46.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77a8165726e8236064dbb45459242600304b42a5ea24ee2948e18e023bf7ba84" +dependencies = [ + "overload", + "winapi", +] + [[package]] name = "num" version = "0.2.1" @@ -1861,6 +1854,12 @@ version = "6.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b7820b9daea5457c9f21c69448905d723fbd21136ccf521748f23fd49e723ee" +[[package]] +name = "overload" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39" + [[package]] name = "parking" version = "2.0.0" @@ -2990,6 +2989,33 @@ dependencies = [ "winapi", ] +[[package]] +name = "time" +version = "0.3.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd0cbfecb4d19b5ea75bb31ad904eb5b9fa13f21079c3b92017ebdf4999a5890" +dependencies = [ + "itoa", + "serde", + "time-core", + "time-macros", +] + +[[package]] +name = "time-core" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2e153e1f1acaef8acc537e68b44906d2db6436e2b35ac2c6b42640fff91f00fd" + +[[package]] +name = "time-macros" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd80a657e71da814b8e5d60d3374fc6d35045062245d80224748ae522dd76f36" +dependencies = [ + "time-core", +] + [[package]] name = "tinyvec" version = "1.6.0" @@ -3223,6 +3249,17 @@ dependencies = [ "tracing-core", ] +[[package]] +name = "tracing-appender" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09d48f71a791638519505cefafe162606f706c25592e4bde4d97600c0195312e" +dependencies = [ + "crossbeam-channel", + "time 0.3.20", + "tracing-subscriber", +] + [[package]] name = "tracing-attributes" version = "0.1.23" @@ -3265,51 +3302,22 @@ dependencies = [ "tracing-core", ] -[[package]] -name = "tracing-serde" -version = "0.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bc6b213177105856957181934e4920de57730fc69bf42c37ee5bb664d406d9e1" -dependencies = [ - "serde", - "tracing-core", -] - [[package]] name = "tracing-subscriber" -version = "0.2.25" +version = "0.3.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0e0d2eaa99c3c2e41547cfa109e910a68ea03823cccad4a0525dcbc9b01e8c71" +checksum = "a6176eae26dd70d0c919749377897b54a9276bd7061339665dd68777926b5a70" dependencies = [ - "ansi_term", - "chrono", - "lazy_static", - "matchers 0.0.1", + "matchers", + "nu-ansi-term", + "once_cell", "regex", - "serde", - "serde_json", "sharded-slab", "smallvec", "thread_local", "tracing", "tracing-core", "tracing-log", - "tracing-serde", -] - -[[package]] -name = "tracing-subscriber" -version = "0.3.16" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a6176eae26dd70d0c919749377897b54a9276bd7061339665dd68777926b5a70" -dependencies = [ - "matchers 0.1.0", - "once_cell", - "regex", - "sharded-slab", - "thread_local", - "tracing", - "tracing-core", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index b04ef07..ce43d2b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,8 +13,9 @@ categories = ["network-programming", "web-programming"] [dependencies] clap = { version = "4.0.32", features = ["env", "default", "derive"]} -tracing = "0.1.36" -tracing-subscriber = "0.2.0" +tracing = "0.1.37" +tracing-appender = "0.2.2" +tracing-subscriber = "0.3.16" tokio = { version = "1", features = ["full", "tracing", "signal"] } prost = "0.11" tonic = "0.8.3" diff --git a/README.md b/README.md index 09a66f7..5fbd233 100644 --- a/README.md +++ b/README.md @@ -95,7 +95,7 @@ Building `nostr-rs-relay` requires an installation of Cargo & Rust: https://www. The following OS packages will be helpful; on Debian/Ubuntu: ```console -$ sudo apt-get install build-essentials cmake protobuf-compiler pkg-config libssl-dev +$ sudo apt-get install build-essential cmake protobuf-compiler pkg-config libssl-dev ``` Clone this repository, and then build a release version of the relay: diff --git a/config.toml b/config.toml index 3dea8ee..e6db8d5 100644 --- a/config.toml +++ b/config.toml @@ -57,6 +57,11 @@ description = "A newly created nostr-rs-relay.\n\nCustomize this with your own i # different nodes. Ignore for single-database instances. #connection_write = "postgresql://postgres:nostr@localhost:7500/nostr" +[logging] +# Directory to store log files. Log files roll over daily. +#folder_path = "./log" +#file_prefix = "nostr-relay" + [grpc] # gRPC interfaces for externalized decisions and other extensions to # functionality. diff --git a/docs/database-maintenance.md b/docs/database-maintenance.md index 82b39e3..e475918 100644 --- a/docs/database-maintenance.md +++ b/docs/database-maintenance.md @@ -78,18 +78,24 @@ PRAGMA foreign_keys = ON; delete from event where event_hash=x'00000000000c1271675dc86e3e1dd1336827bccabb90dc4c9d3b4465efefe00e'; ``` -### Deleting All Events for Pubkey +### Querying and Deleting All Events for Pubkey ```console PRAGMA foreign_keys = ON; + +select lower(hex(author)) as author, count(*) as c from event group by author order by c asc; + delete from event where author=x'000000000002c7831d9c5a99f183afc2813a6f69a16edda7f6fc0ed8110566e6'; ``` -### Deleting All Events of a Kind +### Querying and Deleting All Events of a Kind ```console PRAGMA foreign_keys = ON; + +select printf('%7d', kind), count(*) as c from event group by kind order by c; + delete from event where kind=70202; ``` diff --git a/src/config.rs b/src/config.rs index 32ac2d3..9d92d39 100644 --- a/src/config.rs +++ b/src/config.rs @@ -166,6 +166,13 @@ impl VerifiedUsers { } } +#[derive(Debug, Clone, Serialize, Deserialize)] +#[allow(unused)] +pub struct Logging { + pub folder_path: Option, + pub file_prefix: Option, +} + #[derive(Debug, Clone, Serialize, Deserialize)] #[allow(unused)] pub struct Settings { @@ -180,6 +187,7 @@ pub struct Settings { pub verified_users: VerifiedUsers, pub retention: Retention, pub options: Options, + pub logging: Logging, } impl Settings { @@ -320,6 +328,10 @@ impl Default for Settings { options: Options { reject_future_seconds: None, // Reject events in the future if defined }, + logging: Logging { + folder_path: None, + file_prefix: None, + }, } } } diff --git a/src/main.rs b/src/main.rs index 6a9116d..6e8994e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,9 +7,11 @@ use nostr_rs_relay::server::start_server; use std::sync::mpsc as syncmpsc; use std::sync::mpsc::{Receiver as MpscReceiver, Sender as MpscSender}; use std::thread; -use tracing::info; #[cfg(not(target_env = "msvc"))] use tikv_jemallocator::Jemalloc; +use tracing::info; +use tracing_appender::non_blocking::WorkerGuard; +use tracing_subscriber::EnvFilter; #[cfg(not(target_env = "msvc"))] #[global_allocator] @@ -22,6 +24,8 @@ fn main() { // get config file name from args let config_file_arg = args.config; + let mut _log_guard: Option = None; + // configure settings from the config file (defaults to config.toml) // replace default settings with those read from the config file let mut settings = config::Settings::new(&config_file_arg); @@ -32,7 +36,27 @@ fn main() { ConsoleLayer::builder().with_default_env().init(); } else { // standard logging - tracing_subscriber::fmt::try_init().unwrap(); + if let Some(path) = &settings.logging.folder_path { + // write logs to a folder + let prefix = match &settings.logging.file_prefix { + Some(p) => p.as_str(), + None => "relay", + }; + let file_appender = tracing_appender::rolling::daily(path, prefix); + let (non_blocking, guard) = tracing_appender::non_blocking(file_appender); + let filter = EnvFilter::from_default_env(); + // assign to a variable that is not dropped till the program ends + _log_guard = Some(guard); + + tracing_subscriber::fmt() + .with_env_filter(filter) + .with_writer(non_blocking) + .try_init() + .unwrap(); + } else { + // write to stdout + tracing_subscriber::fmt::try_init().unwrap(); + } } info!("Starting up from main"); diff --git a/src/notice.rs b/src/notice.rs index d243acc..6875de9 100644 --- a/src/notice.rs +++ b/src/notice.rs @@ -5,6 +5,7 @@ pub enum EventResultStatus { Blocked, RateLimited, Error, + Restricted, } pub struct EventResult { @@ -24,7 +25,7 @@ impl EventResultStatus { pub fn to_bool(&self) -> bool { match self { Self::Duplicate | Self::Saved => true, - Self::Invalid | Self::Blocked | Self::RateLimited | Self::Error => false, + Self::Invalid | Self::Blocked | Self::RateLimited | Self::Error | Self::Restricted => false, } } @@ -37,6 +38,7 @@ impl EventResultStatus { Self::Blocked => "blocked", Self::RateLimited => "rate-limited", Self::Error => "error", + Self::Restricted => "restricted", } } } @@ -81,6 +83,11 @@ impl Notice { Notice::prefixed(id, msg, EventResultStatus::Error) } + #[must_use] + pub fn restricted(id: String, msg: &str) -> Notice { + Notice::prefixed(id, msg, EventResultStatus::Restricted) + } + #[must_use] pub fn saved(id: String) -> Notice { Notice::EventResult(EventResult { diff --git a/src/repo/sqlite.rs b/src/repo/sqlite.rs index b156f0c..3ad59bd 100644 --- a/src/repo/sqlite.rs +++ b/src/repo/sqlite.rs @@ -1203,6 +1203,11 @@ pub fn build_pool( .max_lifetime(Some(Duration::from_secs(30))) .build(manager) .unwrap(); + // retrieve a connection to ensure the startup statements run immediately + { + let _ = pool.get(); + } + info!( "Built a connection pool {:?} (min={}, max={})", name, min_size, max_size diff --git a/src/server.rs b/src/server.rs index cfc5a5c..91625b1 100644 --- a/src/server.rs +++ b/src/server.rs @@ -1317,7 +1317,7 @@ async fn nostr_server( }, Err(e) => { info!("authentication error: {} (cid: {})", e, cid); - ws_stream.send(make_notice_message(&Notice::message(format!("Authentication error: {e}")))).await.ok(); + ws_stream.send(make_notice_message(&Notice::restricted(event.id, format!("authentication error: {e}").as_str()))).await.ok(); }, } }