diff --git a/watchtower-plugin/Cargo.toml b/watchtower-plugin/Cargo.toml index fcf85f8..df19470 100755 --- a/watchtower-plugin/Cargo.toml +++ b/watchtower-plugin/Cargo.toml @@ -16,7 +16,7 @@ path = "src/main.rs" backoff = { version = "0.4.0", features = ["tokio"] } hex = { version = "0.4.3", features = [ "serde" ] } home = "0.5.3" -reqwest = { version = "0.11", features = [ "blocking", "json" ] } +reqwest = { version = "0.11", features = [ "blocking", "json", "socks" ] } log = "0.4.16" rusqlite = { version = "0.26.0", features = [ "bundled", "limits" ] } serde = "1.0.130" diff --git a/watchtower-plugin/src/net/http.rs b/watchtower-plugin/src/net/http.rs index d00bb40..d6ad7f6 100644 --- a/watchtower-plugin/src/net/http.rs +++ b/watchtower-plugin/src/net/http.rs @@ -119,21 +119,25 @@ pub async fn send_appointment( /// Generic function to post different types of requests to the tower. pub async fn post_request(endpoint: &str, data: S) -> Result { - reqwest::Client::new() - .post(endpoint) - .json(&data) - .send() - .await - .map_err(|e| { - log::error!("{}", e); - if e.is_connect() | e.is_timeout() { - RequestError::ConnectionError( - "Cannot connect to the tower. Connection refused".into(), - ) - } else { - RequestError::Unexpected("Unexpected error ocurred (see logs for more info)".into()) - } - }) + let client = if endpoint.contains(".onion:") { + let proxy = reqwest::Proxy::http("socks5h://127.0.0.1:9050") + .map_err(|e| RequestError::ConnectionError(format!("{}", e)))?; + reqwest::Client::builder() + .proxy(proxy) + .build() + .map_err(|e| RequestError::ConnectionError(format!("{}", e)))? + } else { + reqwest::Client::new() + }; + + client.post(endpoint).json(&data).send().await.map_err(|e| { + log::error!("{:?}", e); + if e.is_connect() | e.is_timeout() { + RequestError::ConnectionError("Cannot connect to the tower. Connection refused".into()) + } else { + RequestError::Unexpected("Unexpected error ocurred (see logs for more info)".into()) + } + }) } /// Generic function to process the response of a given post request.