Skip to content

Commit

Permalink
Allow disabling grpc at the build level
Browse files Browse the repository at this point in the history
  • Loading branch information
benthecarman committed Sep 5, 2023
1 parent 26f296f commit 332b980
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 41 deletions.
10 changes: 7 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@ license = "MIT"
keywords = ["nostr", "server"]
categories = ["network-programming", "web-programming"]

[features]
default = ["grpc"]
grpc = ["tonic", "prost", "tonic-build"]

[dependencies]
clap = { version = "4.0.32", features = ["env", "default", "derive"]}
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"
prost = { version = "0.11", optional = true }
tonic = { version = "0.8.3", optional = true }
console-subscriber = "0.1.8"
futures = "0.3"
futures-util = "0.3"
Expand Down Expand Up @@ -63,4 +67,4 @@ log = "0.4"
anyhow = "1"

[build-dependencies]
tonic-build = { version="0.8.3", features = ["prost"] }
tonic-build = { version="0.8.3", features = ["prost"], optional = true }
1 change: 1 addition & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
fn main() -> Result<(), Box<dyn std::error::Error>> {
#[cfg(feature = "grpc")]
tonic_build::configure()
.build_server(false)
.protoc_arg("--experimental_allow_proto3_optional")
Expand Down
84 changes: 46 additions & 38 deletions src/db.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
//! Event persistence and querying
use crate::config::Settings;
use crate::config::{Grpc, Settings};
use crate::error::{Error, Result};
use crate::event::Event;
#[cfg(feature = "grpc")]
use crate::nauthz;
use crate::notice::Notice;
use crate::payment::PaymentMessage;
Expand Down Expand Up @@ -136,11 +137,15 @@ pub async fn db_writer(
}
// create a client if GRPC is enabled.
// Check with externalized event admitter service, if one is defined.
let mut grpc_client = if let Some(svr) = settings.grpc.event_admission_server {
Some(nauthz::EventAuthzService::connect(&svr).await)
} else {
None
};
#[allow(unused_variables, unused_mut)]
let mut grpc_client: Option<Grpc> = None;

#[cfg(feature = "grpc")]
{
if let Some(svr) = settings.grpc.event_admission_server {
grpc_client = Some(nauthz::EventAuthzService::connect(&svr).await)
};
}

//let gprc_client = settings.grpc.event_admission_server.map(|s| {
// event_admitter_connect(&s);
Expand Down Expand Up @@ -345,47 +350,50 @@ 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
if let Some(ref mut c) = grpc_client {
trace!("checking if grpc permits");
let grpc_start = Instant::now();
let decision_res = c
.admit_event(
&event,
&subm_event.source_ip,
subm_event.origin,
subm_event.user_agent,
nip05_address,
subm_event.auth_pubkey,
)
.await;
match decision_res {
Ok(decision) => {
if !decision.permitted() {
// GPRC returned a decision to reject this event
info!(
#[cfg(feature = "grpc")]
{
// nip05 address
let nip05_address: Option<crate::nip05::Nip05Name> =
validation.and_then(|x| x.ok().map(|y| y.name));

if let Some(ref mut c) = grpc_client {
trace!("checking if grpc permits");
let grpc_start = Instant::now();
let decision_res = c
.admit_event(
&event,
&subm_event.source_ip,
subm_event.origin,
subm_event.user_agent,
nip05_address,
subm_event.auth_pubkey,
)
.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: {:?})",
event.get_event_id_prefix(),
event.kind,
event.get_author_prefix(),
grpc_start.elapsed(),
subm_event.source_ip
);
notice_tx
.try_send(Notice::blocked(
event.id,
&decision.message().unwrap_or_default(),
))
.ok();
continue;
notice_tx
.try_send(Notice::blocked(
event.id,
&decision.message().unwrap_or_default(),
))
.ok();
continue;
}
}
Err(e) => {
warn!("GRPC server error: {:?}", e);
}
}
Err(e) => {
warn!("GRPC server error: {:?}", e);
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ pub enum Error {
ChannelClosed,
#[error("Authz error")]
AuthzError,
#[cfg(feature = "grpc")]
#[error("Tonic GRPC error")]
TonicError(tonic::Status),
#[error("Invalid AUTH message")]
Expand Down Expand Up @@ -151,6 +152,7 @@ impl From<config::ConfigError> for Error {
}
}

#[cfg(feature = "grpc")]
impl From<tonic::Status> for Error {
/// Wrap Config error
fn from(r: tonic::Status) -> Self {
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub mod error;
pub mod event;
pub mod hexrange;
pub mod info;
#[cfg(feature = "grpc")]
pub mod nauthz;
pub mod nip05;
pub mod notice;
Expand Down

0 comments on commit 332b980

Please sign in to comment.