Add openssl tls backend for all platforms

This commit is contained in:
bytedream 2023-07-20 13:46:09 +02:00
parent 4ec9a0d309
commit dc6bc0d951
5 changed files with 221 additions and 41 deletions

View file

@ -323,6 +323,16 @@ dependencies = [
"url",
]
[[package]]
name = "core-foundation"
version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "194a7a9e6de53fa55116934067c844d9d749312f75c6f6d0980e8c252f8c2146"
dependencies = [
"core-foundation-sys",
"libc",
]
[[package]]
name = "core-foundation-sys"
version = "0.8.4"
@ -355,6 +365,7 @@ dependencies = [
"indicatif",
"lazy_static",
"log",
"native-tls 0.2.11 (git+https://github.com/crunchy-labs/rust-not-so-native-tls.git)",
"num_cpus",
"regex",
"reqwest",
@ -583,6 +594,21 @@ version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
[[package]]
name = "foreign-types"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1"
dependencies = [
"foreign-types-shared",
]
[[package]]
name = "foreign-types-shared"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b"
[[package]]
name = "form_urlencoded"
version = "1.2.0"
@ -802,6 +828,19 @@ dependencies = [
"tokio-rustls",
]
[[package]]
name = "hyper-tls"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d6183ddfa99b85da61a140bea0efc93fdf56ceaa041b37d553518030827f9905"
dependencies = [
"bytes",
"hyper",
"native-tls 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
"tokio",
"tokio-native-tls",
]
[[package]]
name = "iana-time-zone"
version = "0.1.57"
@ -1041,6 +1080,35 @@ dependencies = [
"windows-sys 0.48.0",
]
[[package]]
name = "native-tls"
version = "0.2.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "07226173c32f2926027b63cce4bcd8076c3552846cbe7925f3aaffeac0a3b92e"
dependencies = [
"lazy_static",
"libc",
"log",
"openssl",
"openssl-probe",
"openssl-sys",
"schannel",
"security-framework",
"security-framework-sys",
"tempfile",
]
[[package]]
name = "native-tls"
version = "0.2.11"
source = "git+https://github.com/crunchy-labs/rust-not-so-native-tls.git#bdedf02f48372efeccdf4323920c21bb1a044788"
dependencies = [
"log",
"openssl",
"openssl-probe",
"openssl-sys",
]
[[package]]
name = "nix"
version = "0.26.2"
@ -1103,6 +1171,60 @@ version = "1.18.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d"
[[package]]
name = "openssl"
version = "0.10.55"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "345df152bc43501c5eb9e4654ff05f794effb78d4efe3d53abc158baddc0703d"
dependencies = [
"bitflags 1.3.2",
"cfg-if",
"foreign-types",
"libc",
"once_cell",
"openssl-macros",
"openssl-sys",
]
[[package]]
name = "openssl-macros"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "openssl-probe"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf"
[[package]]
name = "openssl-src"
version = "111.26.0+1.1.1u"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "efc62c9f12b22b8f5208c23a7200a442b2e5999f8bdf80233852122b5a4f6f37"
dependencies = [
"cc",
]
[[package]]
name = "openssl-sys"
version = "0.9.90"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "374533b0e45f3a7ced10fcaeccca020e66656bc03dac384f852e4e5a7a8104a6"
dependencies = [
"cc",
"libc",
"openssl-src",
"pkg-config",
"vcpkg",
]
[[package]]
name = "option-ext"
version = "0.2.0"
@ -1127,6 +1249,12 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
[[package]]
name = "pkg-config"
version = "0.3.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26072860ba924cbfa98ea39c8c19b4dd6a4a25423dbdf219c1eca91aa0cf6964"
[[package]]
name = "portable-atomic"
version = "1.4.1"
@ -1253,10 +1381,12 @@ dependencies = [
"http-body",
"hyper",
"hyper-rustls",
"hyper-tls",
"ipnet",
"js-sys",
"log",
"mime",
"native-tls 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
"once_cell",
"percent-encoding",
"pin-project-lite",
@ -1266,6 +1396,7 @@ dependencies = [
"serde_json",
"serde_urlencoded",
"tokio",
"tokio-native-tls",
"tokio-rustls",
"tokio-socks",
"tower-service",
@ -1372,6 +1503,15 @@ dependencies = [
"regex",
]
[[package]]
name = "schannel"
version = "0.1.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c3733bf4cf7ea0880754e19cb5a462007c4a8c1914bff372ccc95b464f1df88"
dependencies = [
"windows-sys 0.48.0",
]
[[package]]
name = "sct"
version = "0.7.0"
@ -1382,6 +1522,29 @@ dependencies = [
"untrusted",
]
[[package]]
name = "security-framework"
version = "2.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fc758eb7bffce5b308734e9b0c1468893cae9ff70ebf13e7090be8dcbcc83a8"
dependencies = [
"bitflags 1.3.2",
"core-foundation",
"core-foundation-sys",
"libc",
"security-framework-sys",
]
[[package]]
name = "security-framework-sys"
version = "2.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f51d0c0d83bec45f16480d0ce0058397a69e48fcdc52d1dc8855fb68acbd31a7"
dependencies = [
"core-foundation-sys",
"libc",
]
[[package]]
name = "serde"
version = "1.0.171"
@ -1659,6 +1822,16 @@ dependencies = [
"syn",
]
[[package]]
name = "tokio-native-tls"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbae76ab933c85776efabc971569dd6119c580d8f5d448769dec1764bf796ef2"
dependencies = [
"native-tls 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
"tokio",
]
[[package]]
name = "tokio-rustls"
version = "0.24.1"
@ -1783,6 +1956,12 @@ version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a"
[[package]]
name = "vcpkg"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
[[package]]
name = "version_check"
version = "0.9.4"

View file

@ -4,6 +4,10 @@ authors = ["Crunchy Labs Maintainers"]
version = "3.0.0-dev.14"
edition = "2021"
[features]
openssl = ["dep:native-tls", "reqwest/native-tls"]
openssl-static = ["dep:native-tls", "native-tls?/vendored", "reqwest/native-tls-vendored"]
[dependencies]
anyhow = "1.0"
async-trait = "0.1"
@ -30,11 +34,9 @@ tempfile = "3.6"
tokio = { version = "1.29", features = ["macros", "rt-multi-thread", "time"] }
sys-locale = "0.3"
[target.'cfg(target_os = "linux")'.dependencies]
reqwest = { version = "0.11", default-features = false, features = ["socks", "native-tls-alpn", "native-tls-vendored"] }
[target.'cfg(not(target_os = "linux"))'.dependencies]
reqwest = { version = "0.11", default-features = false, features = ["socks"] }
# fork of the `native-tls` crate which uses openssl as backend on every platform. this is done as `reqwest` only supports
# `rustls` and `native-tls` as tls backend
native-tls = { git = "https://github.com/crunchy-labs/rust-not-so-native-tls.git", optional = true }
[build-dependencies]
chrono = "0.4"

View file

@ -8,7 +8,7 @@ use crunchyroll_rs::crunchyroll::CrunchyrollBuilder;
use crunchyroll_rs::error::CrunchyrollError;
use crunchyroll_rs::{Crunchyroll, Locale};
use log::{debug, error, warn, LevelFilter};
use reqwest::{Client, ClientBuilder, Proxy, StatusCode};
use reqwest::Proxy;
use std::{env, fs};
mod archive;
@ -267,16 +267,19 @@ async fn crunchyroll_session(cli: &mut Cli) -> Result<Crunchyroll> {
let proxy = cli.proxy.clone();
let mut builder = Crunchyroll::builder()
.locale(locale)
.client(
get_client(|| {
if let Some(p) = &proxy {
CrunchyrollBuilder::predefined_client_builder().proxy(p.clone())
} else {
CrunchyrollBuilder::predefined_client_builder()
}
})
.await?,
)
.client({
let builder = if let Some(p) = &proxy {
CrunchyrollBuilder::predefined_client_builder().proxy(p.clone())
} else {
CrunchyrollBuilder::predefined_client_builder()
};
#[cfg(any(feature = "openssl", feature = "openssl-static"))]
let client = builder.use_native_tls().build().unwrap();
#[cfg(not(any(feature = "openssl", feature = "openssl-static")))]
let client = builder.build().unwrap();
client
})
.stabilization_locales(cli.experimental_fixes)
.stabilization_season_number(cli.experimental_fixes);
if let Command::Download(download) = &cli.command {
@ -351,25 +354,3 @@ async fn crunchyroll_session(cli: &mut Cli) -> Result<Crunchyroll> {
Ok(crunchy)
}
#[cfg(target_os = "linux")]
async fn get_client<F: Fn() -> ClientBuilder>(f: F) -> Result<Client> {
let client = f().build().unwrap();
if client
.get("https://www.crunchyroll.com")
.send()
.await?
.status()
!= StatusCode::FORBIDDEN
{
return Ok(client);
}
debug!("rustls tls backend probably triggered the cloudflare bot check, using openssl instead");
Ok(f().use_native_tls().build().unwrap())
}
#[cfg(not(target_os = "linux"))]
async fn get_client<F: Fn() -> ClientBuilder>(f: F) -> Result<Client> {
Ok(f().build().unwrap())
}