From 29874cfc098a87612e552590a1f9d96c927e88b8 Mon Sep 17 00:00:00 2001 From: Kavin <20838718+FireMasterK@users.noreply.github.com> Date: Fri, 18 Nov 2022 19:29:55 +0000 Subject: [PATCH] Rewrite DASH manifests. --- src/main.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 015c2dd..be3417b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -30,6 +30,7 @@ async fn main() -> std::io::Result<()> { lazy_static!( static ref RE_DOMAIN: Regex = Regex::new(r"^(?:[a-z\d\.-]*\.)?((?:[a-z\d-]*)\.(?:[a-z\d-]*))$").unwrap(); static ref RE_MANIFEST: Regex = Regex::new("(?m)URI=\"([^\"]+)\"").unwrap(); + static ref RE_DASH_MANIFEST: Regex = Regex::new("BaseURL>(https://[^<]+) Result> { return Err("Domain not allowed".into()); } - let mut url = Url::parse(&*format!("https://{}{}", host, req.path()))?; + let mut url = Url::parse(&format!("https://{}{}", host, req.path()))?; url.set_query(Some(req.query_string())); let mut request = Request::new( @@ -190,6 +191,18 @@ async fn index(req: HttpRequest) -> Result> { return Ok(response.body(modified)); } + if content_type == "video/vnd.mpeg.dash.mpd" || content_type == "application/dash+xml" { + let mut resp_str = resp.text().await.unwrap(); + let clone_resp = resp_str.clone(); + let captures = RE_DASH_MANIFEST.captures_iter(&clone_resp); + for capture in captures { + let url = capture.get(1).unwrap().as_str(); + let new_url = localize_url(url, host); + resp_str = resp_str.replace(url, new_url.as_str()) + .clone(); + } + return Ok(response.body(resp_str)); + } } // Stream response