diff --git a/.env b/.env new file mode 100644 index 0000000..b9fe4ea --- /dev/null +++ b/.env @@ -0,0 +1,2 @@ +PROXY_ADDRESS="http://localhost:3000" +TARGET_URL="https://gist.githubusercontent.com/mattes/23e64faadb5fd4b5112f379903d2572e/raw/ddbf0a56001367467f71bda64347aa881d83533c/example.json" diff --git a/.gitignore b/.gitignore index 1785abb..411b0c8 100644 --- a/.gitignore +++ b/.gitignore @@ -2,9 +2,6 @@ # ref: https://github.com/github/gitignore/blob/main/Rust.gitignore cgt* # ignore instatiations of my test template -# don't leak secret env vars -.env - # exclude compiled files and binaries debug/ target/ diff --git a/Cargo.toml b/Cargo.toml index f5e89e6..1f917e5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,4 +1,5 @@ [workspace] +resolver = "2" members = [ "client", "proxy" diff --git a/client/Cargo.toml b/client/Cargo.toml index db2a136..8276576 100644 --- a/client/Cargo.toml +++ b/client/Cargo.toml @@ -8,9 +8,11 @@ repository = "https://github.com/pluto/web-proof-tee" version = "0.1.0" [dependencies] -anyhow = "1.0" +reqwest = { version = "0.11", features = ["json"] } +tokio = { version = "1.0", features = ["full"] } +serde = { version = "1.0", features = ["derive"] } +serde_json = "1.0" +dotenv = "0.15" +anyhow = "1.0" tracing = "0.1" tracing-subscriber = { version = "0.3", features = ["env-filter", "fmt"] } -tokio = { version = "1", features = ["full"] } -reqwest = "0.12.9" - diff --git a/client/src/lib.rs b/client/src/lib.rs deleted file mode 100644 index 10720ae..0000000 --- a/client/src/lib.rs +++ /dev/null @@ -1,8 +0,0 @@ -#![allow(unused_imports)] -#![allow(unused_variables)] -#![allow(dead_code)] -#![allow(unreachable_code)] -#![allow(non_snake_case)] -#![allow(clippy::clone_on_copy)] -#![allow(unused_mut)] -#[cfg(test)] mod tests; diff --git a/client/src/main.rs b/client/src/main.rs new file mode 100644 index 0000000..1c0e444 --- /dev/null +++ b/client/src/main.rs @@ -0,0 +1,80 @@ +//! Client to connect to a proxy re-encryption server +// #![allow(unused_imports)] +// #![allow(unused_variables)] +// #![allow(dead_code)] +// #![allow(unreachable_code)] +// #![allow(non_snake_case)] +// #![allow(unused_mut)] + +use std::env; + +use anyhow::{Context, Result}; +use dotenv::dotenv; +use serde::Deserialize; +use tracing::{debug, error, info, warn}; + +#[derive(Clone, Debug, Deserialize)] +struct Response { + _hello: String, +} + +#[tokio::main] +async fn main() -> Result<()> { + dotenv().ok(); + tracing_init(); + info!("Starting proxy client"); + + let proxy_addr = env::var("PROXY_ADDRESS").unwrap_or_else(|_| { + let default = "http://localhost:3000".to_string(); + warn!("PROXY_ADDRESS not set, using default: {}", default); + default + }); + let target_url = env::var("TARGET_URL") + .unwrap_or_else(|_| { + let default = "https://gist.githubusercontent.com/mattes/23e64faadb5fd4b5112f379903d2572e/raw/ddbf0a56001367467f71bda64347aa881d83533c/example.json".to_string(); + warn!("TARGET_URL not set, using default: {}", default); + default + }); + + info!( + proxy_addr = %proxy_addr, + target_url = %target_url, + "Sending request through proxy" + ); + let response = reqwest::Client::new() + .get(&proxy_addr) + .header("X-Target-URL", target_url) + .send() + .await + .context("Failed to send request")?; + debug!( + status = %response.status(), + headers = ?response.headers(), + "Received response from proxy" + ); + + if !response.status().is_success() { + let status = response.status(); + let text = response.text().await?; + error!( + status = %status, + error_body = %text, + "Request failed" + ); + anyhow::bail!("Request failed with status {}: {}", status, text); + } + + let data: Response = response.json().await.context("Failed to parse response as JSON")?; + info!( + response = ?data, + "Successfully received and parsed response" + ); + + Ok(()) +} + +fn tracing_init() { + tracing_subscriber::fmt() + .with_env_filter(env::var("RUST_LOG").unwrap_or_else(|_| "info,proxy_client=debug".into())) + .init(); +} diff --git a/proxy/Cargo.toml b/proxy/Cargo.toml index 559cd30..1c18177 100644 --- a/proxy/Cargo.toml +++ b/proxy/Cargo.toml @@ -14,10 +14,10 @@ tracing-subscriber = { version = "0.3", features = ["env-filter", "fmt"] } tokio = { version = "1", features = ["full"] } # futures = "0.3" -[dev-dependencies] -rstest = "0.18" -env_logger = "0.11" -criterion = "0.5" +# [dev-dependencies] +# rstest = "0.18" +# env_logger = "0.11" +# criterion = "0.5" # [[bench]] # name = "bench"