Add parameter to disable rewriting.
This commit is contained in:
parent
82d5b16d85
commit
71cb04d028
1 changed files with 58 additions and 39 deletions
97
src/main.rs
97
src/main.rs
|
@ -98,6 +98,14 @@ async fn index(req: HttpRequest) -> Result<HttpResponse, Box<dyn Error>> {
|
||||||
|
|
||||||
let res = query.get("host");
|
let res = query.get("host");
|
||||||
|
|
||||||
|
let rewrite = {
|
||||||
|
if let Some(rewrite) = query.get("rewrite") {
|
||||||
|
rewrite == "true"
|
||||||
|
} else {
|
||||||
|
true
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
if res.is_none() {
|
if res.is_none() {
|
||||||
return Err("No host provided".into());
|
return Err("No host provided".into());
|
||||||
}
|
}
|
||||||
|
@ -123,8 +131,17 @@ async fn index(req: HttpRequest) -> Result<HttpResponse, Box<dyn Error>> {
|
||||||
return Err("Domain not allowed".into());
|
return Err("Domain not allowed".into());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let qs = {
|
||||||
|
let qs = query.clone();
|
||||||
|
let collected = qs.into_pairs()
|
||||||
|
.into_iter()
|
||||||
|
.filter(|(key, _)| key != "host" && key != "rewrite")
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
QString::new(collected)
|
||||||
|
};
|
||||||
|
|
||||||
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(qs.to_string().as_str()));
|
||||||
|
|
||||||
let mut request = Request::new(
|
let mut request = Request::new(
|
||||||
req.method().clone(),
|
req.method().clone(),
|
||||||
|
@ -157,51 +174,53 @@ async fn index(req: HttpRequest) -> Result<HttpResponse, Box<dyn Error>> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(content_type) = resp.headers().get("content-type") {
|
if rewrite {
|
||||||
if content_type == "image/jpeg" {
|
if let Some(content_type) = resp.headers().get("content-type") {
|
||||||
let resp_bytes = resp.bytes().await.unwrap();
|
if content_type == "image/jpeg" {
|
||||||
|
let resp_bytes = resp.bytes().await.unwrap();
|
||||||
|
|
||||||
let image = image::load_from_memory(&resp_bytes).unwrap();
|
let image = image::load_from_memory(&resp_bytes).unwrap();
|
||||||
|
|
||||||
let encoder = webp::Encoder::from_image(&image).unwrap();
|
let encoder = webp::Encoder::from_image(&image).unwrap();
|
||||||
|
|
||||||
let encoded = encoder.encode(85f32);
|
let encoded = encoder.encode(85f32);
|
||||||
let bytes = encoded.as_bytes().to_vec();
|
let bytes = encoded.as_bytes().to_vec();
|
||||||
|
|
||||||
if bytes.len() < resp_bytes.len() {
|
if bytes.len() < resp_bytes.len() {
|
||||||
response.content_type("image/webp");
|
response.content_type("image/webp");
|
||||||
return Ok(response.body(bytes));
|
return Ok(response.body(bytes));
|
||||||
}
|
|
||||||
|
|
||||||
return Ok(response.body(resp_bytes));
|
|
||||||
}
|
|
||||||
if content_type == "application/x-mpegurl" || content_type == "application/vnd.apple.mpegurl" {
|
|
||||||
let resp_str = resp.text().await.unwrap();
|
|
||||||
|
|
||||||
let modified = resp_str.lines().map(|line| {
|
|
||||||
let captures = RE_MANIFEST.captures(line);
|
|
||||||
if let Some(captures) = captures {
|
|
||||||
let url = captures.get(1).unwrap().as_str();
|
|
||||||
if url.starts_with("https://") {
|
|
||||||
return line.replace(url, localize_url(url, host).as_str());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
localize_url(line, host)
|
|
||||||
}).collect::<Vec<String>>().join("\n");
|
|
||||||
|
|
||||||
return Ok(response.body(modified));
|
return Ok(response.body(resp_bytes));
|
||||||
}
|
}
|
||||||
if content_type == "video/vnd.mpeg.dash.mpd" || content_type == "application/dash+xml" {
|
if content_type == "application/x-mpegurl" || content_type == "application/vnd.apple.mpegurl" {
|
||||||
let mut resp_str = resp.text().await.unwrap();
|
let resp_str = resp.text().await.unwrap();
|
||||||
let clone_resp = resp_str.clone();
|
|
||||||
let captures = RE_DASH_MANIFEST.captures_iter(&clone_resp);
|
let modified = resp_str.lines().map(|line| {
|
||||||
for capture in captures {
|
let captures = RE_MANIFEST.captures(line);
|
||||||
let url = capture.get(1).unwrap().as_str();
|
if let Some(captures) = captures {
|
||||||
let new_url = localize_url(url, host);
|
let url = captures.get(1).unwrap().as_str();
|
||||||
resp_str = resp_str.replace(url, new_url.as_str())
|
if url.starts_with("https://") {
|
||||||
.clone();
|
return line.replace(url, localize_url(url, host).as_str());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
localize_url(line, host)
|
||||||
|
}).collect::<Vec<String>>().join("\n");
|
||||||
|
|
||||||
|
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));
|
||||||
}
|
}
|
||||||
return Ok(response.body(resp_str));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue