From acd2cff747235b3d331c42112fc037f48de8de79 Mon Sep 17 00:00:00 2001 From: robrobinbin <8597693+robrobinbin@users.noreply.github.com> Date: Sun, 24 Jan 2021 11:08:24 +0100 Subject: [PATCH 01/43] Lower number of cached items --- src/utils.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils.rs b/src/utils.rs index 0c472f2..33a430f 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -393,7 +393,7 @@ pub async fn error(msg: String) -> HttpResponse { } // Make a request to a Reddit API and parse the JSON response -#[cached(size=1000,time=60, result = true)] +#[cached(size=100,time=60, result = true)] pub async fn request(path: String) -> Result { let url = format!("https://www.reddit.com{}", path); let user_agent = format!("web:libreddit:{}", env!("CARGO_PKG_VERSION")); From e6c2d084251a0da3b6bb245a600f0defa55505a4 Mon Sep 17 00:00:00 2001 From: Spike <19519553+spikecodes@users.noreply.github.com> Date: Sun, 24 Jan 2021 23:33:18 +0000 Subject: [PATCH 02/43] Add libreddit.himiko.cloud instance --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index c3fda54..ebea15b 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,7 @@ Feel free to [open an issue](https://github.com/spikecodes/libreddit/issues/new) | [libreddit.dothq.co](https://libreddit.dothq.co) | 🇺🇸 US | ✅ | | [libreddit.insanity.wtf](https://libreddit.insanity.wtf) | 🇺🇸 US | ✅ | | [libreddit.kavin.rocks](https://libreddit.kavin.rocks) | 🇮🇳 IN | ✅ | +| [libreddit.himiko.cloud](https://libreddit.himiko.cloud) | 🇧🇬 BG | | | [spjmllawtheisznfs7uryhxumin26ssv2draj7oope3ok3wuhy43eoyd.onion](http://spjmllawtheisznfs7uryhxumin26ssv2draj7oope3ok3wuhy43eoyd.onion) | 🇮🇳 IN | | A checkmark in the "Cloudflare" category here refers to the use of the reverse proxy, [Cloudflare](https://cloudflare). The checkmark will not be listed for a site which uses Cloudflare DNS but rather the proxying service which grants Cloudflare the ability to monitor traffic to the website. From f72c9d39be3f8f416c3f955c14f2a30c4a18368d Mon Sep 17 00:00:00 2001 From: spikecodes <19519553+spikecodes@users.noreply.github.com> Date: Mon, 25 Jan 2021 17:01:02 -0800 Subject: [PATCH 03/43] Simplify proxy --- src/proxy.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/proxy.rs b/src/proxy.rs index bca2b0b..df1ca56 100644 --- a/src/proxy.rs +++ b/src/proxy.rs @@ -21,10 +21,10 @@ pub async fn handler(web::Path(b64): web::Path) -> Result "v.redd.it", ]; - match decode(b64) { - Ok(bytes) => { - let media = String::from_utf8(bytes).unwrap_or_default(); + let decoded = decode(b64).map(|bytes| String::from_utf8(bytes).unwrap_or_default()); + match decoded { + Ok(media) => { match Url::parse(media.as_str()) { Ok(url) => { let domain = url.domain().unwrap_or_default(); From f038aa61f4aa9a7ac5b9048fbd872904b3b569ff Mon Sep 17 00:00:00 2001 From: spikecodes <19519553+spikecodes@users.noreply.github.com> Date: Tue, 26 Jan 2021 10:54:57 -0800 Subject: [PATCH 04/43] Log JSON parse errors and Reddit request failures --- Cargo.lock | 56 ++++++++++++++++++++++++++-------------------------- README.md | 2 +- src/utils.rs | 4 +--- 3 files changed, 30 insertions(+), 32 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index be43310..572f2db 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1034,9 +1034,9 @@ checksum = "dd25036021b0de88a0aff6b850051563c6516d0bf53f8638938edbb9de732736" [[package]] name = "js-sys" -version = "0.3.46" +version = "0.3.47" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf3d7383929f7c9c7c2d0fa596f325832df98c3704f2c60553080f7127a58175" +checksum = "5cfb73131c35423a367daf8cbd24100af0d077668c8c2943f0e7dd775fef0f65" dependencies = [ "wasm-bindgen", ] @@ -1222,9 +1222,9 @@ dependencies = [ [[package]] name = "nom" -version = "6.0.1" +version = "6.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "88034cfd6b4a0d54dd14f4a507eceee36c0b70e5a02236c4e4df571102be17f0" +checksum = "ab6f70b46d6325aa300f1c7bb3d470127dfc27806d8ea6bf294ee0ce643ce2b1" dependencies = [ "bitvec", "lexical-core", @@ -1576,18 +1576,18 @@ checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" [[package]] name = "serde" -version = "1.0.120" +version = "1.0.123" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "166b2349061381baf54a58e4b13c89369feb0ef2eaa57198899e2312aac30aab" +checksum = "92d5161132722baa40d802cc70b15262b98258453e85e5d1d365c757c73869ae" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.120" +version = "1.0.123" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0ca2a8cb5805ce9e3b95435e3765b7b553cecc762d938d409434338386cb5775" +checksum = "9391c295d64fc0abb2c556bad848f33cb8296276b1ad2677d1ae1ace4f258f31" dependencies = [ "proc-macro2", "quote", @@ -1746,9 +1746,9 @@ checksum = "6446ced80d6c486436db5c078dde11a9f73d42b57fb273121e160b84f63d894c" [[package]] name = "syn" -version = "1.0.59" +version = "1.0.60" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "07cb8b1b4ebf86a89ee88cbd201b022b94138c623644d035185c84d3f41b7e66" +checksum = "c700597eca8a5a762beb35753ef6b94df201c81cca676604f547495a0d7f0081" dependencies = [ "proc-macro2", "quote", @@ -1783,9 +1783,9 @@ dependencies = [ [[package]] name = "thread_local" -version = "1.1.1" +version = "1.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "301bdd13d23c49672926be451130892d274d3ba0b410c18e00daa7990ff38d99" +checksum = "d8208a331e1cb318dd5bd76951d2b8fc48ca38a69f5f4e4af1b6a9f8c6236915" dependencies = [ "once_cell", ] @@ -1801,9 +1801,9 @@ dependencies = [ [[package]] name = "time" -version = "0.2.24" +version = "0.2.25" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "273d3ed44dca264b0d6b3665e8d48fb515042d42466fad93d2a45b90ec4058f7" +checksum = "1195b046942c221454c2539395f85413b33383a067449d78aab2b7b052a142f7" dependencies = [ "const_fn", "libc", @@ -1839,9 +1839,9 @@ dependencies = [ [[package]] name = "tinyvec" -version = "1.1.0" +version = "1.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ccf8dbc19eb42fba10e8feaaec282fb50e2c14b2726d6301dbfeed0f73306a6f" +checksum = "317cca572a0e89c3ce0ca1f1bdc9369547fe318a683418e42ac8f59d14701023" dependencies = [ "tinyvec_macros", ] @@ -2062,9 +2062,9 @@ checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" [[package]] name = "wasm-bindgen" -version = "0.2.69" +version = "0.2.70" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3cd364751395ca0f68cafb17666eee36b63077fb5ecd972bbcd74c90c4bf736e" +checksum = "55c0f7123de74f0dab9b7d00fd614e7b19349cd1e2f5252bbe9b1754b59433be" dependencies = [ "cfg-if 1.0.0", "wasm-bindgen-macro", @@ -2072,9 +2072,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-backend" -version = "0.2.69" +version = "0.2.70" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1114f89ab1f4106e5b55e688b828c0ab0ea593a1ea7c094b141b14cbaaec2d62" +checksum = "7bc45447f0d4573f3d65720f636bbcc3dd6ce920ed704670118650bcd47764c7" dependencies = [ "bumpalo", "lazy_static", @@ -2087,9 +2087,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro" -version = "0.2.69" +version = "0.2.70" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a6ac8995ead1f084a8dea1e65f194d0973800c7f571f6edd70adf06ecf77084" +checksum = "3b8853882eef39593ad4174dd26fc9865a64e84026d223f63bb2c42affcbba2c" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -2097,9 +2097,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.69" +version = "0.2.70" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b5a48c72f299d80557c7c62e37e7225369ecc0c963964059509fbafe917c7549" +checksum = "4133b5e7f2a531fa413b3a1695e925038a05a71cf67e87dafa295cb645a01385" dependencies = [ "proc-macro2", "quote", @@ -2110,15 +2110,15 @@ dependencies = [ [[package]] name = "wasm-bindgen-shared" -version = "0.2.69" +version = "0.2.70" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7e7811dd7f9398f14cc76efd356f98f03aa30419dea46aa810d71e819fc97158" +checksum = "dd4945e4943ae02d15c13962b38a5b1e81eadd4b71214eee75af64a4d6a4fd64" [[package]] name = "web-sys" -version = "0.3.46" +version = "0.3.47" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "222b1ef9334f92a21d3fb53dc3fd80f30836959a90f9274a626d7e06315ba3c3" +checksum = "c40dc691fc48003eba817c38da7113c15698142da971298003cac3ef175680b3" dependencies = [ "js-sys", "wasm-bindgen", diff --git a/README.md b/README.md index ebea15b..1a03d6d 100644 --- a/README.md +++ b/README.md @@ -124,7 +124,7 @@ Results from Google Lighthouse ([Libreddit Report](https://lighthouse-dot-webdot For transparency, I hope to describe all the ways Libreddit handles user privacy. -**Logging:** In production (when running the binary, hosting with docker, or using the official instances), Libreddit logs nothing. When debugging (running from source without `--release`), Libreddit logs post IDs and URL paths fetched to aid with troubleshooting. +**Logging:** In production (when running the binary, hosting with docker, or using the official instances), Libreddit logs when Reddit is ratelimiting Libreddit and when Reddit's JSON responses can't be parsed. When debugging (running from source without `--release`), Libreddit logs post IDs and URL paths fetched to aid with troubleshooting. **DNS:** Both official domains (`libredd.it` and `libreddit.spike.codes`) use Cloudflare as the DNS resolver. Though, the sites are not proxied through Cloudflare meaning Cloudflare doesn't have access to user traffic. diff --git a/src/utils.rs b/src/utils.rs index 33a430f..865c97c 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -393,7 +393,7 @@ pub async fn error(msg: String) -> HttpResponse { } // Make a request to a Reddit API and parse the JSON response -#[cached(size=100,time=60, result = true)] +// #[cached(size=100,time=60, result = true)] pub async fn request(path: String) -> Result { let url = format!("https://www.reddit.com{}", path); let user_agent = format!("web:libreddit:{}", env!("CARGO_PKG_VERSION")); @@ -462,7 +462,6 @@ pub async fn request(path: String) -> Result { match from_str(&response.into_string().unwrap()) { Ok(json) => Ok(json), Err(_) => { - #[cfg(debug_assertions)] dbg!(format!("{} - Failed to parse page JSON data", url)); Err("Failed to parse page JSON data".to_string()) } @@ -476,7 +475,6 @@ pub async fn request(path: String) -> Result { } // If failed to send request Err(_e) => { - #[cfg(debug_assertions)] dbg!(format!("{} - {}", url, _e)); Err("Couldn't send request to Reddit, this instance may be being rate-limited. Try another.".to_string()) } From d5b1c3a5bb0da63dba6fb843449307e3ad52cbe6 Mon Sep 17 00:00:00 2001 From: spikecodes <19519553+spikecodes@users.noreply.github.com> Date: Tue, 26 Jan 2021 11:00:07 -0800 Subject: [PATCH 05/43] Fix connection reset error --- src/utils.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils.rs b/src/utils.rs index 865c97c..52a5637 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -459,7 +459,7 @@ pub async fn request(path: String) -> Result { // If response is success Ok(response) => { // Parse the response from Reddit as JSON - match from_str(&response.into_string().unwrap()) { + match from_str(&response.into_string().unwrap_or_default()) { Ok(json) => Ok(json), Err(_) => { dbg!(format!("{} - Failed to parse page JSON data", url)); From 8c04365049680d4520c98fc767189632b7f72f1d Mon Sep 17 00:00:00 2001 From: spikecodes <19519553+spikecodes@users.noreply.github.com> Date: Wed, 27 Jan 2021 17:48:58 -0800 Subject: [PATCH 06/43] Improve error logging --- Cargo.lock | 107 +++------------------------------------------------ Cargo.toml | 5 +-- src/utils.rs | 10 ++--- 3 files changed, 12 insertions(+), 110 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 572f2db..bc668a3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -346,15 +346,6 @@ dependencies = [ "toml", ] -[[package]] -name = "async-mutex" -version = "1.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "479db852db25d9dbf6204e6cb6253698f175c15726470f78af0d918e99d6156e" -dependencies = [ - "event-listener", -] - [[package]] name = "async-recursion" version = "0.3.1" @@ -520,40 +511,6 @@ dependencies = [ "bytes 1.0.1", ] -[[package]] -name = "cached" -version = "0.23.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5e2afe73808fbaac302e39c9754bfc3c4b4d0f99c9c240b9f4e4efc841ad1b74" -dependencies = [ - "async-mutex", - "async-trait", - "cached_proc_macro", - "cached_proc_macro_types", - "futures", - "hashbrown", - "once_cell", -] - -[[package]] -name = "cached_proc_macro" -version = "0.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bf857ae42d910aede5c5186e62684b0d7a597ce2fe3bd14448ab8f7ef439848c" -dependencies = [ - "async-mutex", - "cached_proc_macro_types", - "darling", - "quote", - "syn", -] - -[[package]] -name = "cached_proc_macro_types" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3a4f925191b4367301851c6d99b09890311d74b0d43f274c0b34c86d308a3663" - [[package]] name = "cc" version = "1.0.66" @@ -616,41 +573,6 @@ dependencies = [ "cfg-if 1.0.0", ] -[[package]] -name = "darling" -version = "0.10.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0d706e75d87e35569db781a9b5e2416cff1236a47ed380831f959382ccd5f858" -dependencies = [ - "darling_core", - "darling_macro", -] - -[[package]] -name = "darling_core" -version = "0.10.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f0c960ae2da4de88a91b2d920c2a7233b400bc33cb28453a2987822d8392519b" -dependencies = [ - "fnv", - "ident_case", - "proc-macro2", - "quote", - "strsim", - "syn", -] - -[[package]] -name = "darling_macro" -version = "0.10.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9b5a2f4ac4969822c62224815d069952656cadc7084fdca9751e6d959189b72" -dependencies = [ - "darling_core", - "quote", - "syn", -] - [[package]] name = "derive_more" version = "0.99.11" @@ -704,12 +626,6 @@ dependencies = [ "syn", ] -[[package]] -name = "event-listener" -version = "2.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f7531096570974c3a9dcf9e4b8e1cede1ec26cf5046219fb3b9d897503b9be59" - [[package]] name = "flate2" version = "1.0.19" @@ -969,12 +885,6 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6cab2627acfc432780848602f3f558f7e9dd427352224b0d9324025796d2a5e" -[[package]] -name = "ident_case" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" - [[package]] name = "idna" version = "0.2.0" @@ -1078,9 +988,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.82" +version = "0.2.83" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "89203f3fba0a3795506acaad8ebce3c80c0af93f994d5a1d7a0b1eeb23271929" +checksum = "7eb0c4e9c72ee9d69b767adebc5f4788462a3b45624acd919475c92597bcaf4f" [[package]] name = "libreddit" @@ -1090,7 +1000,6 @@ dependencies = [ "askama", "async-recursion", "base64 0.13.0", - "cached", "futures", "regex", "serde", @@ -1117,11 +1026,11 @@ dependencies = [ [[package]] name = "log" -version = "0.4.13" +version = "0.4.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fcf3805d4480bb5b86070dcfeb9e2cb2ebc148adb753c5cca5f884d1d65a42b2" +checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710" dependencies = [ - "cfg-if 0.1.10", + "cfg-if 1.0.0", ] [[package]] @@ -1738,12 +1647,6 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "213701ba3370744dcd1a12960caa4843b3d68b4d1c0a5d575e0d65b2ee9d16c0" -[[package]] -name = "strsim" -version = "0.9.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6446ced80d6c486436db5c078dde11a9f73d42b57fb273121e160b84f63d894c" - [[package]] name = "syn" version = "1.0.60" diff --git a/Cargo.toml b/Cargo.toml index 63a3582..42b8bb6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,11 +12,10 @@ base64 = "0.13" actix-web = { version = "3.3", features = ["rustls"] } futures = "0.3" askama = "0.10" -ureq = "2.0" +ureq = "2" serde = { version = "1.0", default_features = false, features = ["derive"] } serde_json = "1.0" async-recursion = "0.3" url = "2.2" regex = "1.4" -time = "0.2" -cached = "0.23.0" +time = "0.2" \ No newline at end of file diff --git a/src/utils.rs b/src/utils.rs index 52a5637..875ac30 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -9,7 +9,7 @@ use serde_json::{from_str, Value}; use std::collections::HashMap; use time::{Duration, OffsetDateTime}; use url::Url; -use cached::proc_macro::cached; +// use cached::proc_macro::cached; // // STRUCTS @@ -461,8 +461,8 @@ pub async fn request(path: String) -> Result { // Parse the response from Reddit as JSON match from_str(&response.into_string().unwrap_or_default()) { Ok(json) => Ok(json), - Err(_) => { - dbg!(format!("{} - Failed to parse page JSON data", url)); + Err(e) => { + println!("{} - Failed to parse page JSON data: {}", url, e); Err("Failed to parse page JSON data".to_string()) } } @@ -474,8 +474,8 @@ pub async fn request(path: String) -> Result { Err("Page not found".to_string()) } // If failed to send request - Err(_e) => { - dbg!(format!("{} - {}", url, _e)); + Err(e) => { + println!("{} - Couldn't send request to Reddit: {}", url, e); Err("Couldn't send request to Reddit, this instance may be being rate-limited. Try another.".to_string()) } } From 5fe9ce8d7b0450d13f22f45ca77de39f367e2acb Mon Sep 17 00:00:00 2001 From: spikecodes <19519553+spikecodes@users.noreply.github.com> Date: Wed, 27 Jan 2021 21:48:32 -0800 Subject: [PATCH 07/43] Refactor JSON parsing error logging --- src/utils.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/utils.rs b/src/utils.rs index 875ac30..f06eb79 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -459,10 +459,11 @@ pub async fn request(path: String) -> Result { // If response is success Ok(response) => { // Parse the response from Reddit as JSON - match from_str(&response.into_string().unwrap_or_default()) { + let json_string = &response.into_string().unwrap_or_default(); + match from_str(json_string) { Ok(json) => Ok(json), Err(e) => { - println!("{} - Failed to parse page JSON data: {}", url, e); + println!("{} - Failed to parse page JSON data: {} - {}", url, e, json_string); Err("Failed to parse page JSON data".to_string()) } } From 00b135fb0f5129833788fbd94dffcf8ed8240895 Mon Sep 17 00:00:00 2001 From: spikecodes <19519553+spikecodes@users.noreply.github.com> Date: Thu, 28 Jan 2021 15:50:18 -0800 Subject: [PATCH 08/43] Fix #75 --- Cargo.lock | 4 ++-- static/style.css | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bc668a3..5c5e272 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1757,9 +1757,9 @@ checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c" [[package]] name = "tokio" -version = "0.2.24" +version = "0.2.25" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "099837d3464c16a808060bb3f02263b412f6fafcb5d01c533d309985fbeebe48" +checksum = "6703a273949a90131b290be1fe7b039d0fc884aa1935860dfcbe056f28cd8092" dependencies = [ "bytes 0.5.6", "futures-core", diff --git a/static/style.css b/static/style.css index 6c2f914..e8594b2 100644 --- a/static/style.css +++ b/static/style.css @@ -99,7 +99,7 @@ nav #version { opacity: 50%; } #settings_link { font-size: 18px; - margin-left: 20px; + margin-left: 10px; opacity: 0.8; } @@ -304,6 +304,7 @@ select, #search { align-items: center; border-right: 2px var(--outside) solid; padding: 0 10px; + max-width: 50%; } #restrict_sr { margin-right: 5px; } @@ -973,5 +974,5 @@ td, th { #user, #sidebar { margin: 20px 0; } #logo { margin: 5px auto; } - #searchbox { width: 100%; } + #searchbox { width: calc(100vw - 35px); } } From 30c33d91e1c1fc6402c6eb3295f9725954c7a93d Mon Sep 17 00:00:00 2001 From: spikecodes <19519553+spikecodes@users.noreply.github.com> Date: Thu, 28 Jan 2021 20:06:35 -0800 Subject: [PATCH 09/43] Specify video MIME in posts --- templates/post.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/post.html b/templates/post.html index cdddaf2..a44721c 100644 --- a/templates/post.html +++ b/templates/post.html @@ -73,7 +73,7 @@ {% else if post.post_type == "video" || post.post_type == "gif" %} - + {% else if post.post_type == "link" %} {{ post.media.url }} {% endif %} From f55ea5a3531e69ea24aa3e4d08ecef09693ef001 Mon Sep 17 00:00:00 2001 From: spikecodes <19519553+spikecodes@users.noreply.github.com> Date: Thu, 28 Jan 2021 21:53:10 -0800 Subject: [PATCH 10/43] Specify default headers for security --- src/main.rs | 8 ++++++++ templates/base.html | 2 +- templates/post.html | 2 +- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index c705bbb..ec479f2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,12 +21,14 @@ async fn style() -> HttpResponse { async fn robots() -> HttpResponse { HttpResponse::Ok() + .content_type("text/plain") .header("Cache-Control", "public, max-age=1209600, s-maxage=86400") .body("User-agent: *\nAllow: /") } async fn favicon() -> HttpResponse { HttpResponse::Ok() + .content_type("image/x-icon") .header("Cache-Control", "public, max-age=1209600, s-maxage=86400") .body(include_bytes!("../static/favicon.ico").as_ref()) } @@ -66,6 +68,12 @@ async fn main() -> std::io::Result<()> { }) // Append trailing slash and remove double slashes .wrap(middleware::NormalizePath::default()) + // Apply default headers for security + .wrap(middleware::DefaultHeaders::new() + .header("Referrer-Policy", "no-referrer") + .header("X-Content-Type-Options", "nosniff") + .header("X-Frame-Options", "DENY") + .header("Content-Security-Policy", "default-src 'self'; style-src 'self' 'unsafe-inline'; base-uri 'none'; img-src 'self' data:; form-action 'self'; frame-ancestors: 'none';")) // Default service in case no routes match .default_service(web::get().to(|| utils::error("Nothing here".to_string()))) // Read static files diff --git a/templates/base.html b/templates/base.html index 0de8a8e..2eccb53 100644 --- a/templates/base.html +++ b/templates/base.html @@ -4,7 +4,7 @@ {% block head %} {% block title %}Libreddit{% endblock %} - + diff --git a/templates/post.html b/templates/post.html index a44721c..cdddaf2 100644 --- a/templates/post.html +++ b/templates/post.html @@ -73,7 +73,7 @@ {% else if post.post_type == "video" || post.post_type == "gif" %} - + {% else if post.post_type == "link" %} {{ post.media.url }} {% endif %} From 49a6168607e1c0762f02d18773241155848c01b7 Mon Sep 17 00:00:00 2001 From: spikecodes <19519553+spikecodes@users.noreply.github.com> Date: Fri, 29 Jan 2021 14:39:03 -0800 Subject: [PATCH 11/43] Improve CSP --- src/main.rs | 3 +-- templates/base.html | 4 +--- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index ec479f2..40f828c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,7 +21,6 @@ async fn style() -> HttpResponse { async fn robots() -> HttpResponse { HttpResponse::Ok() - .content_type("text/plain") .header("Cache-Control", "public, max-age=1209600, s-maxage=86400") .body("User-agent: *\nAllow: /") } @@ -73,7 +72,7 @@ async fn main() -> std::io::Result<()> { .header("Referrer-Policy", "no-referrer") .header("X-Content-Type-Options", "nosniff") .header("X-Frame-Options", "DENY") - .header("Content-Security-Policy", "default-src 'self'; style-src 'self' 'unsafe-inline'; base-uri 'none'; img-src 'self' data:; form-action 'self'; frame-ancestors: 'none';")) + .header("Content-Security-Policy", "default-src 'none'; style-src 'self' 'unsafe-inline'; base-uri 'none'; img-src 'self' data:; form-action 'self'; frame-ancestors 'none';")) // Default service in case no routes match .default_service(web::get().to(|| utils::error("Nothing here".to_string()))) // Read static files diff --git a/templates/base.html b/templates/base.html index 2eccb53..7764eae 100644 --- a/templates/base.html +++ b/templates/base.html @@ -3,13 +3,11 @@ {% block head %} {% block title %}Libreddit{% endblock %} - - - + {% endblock %} a { + padding: 10px 20px; + border-radius: 5px; + color: var(--foreground); +} + +#sub_subscription > .add { color: var(--foreground); background-color: var(--accent); } +#sub_subscription > .remove { color: var(--text); background-color: var(--highlighted); } + +#sub_list { + display: flex; + align-items: center; + justify-content: center; + padding: 10px 20px; +} + +#subs { + border-radius: 5px; + box-shadow: var(--shadow); + background: var(--outside); + display: flex; + overflow: auto; +} + +#subs a { + padding: 10px 20px; +} + +#subs > .selected { + background-color: var(--accent); + color: var(--foreground); +} + /* Wiki Pages */ #wiki { @@ -954,10 +994,12 @@ td, th { } @media screen and (max-width: 800px) { + body { padding-top: 100px } + main { flex-direction: column-reverse; padding: 10px; - margin: 100px 0 10px 0; + margin: 0 0 10px 0; max-width: 100%; } @@ -967,6 +1009,8 @@ td, th { width: calc(100% - 20px); } + #sub_list { padding: 10px; } + aside, #subreddit, #user { margin: 0; max-width: 100%; diff --git a/templates/base.html b/templates/base.html index 7764eae..2451508 100644 --- a/templates/base.html +++ b/templates/base.html @@ -26,6 +26,10 @@ {% block search %}{% endblock %} code + + + {% block sub_list %} + {% endblock %} {% block body %} diff --git a/templates/post.html b/templates/post.html index cdddaf2..0d8596b 100644 --- a/templates/post.html +++ b/templates/post.html @@ -13,6 +13,10 @@ {% endblock %} +{% block sub_list %} + {% call utils::sub_list(post.community.as_str()) %} +{% endblock %} + {% macro comment(item) -%}
diff --git a/templates/search.html b/templates/search.html index ecbe340..c244fc3 100644 --- a/templates/search.html +++ b/templates/search.html @@ -3,6 +3,10 @@ {% block title %}Libreddit: search results - {{ params.q }}{% endblock %} +{% block sub_list %} + {% call utils::sub_list("") %} +{% endblock %} + {% block content %}
diff --git a/templates/settings.html b/templates/settings.html index 0dbec00..3fe6910 100644 --- a/templates/settings.html +++ b/templates/settings.html @@ -7,6 +7,10 @@ {% call utils::search("".to_owned(), "", "") %} {% endblock %} +{% block sub_list %} + {% call utils::sub_list("") %} +{% endblock %} + {% block content %}
diff --git a/templates/subreddit.html b/templates/subreddit.html index 9a26aaa..eb71961 100644 --- a/templates/subreddit.html +++ b/templates/subreddit.html @@ -11,6 +11,10 @@ {% call utils::search(["/r/", sub.name.as_str()].concat(), "") %} {% endblock %} +{% block sub_list %} + {% call utils::sub_list(sub.name.as_str()) %} +{% endblock %} + {% block body %}
@@ -121,6 +125,13 @@
{{ sub.members }}
{{ sub.active }}
+
+ {% if prefs.subs.contains(sub.name) %} + + {% else %} + + {% endif %} +
{% endif %} {%- endmacro %} diff --git a/templates/wiki.html b/templates/wiki.html index 0c946d8..882f228 100644 --- a/templates/wiki.html +++ b/templates/wiki.html @@ -10,7 +10,7 @@ {% call utils::search(["/r/", sub.as_str()].concat(), "") %} {% endblock %} -{% block sub_list %} +{% block subscriptions %} {% call utils::sub_list(sub.as_str()) %} {% endblock %} From dc2030e6f3c46a9f8af52d8f93a54dba4b6634b7 Mon Sep 17 00:00:00 2001 From: Matthew Crossman Date: Sat, 30 Jan 2021 21:21:54 +1100 Subject: [PATCH 17/43] Vertical list subscriptions. --- static/style.css | 50 +++++++++++++++++++++++++++++++--------- templates/base.html | 3 ++- templates/post.html | 2 +- templates/search.html | 2 +- templates/settings.html | 2 +- templates/subreddit.html | 2 +- templates/user.html | 2 +- templates/utils.html | 20 ++++++++-------- templates/wiki.html | 2 +- 9 files changed, 58 insertions(+), 27 deletions(-) diff --git a/static/style.css b/static/style.css index 9e57140..61c2e73 100644 --- a/static/style.css +++ b/static/style.css @@ -84,7 +84,7 @@ nav { font-size: 20px; - z-index: 1; + z-index: 2; top: 0; padding: 5px 15px; min-height: 40px; @@ -256,28 +256,49 @@ aside { /* Subscribed subreddit list */ -#subscriptions { +#subscriptions_container { display: flex; max-width: 1000px; margin: 0 auto; - align-items: center; padding: 10px 20px; } -.wide #subscriptions { +#subscriptions_container.narrow { + max-width: 750px; +} + +.wide #subscriptions_container, .wide #subscriptions_container.narrow { max-width: calc(100% - 40px); } +#subscriptions { + display: inline-block; + position: relative; + border-radius: 5px; + background-color: var(--outside); + align-items: center; + box-sizing: border-box; +} + +#subscriptions > summary { + padding: 10px 20px 10px 15px; +} + #sub_list { + position: absolute; + display: flex; + min-width: 100%; border-radius: 5px; box-shadow: var(--shadow); background: var(--outside); - display: flex; + flex-direction: column; overflow: auto; + z-index: 1; } #sub_list > a { padding: 10px 20px; + transition: 0.2s background; } #sub_list > .selected { @@ -285,6 +306,10 @@ aside { color: var(--foreground); } +#sub_list > a:not(.selected):hover { + background-color: var(--foreground); +} + /* Wiki Pages */ #wiki { @@ -505,10 +530,6 @@ a.search_subreddit:hover { .post:not(:last-child) { margin-bottom: 10px; } -.post.highlighted { - margin: 20px 0; -} - .post:hover { background: var(--foreground); } @@ -1022,9 +1043,16 @@ td, th { width: calc(100% - 20px); } - #sub_list { + #subscriptions_container { padding: 10px; - max-width: 100%; + justify-content: center; + } + #subscriptions { position: unset; } + + #sub_list { + left: 10px; + right: 10px; + min-width: auto; } aside, #subreddit, #user { diff --git a/templates/base.html b/templates/base.html index 520b737..e333e05 100644 --- a/templates/base.html +++ b/templates/base.html @@ -23,10 +23,11 @@ v{{ env!("CARGO_PKG_VERSION") }} settings

- {% block subscriptions %}{% endblock %} {% block search %}{% endblock %} code + + {% block subscriptions %}{% endblock %} {% block body %} diff --git a/templates/post.html b/templates/post.html index 619005e..a8db811 100644 --- a/templates/post.html +++ b/templates/post.html @@ -14,7 +14,7 @@ {% endblock %} {% block subscriptions %} - {% call utils::sub_list(post.community.as_str()) %} + {% call utils::sub_list(post.community.as_str(), "narrow") %} {% endblock %} diff --git a/templates/search.html b/templates/search.html index 37edfc6..33e644a 100644 --- a/templates/search.html +++ b/templates/search.html @@ -4,7 +4,7 @@ {% block title %}Libreddit: search results - {{ params.q }}{% endblock %} {% block subscriptions %} - {% call utils::sub_list("") %} + {% call utils::sub_list("", "narrow") %} {% endblock %} {% block content %} diff --git a/templates/settings.html b/templates/settings.html index e0e3bf3..a2514ea 100644 --- a/templates/settings.html +++ b/templates/settings.html @@ -8,7 +8,7 @@ {% endblock %} {% block subscriptions %} - {% call utils::sub_list("") %} + {% call utils::sub_list("", "narrow") %} {% endblock %} {% block content %} diff --git a/templates/subreddit.html b/templates/subreddit.html index e3d550d..94c94d6 100644 --- a/templates/subreddit.html +++ b/templates/subreddit.html @@ -12,7 +12,7 @@ {% endblock %} {% block subscriptions %} - {% call utils::sub_list(sub.name.as_str()) %} + {% call utils::sub_list(sub.name.as_str(), "wide") %} {% endblock %} {% block body %} diff --git a/templates/user.html b/templates/user.html index c19a3ca..2f5f250 100644 --- a/templates/user.html +++ b/templates/user.html @@ -8,7 +8,7 @@ {% block title %}{{ user.name.replace("u/", "") }} (u/{{ user.name }}) - Libreddit{% endblock %} {% block subscriptions %} - {% call utils::sub_list("") %} + {% call utils::sub_list("", "wide") %} {% endblock %} {% block body %} diff --git a/templates/utils.html b/templates/utils.html index f8a0318..f036045 100644 --- a/templates/utils.html +++ b/templates/utils.html @@ -40,15 +40,17 @@ {% endfor %} {%- endmacro %} -{% macro sub_list(current) -%} +{% macro sub_list(current, width) -%} {% if prefs.subs.len() > 0 %} -
- subscribed -
- {% for sub in prefs.subs %} - {{ sub }} - {% endfor %} -
-
+
+
+ Subscriptions +
+ {% for sub in prefs.subs %} + {{ sub }} + {% endfor %} +
+
+
{% endif %} {%- endmacro %} diff --git a/templates/wiki.html b/templates/wiki.html index 882f228..0a05fb3 100644 --- a/templates/wiki.html +++ b/templates/wiki.html @@ -11,7 +11,7 @@ {% endblock %} {% block subscriptions %} - {% call utils::sub_list(sub.as_str()) %} + {% call utils::sub_list(sub.as_str(), "narrow") %} {% endblock %} {% block body %} From 449899962a57fdb8864016791baa9a149b9ca17f Mon Sep 17 00:00:00 2001 From: Matthew Crossman Date: Sat, 30 Jan 2021 22:27:49 +1100 Subject: [PATCH 18/43] Change subscription to get. Add subs to settings. --- src/main.rs | 2 +- src/subreddit.rs | 11 ++++++++++- static/style.css | 34 +++++++++++++++++++++++++++++----- templates/settings.html | 20 +++++++++++++++----- templates/subreddit.html | 16 +++++++--------- 5 files changed, 62 insertions(+), 21 deletions(-) diff --git a/src/main.rs b/src/main.rs index 7690c11..4cc134d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -100,7 +100,7 @@ async fn main() -> std::io::Result<()> { .route("/", web::get().to(subreddit::page)) .route("/{sort:hot|new|top|rising|controversial}/", web::get().to(subreddit::page)) // Handle subscribe/unsubscribe - .route("/{action:subscribe|unsubscribe}/", web::post().to(subreddit::subscriptions)) + .route("/{action:subscribe|unsubscribe}/", web::get().to(subreddit::subscriptions)) // View post on subreddit .service( web::scope("/comments/{id}/{title}") diff --git a/src/subreddit.rs b/src/subreddit.rs index 8cd46a2..a395740 100644 --- a/src/subreddit.rs +++ b/src/subreddit.rs @@ -103,7 +103,16 @@ pub async fn subscriptions(req: HttpRequest) -> HttpResponse { } // Redirect back to subreddit - let path = format!("/r/{}", sub); + // check for redirect parameter if unsubscribing from outside sidebar + let redirect_path = param(&format!("{}?{}", req.path(), req.query_string()), "redirect"); + let path; + + if redirect_path.len() > 1 && redirect_path.chars().nth(0).unwrap() == '/' { + path = redirect_path; + } else { + path = format!("/r/{}", sub); + } + res .content_type("text/html") .set_header("Location", path.to_string()) diff --git a/static/style.css b/static/style.css index 61c2e73..674aaf1 100644 --- a/static/style.css +++ b/static/style.css @@ -239,7 +239,7 @@ aside { margin-top: 20px; } -#sub_subscription > input { +#sub_subscription > a { padding: 10px 20px; border-radius: 5px; } @@ -876,7 +876,7 @@ a.search_subreddit:hover { opacity: 0.75; } -#prefs { +.prefs { display: flex; flex-direction: column; justify-content: space-between; @@ -886,7 +886,7 @@ a.search_subreddit:hover { border-radius: 5px; } -#prefs > div { +.prefs > div { display: flex; justify-content: space-between; width: 100%; @@ -894,11 +894,11 @@ a.search_subreddit:hover { align-items: center; } -#prefs > div:not(:last-of-type) { +.prefs > div:not(:last-of-type) { margin-bottom: 10px; } -#prefs select { +.prefs select { border-radius: 5px; box-shadow: var(--shadow); margin-left: 20px; @@ -917,6 +917,30 @@ input[type="submit"] { -webkit-appearance: none; -moz-appearance: none; } + +#settings_subs { + list-style: none; + padding: 0; +} + +#settings_subs > li { + display: flex; + margin: 10px 0; +} +#settings_subs > li:last-of-type { margin-bottom: 0; } + +#settings_subs > li > span { + padding: 10px 0; + margin-right: auto; +} + +#settings_subs > li > a { + margin-left: 30px; + padding: 10px 20px; + border-radius: 5px; + background-color: var(--highlighted); +} + /* Markdown */ .md > *:not(:first-child) { diff --git a/templates/settings.html b/templates/settings.html index a2514ea..96e397c 100644 --- a/templates/settings.html +++ b/templates/settings.html @@ -7,13 +7,9 @@ {% call utils::search("".to_owned(), "", "") %} {% endblock %} -{% block subscriptions %} - {% call utils::sub_list("", "narrow") %} -{% endblock %} - {% block content %}
-
+

Appearance

@@ -52,5 +48,19 @@

Note: settings are saved in browser cookies. Clearing your cookie data will reset them.

+ {% if prefs.subs.len() > 0 %} + + {% endif %} + {% endblock %} diff --git a/templates/subreddit.html b/templates/subreddit.html index 94c94d6..ee6ed93 100644 --- a/templates/subreddit.html +++ b/templates/subreddit.html @@ -125,15 +125,13 @@
{{ sub.members }}
{{ sub.active }}
- {% if prefs.subs.contains(sub.name) %} -
- -
- {% else %} -
- -
- {% endif %} +
+ {% if prefs.subs.contains(sub.name) %} + + {% else %} + + {% endif %} +