mirror of
https://github.com/scsibug/nostr-rs-relay.git
synced 2024-11-24 17:49:07 -05:00
Allow disabling grpc at the build level
This commit is contained in:
parent
26f296f76f
commit
c83ba7db39
10
Cargo.toml
10
Cargo.toml
|
@ -11,14 +11,18 @@ license = "MIT"
|
||||||
keywords = ["nostr", "server"]
|
keywords = ["nostr", "server"]
|
||||||
categories = ["network-programming", "web-programming"]
|
categories = ["network-programming", "web-programming"]
|
||||||
|
|
||||||
|
[features]
|
||||||
|
default = ["grpc"]
|
||||||
|
grpc = ["tonic", "prost", "tonic-build"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
clap = { version = "4.0.32", features = ["env", "default", "derive"]}
|
clap = { version = "4.0.32", features = ["env", "default", "derive"]}
|
||||||
tracing = "0.1.37"
|
tracing = "0.1.37"
|
||||||
tracing-appender = "0.2.2"
|
tracing-appender = "0.2.2"
|
||||||
tracing-subscriber = "0.3.16"
|
tracing-subscriber = "0.3.16"
|
||||||
tokio = { version = "1", features = ["full", "tracing", "signal"] }
|
tokio = { version = "1", features = ["full", "tracing", "signal"] }
|
||||||
prost = "0.11"
|
prost = { version = "0.11", optional = true }
|
||||||
tonic = "0.8.3"
|
tonic = { version = "0.8.3", optional = true }
|
||||||
console-subscriber = "0.1.8"
|
console-subscriber = "0.1.8"
|
||||||
futures = "0.3"
|
futures = "0.3"
|
||||||
futures-util = "0.3"
|
futures-util = "0.3"
|
||||||
|
@ -63,4 +67,4 @@ log = "0.4"
|
||||||
anyhow = "1"
|
anyhow = "1"
|
||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
tonic-build = { version="0.8.3", features = ["prost"] }
|
tonic-build = { version="0.8.3", features = ["prost"], optional = true }
|
||||||
|
|
1
build.rs
1
build.rs
|
@ -1,4 +1,5 @@
|
||||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
#[cfg(feature = "grpc")]
|
||||||
tonic_build::configure()
|
tonic_build::configure()
|
||||||
.build_server(false)
|
.build_server(false)
|
||||||
.protoc_arg("--experimental_allow_proto3_optional")
|
.protoc_arg("--experimental_allow_proto3_optional")
|
||||||
|
|
69
src/db.rs
69
src/db.rs
|
@ -2,6 +2,7 @@
|
||||||
use crate::config::Settings;
|
use crate::config::Settings;
|
||||||
use crate::error::{Error, Result};
|
use crate::error::{Error, Result};
|
||||||
use crate::event::Event;
|
use crate::event::Event;
|
||||||
|
#[cfg(feature = "grpc")]
|
||||||
use crate::nauthz;
|
use crate::nauthz;
|
||||||
use crate::notice::Notice;
|
use crate::notice::Notice;
|
||||||
use crate::payment::PaymentMessage;
|
use crate::payment::PaymentMessage;
|
||||||
|
@ -136,6 +137,7 @@ pub async fn db_writer(
|
||||||
}
|
}
|
||||||
// create a client if GRPC is enabled.
|
// create a client if GRPC is enabled.
|
||||||
// Check with externalized event admitter service, if one is defined.
|
// Check with externalized event admitter service, if one is defined.
|
||||||
|
#[cfg(feature = "grpc")]
|
||||||
let mut grpc_client = if let Some(svr) = settings.grpc.event_admission_server {
|
let mut grpc_client = if let Some(svr) = settings.grpc.event_admission_server {
|
||||||
Some(nauthz::EventAuthzService::connect(&svr).await)
|
Some(nauthz::EventAuthzService::connect(&svr).await)
|
||||||
} else {
|
} else {
|
||||||
|
@ -345,29 +347,31 @@ pub async fn db_writer(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// nip05 address
|
|
||||||
let nip05_address: Option<crate::nip05::Nip05Name> =
|
|
||||||
validation.and_then(|x| x.ok().map(|y| y.name));
|
|
||||||
|
|
||||||
// GRPC check
|
// GRPC check
|
||||||
if let Some(ref mut c) = grpc_client {
|
#[cfg(feature = "grpc")]
|
||||||
trace!("checking if grpc permits");
|
{
|
||||||
let grpc_start = Instant::now();
|
// nip05 address
|
||||||
let decision_res = c
|
let nip05_address: Option<crate::nip05::Nip05Name> =
|
||||||
.admit_event(
|
validation.and_then(|x| x.ok().map(|y| y.name));
|
||||||
&event,
|
|
||||||
&subm_event.source_ip,
|
if let Some(ref mut c) = grpc_client {
|
||||||
subm_event.origin,
|
trace!("checking if grpc permits");
|
||||||
subm_event.user_agent,
|
let grpc_start = Instant::now();
|
||||||
nip05_address,
|
let decision_res = c
|
||||||
subm_event.auth_pubkey,
|
.admit_event(
|
||||||
)
|
&event,
|
||||||
.await;
|
&subm_event.source_ip,
|
||||||
match decision_res {
|
subm_event.origin,
|
||||||
Ok(decision) => {
|
subm_event.user_agent,
|
||||||
if !decision.permitted() {
|
nip05_address,
|
||||||
// GPRC returned a decision to reject this event
|
subm_event.auth_pubkey,
|
||||||
info!(
|
)
|
||||||
|
.await;
|
||||||
|
match decision_res {
|
||||||
|
Ok(decision) => {
|
||||||
|
if !decision.permitted() {
|
||||||
|
// GPRC returned a decision to reject this event
|
||||||
|
info!(
|
||||||
"GRPC rejected event: {:?} (kind: {}) from: {:?} in: {:?} (IP: {:?})",
|
"GRPC rejected event: {:?} (kind: {}) from: {:?} in: {:?} (IP: {:?})",
|
||||||
event.get_event_id_prefix(),
|
event.get_event_id_prefix(),
|
||||||
event.kind,
|
event.kind,
|
||||||
|
@ -375,17 +379,18 @@ pub async fn db_writer(
|
||||||
grpc_start.elapsed(),
|
grpc_start.elapsed(),
|
||||||
subm_event.source_ip
|
subm_event.source_ip
|
||||||
);
|
);
|
||||||
notice_tx
|
notice_tx
|
||||||
.try_send(Notice::blocked(
|
.try_send(Notice::blocked(
|
||||||
event.id,
|
event.id,
|
||||||
&decision.message().unwrap_or_default(),
|
&decision.message().unwrap_or_default(),
|
||||||
))
|
))
|
||||||
.ok();
|
.ok();
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
warn!("GRPC server error: {:?}", e);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
warn!("GRPC server error: {:?}", e);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -66,6 +66,7 @@ pub enum Error {
|
||||||
ChannelClosed,
|
ChannelClosed,
|
||||||
#[error("Authz error")]
|
#[error("Authz error")]
|
||||||
AuthzError,
|
AuthzError,
|
||||||
|
#[cfg(feature = "grpc")]
|
||||||
#[error("Tonic GRPC error")]
|
#[error("Tonic GRPC error")]
|
||||||
TonicError(tonic::Status),
|
TonicError(tonic::Status),
|
||||||
#[error("Invalid AUTH message")]
|
#[error("Invalid AUTH message")]
|
||||||
|
@ -151,6 +152,7 @@ impl From<config::ConfigError> for Error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "grpc")]
|
||||||
impl From<tonic::Status> for Error {
|
impl From<tonic::Status> for Error {
|
||||||
/// Wrap Config error
|
/// Wrap Config error
|
||||||
fn from(r: tonic::Status) -> Self {
|
fn from(r: tonic::Status) -> Self {
|
||||||
|
|
|
@ -8,6 +8,7 @@ pub mod error;
|
||||||
pub mod event;
|
pub mod event;
|
||||||
pub mod hexrange;
|
pub mod hexrange;
|
||||||
pub mod info;
|
pub mod info;
|
||||||
|
#[cfg(feature = "grpc")]
|
||||||
pub mod nauthz;
|
pub mod nauthz;
|
||||||
pub mod nip05;
|
pub mod nip05;
|
||||||
pub mod notice;
|
pub mod notice;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user