Make Proxy Optional Feature

This commit is contained in:
spikecodes 2020-11-23 16:57:37 -08:00
parent 8a488af594
commit b218ec6065
4 changed files with 42 additions and 26 deletions

30
Cargo.lock generated
View File

@ -486,7 +486,7 @@ dependencies = [
"memchr", "memchr",
"num_cpus", "num_cpus",
"once_cell", "once_cell",
"pin-project-lite", "pin-project-lite 0.1.11",
"pin-utils", "pin-utils",
"slab", "slab",
"wasm-bindgen-futures", "wasm-bindgen-futures",
@ -1006,7 +1006,7 @@ dependencies = [
"futures-io", "futures-io",
"memchr", "memchr",
"parking", "parking",
"pin-project-lite", "pin-project-lite 0.1.11",
"waker-fn", "waker-fn",
] ]
@ -1236,7 +1236,7 @@ dependencies = [
"cookie", "cookie",
"futures-lite", "futures-lite",
"infer", "infer",
"pin-project-lite", "pin-project-lite 0.1.11",
"rand", "rand",
"serde", "serde",
"serde_json", "serde_json",
@ -1724,6 +1724,12 @@ version = "0.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c917123afa01924fc84bb20c4c03f004d9c38e5127e3c039bbf7f4b9c76a2f6b" checksum = "c917123afa01924fc84bb20c4c03f004d9c38e5127e3c039bbf7f4b9c76a2f6b"
[[package]]
name = "pin-project-lite"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6b063f57ec186e6140e2b8b6921e5f1bd89c7356dda5b33acc5401203ca6131c"
[[package]] [[package]]
name = "pin-utils" name = "pin-utils"
version = "0.1.0" version = "0.1.0"
@ -1908,9 +1914,9 @@ dependencies = [
[[package]] [[package]]
name = "ring" name = "ring"
version = "0.16.16" version = "0.16.17"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b72b84d47e8ec5a4f2872e8262b8f8256c5be1c938a7d6d3a867a3ba8f722f74" checksum = "c5911690c9b773bab7e657471afc207f3827b249a657241327e3544d79bcabdd"
dependencies = [ dependencies = [
"cc", "cc",
"libc", "libc",
@ -2234,7 +2240,7 @@ dependencies = [
"log", "log",
"mime_guess", "mime_guess",
"once_cell", "once_cell",
"pin-project-lite", "pin-project-lite 0.1.11",
"serde", "serde",
"serde_json", "serde_json",
"web-sys", "web-sys",
@ -2378,7 +2384,7 @@ dependencies = [
"memchr", "memchr",
"mio", "mio",
"mio-uds", "mio-uds",
"pin-project-lite", "pin-project-lite 0.1.11",
"signal-hook-registry", "signal-hook-registry",
"slab", "slab",
"winapi 0.3.9", "winapi 0.3.9",
@ -2406,7 +2412,7 @@ dependencies = [
"futures-core", "futures-core",
"futures-sink", "futures-sink",
"log", "log",
"pin-project-lite", "pin-project-lite 0.1.11",
"tokio", "tokio",
] ]
@ -2421,13 +2427,13 @@ dependencies = [
[[package]] [[package]]
name = "tracing" name = "tracing"
version = "0.1.21" version = "0.1.22"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0987850db3733619253fe60e17cb59b82d37c7e6c0236bb81e4d6b87c879f27" checksum = "9f47026cdc4080c07e49b37087de021820269d996f581aac150ef9e5583eefe3"
dependencies = [ dependencies = [
"cfg-if 0.1.10", "cfg-if 1.0.0",
"log", "log",
"pin-project-lite", "pin-project-lite 0.2.0",
"tracing-attributes", "tracing-attributes",
"tracing-core", "tracing-core",
] ]

View File

@ -7,8 +7,12 @@ version = "0.1.6"
authors = ["spikecodes <19519553+spikecodes@users.noreply.github.com>"] authors = ["spikecodes <19519553+spikecodes@users.noreply.github.com>"]
edition = "2018" edition = "2018"
[features]
default = ["proxy"]
proxy = ["actix-web/rustls"]
[dependencies] [dependencies]
actix-web = { version = "3.2.0", features = ["rustls"] } actix-web = "3.2.0"
surf = "2.1.0" surf = "2.1.0"
askama = "0.8.0" askama = "0.8.0"
serde = "1.0.117" serde = "1.0.117"

View File

@ -74,18 +74,20 @@ async fn media(data: &serde_json::Value) -> String {
let post_hint: &str = data["data"]["post_hint"].as_str().unwrap_or(""); let post_hint: &str = data["data"]["post_hint"].as_str().unwrap_or("");
let has_media: bool = data["data"]["media"].is_object(); let has_media: bool = data["data"]["media"].is_object();
let prefix = if cfg!(feature = "proxy") { "/imageproxy/" } else { "" };
let media: String = if !has_media { let media: String = if !has_media {
format!(r#"<h4 class="post_body"><a href="{u}">{u}</a></h4>"#, u = data["data"]["url"].as_str().unwrap()) format!(r#"<h4 class="post_body"><a href="{u}">{u}</a></h4>"#, u = data["data"]["url"].as_str().unwrap())
} else { } else {
format!(r#"<img class="post_image" src="/imageproxy/{}.png"/>"#, data["data"]["url"].as_str().unwrap()) format!(r#"<img class="post_image" src="{}{}.png"/>"#, prefix, data["data"]["url"].as_str().unwrap())
}; };
match post_hint { match post_hint {
"hosted:video" => format!( "hosted:video" => format!(
r#"<video class="post_image" src="/imageproxy/{}" controls/>"#, r#"<video class="post_image" src="{}{}" controls/>"#,
data["data"]["media"]["reddit_video"]["fallback_url"].as_str().unwrap() prefix, data["data"]["media"]["reddit_video"]["fallback_url"].as_str().unwrap()
), ),
"image" => format!(r#"<img class="post_image" src="/imageproxy/{}"/>"#, data["data"]["url"].as_str().unwrap()), "image" => format!(r#"<img class="post_image" src="{}{}"/>"#, prefix, data["data"]["url"].as_str().unwrap()),
"self" => String::from(""), "self" => String::from(""),
_ => media, _ => media,
} }

View File

@ -2,13 +2,17 @@ use actix_web::{get, web, HttpResponse, Result, client::Client, Error};
#[get("/imageproxy/{url:.*}")] #[get("/imageproxy/{url:.*}")]
async fn handler(web::Path(url): web::Path<String>) -> Result<HttpResponse> { async fn handler(web::Path(url): web::Path<String>) -> Result<HttpResponse> {
dbg!(&url); if cfg!(feature = "proxy") {
let client = Client::default(); dbg!(&url);
client.get(url) let client = Client::default();
.send() client.get(url)
.await .send()
.map_err(Error::from) .await
.and_then(|res| { .map_err(Error::from)
Ok(HttpResponse::build(res.status()).streaming(res)) .and_then(|res| {
}) Ok(HttpResponse::build(res.status()).streaming(res))
})
} else {
Ok(HttpResponse::Ok().body(""))
}
} }