Fork Sync: Update from parent repository (#5)

* docs: typo in `build-essential` package name

* improvement(NIP-42): use 'restricted:' prefix for auth error msgs

* docs: add database maintenance example queries

* feat: allow logging output to file

* feat: roll over logs daily

* refactor: reorder imports

* improvement: default to logging on stdout

* fix: ensure startup SQL runs, even with zero min writers

---------

Co-authored-by: thesimplekid <tsk@thesimplekid.com>
Co-authored-by: rorp <rorp@protonmail.com>
Co-authored-by: Yuval Adam <_@yuv.al>
Co-authored-by: Jamin M <jaminmenter@outlook.com>
Co-authored-by: Greg Heartsfield <scsibug@imap.cc>
This commit is contained in:
github-actions[bot] 2023-06-25 11:02:44 +02:00 committed by GitHub
parent 31331aa1e4
commit 70da8eff42
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 132 additions and 64 deletions

118
Cargo.lock generated
View File

@ -54,15 +54,6 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "ansi_term"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2"
dependencies = [
"winapi",
]
[[package]] [[package]]
name = "anyhow" name = "anyhow"
version = "1.0.69" version = "1.0.69"
@ -420,7 +411,7 @@ dependencies = [
"js-sys", "js-sys",
"num-integer", "num-integer",
"num-traits", "num-traits",
"time", "time 0.1.45",
"wasm-bindgen", "wasm-bindgen",
"winapi", "winapi",
] ]
@ -565,7 +556,7 @@ dependencies = [
"tonic", "tonic",
"tracing", "tracing",
"tracing-core", "tracing-core",
"tracing-subscriber 0.3.16", "tracing-subscriber",
] ]
[[package]] [[package]]
@ -1511,15 +1502,6 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "matchers"
version = "0.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f099785f7595cc4b4553a174ce30dd7589ef93391ff414dbb67f62392b9e0ce1"
dependencies = [
"regex-automata",
]
[[package]] [[package]]
name = "matchers" name = "matchers"
version = "0.1.0" version = "0.1.0"
@ -1695,12 +1677,23 @@ dependencies = [
"tonic", "tonic",
"tonic-build", "tonic-build",
"tracing", "tracing",
"tracing-subscriber 0.2.25", "tracing-appender",
"tracing-subscriber",
"tungstenite", "tungstenite",
"url", "url",
"uuid", "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]] [[package]]
name = "num" name = "num"
version = "0.2.1" version = "0.2.1"
@ -1861,6 +1854,12 @@ version = "6.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b7820b9daea5457c9f21c69448905d723fbd21136ccf521748f23fd49e723ee" checksum = "9b7820b9daea5457c9f21c69448905d723fbd21136ccf521748f23fd49e723ee"
[[package]]
name = "overload"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39"
[[package]] [[package]]
name = "parking" name = "parking"
version = "2.0.0" version = "2.0.0"
@ -2990,6 +2989,33 @@ dependencies = [
"winapi", "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]] [[package]]
name = "tinyvec" name = "tinyvec"
version = "1.6.0" version = "1.6.0"
@ -3223,6 +3249,17 @@ dependencies = [
"tracing-core", "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]] [[package]]
name = "tracing-attributes" name = "tracing-attributes"
version = "0.1.23" version = "0.1.23"
@ -3265,51 +3302,22 @@ dependencies = [
"tracing-core", "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]] [[package]]
name = "tracing-subscriber" name = "tracing-subscriber"
version = "0.2.25" version = "0.3.16"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0e0d2eaa99c3c2e41547cfa109e910a68ea03823cccad4a0525dcbc9b01e8c71" checksum = "a6176eae26dd70d0c919749377897b54a9276bd7061339665dd68777926b5a70"
dependencies = [ dependencies = [
"ansi_term", "matchers",
"chrono", "nu-ansi-term",
"lazy_static", "once_cell",
"matchers 0.0.1",
"regex", "regex",
"serde",
"serde_json",
"sharded-slab", "sharded-slab",
"smallvec", "smallvec",
"thread_local", "thread_local",
"tracing", "tracing",
"tracing-core", "tracing-core",
"tracing-log", "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]] [[package]]

View File

@ -13,8 +13,9 @@ categories = ["network-programming", "web-programming"]
[dependencies] [dependencies]
clap = { version = "4.0.32", features = ["env", "default", "derive"]} clap = { version = "4.0.32", features = ["env", "default", "derive"]}
tracing = "0.1.36" tracing = "0.1.37"
tracing-subscriber = "0.2.0" tracing-appender = "0.2.2"
tracing-subscriber = "0.3.16"
tokio = { version = "1", features = ["full", "tracing", "signal"] } tokio = { version = "1", features = ["full", "tracing", "signal"] }
prost = "0.11" prost = "0.11"
tonic = "0.8.3" tonic = "0.8.3"

View File

@ -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: The following OS packages will be helpful; on Debian/Ubuntu:
```console ```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: Clone this repository, and then build a release version of the relay:

View File

@ -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. # different nodes. Ignore for single-database instances.
#connection_write = "postgresql://postgres:nostr@localhost:7500/nostr" #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]
# gRPC interfaces for externalized decisions and other extensions to # gRPC interfaces for externalized decisions and other extensions to
# functionality. # functionality.

View File

@ -78,18 +78,24 @@ PRAGMA foreign_keys = ON;
delete from event where event_hash=x'00000000000c1271675dc86e3e1dd1336827bccabb90dc4c9d3b4465efefe00e'; delete from event where event_hash=x'00000000000c1271675dc86e3e1dd1336827bccabb90dc4c9d3b4465efefe00e';
``` ```
### Deleting All Events for Pubkey ### Querying and Deleting All Events for Pubkey
```console ```console
PRAGMA foreign_keys = ON; 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'; delete from event where author=x'000000000002c7831d9c5a99f183afc2813a6f69a16edda7f6fc0ed8110566e6';
``` ```
### Deleting All Events of a Kind ### Querying and Deleting All Events of a Kind
```console ```console
PRAGMA foreign_keys = ON; 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; delete from event where kind=70202;
``` ```

View File

@ -166,6 +166,13 @@ impl VerifiedUsers {
} }
} }
#[derive(Debug, Clone, Serialize, Deserialize)]
#[allow(unused)]
pub struct Logging {
pub folder_path: Option<String>,
pub file_prefix: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
#[allow(unused)] #[allow(unused)]
pub struct Settings { pub struct Settings {
@ -180,6 +187,7 @@ pub struct Settings {
pub verified_users: VerifiedUsers, pub verified_users: VerifiedUsers,
pub retention: Retention, pub retention: Retention,
pub options: Options, pub options: Options,
pub logging: Logging,
} }
impl Settings { impl Settings {
@ -320,6 +328,10 @@ impl Default for Settings {
options: Options { options: Options {
reject_future_seconds: None, // Reject events in the future if defined reject_future_seconds: None, // Reject events in the future if defined
}, },
logging: Logging {
folder_path: None,
file_prefix: None,
},
} }
} }
} }

View File

@ -7,9 +7,11 @@ use nostr_rs_relay::server::start_server;
use std::sync::mpsc as syncmpsc; use std::sync::mpsc as syncmpsc;
use std::sync::mpsc::{Receiver as MpscReceiver, Sender as MpscSender}; use std::sync::mpsc::{Receiver as MpscReceiver, Sender as MpscSender};
use std::thread; use std::thread;
use tracing::info;
#[cfg(not(target_env = "msvc"))] #[cfg(not(target_env = "msvc"))]
use tikv_jemallocator::Jemalloc; use tikv_jemallocator::Jemalloc;
use tracing::info;
use tracing_appender::non_blocking::WorkerGuard;
use tracing_subscriber::EnvFilter;
#[cfg(not(target_env = "msvc"))] #[cfg(not(target_env = "msvc"))]
#[global_allocator] #[global_allocator]
@ -22,6 +24,8 @@ fn main() {
// get config file name from args // get config file name from args
let config_file_arg = args.config; let config_file_arg = args.config;
let mut _log_guard: Option<WorkerGuard> = None;
// configure settings from the config file (defaults to config.toml) // configure settings from the config file (defaults to config.toml)
// replace default settings with those read from the config file // replace default settings with those read from the config file
let mut settings = config::Settings::new(&config_file_arg); let mut settings = config::Settings::new(&config_file_arg);
@ -32,8 +36,28 @@ fn main() {
ConsoleLayer::builder().with_default_env().init(); ConsoleLayer::builder().with_default_env().init();
} else { } else {
// standard logging // standard logging
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(); tracing_subscriber::fmt::try_init().unwrap();
} }
}
info!("Starting up from main"); info!("Starting up from main");
// get database directory from args // get database directory from args

View File

@ -5,6 +5,7 @@ pub enum EventResultStatus {
Blocked, Blocked,
RateLimited, RateLimited,
Error, Error,
Restricted,
} }
pub struct EventResult { pub struct EventResult {
@ -24,7 +25,7 @@ impl EventResultStatus {
pub fn to_bool(&self) -> bool { pub fn to_bool(&self) -> bool {
match self { match self {
Self::Duplicate | Self::Saved => true, 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::Blocked => "blocked",
Self::RateLimited => "rate-limited", Self::RateLimited => "rate-limited",
Self::Error => "error", Self::Error => "error",
Self::Restricted => "restricted",
} }
} }
} }
@ -81,6 +83,11 @@ impl Notice {
Notice::prefixed(id, msg, EventResultStatus::Error) Notice::prefixed(id, msg, EventResultStatus::Error)
} }
#[must_use]
pub fn restricted(id: String, msg: &str) -> Notice {
Notice::prefixed(id, msg, EventResultStatus::Restricted)
}
#[must_use] #[must_use]
pub fn saved(id: String) -> Notice { pub fn saved(id: String) -> Notice {
Notice::EventResult(EventResult { Notice::EventResult(EventResult {

View File

@ -1203,6 +1203,11 @@ pub fn build_pool(
.max_lifetime(Some(Duration::from_secs(30))) .max_lifetime(Some(Duration::from_secs(30)))
.build(manager) .build(manager)
.unwrap(); .unwrap();
// retrieve a connection to ensure the startup statements run immediately
{
let _ = pool.get();
}
info!( info!(
"Built a connection pool {:?} (min={}, max={})", "Built a connection pool {:?} (min={}, max={})",
name, min_size, max_size name, min_size, max_size

View File

@ -1317,7 +1317,7 @@ async fn nostr_server(
}, },
Err(e) => { Err(e) => {
info!("authentication error: {} (cid: {})", e, cid); 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();
}, },
} }
} }