Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions Cargo-minimal.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2637,7 +2637,6 @@ dependencies = [
"hyper-util",
"ohttp-relay",
"payjoin",
"prometheus",
"rand 0.8.5",
"serde",
"tempfile",
Expand Down Expand Up @@ -2692,7 +2691,9 @@ dependencies = [
"ohttp-relay",
"payjoin-directory",
"payjoin-test-utils",
"prometheus",
"rand 0.8.5",
"reqwest",
"rustls 0.23.31",
"serde",
"tempfile",
Expand Down Expand Up @@ -2905,24 +2906,38 @@ dependencies = [

[[package]]
name = "prometheus"
version = "0.13.4"
version = "0.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d33c28a30771f7f96db69893f78b857f7450d7e0237e9c8fc6427a81bae7ed1"
checksum = "3ca5326d8d0b950a9acd87e6a3f94745394f62e4dae1b1ee22b2bc0c394af43a"
dependencies = [
"cfg-if",
"fnv",
"lazy_static",
"memchr",
"parking_lot 0.12.3",
"protobuf",
"thiserror 1.0.63",
"thiserror 2.0.17",
]

[[package]]
name = "protobuf"
version = "2.28.0"
version = "3.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d65a1d4ddae7d8b5de68153b48f6aa3bba8cb002b243dbdbc55a5afbc98f99f4"
dependencies = [
"once_cell",
"protobuf-support",
"thiserror 1.0.63",
]

[[package]]
name = "protobuf-support"
version = "3.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "106dd99e98437432fed6519dedecfade6a06a73bb7b2a1e019fdd2bee5778d94"
checksum = "3e36c2f31e0a47f9280fb347ef5e461ffcd2c52dd520d8e216b52f93b0b0d7d6"
dependencies = [
"thiserror 1.0.63",
]

[[package]]
name = "quinn"
Expand Down
27 changes: 21 additions & 6 deletions Cargo-recent.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2637,7 +2637,6 @@ dependencies = [
"hyper-util",
"ohttp-relay",
"payjoin",
"prometheus",
"rand 0.8.5",
"serde",
"tempfile",
Expand Down Expand Up @@ -2692,7 +2691,9 @@ dependencies = [
"ohttp-relay",
"payjoin-directory",
"payjoin-test-utils",
"prometheus",
"rand 0.8.5",
"reqwest",
"rustls 0.23.31",
"serde",
"tempfile",
Expand Down Expand Up @@ -2905,24 +2906,38 @@ dependencies = [

[[package]]
name = "prometheus"
version = "0.13.4"
version = "0.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d33c28a30771f7f96db69893f78b857f7450d7e0237e9c8fc6427a81bae7ed1"
checksum = "3ca5326d8d0b950a9acd87e6a3f94745394f62e4dae1b1ee22b2bc0c394af43a"
dependencies = [
"cfg-if",
"fnv",
"lazy_static",
"memchr",
"parking_lot 0.12.3",
"protobuf",
"thiserror 1.0.63",
"thiserror 2.0.17",
]

[[package]]
name = "protobuf"
version = "2.28.0"
version = "3.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d65a1d4ddae7d8b5de68153b48f6aa3bba8cb002b243dbdbc55a5afbc98f99f4"
dependencies = [
"once_cell",
"protobuf-support",
"thiserror 1.0.63",
]

[[package]]
name = "protobuf-support"
version = "3.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "106dd99e98437432fed6519dedecfade6a06a73bb7b2a1e019fdd2bee5778d94"
checksum = "3e36c2f31e0a47f9280fb347ef5e461ffcd2c52dd520d8e216b52f93b0b0d7d6"
dependencies = [
"thiserror 1.0.63",
]

[[package]]
name = "quinn"
Expand Down
1 change: 0 additions & 1 deletion payjoin-directory/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ ohttp-relay = { path = "../ohttp-relay" }
payjoin = { version = "1.0.0-rc.1", features = [
"directory",
], default-features = false }
prometheus = "0.13.4"
rand = "0.8"
serde = { version = "1.0.219", features = ["derive"] }
tokio = { version = "1.47.1", features = ["full"] }
Expand Down
3 changes: 0 additions & 3 deletions payjoin-directory/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ pub struct Cli {
#[arg(long, short = 'p', env = "PJ_DIR_PORT", help = "The port to bind [default: 8080]")]
pub port: Option<u16>, // TODO tokio_listener::ListenerAddressLFlag

#[arg(long, env = "PJ_METRIC_PORT", help = "The port to bind for prometheus metrics export")]
pub metrics_port: Option<u16>, // TODO tokio_listener::ListenerAddressLFlag

#[cfg(feature = "acme")]
#[clap(flatten)]
pub acme: AcmeCli,
Expand Down
7 changes: 0 additions & 7 deletions payjoin-directory/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use crate::cli::Cli;
#[derive(Debug, Clone, Deserialize)]
pub struct Config {
pub listen_addr: String, // TODO tokio_listener::ListenerAddressLFlag
pub metrics_listen_addr: Option<String>, // TODO tokio_listener::ListenerAddressLFlag
pub timeout: Duration,
pub storage_dir: PathBuf,
pub ohttp_keys: PathBuf, // TODO OhttpConfig struct with rotation params, etc
Expand Down Expand Up @@ -52,7 +51,6 @@ impl Config {

Ok(Config {
listen_addr: built_config.get("listen_addr")?,
metrics_listen_addr: built_config.get("metrics_listen_addr").ok(),
timeout: Duration::from_secs(built_config.get("timeout")?),
storage_dir: built_config.get("storage_dir")?,
ohttp_keys: built_config.get("ohttp_keys")?,
Expand All @@ -75,11 +73,6 @@ fn add_defaults(config: Builder, cli: &Cli) -> Result<Builder, ConfigError> {
let config = config
.set_default("listen_addr", "[::]:8080")?
.set_override_option("listen_addr", cli.port.map(|port| format!("[::]:{}", port)))?
.set_default("metrics_listen_addr", Option::<String>::None)?
.set_override_option(
"metrics_listen_addr",
cli.metrics_port.map(|port| format!("localhost:{}", port)),
)?
.set_default("timeout", Some(30))?
.set_override_option("timeout", cli.timeout)?
.set_default("ohttp_keys", "ohttp_keys")?
Expand Down
68 changes: 3 additions & 65 deletions payjoin-directory/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use anyhow::Result;
use futures::StreamExt;
use http_body_util::combinators::BoxBody;
use http_body_util::{BodyExt, Empty, Full};
use hyper::body::{Body, Bytes, Incoming};
use hyper::body::{Body, Bytes};
use hyper::header::{HeaderValue, ACCESS_CONTROL_ALLOW_ORIGIN, CONTENT_TYPE};
use hyper::server::conn::http1;
use hyper::{Method, Request, Response, StatusCode, Uri};
Expand All @@ -27,7 +27,6 @@ pub mod key_config;
use ohttp_relay::SentinelTag;

pub use crate::key_config::*;
use crate::metrics::Metrics;

const CHACHA20_POLY1305_NONCE_LEN: usize = 32; // chacha20poly1305 n_k
const POLY1305_TAG_SIZE: usize = 16;
Expand All @@ -43,7 +42,6 @@ pub(crate) mod db;

pub mod cli;
pub mod config;
pub mod metrics;

pub type BoxError = Box<dyn std::error::Error + Send + Sync>;

Expand All @@ -69,7 +67,6 @@ fn init_tls_acceptor(cert_key: (Vec<u8>, Vec<u8>)) -> Result<tokio_rustls::TlsAc
pub struct Service<D: Db> {
db: D,
ohttp: ohttp::Server,
metrics: Metrics,
sentinel_tag: SentinelTag,
}

Expand All @@ -94,8 +91,8 @@ where
}

impl<D: Db> Service<D> {
pub fn new(db: D, ohttp: ohttp::Server, metrics: Metrics, sentinel_tag: SentinelTag) -> Self {
Self { db, ohttp, metrics, sentinel_tag }
pub fn new(db: D, ohttp: ohttp::Server, sentinel_tag: SentinelTag) -> Self {
Self { db, ohttp, sentinel_tag }
}

#[cfg(feature = "_manual-tls")]
Expand All @@ -111,7 +108,6 @@ impl<D: Db> Service<D> {
let tls_acceptor = tls_acceptor.clone();
let service = self.clone();
tokio::spawn(async move {
service.metrics.record_connection();
let tls_stream = match tls_acceptor.accept(stream).await {
Ok(tls_stream) => tls_stream,
Err(e) => {
Expand Down Expand Up @@ -172,7 +168,6 @@ impl<D: Db> Service<D> {
where
I: tokio::io::AsyncRead + tokio::io::AsyncWrite + Send + Unpin + 'static,
{
self.metrics.record_connection();
let hyper_service = TowerToHyperService::new(self.clone());
if let Err(err) = http1::Builder::new()
.serve_connection(TokioIo::new(stream), hyper_service)
Expand Down Expand Up @@ -441,63 +436,6 @@ impl<D: Db> Service<D> {

res
}
async fn handle_metrics(&self) -> Response<BoxBody<Bytes, hyper::Error>> {
match self.metrics.generate_metrics() {
Ok(metrics_data) => {
let mut response = Response::new(full(metrics_data));
response.headers_mut().insert(
CONTENT_TYPE,
HeaderValue::from_static("text/plain; version=0.0.4; charset=utf-8"),
);
response
}
Err(e) => {
error!("failed to generate metrics: {}", e);
let mut response = Response::new(full("Error generating metrics"));
*response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR;
response
}
}
}

pub async fn serve_metrics_tcp(
&self,
listener: tokio::net::TcpListener,
) -> Result<(), BoxError> {
while let Ok((stream, _)) = listener.accept().await {
let io = TokioIo::new(stream);
let service = self.clone();
tokio::spawn(async move {
let service = hyper::service::service_fn(move |req| {
let this = service.clone();
async move { this.serve_metrics_request(req).await }
});
if let Err(err) =
http1::Builder::new().serve_connection(io, service).with_upgrades().await
{
error!("Error serving connection: {:?}", err);
}
});
}

Ok(())
}

async fn serve_metrics_request(
&self,
req: Request<Incoming>,
) -> Result<Response<BoxBody<Bytes, hyper::Error>>, anyhow::Error> {
let path = req.uri().path().to_string();
let (parts, _body) = req.into_parts();

let path_segments: Vec<&str> = path.split('/').collect();
let response = match (parts.method, path_segments.as_slice()) {
(Method::GET, ["", ""]) => self.handle_metrics().await,
_ => not_found(),
};

Ok(response)
}
}

fn handle_peek<Error: db::SendableError>(
Expand Down
17 changes: 1 addition & 16 deletions payjoin-directory/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use clap::Parser;
use ohttp_relay::SentinelTag;
use payjoin_directory::metrics::Metrics;
use payjoin_directory::*;
use tokio::net::TcpListener;
use tracing_subscriber::filter::LevelFilter;
Expand All @@ -26,25 +25,11 @@ async fn main() -> Result<(), BoxError> {
}
};

let metrics = Metrics::new();
let db = payjoin_directory::FilesDb::init(config.timeout, config.storage_dir)
.await
.expect("Failed to initialize persistent storage");

let service = Service::new(db, ohttp.into(), metrics, SentinelTag::new([0u8; 32]));

// Start metrics server in the background
if let Some(addr) = config.metrics_listen_addr {
let metrics_listener = TcpListener::bind(addr.clone()).await?;
let service_clone = service.clone();

tokio::spawn(async move {
if let Err(e) = service_clone.serve_metrics_tcp(metrics_listener).await {
eprintln!("Metrics server error: {e}");
}
println!("Metrics server started on {}", addr);
});
}
let service = Service::new(db, ohttp.into(), SentinelTag::new([0u8; 32]));

let listener = TcpListener::bind(config.listen_addr).await?;

Expand Down
60 changes: 0 additions & 60 deletions payjoin-directory/src/metrics.rs

This file was deleted.

Loading
Loading