Skip to content

Commit

Permalink
fix: add session id (#378)
Browse files Browse the repository at this point in the history
* fix: add session id

* docs: document config interface

* fix: propagate name

* fix: mut
  • Loading branch information
0xJepsen authored Jan 16, 2025
1 parent 9e306e6 commit d8c81d5
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 6 deletions.
56 changes: 55 additions & 1 deletion client/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,30 @@ pub struct Config {
}

impl Config {
pub fn session_id(&mut self) -> String {
/// Sets the session ID for the configuration.
///
/// If the `session_id` field is empty, this function generates a new UUID and assigns it to the
/// `session_id` field. It then returns the session ID.
///
/// # Returns
/// - `String`: The session ID, either newly generated or existing.
pub fn set_session_id(&mut self) -> String {
if self.session_id.is_empty() {
self.session_id = uuid::Uuid::new_v4().to_string();
}
self.session_id.clone()
}

/// Extracts the host from the target URL.
///
/// Parses the `target_url` field of the `Config` struct and extracts the host component.
///
/// # Returns
/// - `Ok(String)`: The host as a string if it is found in the target URL.
/// - `Err(ClientErrors)`: An error if the URL is invalid or the host is not found.
///
/// # Errors
/// - Returns `ClientErrors::Other` if the host is not found in the target URL.
pub fn target_host(&self) -> Result<String, ClientErrors> {
let target_url = Url::parse(&self.target_url)?;
let host = target_url
Expand All @@ -68,6 +85,18 @@ impl Config {
Ok(host)
}

/// Extracts the port from the target URL.
///
/// Parses the `target_url` field of the `Config` struct and extracts the port component.
/// If the port is not explicitly specified in the URL, it returns the default port for the scheme
/// (e.g., 80 for HTTP, 443 for HTTPS).
///
/// # Returns
/// - `Ok(u16)`: The port as a u16 if it is found or known for the target URL.
/// - `Err(ClientErrors)`: An error if the URL is invalid or the port is not found.
///
/// # Errors
/// - Returns `ClientErrors::Other` if the port is not found in the target URL.
pub fn target_port(&self) -> Result<u16, ClientErrors> {
let target_url = Url::parse(&self.target_url)?;
let port = target_url
Expand All @@ -76,11 +105,36 @@ impl Config {
Ok(port)
}

/// Checks if the target URL uses HTTPS.
///
/// Parses the `target_url` field of the `Config` struct and checks if the scheme is HTTPS.
///
/// # Returns
/// - `Ok(bool)`: `true` if the scheme is HTTPS, `false` otherwise.
/// - `Err(ClientErrors)`: An error if the URL is invalid.
///
/// # Errors
/// - Returns `ClientErrors::Other` if the URL is invalid.
pub fn target_is_https(&self) -> Result<bool, ClientErrors> {
let target_url = Url::parse(&self.target_url)?;
Ok(target_url.scheme() == "https")
}

/// Converts the configuration into an HTTP request.
///
/// This function constructs an HTTP request using the method, URL, headers, and body
/// specified in the configuration. It ensures that necessary headers like "Host",
/// "Accept-Encoding", "Connection", and "Accept" are set appropriately.
///
/// # Returns
/// - `Ok(Request<Full<Bytes>>)` if the request is successfully constructed.
/// - `Err(ClientErrors)` if there is an error in constructing the request, such as invalid
/// headers or body encoding issues.
///
/// # Errors
/// - Returns `ClientErrors::Other` if headers cannot be retrieved or set.
/// - Returns `ClientErrors::Other` if the host cannot be parsed.
/// - Returns `ClientErrors::Other` if the body cannot be decoded from base64.
pub fn to_request(&self) -> Result<Request<Full<Bytes>>, ClientErrors> {
let mut request =
Request::builder().method(self.target_method.as_str()).uri(self.target_url.clone());
Expand Down
2 changes: 1 addition & 1 deletion client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ async fn main() -> Result<(), ClientErrors> {

let config_json = std::fs::read_to_string(args.config)?;
let mut config: Config = serde_json::from_str(&config_json)?;
config.session_id();
config.set_session_id();

let proving_params = std::fs::read(circuits::PROVING_PARAMS_1024).unwrap();
let proof = client::prover_inner(config, Some(proving_params)).await?;
Expand Down
2 changes: 1 addition & 1 deletion client/src/tlsn_native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub async fn setup_tcp_connection(
config: &mut Config,
prover_config: ProverConfig,
) -> Prover<Closed> {
let session_id = config.session_id();
let session_id = config.set_session_id();
let root_store = crate::tls::rustls_default_root_store();

let client_notary_config = ClientConfig::builder()
Expand Down
2 changes: 1 addition & 1 deletion client/src/tlsn_wasm32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub async fn setup_connection(
config: &mut Config,
prover_config: ProverConfig,
) -> Result<Prover<Closed>, ClientErrors> {
let session_id = config.session_id();
let session_id = config.set_session_id();

let websocket_proxy_url = config.websocket_proxy_url.clone();
let websocket_proxy_url = websocket_proxy_url
Expand Down
2 changes: 1 addition & 1 deletion client_ios/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub unsafe extern "C" fn prover(config_json: *const c_char) -> *const c_char {
};

let mut config: Config = serde_json::from_str(config_str).unwrap();
config.session_id();
config.set_session_id();
let rt = tokio::runtime::Runtime::new().unwrap();
let start = Instant::now();
debug!("starting proving");
Expand Down
3 changes: 2 additions & 1 deletion client_wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ pub async fn prover(config: JsValue, proving_params: ProvingParamsWasm) -> Resul
panic::set_hook(Box::new(console_error_panic_hook::hook));

debug!("start config serde");
let config: Config = serde_wasm_bindgen::from_value(config).unwrap(); // TODO replace unwrap
let mut config: Config = serde_wasm_bindgen::from_value(config).unwrap(); // TODO replace unwrap
config.set_session_id();
debug!("end config serde");

let proof = client::prover_inner(config, Some(proving_params.aux_params.to_vec()))
Expand Down

0 comments on commit d8c81d5

Please sign in to comment.