Adds Tor support to cln-plugin

This commit is contained in:
Sergi Delgado Segura 2022-08-18 13:18:40 +02:00
parent 56bfe4c9e5
commit eac238628e
2 changed files with 20 additions and 16 deletions

View file

@ -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"

View file

@ -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<S: Serialize>(endpoint: &str, data: S) -> Result<Response, RequestError> {
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.