Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow bypassing proxy for some domains with ProxyAllImages #4874

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions config/defaults.hjson
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
18 changes: 16 additions & 2 deletions crates/routes/src/images.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Panicing for non-domain hosts is unnecessary

Suggested change
.any(|s| url.domain().expect("Invalid URL").starts_with(s))
.any(|s| url.domain().is_some_and(|domain| domain.starts_with(s)))

{
return Ok(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can just do Ok(....)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return is required here because the if statement is not the last expression

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, &params.url);

image(url, req, &client).await
}

Expand Down
4 changes: 4 additions & 0 deletions crates/utils/src/settings/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,

/// Timeout for uploading images to pictrs (in seconds)
#[default(30)]
pub upload_timeout: u64,
Expand Down