From 74bee220e47fb2898906afd6f375ffd520c38657 Mon Sep 17 00:00:00 2001 From: Fijxu Date: Tue, 8 Oct 2024 16:36:37 -0300 Subject: [PATCH] fixup! Feat: Experimental support for external videoplayback proxies --- src/invidious.cr | 4 ++++ src/invidious/jobs/check_external_proxy.cr | 13 +++++++++++++ src/invidious/routes/api/manifest.cr | 15 +++++++++++++-- 3 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 src/invidious/jobs/check_external_proxy.cr diff --git a/src/invidious.cr b/src/invidious.cr index 8f79bcf3..97ae8254 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -188,6 +188,10 @@ Invidious::Jobs.register Invidious::Jobs::ClearExpiredItemsJob.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 def popular_videos diff --git a/src/invidious/jobs/check_external_proxy.cr b/src/invidious/jobs/check_external_proxy.cr new file mode 100644 index 00000000..ce0b296e --- /dev/null +++ b/src/invidious/jobs/check_external_proxy.cr @@ -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 diff --git a/src/invidious/routes/api/manifest.cr b/src/invidious/routes/api/manifest.cr index 0f115ed3..dcfec909 100644 --- a/src/invidious/routes/api/manifest.cr +++ b/src/invidious/routes/api/manifest.cr @@ -1,4 +1,15 @@ 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 def self.get_dash_video_id(env) env.response.headers.add("Access-Control-Allow-Origin", "*") @@ -35,7 +46,7 @@ module Invidious::Routes::API::Manifest if local uri = URI.parse(url) - if CONFIG.external_videoplayback_proxy + if @@proxy_alive url = "#{CONFIG.external_videoplayback_proxy}#{uri.request_target}host/#{uri.host}/" else url = "#{HOST_URL}#{uri.request_target}host/#{uri.host}/" @@ -52,7 +63,7 @@ module Invidious::Routes::API::Manifest if local adaptive_fmts.each do |fmt| - if CONFIG.external_videoplayback_proxy + 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}")