From 7d1c6e9a402c14833b94cd33f7f170ffcbc820ed Mon Sep 17 00:00:00 2001 From: Dessalines Date: Thu, 25 Jun 2020 15:36:03 -0400 Subject: [PATCH] Switching back to isahc. --- server/Cargo.lock | 190 ++++++++++++++++------- server/Cargo.toml | 2 +- server/src/apub/activities.rs | 13 +- server/src/apub/extensions/signatures.rs | 23 ++- server/src/apub/fetcher.rs | 5 +- server/src/apub/mod.rs | 3 +- server/src/lib.rs | 8 +- 7 files changed, 160 insertions(+), 84 deletions(-) diff --git a/server/Cargo.lock b/server/Cargo.lock index f124396b..a8f5c880 100644 --- a/server/Cargo.lock +++ b/server/Cargo.lock @@ -440,20 +440,6 @@ dependencies = [ "syn", ] -[[package]] -name = "attohttpc" -version = "0.14.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "93610ce1c035e8a273fe56a19852e42798f3c019ca2726e52d2971197f116525" -dependencies = [ - "http", - "log", - "rustls", - "url", - "webpki", - "webpki-roots", -] - [[package]] name = "atty" version = "0.2.14" @@ -795,6 +781,37 @@ dependencies = [ "lazy_static", ] +[[package]] +name = "curl" +version = "0.4.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b0447a642435be046540f042950d874a4907f9fee28c0513a0beb3ba89f91eb7" +dependencies = [ + "curl-sys", + "libc", + "openssl-probe", + "openssl-sys", + "schannel", + "socket2", + "winapi 0.3.8", +] + +[[package]] +name = "curl-sys" +version = "0.4.32+curl-7.70.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "834425a2f22fdd621434196965bf99fbfd9eaed96348488e27b7ac40736c560b" +dependencies = [ + "cc", + "libc", + "libnghttp2-sys", + "libz-sys", + "openssl-sys", + "pkg-config", + "vcpkg", + "winapi 0.3.8", +] + [[package]] name = "darling" version = "0.10.2" @@ -1430,6 +1447,31 @@ dependencies = [ "winreg", ] +[[package]] +name = "isahc" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f54e7cf252df9a36605ccfabea2a754ad30c24b51b77f830486e555ac8e76bce" +dependencies = [ + "bytes", + "crossbeam-channel", + "crossbeam-utils", + "curl", + "curl-sys", + "encoding_rs", + "futures-channel", + "futures-io", + "futures-util", + "http", + "lazy_static", + "log", + "mime", + "slab", + "sluice", + "tracing", + "tracing-futures", +] + [[package]] name = "itertools" version = "0.9.0" @@ -1502,7 +1544,6 @@ dependencies = [ "actix-rt", "actix-web", "actix-web-actors", - "attohttpc", "base64 0.12.2", "bcrypt", "chrono", @@ -1517,6 +1558,7 @@ dependencies = [ "htmlescape", "http", "http-signature-normalization", + "isahc", "itertools", "jsonwebtoken", "lazy_static", @@ -1589,6 +1631,28 @@ version = "0.2.71" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9457b06509d27052635f90d6466700c65095fdf75409b3fbdd903e988b886f49" +[[package]] +name = "libnghttp2-sys" +version = "0.1.4+1.41.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "03624ec6df166e79e139a2310ca213283d6b3c30810c54844f307086d4488df1" +dependencies = [ + "cc", + "libc", +] + +[[package]] +name = "libz-sys" +version = "1.0.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2eb5e43362e38e2bca2fd5f5134c4d4564a23a5c28e9b95411652021a8675ebe" +dependencies = [ + "cc", + "libc", + "pkg-config", + "vcpkg", +] + [[package]] name = "linked-hash-map" version = "0.3.0" @@ -2357,19 +2421,6 @@ dependencies = [ "semver", ] -[[package]] -name = "rustls" -version = "0.17.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c0d4a31f5d68413404705d6982529b0e11a9aacd4839d1d6222ee3b8cb4015e1" -dependencies = [ - "base64 0.11.0", - "log", - "ring", - "sct", - "webpki", -] - [[package]] name = "ryu" version = "1.0.5" @@ -2413,16 +2464,6 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" -[[package]] -name = "sct" -version = "0.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e3042af939fca8c3453b7af0f1c66e533a15a86169e39de2657310ade8f98d3c" -dependencies = [ - "ring", - "untrusted", -] - [[package]] name = "security-framework" version = "0.4.4" @@ -2590,6 +2631,18 @@ version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8" +[[package]] +name = "sluice" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fed13b7cb46f13a15db2c4740f087a848acc8b31af89f95844d40137451f89b1" +dependencies = [ + "futures-channel", + "futures-core", + "futures-io", + "futures-util", +] + [[package]] name = "smallvec" version = "1.4.0" @@ -2808,6 +2861,48 @@ dependencies = [ "tokio", ] +[[package]] +name = "tracing" +version = "0.1.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a41f40ed0e162c911ac6fcb53ecdc8134c46905fdbbae8c50add462a538b495f" +dependencies = [ + "cfg-if", + "log", + "tracing-attributes", + "tracing-core", +] + +[[package]] +name = "tracing-attributes" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "99bbad0de3fd923c9c3232ead88510b783e5a4d16a6154adffa3d53308de984c" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tracing-core" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0aa83a9a47081cd522c09c81b31aec2c9273424976f922ad61c053b58350b715" +dependencies = [ + "lazy_static", +] + +[[package]] +name = "tracing-futures" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab7bb6f14721aa00656086e9335d363c5c8747bae02ebe32ea2c7dece5689b4c" +dependencies = [ + "pin-project", + "tracing", +] + [[package]] name = "trust-dns-proto" version = "0.18.0-alpha.2" @@ -3105,25 +3200,6 @@ dependencies = [ "wasm-bindgen", ] -[[package]] -name = "webpki" -version = "0.21.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ab146130f5f790d45f82aeeb09e55a256573373ec64409fc19a6fb82fb1032ae" -dependencies = [ - "ring", - "untrusted", -] - -[[package]] -name = "webpki-roots" -version = "0.19.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f8eff4b7516a57307f9349c64bf34caa34b940b66fed4b2fb3136cb7386e5739" -dependencies = [ - "webpki", -] - [[package]] name = "widestring" version = "0.4.2" diff --git a/server/Cargo.toml b/server/Cargo.toml index 52a9bae8..df6c90af 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -40,7 +40,7 @@ htmlescape = "0.3.1" url = { version = "2.1.1", features = ["serde"] } config = {version = "0.10.1", default-features = false, features = ["hjson"] } percent-encoding = "2.1.0" -attohttpc = { version = "0.14.0", default-features = false, features = ["tls-rustls"] } +isahc = "0.9.2" comrak = "0.7" openssl = "0.10" http = "0.2.1" diff --git a/server/src/apub/activities.rs b/server/src/apub/activities.rs index 3c4034c9..b5bb9d76 100644 --- a/server/src/apub/activities.rs +++ b/server/src/apub/activities.rs @@ -5,6 +5,7 @@ use crate::{ use activitystreams::{context, object::properties::ObjectProperties, public, Activity, Base}; use diesel::PgConnection; use failure::{Error, _core::fmt::Debug}; +use isahc::prelude::*; use log::debug; use serde::Serialize; use url::Url; @@ -56,18 +57,16 @@ where for t in to { let to_url = Url::parse(&t)?; if !is_apub_id_valid(&to_url) { - debug!("Not sending activity to {} (invalid or blacklisted)", t); + debug!("Not sending activity to {} (invalid or blocklisted)", t); continue; } - let mut request = attohttpc::post(t).header("Host", to_url.domain().unwrap()); - let signature = sign(&mut request, actor)?; + let request = Request::post(t).header("Host", to_url.domain().unwrap()); + let signature = sign(&request, actor)?; let res = request .header("Signature", signature) .header("Content-Type", "application/json") - .text(json.to_owned()) - .send()? - .text()?; - + .body(json.to_owned())? + .send()?; debug!("Result for activity send: {:?}", res); } Ok(()) diff --git a/server/src/apub/extensions/signatures.rs b/server/src/apub/extensions/signatures.rs index 7aa9489c..4156f0b3 100644 --- a/server/src/apub/extensions/signatures.rs +++ b/server/src/apub/extensions/signatures.rs @@ -1,8 +1,8 @@ use crate::apub::ActorType; use activitystreams::ext::Extension; use actix_web::HttpRequest; -use attohttpc::RequestBuilder; use failure::Error; +use http::request::Builder; use http_signature_normalization::Config; use log::debug; use openssl::{ @@ -35,29 +35,28 @@ pub fn generate_actor_keypair() -> Result { }) } -// TODO is it possible to create this signature, with just the url and actor? /// Signs request headers with the given keypair. -pub fn sign(request: &mut RequestBuilder, actor: &dyn ActorType) -> Result { +pub fn sign(request: &Builder, actor: &dyn ActorType) -> Result { let signing_key_id = format!("{}#main-key", actor.actor_id()); let headers = request - .inspect() - .headers() + .headers_ref() + .unwrap() .iter() .map(|h| -> Result<(String, String), Error> { Ok((h.0.as_str().to_owned(), h.1.to_str()?.to_owned())) }) .collect::, Error>>()?; - let mut path_and_query = request.inspect().url().path().to_owned(); - if let Some(query) = request.inspect().url().query() { - path_and_query.push_str(query); - } - let signature_header_value = HTTP_SIG_CONFIG .begin_sign( - request.inspect().method().as_str(), - &path_and_query, + request.method_ref().unwrap().as_str(), + request + .uri_ref() + .unwrap() + .path_and_query() + .unwrap() + .as_str(), headers, )? .sign(signing_key_id, |signing_string| { diff --git a/server/src/apub/fetcher.rs b/server/src/apub/fetcher.rs index 20bab4e2..7f7a3f97 100644 --- a/server/src/apub/fetcher.rs +++ b/server/src/apub/fetcher.rs @@ -2,6 +2,7 @@ use activitystreams::object::Note; use actix_web::Result; use diesel::{result::Error::NotFound, PgConnection}; use failure::{Error, _core::fmt::Debug}; +use isahc::prelude::*; use log::debug; use serde::Deserialize; use std::time::Duration; @@ -63,11 +64,11 @@ where } // TODO: this function should return a future let timeout = Duration::from_secs(60); - let text: String = attohttpc::get(url.as_str()) + let text = Request::get(url.as_str()) .header("Accept", APUB_JSON_CONTENT_TYPE) .connect_timeout(timeout) .timeout(timeout) - // .body(()) + .body(())? .send()? .text()?; let res: Response = serde_json::from_str(&text)?; diff --git a/server/src/apub/mod.rs b/server/src/apub/mod.rs index 7d2aee65..6a2d6cff 100644 --- a/server/src/apub/mod.rs +++ b/server/src/apub/mod.rs @@ -32,6 +32,7 @@ use actix_web::{body::Body, HttpResponse, Result}; use chrono::NaiveDateTime; use diesel::PgConnection; use failure::Error; +use isahc::prelude::*; use log::debug; use serde::Serialize; use url::Url; @@ -252,7 +253,7 @@ pub fn fetch_webfinger_url(mention: &MentionData) -> Result { mention.domain ); debug!("Fetching webfinger url: {}", &fetch_url); - let text: String = attohttpc::get(&fetch_url).send()?.text()?; + let text = isahc::get(&fetch_url)?.text()?; let res: WebFingerResponse = serde_json::from_str(&text)?; let link = res .links diff --git a/server/src/lib.rs b/server/src/lib.rs index 2391449c..b7c21049 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -39,6 +39,7 @@ pub mod websocket; use crate::settings::Settings; use actix_web::dev::ConnectionInfo; use chrono::{DateTime, FixedOffset, Local, NaiveDateTime, Utc}; +use isahc::prelude::*; use itertools::Itertools; use lettre::{ smtp::{ @@ -85,8 +86,7 @@ pub fn is_email_regex(test: &str) -> bool { } pub fn is_image_content_type(test: &str) -> Result<(), failure::Error> { - if attohttpc::get(test) - .send()? + if isahc::get(test)? .headers() .get("Content-Type") .ok_or_else(|| format_err!("No Content-Type header"))? @@ -180,7 +180,7 @@ pub struct IframelyResponse { pub fn fetch_iframely(url: &str) -> Result { let fetch_url = format!("http://iframely/oembed?url={}", url); - let text: String = attohttpc::get(&fetch_url).send()?.text()?; + let text = isahc::get(&fetch_url)?.text()?; let res: IframelyResponse = serde_json::from_str(&text)?; Ok(res) } @@ -204,7 +204,7 @@ pub fn fetch_pictrs(image_url: &str) -> Result { "http://pictrs:8080/image/download?url={}", utf8_percent_encode(image_url, NON_ALPHANUMERIC) // TODO this might not be needed ); - let text = attohttpc::get(&fetch_url).send()?.text()?; + let text = isahc::get(&fetch_url)?.text()?; let res: PictrsResponse = serde_json::from_str(&text)?; if res.msg == "ok" { Ok(res) -- 2.44.1