Rewrite DASH manifests.

This commit is contained in:
Kavin 2022-11-18 19:29:55 +00:00
parent 6d6a1534e3
commit 29874cfc09
No known key found for this signature in database
GPG key ID: 49451E4482CC5BCD

View file

@ -30,6 +30,7 @@ async fn main() -> std::io::Result<()> {
lazy_static!( lazy_static!(
static ref RE_DOMAIN: Regex = Regex::new(r"^(?:[a-z\d\.-]*\.)?((?:[a-z\d-]*)\.(?:[a-z\d-]*))$").unwrap(); 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_MANIFEST: Regex = Regex::new("(?m)URI=\"([^\"]+)\"").unwrap();
static ref RE_DASH_MANIFEST: Regex = Regex::new("BaseURL>(https://[^<]+)</BaseURL").unwrap();
); );
lazy_static!( lazy_static!(
@ -122,7 +123,7 @@ async fn index(req: HttpRequest) -> Result<HttpResponse, Box<dyn Error>> {
return Err("Domain not allowed".into()); 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())); url.set_query(Some(req.query_string()));
let mut request = Request::new( let mut request = Request::new(
@ -190,6 +191,18 @@ async fn index(req: HttpRequest) -> Result<HttpResponse, Box<dyn Error>> {
return Ok(response.body(modified)); 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 // Stream response