Feat: Experimental support for external videoplayback proxies

This commit is contained in:
Fijxu 2024-09-27 19:09:50 -03:00
parent b1f25a69ad
commit eff8673efc
Signed by: Fijxu
GPG key ID: 32C1DDF333EDA6A4
6 changed files with 50 additions and 7 deletions

View file

@ -188,6 +188,10 @@ Invidious::Jobs.register Invidious::Jobs::ClearExpiredItemsJob.new
Invidious::Jobs.register Invidious::Jobs::InstanceListRefreshJob.new Invidious::Jobs.register Invidious::Jobs::InstanceListRefreshJob.new
if CONFIG.external_videoplayback_proxy
Invidious::Jobs.register Invidious::Jobs::CheckExternalProxy.new
end
Invidious::Jobs.start_all Invidious::Jobs.start_all
def popular_videos def popular_videos

View file

@ -172,6 +172,8 @@ class Config
# List of names of the backends # List of names of the backends
property backends : Array(String) = [] of String property backends : Array(String) = [] of String
property external_videoplayback_proxy : String?
# Materialious redirects # Materialious redirects
property materialious_domain : String? property materialious_domain : String?

View file

@ -0,0 +1,13 @@
class Invidious::Jobs::CheckExternalProxy < Invidious::Jobs::BaseJob
def initialize
end
def begin
loop do
Invidious::Routes::API::Manifest.check_external_proxy
LOGGER.info("CheckExternalProxy: Done, sleeping for 1 minute")
sleep 1.minutes
Fiber.yield
end
end
end

View file

@ -1,4 +1,15 @@
module Invidious::Routes::API::Manifest module Invidious::Routes::API::Manifest
@@proxy_alive : Bool = false
def self.check_external_proxy
begin
response = HTTP::Client.get("#{CONFIG.external_videoplayback_proxy}")
@@proxy_alive = response.status_code == 200
rescue
@@proxy_alive = false
end
end
# /api/manifest/dash/id/:id # /api/manifest/dash/id/:id
def self.get_dash_video_id(env) def self.get_dash_video_id(env)
env.response.headers.add("Access-Control-Allow-Origin", "*") env.response.headers.add("Access-Control-Allow-Origin", "*")
@ -35,7 +46,11 @@ module Invidious::Routes::API::Manifest
if local if local
uri = URI.parse(url) uri = URI.parse(url)
url = "#{HOST_URL}#{uri.request_target}host/#{uri.host}/" if @@proxy_alive
url = "#{CONFIG.external_videoplayback_proxy}#{uri.request_target}host/#{uri.host}/"
else
url = "#{HOST_URL}#{uri.request_target}host/#{uri.host}/"
end
end end
"<BaseURL>#{url}</BaseURL>" "<BaseURL>#{url}</BaseURL>"
@ -48,21 +63,24 @@ module Invidious::Routes::API::Manifest
if local if local
adaptive_fmts.each do |fmt| adaptive_fmts.each do |fmt|
fmt["url"] = JSON::Any.new("#{HOST_URL}#{URI.parse(fmt["url"].as_s).request_target}") if @@proxy_alive
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
end end
audio_streams = video.audio_streams.sort_by { |stream| {stream["bitrate"].as_i} }.reverse! 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! 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 if CONFIG.max_dash_resolution
video_streams.reject! do |z| video_streams.reject! do |z|
(z["height"].as_i > CONFIG.max_dash_resolution.not_nil!) if z["height"]? (z["height"].as_i > CONFIG.max_dash_resolution.not_nil!) if z["height"]?
end end
end end
manifest = XML.build(indent: " ", encoding: "UTF-8") do |xml| manifest = XML.build(indent: " ", encoding: "UTF-8") do |xml|
xml.element("MPD", "xmlns": "urn:mpeg:dash:schema:mpd:2011", xml.element("MPD", "xmlns": "urn:mpeg:dash:schema:mpd:2011",
"profiles": "urn:mpeg:dash:profile:full:2011", minBufferTime: "PT1.5S", type: "static", "profiles": "urn:mpeg:dash:profile:full:2011", minBufferTime: "PT1.5S", type: "static",

View file

@ -28,6 +28,12 @@ module Invidious::Routes::BeforeAll
extra_media_csp = "" extra_media_csp = ""
end 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 # Only allow the pages at /embed/* to be embedded
if env.request.resource.starts_with?("/embed") if env.request.resource.starts_with?("/embed")
frame_ancestors = "'self' file: http: https:" frame_ancestors = "'self' file: http: https:"
@ -43,7 +49,7 @@ module Invidious::Routes::BeforeAll
"style-src 'self' 'unsafe-inline'", "style-src 'self' 'unsafe-inline'",
"img-src 'self' data:", "img-src 'self' data:",
"font-src 'self' data:", "font-src 'self' data:",
"connect-src 'self'", "connect-src 'self'" + external_videoplayback_proxy,
"manifest-src 'self'", "manifest-src 'self'",
"media-src 'self' blob:" + extra_media_csp, "media-src 'self' blob:" + extra_media_csp,
"child-src 'self' blob:", "child-src 'self' blob:",

View file

@ -120,7 +120,7 @@ module Invidious::Routes::Watch
fmt_stream = video.fmt_stream fmt_stream = video.fmt_stream
adaptive_fmts = video.adaptive_fmts 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 if CONFIG.max_dash_resolution
adaptive_fmts.reject! do |z| adaptive_fmts.reject! do |z|
(z["height"].as_i > CONFIG.max_dash_resolution.not_nil!) if z["height"]? (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 video_streams = video.video_streams
audio_streams = video.audio_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 if CONFIG.max_dash_resolution
video_streams.reject! do |z| video_streams.reject! do |z|
(z["height"].as_i > CONFIG.max_dash_resolution.not_nil!) if z["height"]? (z["height"].as_i > CONFIG.max_dash_resolution.not_nil!) if z["height"]?