diff --git a/src/invidious.cr b/src/invidious.cr index 4043265e..a2d8d626 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -223,6 +223,10 @@ else LOGGER.info("jobs: Disabling RefreshSessionTokens job. Invidious will use the tokens that are on the configuration file") end +if CONFIG.invidious_companion.present? + Invidious::Jobs.register Invidious::Jobs::CheckBackend.new +end + Invidious::Jobs.start_all def popular_videos diff --git a/src/invidious/helpers/backend_info.cr b/src/invidious/helpers/backend_info.cr new file mode 100644 index 00000000..720dbc3f --- /dev/null +++ b/src/invidious/helpers/backend_info.cr @@ -0,0 +1,63 @@ +module BackendInfo + extend self + @@status : Int32 = 0 + @@exvpp_url : String = "" + + def self.check_backends + check_videoplayback_proxy() + check_companion() + end + + def check_companion + begin + response = HTTP::Client.get "#{CONFIG.invidious_companion.sample.private_url}/healthz" + if response.status_code == 200 + @@status = 1 + check_videoplayback_proxy() + else + @@status = 0 + end + rescue + @@status = 0 + end + end + + def check_videoplayback_proxy + begin + info = HTTP::Client.get "#{CONFIG.invidious_companion.sample.private_url}/info" + exvpp = JSON.parse(info.body)["external_videoplayback_proxy"]?.try &.to_s + if exvpp.nil? || exvpp.empty? + @@status = 2 + return + else + begin + exvpp_health = HTTP::Client.get "#{exvpp}/health" + if exvpp_health.status_code == 200 + @@status = 2 + return + end + rescue + @@status = 1 + end + end + rescue + end + end + + def get_videoplayback_proxy + begin + response = HTTP::Client.get "#{CONFIG.invidious_companion.sample.private_url}/info" + exvpp_url = JSON.parse(response.body)["external_videoplayback_proxy"].to_s + @@exvpp_url = exvpp_url + rescue + end + end + + def get_status + return @@status + end + + def get_exvpp + return @@exvpp_url + end +end diff --git a/src/invidious/jobs/cbackend_checker.cr b/src/invidious/jobs/cbackend_checker.cr new file mode 100644 index 00000000..be8e1c7c --- /dev/null +++ b/src/invidious/jobs/cbackend_checker.cr @@ -0,0 +1,14 @@ +class Invidious::Jobs::CheckBackend < Invidious::Jobs::BaseJob + def initialize + end + + def begin + loop do + # BackendInfo.check_backends + BackendInfo.get_videoplayback_proxy + LOGGER.info("Backend Checker: Done, sleeping for 120 seconds") + sleep 120.seconds + Fiber.yield + end + end +end diff --git a/src/invidious/routes/before_all.cr b/src/invidious/routes/before_all.cr index 8bfde738..e49bb300 100644 --- a/src/invidious/routes/before_all.cr +++ b/src/invidious/routes/before_all.cr @@ -26,6 +26,11 @@ module Invidious::Routes::BeforeAll if CONFIG.invidious_companion.present? extra_media_csp = " #{CONFIG.invidious_companion.sample.public_url}" extra_connect_csp = " #{CONFIG.invidious_companion.sample.public_url}" + exvpp_url = BackendInfo.get_exvpp + if !exvpp_url.empty? + extra_media_csp += " #{exvpp_url}" + extra_connect_csp += " #{exvpp_url}" + end end if !CONFIG.external_videoplayback_proxy.empty?