-
-
Notifications
You must be signed in to change notification settings - Fork 267
/
proxy.rs
40 lines (35 loc) · 1.01 KB
/
proxy.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
use actix_web::{client::Client, error, web, Error, HttpResponse, Result};
use url::Url;
use base64::decode;
pub async fn handler(web::Path(b64): web::Path<String>) -> Result<HttpResponse> {
let domains = vec![
"a.thumbs.redditmedia.com",
"b.thumbs.redditmedia.com",
"preview.redd.it",
"external-preview.redd.it",
"i.redd.it",
"v.redd.it",
];
match decode(b64) {
Ok(bytes) => {
let media = String::from_utf8(bytes).unwrap();
match Url::parse(media.as_str()) {
Ok(url) => {
let domain = url.domain().unwrap_or_default();
if domains.contains(&domain) {
Client::default()
.get(media.replace("&", "&"))
.send()
.await
.map_err(Error::from)
.map(|res| HttpResponse::build(res.status()).streaming(res))
} else {
Err(error::ErrorForbidden("Resource must be from Reddit"))
}
}
Err(_) => Err(error::ErrorBadRequest("Can't parse encoded base64 URL")),
}
}
Err(_) => Err(error::ErrorBadRequest("Can't decode base64 URL")),
}
}