From 31fc2767d8da6f311486e5ab4ba952f80176f445 Mon Sep 17 00:00:00 2001 From: sunaurus Date: Fri, 28 Jun 2024 22:27:34 +0300 Subject: [PATCH] Allow bypassing proxy for some domains with ProxyAllImages --- config/defaults.hjson | 5 +++++ crates/routes/src/images.rs | 18 ++++++++++++++++-- crates/utils/src/settings/structs.rs | 4 ++++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/config/defaults.hjson b/config/defaults.hjson index e8a70ebae..6340ef515 100644 --- a/config/defaults.hjson +++ b/config/defaults.hjson @@ -73,6 +73,11 @@ # # Requires pict-rs 0.5 "ProxyAllImages" + # Allows bypassing proxy for specific image hosts when using ProxyAllImages + proxy_bypass_domains: [ + "string" + /* ... */ + ] # Timeout for uploading images to pictrs (in seconds) upload_timeout: 30 } diff --git a/crates/routes/src/images.rs b/crates/routes/src/images.rs index 049bd6cc8..7d59ad78d 100644 --- a/crates/routes/src/images.rs +++ b/crates/routes/src/images.rs @@ -5,9 +5,10 @@ use actix_web::{ StatusCode, }, web, - web::Query, + web::{Query, Redirect}, HttpRequest, HttpResponse, + Responder, }; use futures::stream::{Stream, StreamExt}; use lemmy_api_common::{context::LemmyContext, request::PictrsResponse}; @@ -225,9 +226,22 @@ pub async fn image_proxy( // for arbitrary purposes. RemoteImage::validate(&mut context.pool(), url.clone().into()).await?; + if context + .settings() + .pictrs_config()? + .proxy_bypass_domains + .iter() + .any(|s| url.domain().expect("Invalid URL").starts_with(s)) + { + return Ok( + Redirect::to(url.to_string()) + .respond_to(&req) + .map_into_boxed_body(), + ); + } + let pictrs_config = context.settings().pictrs_config()?; let url = format!("{}image/original?proxy={}", pictrs_config.url, ¶ms.url); - image(url, req, &client).await } diff --git a/crates/utils/src/settings/structs.rs b/crates/utils/src/settings/structs.rs index 75aa56a88..73b9c2528 100644 --- a/crates/utils/src/settings/structs.rs +++ b/crates/utils/src/settings/structs.rs @@ -91,6 +91,10 @@ pub struct PictrsConfig { #[default(PictrsImageMode::StoreLinkPreviews)] pub(super) image_mode: PictrsImageMode, + /// Allows bypassing proxy for specific image hosts when using ProxyAllImages + #[default([].to_vec())] + pub proxy_bypass_domains: Vec, + /// Timeout for uploading images to pictrs (in seconds) #[default(30)] pub upload_timeout: u64,