From b1658c94afe276b24af300a0c699ab60474b3db1 Mon Sep 17 00:00:00 2001 From: Andrea Spacca Date: Wed, 1 Mar 2023 11:38:21 +0900 Subject: [PATCH 1/5] feature parity with legacy ytproxy --- src/main.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 10688a5..fcf88fc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,9 @@ use std::env; use std::error::Error; +use std::string::ToString; use actix_web::{App, HttpRequest, HttpResponse, HttpResponseBuilder, HttpServer, web}; +use actix_web::dev::ResourcePath; use actix_web::http::Method; use image::EncodableLayout; use lazy_static::lazy_static; @@ -49,6 +51,10 @@ lazy_static!( }; ); +lazy_static!( + static ref PREFIX_PATH: String = env::var("PREFIX_PATH").unwrap_or_else(|_| "".to_string()); +); + const ALLOWED_DOMAINS: [&str; 7] = [ "youtube.com", "googlevideo.com", @@ -59,6 +65,7 @@ const ALLOWED_DOMAINS: [&str; 7] = [ "odycdn.com", ]; + fn add_headers(response: &mut HttpResponseBuilder) { response .append_header(("Access-Control-Allow-Origin", "*")) @@ -73,6 +80,11 @@ fn is_header_allowed(header: &str) -> bool { } !matches!(header, "host" | + "authorization" | + "origin" | + "referer" | + "cookie" | + "etag" | "content-length" | "set-cookie" | "alt-svc" | @@ -242,7 +254,7 @@ fn localize_url(url: &str, host: &str) -> String { url.query_pairs_mut() .append_pair("host", &host); - return format!("{}?{}", url.path(), url.query().unwrap()); + return format!("{}{}?{}", PREFIX_PATH.as_str(), url.path(), url.query().unwrap()); } else if url.ends_with(".m3u8") || url.ends_with(".ts") { return if url.contains('?') { format!("{}&host={}", url, host) From f386d6ae0ccf747fb9b5ce8e03f64428bddc63fc Mon Sep 17 00:00:00 2001 From: Andrea Spacca Date: Sat, 2 Dec 2023 09:44:28 +0900 Subject: [PATCH 2/5] fix --- src/main.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index c6d99ae..e3cd977 100644 --- a/src/main.rs +++ b/src/main.rs @@ -47,7 +47,7 @@ async fn main() -> std::io::Result<()> { .await } -static PREFIX_PATH: Lazy = Lazy::new(|| String = env::var("PREFIX_PATH").unwrap_or_else(|_| "".to_string()); +static PREFIX_PATH: Lazy = Lazy::new(|| String::from(env::var("PREFIX_PATH").unwrap_or_else(|_| "".to_string()))); static RE_DOMAIN: Lazy = Lazy::new(|| Regex::new(r"^(?:[a-z\d.-]*\.)?([a-z\d-]*\.[a-z\d-]*)$").unwrap()); static RE_MANIFEST: Lazy = Lazy::new(|| Regex::new("(?m)URI=\"([^\"]+)\"").unwrap()); @@ -624,12 +624,12 @@ fn finalize_url(path: &str, query: BTreeMap) -> String { if qhash.is_some() { let mut query = QString::new(query.into_iter().collect::>()); query.add_pair(("qhash", qhash.unwrap())); - return format!("{}?{}", path, query); + return format!("{}{}?{}", PREFIX_PATH.as_str(), path, query); } } let query = QString::new(query.into_iter().collect::>()); - format!("{}{}?{}", PREFIX_PATH.as_str(), path, query); + format!("{}{}?{}", PREFIX_PATH.as_str(), path, query) } fn localize_url(url: &str, host: &str) -> String { From a83a14fc0c482d3fd3ee8f6904e76dcc38a3c12b Mon Sep 17 00:00:00 2001 From: Andrea Spacca Date: Mon, 11 Mar 2024 23:55:50 +0900 Subject: [PATCH 3/5] cr fixes --- Cargo.lock | 1 + Cargo.toml | 3 +++ src/main.rs | 2 +- src/utils.rs | 20 +++++++++++++++----- 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 54c7cd8..320cbcf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1423,6 +1423,7 @@ dependencies = [ "bytes", "futures-util", "image", + "lazy_static", "libwebp-sys", "mimalloc", "once_cell", diff --git a/Cargo.toml b/Cargo.toml index bf231f9..b66df02 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,6 +26,7 @@ regex = "1.10.3" blake3 = { version = "1.5.0", optional = true } bytes = "1.5.0" futures-util = "0.3.30" +lazy_static = "1.4.0" [features] default = ["webp", "mimalloc", "reqwest-rustls", "qhash"] @@ -42,5 +43,7 @@ optimized = ["libwebp-sys?/sse41", "libwebp-sys?/avx2", "libwebp-sys?/neon"] qhash = ["blake3"] +prefix-path = [] + [profile.release] lto = true diff --git a/src/main.rs b/src/main.rs index 531d96b..ae453e1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -511,4 +511,4 @@ async fn index(req: HttpRequest) -> Result> { // Stream response Ok(response.streaming(resp.bytes_stream())) -} \ No newline at end of file +} diff --git a/src/utils.rs b/src/utils.rs index 9f741ad..b659c41 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,11 +1,22 @@ -use once_cell::sync::Lazy; +use lazy_static::lazy_static; use qstring::QString; use reqwest::Url; use std::borrow::Cow; use std::collections::BTreeMap; use std::env; -static PREFIX_PATH: Lazy = Lazy::new(|| String::from(env::var("PREFIX_PATH").unwrap_or_else(|_| "".to_string()))); +#[cfg(not(feature = "prefix-path"))] +lazy_static! { + static ref PREFIX_PATH: Option = Some(String::from("")); +} + +#[cfg(feature = "prefix-path")] +lazy_static! { + static ref PREFIX_PATH: Option = match env::var("PREFIX_PATH") { + Ok(v) => Some(String::from(v)), + Err(e) => panic!("$PREFIX_PATH is not set ({})", e) + }; +} pub fn read_buf(buf: &[u8], pos: &mut usize) -> u8 { let byte = buf[*pos]; @@ -17,7 +28,6 @@ fn finalize_url(path: &str, query: BTreeMap) -> String { #[cfg(feature = "qhash")] { use std::collections::BTreeSet; - use std::env; let qhash = { let secret = env::var("HASH_SECRET"); @@ -50,12 +60,12 @@ fn finalize_url(path: &str, query: BTreeMap) -> String { if qhash.is_some() { let mut query = QString::new(query.into_iter().collect::>()); query.add_pair(("qhash", qhash.unwrap())); - return format!("{}{}?{}", PREFIX_PATH.as_str(), path, query); + return format!("{}{}?{}", PREFIX_PATH.as_ref().unwrap().to_string(), path, query); } } let query = QString::new(query.into_iter().collect::>()); - format!("{}{}?{}", PREFIX_PATH.as_str(), path, query) + format!("{}{}?{}", PREFIX_PATH.as_ref().unwrap().to_string(), path, query) } pub fn localize_url(url: &str, host: &str) -> String { From 67369a494a00ae3404d517f52fa850a94efff95c Mon Sep 17 00:00:00 2001 From: Andrea Spacca Date: Sun, 7 Apr 2024 08:59:36 +0900 Subject: [PATCH 4/5] cr fixes --- Cargo.lock | 1 - Cargo.toml | 1 - src/utils.rs | 39 +++++++++++++++++++++++++-------------- 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 428c2d0..0645208 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1369,7 +1369,6 @@ dependencies = [ "bytes", "futures-util", "image", - "lazy_static", "libwebp-sys", "mimalloc", "once_cell", diff --git a/Cargo.toml b/Cargo.toml index c413696..1c9d347 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,7 +26,6 @@ regex = "1.10.4" blake3 = { version = "1.5.1", optional = true } bytes = "1.6.0" futures-util = "0.3.30" -lazy_static = "1.4.0" [features] default = ["webp", "mimalloc", "reqwest-rustls", "qhash"] diff --git a/src/utils.rs b/src/utils.rs index b659c41..5f733d5 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,22 +1,15 @@ -use lazy_static::lazy_static; +use once_cell::sync::Lazy; use qstring::QString; use reqwest::Url; use std::borrow::Cow; use std::collections::BTreeMap; use std::env; -#[cfg(not(feature = "prefix-path"))] -lazy_static! { - static ref PREFIX_PATH: Option = Some(String::from("")); -} - #[cfg(feature = "prefix-path")] -lazy_static! { - static ref PREFIX_PATH: Option = match env::var("PREFIX_PATH") { - Ok(v) => Some(String::from(v)), - Err(e) => panic!("$PREFIX_PATH is not set ({})", e) - }; -} +static PREFIX_PATH: Lazy> = Lazy::new(|| match env::var("PREFIX_PATH") { + Ok(v) => Some(String::from(v)), + Err(e) => panic!("$PREFIX_PATH is not set ({})", e) +}); pub fn read_buf(buf: &[u8], pos: &mut usize) -> u8 { let byte = buf[*pos]; @@ -60,12 +53,30 @@ fn finalize_url(path: &str, query: BTreeMap) -> String { if qhash.is_some() { let mut query = QString::new(query.into_iter().collect::>()); query.add_pair(("qhash", qhash.unwrap())); - return format!("{}{}?{}", PREFIX_PATH.as_ref().unwrap().to_string(), path, query); + #[cfg(not(feature = "prefix-path"))] + { + return format!("{}?{}", path, query); + } + + + #[cfg(feature = "prefix-path")] + { + return format!("{}{}?{}", PREFIX_PATH.as_ref().unwrap().to_string(), path, query); + } } } let query = QString::new(query.into_iter().collect::>()); - format!("{}{}?{}", PREFIX_PATH.as_ref().unwrap().to_string(), path, query) + #[cfg(not(feature = "prefix-path"))] + { + format!("{}?{}", path, query) + } + + + #[cfg(feature = "prefix-path")] + { + format!("{}{}?{}", PREFIX_PATH.as_ref().unwrap().to_string(), path, query) + } } pub fn localize_url(url: &str, host: &str) -> String { From 082d1ae31c164e9125f2db9472fb6c439837a169 Mon Sep 17 00:00:00 2001 From: Andrea Spacca Date: Mon, 8 Apr 2024 06:45:39 +0900 Subject: [PATCH 5/5] fix CI --- src/main.rs | 2 -- src/utils.rs | 12 +++++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index 8ebf170..d255d45 100644 --- a/src/main.rs +++ b/src/main.rs @@ -113,8 +113,6 @@ fn is_header_allowed(header: &str) -> bool { header, "host" | "authorization" - | "origin" - | "referer" | "cookie" | "etag" | "content-length" diff --git a/src/utils.rs b/src/utils.rs index 5f733d5..3314be2 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,4 +1,3 @@ -use once_cell::sync::Lazy; use qstring::QString; use reqwest::Url; use std::borrow::Cow; @@ -6,10 +5,13 @@ use std::collections::BTreeMap; use std::env; #[cfg(feature = "prefix-path")] -static PREFIX_PATH: Lazy> = Lazy::new(|| match env::var("PREFIX_PATH") { - Ok(v) => Some(String::from(v)), - Err(e) => panic!("$PREFIX_PATH is not set ({})", e) -}); +{ + use once_cell::sync::Lazy; + static PREFIX_PATH: Lazy> = Lazy::new(|| match env::var("PREFIX_PATH") { + Ok(v) => Some(String::from(v)), + Err(e) => panic!("$PREFIX_PATH is not set ({})", e) + }); +} pub fn read_buf(buf: &[u8], pos: &mut usize) -> u8 { let byte = buf[*pos];