Allow more minimal cargo features, tidy code, allow setting unix domain socket filename

This commit is contained in:
chaos
2023-09-27 21:40:47 +01:00
parent 884df6a08e
commit f3754b46cf
3 changed files with 274 additions and 31 deletions
+22 -13
View File
@@ -1,17 +1,21 @@
use actix_web::http::Method;
use actix_web::{web, App, HttpRequest, HttpResponse, HttpResponseBuilder, HttpServer};
use mimalloc::MiMalloc;
use once_cell::sync::Lazy;
use qstring::QString;
use regex::Regex;
use reqwest::{Body, Client, Request, Url};
use std::env;
use std::error::Error;
use tokio::sync::oneshot;
use tokio::task::spawn_blocking;
#[cfg(not(any(feature = "reqwest-native-tls", feature = "reqwest-rustls")))]
compile_error!("feature \"reqwest-native-tls\" or \"reqwest-rustls\" must be set for proxy to have TLS support");
#[cfg(any(feature = "webp", feature = "avif"))]
use tokio::{sync::oneshot, task::spawn_blocking};
#[cfg(feature = "mimalloc")]
#[global_allocator]
static GLOBAL: MiMalloc = MiMalloc;
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
#[actix_web::main]
async fn main() -> std::io::Result<()> {
@@ -22,8 +26,8 @@ async fn main() -> std::io::Result<()> {
App::new().default_service(web::to(index))
});
// get port from env
if env::var("UDS").is_ok() {
server.bind_uds("./socket/actix.sock")?
if let Ok(unix_socket) = env::var("BIND_UNIX") {
server.bind_uds(unix_socket)?
} else {
let bind = env::var("BIND").unwrap_or_else(|_| "0.0.0.0:8080".to_string());
server.bind(bind)?
@@ -48,17 +52,17 @@ static CLIENT: Lazy<Client> = Lazy::new(|| {
None
};
let builder = if proxy.is_some() {
let builder = if let Some(proxy) = proxy {
// proxy basic auth
if let Ok(proxy_auth_user) = env::var("PROXY_USER") {
let proxy_auth_pass = env::var("PROXY_PASS").unwrap_or_default();
builder.proxy(
proxy
.unwrap()
.basic_auth(&proxy_auth_user, &proxy_auth_pass),
)
} else {
builder.proxy(proxy.unwrap())
builder.proxy(proxy)
}
} else {
builder
@@ -133,6 +137,9 @@ async fn index(req: HttpRequest) -> Result<HttpResponse, Box<dyn Error>> {
return Err("No host provided".into());
}
#[cfg(any(feature = "webp", feature = "avif"))]
let disallow_image_transcoding = env::var("DISALLOW_IMAGE_TRANSCODING").is_ok();
let rewrite = query.get("rewrite") != Some("false");
#[cfg(feature = "avif")]
@@ -213,7 +220,9 @@ async fn index(req: HttpRequest) -> Result<HttpResponse, Box<dyn Error>> {
if rewrite {
if let Some(content_type) = resp.headers().get("content-type") {
#[cfg(feature = "avif")]
if content_type == "image/webp" || content_type == "image/jpeg" && avif {
if disallow_image_transcoding
&& (content_type == "image/webp" || content_type == "image/jpeg" && avif)
{
let resp_bytes = resp.bytes().await.unwrap();
let (tx, rx) = oneshot::channel::<(Vec<u8>, &'static str)>();
spawn_blocking(|| {
@@ -235,11 +244,11 @@ async fn index(req: HttpRequest) -> Result<HttpResponse, Box<dyn Error>> {
.with_speed(7)
.encode_rgb(buffer);
return if let Ok(res) = res {
if let Ok(res) = res {
tx.send((res.avif_file.to_vec(), "image/avif")).unwrap();
} else {
tx.send((resp_bytes.into(), "image/jpeg")).unwrap();
};
}
});
let (body, content_type) = rx.await.unwrap();
response.content_type(content_type);
@@ -247,7 +256,7 @@ async fn index(req: HttpRequest) -> Result<HttpResponse, Box<dyn Error>> {
}
#[cfg(feature = "webp")]
if content_type == "image/jpeg" {
if !disallow_image_transcoding && content_type == "image/jpeg" {
let resp_bytes = resp.bytes().await.unwrap();
let (tx, rx) = oneshot::channel::<(Vec<u8>, &'static str)>();
spawn_blocking(|| {