HLS video playback (#182)

* HLS video playback

Signed-off-by: Adrian Lebioda <adrianlebioda@gmail.com>

* Add LibreJS compliance

* Locally host hls.js

* Notification about HLS under videos that support it

Signed-off-by: Adrian Lebioda <adrianlebioda@gmail.com>

* Use .contains() instead of .find() == None

* Make list of preferences constant

* Change headers_keys from Vector into Array

* Fix incorrect detecting of # in paths

* Remove trailing-slash-appending if statement

* Change HLS notification styling

Co-authored-by: spikecodes <19519553+spikecodes@users.noreply.github.com>
This commit is contained in:
Adrian Lebioda 2021-05-10 01:25:52 +00:00 committed by GitHub
parent dc9fbc1a05
commit 928907086c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 265 additions and 28 deletions

View file

@ -13,10 +13,10 @@ pub async fn proxy(req: Request<Body>, format: &str) -> Result<Response<Body>, S
url = url.replace(&format!("{{{}}}", name), value);
}
stream(&url).await
stream(&url, &req).await
}
async fn stream(url: &str) -> Result<Response<Body>, String> {
async fn stream(url: &str, req: &Request<Body>) -> Result<Response<Body>, String> {
// First parameter is target URL (mandatory).
let url = Uri::from_str(url).map_err(|_| "Couldn't parse URL".to_string())?;
@ -26,8 +26,20 @@ async fn stream(url: &str) -> Result<Response<Body>, String> {
// Build the hyper client from the HTTPS connector.
let client: client::Client<_, hyper::Body> = client::Client::builder().build(https);
let mut builder = Request::get(url);
// Copy useful headers from original request
let headers = req.headers();
for &key in &["Range", "If-Modified-Since", "Cache-Control"] {
if let Some(value) = headers.get(key) {
builder = builder.header(key, value);
}
}
let stream_request = builder.body(Body::default()).expect("stream");
client
.get(url)
.request(stream_request)
.await
.map(|mut res| {
let mut rm = |key: &str| res.headers_mut().remove(key);
@ -40,6 +52,8 @@ async fn stream(url: &str) -> Result<Response<Body>, String> {
rm("x-cdn-client-region");
rm("x-cdn-name");
rm("x-cdn-server-region");
rm("x-reddit-cdn");
rm("x-reddit-video-features");
res
})

View file

@ -139,7 +139,7 @@ async fn main() {
"Referrer-Policy" => "no-referrer",
"X-Content-Type-Options" => "nosniff",
"X-Frame-Options" => "DENY",
"Content-Security-Policy" => "default-src 'none'; manifest-src 'self'; media-src 'self'; style-src 'self' 'unsafe-inline'; base-uri 'none'; img-src 'self' data:; form-action 'self'; frame-ancestors 'none';"
"Content-Security-Policy" => "default-src 'none'; script-src 'self' blob:; manifest-src 'self'; media-src 'self' data: blob: about:; style-src 'self' 'unsafe-inline'; base-uri 'none'; img-src 'self' data:; form-action 'self'; frame-ancestors 'none'; connect-src 'self'; worker-src blob:;"
};
if let Some(expire_time) = hsts {
@ -158,9 +158,16 @@ async fn main() {
app.at("/logo.png").get(|_| pwa_logo().boxed());
app.at("/touch-icon-iphone.png").get(|_| iphone_logo().boxed());
app.at("/apple-touch-icon.png").get(|_| iphone_logo().boxed());
app
.at("/playHLSVideo.js")
.get(|_| resource(include_str!("../static/playHLSVideo.js"), "text/javascript", false).boxed());
app
.at("/hls.min.js")
.get(|_| resource(include_str!("../static/hls.min.js"), "text/javascript", false).boxed());
// Proxy media through Libreddit
app.at("/vid/:id/:size").get(|r| proxy(r, "https://v.redd.it/{id}/DASH_{size}").boxed());
app.at("/hls/:id/*path").get(|r| proxy(r, "https://v.redd.it/{id}/{path}").boxed());
app.at("/img/:id").get(|r| proxy(r, "https://i.redd.it/{id}").boxed());
app.at("/thumb/:point/:id").get(|r| proxy(r, "https://{point}.thumbs.redditmedia.com/{id}").boxed());
app.at("/emoji/:id/:name").get(|r| proxy(r, "https://emoji.redditmedia.com/{id}/{name}").boxed());
@ -184,6 +191,7 @@ async fn main() {
// Configure settings
app.at("/settings").get(|r| settings::get(r).boxed()).post(|r| settings::set(r).boxed());
app.at("/settings/restore").get(|r| settings::restore(r).boxed());
app.at("/settings/update").get(|r| settings::update(r).boxed());
// Subreddit services
app.at("/r/:sub").get(|r| subreddit::community(r).boxed());

View file

@ -106,6 +106,7 @@ async fn parse_post(json: &serde_json::Value) -> Post {
media,
thumbnail: Media {
url: format_url(val(post, "thumbnail").as_str()),
alt_url: String::new(),
width: post["data"]["thumbnail_width"].as_i64().unwrap_or_default(),
height: post["data"]["thumbnail_height"].as_i64().unwrap_or_default(),
poster: "".to_string(),

View file

@ -31,6 +31,7 @@ struct SearchTemplate {
sub: String,
params: SearchParams,
prefs: Preferences,
url: String,
}
// SERVICES
@ -56,6 +57,8 @@ pub async fn find(req: Request<Body>) -> Result<Response<Body>, String> {
Vec::new()
};
let url = String::from(req.uri().path_and_query().map_or("", |val| val.as_str()));
match Post::fetch(&path, String::new()).await {
Ok((posts, after)) => template(SearchTemplate {
posts,
@ -70,6 +73,7 @@ pub async fn find(req: Request<Body>) -> Result<Response<Body>, String> {
restrict_sr: param(&path, "restrict_sr"),
},
prefs: Preferences::new(req),
url,
}),
Err(msg) => error(req, msg).await,
}

View file

@ -16,6 +16,21 @@ struct SettingsTemplate {
prefs: Preferences,
}
// CONSTANTS
const PREFS: [&str; 10] = [
"theme",
"front_page",
"layout",
"wide",
"comment_sort",
"post_sort",
"show_nsfw",
"use_hls",
"hide_hls_notification",
"subscriptions",
];
// FUNCTIONS
// Retrieve cookies from request "Cookie" header
@ -50,9 +65,7 @@ pub async fn set(req: Request<Body>) -> Result<Response<Body>, String> {
let mut res = redirect("/settings".to_string());
let names = vec!["theme", "front_page", "layout", "wide", "comment_sort", "post_sort", "show_nsfw"];
for name in names {
for &name in &PREFS {
match form.get(name) {
Some(value) => res.insert_cookie(
Cookie::build(name.to_owned(), value.to_owned())
@ -68,8 +81,7 @@ pub async fn set(req: Request<Body>) -> Result<Response<Body>, String> {
Ok(res)
}
// Set cookies using response "Set-Cookie" header
pub async fn restore(req: Request<Body>) -> Result<Response<Body>, String> {
fn set_cookies_method(req: Request<Body>, remove_cookies: bool) -> Response<Body> {
// Split the body into parts
let (parts, _) = req.into_parts();
@ -85,16 +97,14 @@ pub async fn restore(req: Request<Body>) -> Result<Response<Body>, String> {
let form = url::form_urlencoded::parse(query).collect::<HashMap<_, _>>();
let names = vec!["theme", "front_page", "layout", "wide", "comment_sort", "post_sort", "show_nsfw", "subscriptions"];
let path = match form.get("redirect") {
Some(value) => format!("/{}/", value),
Some(value) => format!("/{}", value.replace("%26", "&").replace("%23", "#")),
None => "/".to_string(),
};
let mut res = redirect(path);
for name in names {
for &name in &PREFS {
match form.get(name) {
Some(value) => res.insert_cookie(
Cookie::build(name.to_owned(), value.to_owned())
@ -103,9 +113,22 @@ pub async fn restore(req: Request<Body>) -> Result<Response<Body>, String> {
.expires(OffsetDateTime::now_utc() + Duration::weeks(52))
.finish(),
),
None => res.remove_cookie(name.to_string()),
None => {
if remove_cookies {
res.remove_cookie(name.to_string())
}
}
};
}
Ok(res)
res
}
// Set cookies using response "Set-Cookie" header
pub async fn restore(req: Request<Body>) -> Result<Response<Body>, String> {
Ok(set_cookies_method(req, true))
}
pub async fn update(req: Request<Body>) -> Result<Response<Body>, String> {
Ok(set_cookies_method(req, false))
}

View file

@ -16,6 +16,7 @@ struct SubredditTemplate {
sort: (String, String),
ends: (String, String),
prefs: Preferences,
url: String,
}
#[derive(Template)]
@ -79,12 +80,15 @@ pub async fn community(req: Request<Body>) -> Result<Response<Body>, String> {
Subreddit::default()
};
let url = String::from(req.uri().path_and_query().map_or("", |val| val.as_str()));
template(SubredditTemplate {
sub,
posts,
sort: (sort, param(&path, "t")),
ends: (param(&path, "after"), after),
prefs: Preferences::new(req),
url,
})
}
Err(msg) => match msg.as_str() {

View file

@ -16,6 +16,7 @@ struct UserTemplate {
sort: (String, String),
ends: (String, String),
prefs: Preferences,
url: String,
}
// FUNCTIONS
@ -33,6 +34,7 @@ pub async fn profile(req: Request<Body>) -> Result<Response<Body>, String> {
// Request user posts/comments from Reddit
let posts = Post::fetch(&path, "Comment".to_string()).await;
let url = String::from(req.uri().path_and_query().map_or("", |val| val.as_str()));
match posts {
Ok((posts, after)) => {
@ -45,6 +47,7 @@ pub async fn profile(req: Request<Body>) -> Result<Response<Body>, String> {
sort: (sort, param(&path, "t")),
ends: (param(&path, "after"), after),
prefs: Preferences::new(req),
url,
})
}
// If there is an error show error page

View file

@ -75,6 +75,7 @@ pub struct Flags {
pub struct Media {
pub url: String,
pub alt_url: String,
pub width: i64,
pub height: i64,
pub poster: String,
@ -85,12 +86,28 @@ impl Media {
let mut gallery = Vec::new();
// If post is a video, return the video
let (post_type, url_val) = if data["preview"]["reddit_video_preview"]["fallback_url"].is_string() {
let (post_type, url_val, alt_url_val) = if data["preview"]["reddit_video_preview"]["fallback_url"].is_string() {
// Return reddit video
("video", &data["preview"]["reddit_video_preview"]["fallback_url"])
(
if data["preview"]["reddit_video_preview"]["is_gif"].as_bool().unwrap_or(false) {
"gif"
} else {
"video"
},
&data["preview"]["reddit_video_preview"]["fallback_url"],
Some(&data["preview"]["reddit_video_preview"]["hls_url"]),
)
} else if data["secure_media"]["reddit_video"]["fallback_url"].is_string() {
// Return reddit video
("video", &data["secure_media"]["reddit_video"]["fallback_url"])
(
if data["preview"]["reddit_video_preview"]["is_gif"].as_bool().unwrap_or(false) {
"gif"
} else {
"video"
},
&data["secure_media"]["reddit_video"]["fallback_url"],
Some(&data["secure_media"]["reddit_video"]["hls_url"]),
)
} else if data["post_hint"].as_str().unwrap_or("") == "image" {
// Handle images, whether GIFs or pics
let preview = &data["preview"]["images"][0];
@ -98,26 +115,26 @@ impl Media {
if mp4.is_object() {
// Return the mp4 if the media is a gif
("gif", &mp4["source"]["url"])
("gif", &mp4["source"]["url"], None)
} else {
// Return the picture if the media is an image
if data["domain"] == "i.redd.it" {
("image", &data["url"])
("image", &data["url"], None)
} else {
("image", &preview["source"]["url"])
("image", &preview["source"]["url"], None)
}
}
} else if data["is_self"].as_bool().unwrap_or_default() {
// If type is self, return permalink
("self", &data["permalink"])
("self", &data["permalink"], None)
} else if data["is_gallery"].as_bool().unwrap_or_default() {
// If this post contains a gallery of images
gallery = GalleryMedia::parse(&data["gallery_data"]["items"], &data["media_metadata"]);
("gallery", &data["url"])
("gallery", &data["url"], None)
} else {
// If type can't be determined, return url
("link", &data["url"])
("link", &data["url"], None)
};
let source = &data["preview"]["images"][0]["source"];
@ -128,10 +145,13 @@ impl Media {
format_url(url_val.as_str().unwrap_or_default())
};
let alt_url = alt_url_val.map_or(String::new(), |val| format_url(val.as_str().unwrap_or_default()));
(
post_type.to_string(),
Self {
url,
alt_url,
width: source["width"].as_i64().unwrap_or_default(),
height: source["height"].as_i64().unwrap_or_default(),
poster: format_url(source["url"].as_str().unwrap_or_default()),
@ -259,6 +279,7 @@ impl Post {
post_type,
thumbnail: Media {
url: format_url(val(post, "thumbnail").as_str()),
alt_url: String::new(),
width: data["thumbnail_width"].as_i64().unwrap_or_default(),
height: data["thumbnail_height"].as_i64().unwrap_or_default(),
poster: "".to_string(),
@ -365,6 +386,8 @@ pub struct Preferences {
pub layout: String,
pub wide: String,
pub show_nsfw: String,
pub hide_hls_notification: String,
pub use_hls: String,
pub comment_sort: String,
pub post_sort: String,
pub subscriptions: Vec<String>,
@ -379,6 +402,8 @@ impl Preferences {
layout: cookie(&req, "layout"),
wide: cookie(&req, "wide"),
show_nsfw: cookie(&req, "show_nsfw"),
use_hls: cookie(&req, "use_hls"),
hide_hls_notification: cookie(&req, "hide_hls_notification"),
comment_sort: cookie(&req, "comment_sort"),
post_sort: cookie(&req, "post_sort"),
subscriptions: cookie(&req, "subscriptions").split('+').map(String::from).filter(|s| !s.is_empty()).collect(),
@ -439,8 +464,32 @@ pub fn format_url(url: &str) -> String {
.unwrap_or_default()
};
macro_rules! chain {
() => {
{
String::new()
}
};
( $first_fn:expr, $($other_fns:expr), *) => {
{
let result = $first_fn;
if result.is_empty() {
chain!($($other_fns,)*)
}
else
{
result
}
}
};
}
match domain {
"v.redd.it" => capture(r"https://v\.redd\.it/(.*)/DASH_([0-9]{2,4}(\.mp4|$))", "/vid/", 2),
"v.redd.it" => chain!(
capture(r"https://v\.redd\.it/(.*)/DASH_([0-9]{2,4}(\.mp4|$))", "/vid/", 2),
capture(r"https://v\.redd\.it/(.+)/(HLSPlaylist\.m3u8.*)$", "/hls/", 2)
),
"i.redd.it" => capture(r"https://i\.redd\.it/(.*)", "/img/", 1),
"a.thumbs.redditmedia.com" => capture(r"https://a\.thumbs\.redditmedia\.com/(.*)", "/thumb/a/", 1),
"b.thumbs.redditmedia.com" => capture(r"https://b\.thumbs\.redditmedia\.com/(.*)", "/thumb/b/", 1),