Skip to content

Commit

Permalink
client implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
thor314 committed Nov 12, 2024
1 parent b03ef9e commit 6752d4f
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 19 deletions.
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
PROXY_ADDRESS="http://localhost:3000"
TARGET_URL="https://gist.githubusercontent.com/mattes/23e64faadb5fd4b5112f379903d2572e/raw/ddbf0a56001367467f71bda64347aa881d83533c/example.json"
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[workspace]
resolver = "2"
members = [
"client",
"proxy"
Expand Down
10 changes: 6 additions & 4 deletions client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

8 changes: 0 additions & 8 deletions client/src/lib.rs

This file was deleted.

80 changes: 80 additions & 0 deletions client/src/main.rs
Original file line number Diff line number Diff line change
@@ -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();
}
8 changes: 4 additions & 4 deletions proxy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 6752d4f

Please sign in to comment.