Add native-tls/openssl tls backend for linux

This commit is contained in:
bytedream 2023-07-17 16:08:54 +02:00
parent 566422cb06
commit 4ec9a0d309
3 changed files with 207 additions and 1 deletions

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::Proxy;
use reqwest::{Client, ClientBuilder, Proxy, StatusCode};
use std::{env, fs};
mod archive;
@ -264,8 +264,19 @@ async fn crunchyroll_session(cli: &mut Cli) -> Result<Crunchyroll> {
lang
};
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?,
)
.stabilization_locales(cli.experimental_fixes)
.stabilization_season_number(cli.experimental_fixes);
if let Command::Download(download) = &cli.command {
@ -340,3 +351,25 @@ 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())
}