fix (backend-rs): use proxy and proxyBypassHosts config

This commit is contained in:
naskya 2024-04-26 01:27:23 +09:00
parent 320f933e9d
commit 13b648f6bf
No known key found for this signature in database
GPG key ID: 712D413B3A9FED5C
3 changed files with 29 additions and 15 deletions

View file

@ -1,9 +1,8 @@
use crate::misc::redis_cache::{get_cache, set_cache, CacheError};
use crate::util::http_client;
use image::{io::Reader, ImageError, ImageFormat};
use nom_exif::{parse_jpeg_exif, EntryValue, ExifTag};
use once_cell::sync::OnceCell;
use std::io::Cursor;
use std::time::Duration;
use tokio::sync::Mutex;
#[derive(thiserror::Error, Debug)]
@ -35,18 +34,6 @@ const BROWSER_SAFE_IMAGE_TYPES: [ImageFormat; 8] = [
ImageFormat::Avif,
];
static CLIENT: OnceCell<reqwest::Client> = OnceCell::new();
fn client() -> Result<reqwest::Client, reqwest::Error> {
CLIENT
.get_or_try_init(|| {
reqwest::Client::builder()
.timeout(Duration::from_secs(5))
.build()
})
.cloned()
}
static MTX_GUARD: Mutex<()> = Mutex::const_new(());
#[derive(Debug, PartialEq)]
@ -78,7 +65,7 @@ pub async fn get_image_size_from_url(url: &str) -> Result<ImageSize, Error> {
tracing::info!("retrieving image size from {}", url);
let image_bytes = client()?.get(url).send().await?.bytes().await?;
let image_bytes = http_client()?.get(url).send().await?.bytes().await?;
let reader = Reader::new(Cursor::new(&image_bytes)).with_guessed_format()?;
let format = reader.format();

View file

@ -0,0 +1,24 @@
use crate::config::CONFIG;
use once_cell::sync::OnceCell;
use reqwest::{Client, Error, NoProxy, Proxy};
use std::time::Duration;
static CLIENT: OnceCell<Client> = OnceCell::new();
pub fn http_client() -> Result<Client, Error> {
CLIENT
.get_or_try_init(|| {
let mut builder = Client::builder().timeout(Duration::from_secs(5));
if let Some(proxy_url) = &CONFIG.proxy {
let mut proxy = Proxy::all(proxy_url)?;
if let Some(proxy_bypass_hosts) = &CONFIG.proxy_bypass_hosts {
proxy = proxy.no_proxy(NoProxy::from_string(&proxy_bypass_hosts.join(",")));
}
builder = builder.proxy(proxy);
}
builder.build()
})
.cloned()
}

View file

@ -1,2 +1,5 @@
pub use http_client::http_client;
pub mod http_client;
pub mod id;
pub mod random;