From e1a1bf0f79eb6c541ed9b67a7db8ad5e50abe8d4 Mon Sep 17 00:00:00 2001 From: Fijxu Date: Fri, 27 Sep 2024 19:09:50 -0300 Subject: [PATCH] Feat: Experimental support for external videoplayback proxies --- src/invidious/config.cr | 2 ++ src/invidious/routes/api/manifest.cr | 15 +++++++++++---- src/invidious/routes/before_all.cr | 8 +++++++- src/invidious/routes/watch.cr | 4 ++-- 4 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/invidious/config.cr b/src/invidious/config.cr index d9c41604..4c0b2686 100644 --- a/src/invidious/config.cr +++ b/src/invidious/config.cr @@ -172,6 +172,8 @@ class Config # List of names of the backends property backends : Array(String) = [] of String + property external_videoplayback_proxy : String? + # Materialious redirects property materialious_domain : String? diff --git a/src/invidious/routes/api/manifest.cr b/src/invidious/routes/api/manifest.cr index 8ba735bb..0f115ed3 100644 --- a/src/invidious/routes/api/manifest.cr +++ b/src/invidious/routes/api/manifest.cr @@ -35,7 +35,11 @@ module Invidious::Routes::API::Manifest if local uri = URI.parse(url) - url = "#{HOST_URL}#{uri.request_target}host/#{uri.host}/" + if CONFIG.external_videoplayback_proxy + url = "#{CONFIG.external_videoplayback_proxy}#{uri.request_target}host/#{uri.host}/" + else + url = "#{HOST_URL}#{uri.request_target}host/#{uri.host}/" + end end "#{url}" @@ -48,21 +52,24 @@ module Invidious::Routes::API::Manifest if local adaptive_fmts.each do |fmt| - fmt["url"] = JSON::Any.new("#{HOST_URL}#{URI.parse(fmt["url"].as_s).request_target}") + if CONFIG.external_videoplayback_proxy + fmt["url"] = JSON::Any.new("#{CONFIG.external_videoplayback_proxy}#{URI.parse(fmt["url"].as_s).request_target}") + else + fmt["url"] = JSON::Any.new("#{HOST_URL}#{URI.parse(fmt["url"].as_s).request_target}") + end end end audio_streams = video.audio_streams.sort_by { |stream| {stream["bitrate"].as_i} }.reverse! video_streams = video.video_streams.sort_by { |stream| {stream["width"].as_i, stream["fps"].as_i} }.reverse! - # Removes all the resolutions with a height higher than CONFIG.max_dash_resolution + # Removes all the resolutions with a height higher than CONFIG.max_dash_resolution if CONFIG.max_dash_resolution video_streams.reject! do |z| (z["height"].as_i > CONFIG.max_dash_resolution.not_nil!) if z["height"]? end end - manifest = XML.build(indent: " ", encoding: "UTF-8") do |xml| xml.element("MPD", "xmlns": "urn:mpeg:dash:schema:mpd:2011", "profiles": "urn:mpeg:dash:profile:full:2011", minBufferTime: "PT1.5S", type: "static", diff --git a/src/invidious/routes/before_all.cr b/src/invidious/routes/before_all.cr index 5695dee9..54c30fc5 100644 --- a/src/invidious/routes/before_all.cr +++ b/src/invidious/routes/before_all.cr @@ -28,6 +28,12 @@ module Invidious::Routes::BeforeAll extra_media_csp = "" end + if CONFIG.external_videoplayback_proxy + external_videoplayback_proxy = " #{CONFIG.external_videoplayback_proxy}" + else + external_videoplayback_proxy = "" + end + # Only allow the pages at /embed/* to be embedded if env.request.resource.starts_with?("/embed") frame_ancestors = "'self' file: http: https:" @@ -43,7 +49,7 @@ module Invidious::Routes::BeforeAll "style-src 'self' 'unsafe-inline'", "img-src 'self' data:", "font-src 'self' data:", - "connect-src 'self'", + "connect-src 'self'" + external_videoplayback_proxy, "manifest-src 'self'", "media-src 'self' blob:" + extra_media_csp, "child-src 'self' blob:", diff --git a/src/invidious/routes/watch.cr b/src/invidious/routes/watch.cr index a5ff1497..612a689f 100644 --- a/src/invidious/routes/watch.cr +++ b/src/invidious/routes/watch.cr @@ -120,7 +120,7 @@ module Invidious::Routes::Watch fmt_stream = video.fmt_stream adaptive_fmts = video.adaptive_fmts - # Removes all the resolutions with a height higher than CONFIG.max_dash_resolution + # Removes all the resolutions with a height higher than CONFIG.max_dash_resolution if CONFIG.max_dash_resolution adaptive_fmts.reject! do |z| (z["height"].as_i > CONFIG.max_dash_resolution.not_nil!) if z["height"]? @@ -135,7 +135,7 @@ module Invidious::Routes::Watch video_streams = video.video_streams audio_streams = video.audio_streams - # Removes all the resolutions with a height higher than CONFIG.max_dash_resolution + # Removes all the resolutions with a height higher than CONFIG.max_dash_resolution if CONFIG.max_dash_resolution video_streams.reject! do |z| (z["height"].as_i > CONFIG.max_dash_resolution.not_nil!) if z["height"]?