From e8e7a01dfb581710d0f6874d62039fc21b3083e2 Mon Sep 17 00:00:00 2001 From: Fijxu Date: Fri, 6 Jun 2025 11:58:57 -0400 Subject: [PATCH] chore+fix(backends): Remove external videoplayback proxy support, fix automatic backend switching on the last backend, sort working ends, skip companion cookie setting on images, thumbnails, etc. external videoplayback proxy support was unused and is not really useful now due to youtube pinning the IP of where a video is being requested to that IP adresss, so requesting the video from one companion and then offloading the video traffic to another server with another IP is not going to work. --- src/invidious/helpers/backend_info.cr | 80 ++++++-------------------- src/invidious/routes/before_all.cr | 81 +++++++++++++++------------ src/invidious/views/template.ecr | 10 ++-- 3 files changed, 67 insertions(+), 104 deletions(-) diff --git a/src/invidious/helpers/backend_info.cr b/src/invidious/helpers/backend_info.cr index 4bb4899f..35d700f4 100644 --- a/src/invidious/helpers/backend_info.cr +++ b/src/invidious/helpers/backend_info.cr @@ -1,18 +1,17 @@ module BackendInfo extend self - @@exvpp_url : Array(String) = Array.new(CONFIG.invidious_companion.size, "") - @@status : Array(Int32) = Array.new(CONFIG.invidious_companion.size, 0) + + enum Status + Dead = 0 + Working = 1 + end + + @@status : Array(Int32) = Array.new(CONFIG.invidious_companion.size, Status::Dead.to_i) @@csp : Array(String) = Array.new(CONFIG.invidious_companion.size, "") @@working_ends : Array(Int32) = Array(Int32).new(0) @@csp_mutex : Mutex = Mutex.new @@check_mutex : Mutex = Mutex.new - enum BackendStatus - Dead - Problems - Working - end - def check_backends check_companion() LOGGER.debug("Invidious companion: New working_ends \"#{@@working_ends}\"") @@ -25,6 +24,7 @@ module BackendInfo channels = Channel(Nil).new(comp_size) updated_ends = Array(Int32).new(0) updated_status = Array(Int32).new(CONFIG.invidious_companion.size, 0) + LOGGER.debug("Invidious companion: comp_size \"#{comp_size}\"") CONFIG.invidious_companion.each_with_index do |companion, index| spawn do @@ -33,19 +33,22 @@ module BackendInfo client.connect_timeout = 10.seconds response = client.get("/healthz") if response.status_code == 200 - check_videoplayback_proxy(companion, index, updated_status, updated_ends) - generate_csp([companion.public_url.to_s, companion.i2p_public_url.to_s], @@exvpp_url[index], index) + @@check_mutex.synchronize do + updated_status[index] = Status::Working.to_i + updated_ends.push(index) + end + generate_csp([companion.public_url.to_s, companion.i2p_public_url.to_s], index) else @@check_mutex.synchronize do - updated_status[index] = BackendStatus::Dead.to_i + updated_status[index] = Status::Dead.to_i end end rescue @@check_mutex.synchronize do - updated_status[index] = BackendStatus::Dead.to_i + updated_status[index] = Status::Dead.to_i end ensure - LOGGER.debug("Invidious companion: Done Index: \"#{index}\"") + LOGGER.trace("Invidious companion: Done Index: \"#{index}\"") channels.send(nil) end end @@ -53,59 +56,16 @@ module BackendInfo # Wait until we receive a signal from them all LOGGER.debug("Invidious companion: Updating working_ends") comp_size.times { channels.receive } - @@working_ends = updated_ends + @@working_ends = updated_ends.sort! @@status = updated_status end - private def check_videoplayback_proxy(companion : Config::CompanionConfig, index : Int32, updated_status : Array(Int32), updated_ends : Array(Int32)) - begin - info = HTTP::Client.get "#{companion.private_url}/info" - exvpp_url = JSON.parse(info.body)["external_videoplayback_proxy"]?.try &.to_s - rescue JSON::ParseException - @@check_mutex.synchronize do - updated_status[index] = BackendStatus::Working.to_i - updated_ends.push(index) - end - return - end - - exvpp_url = "" if exvpp_url.nil? - @@exvpp_url[index] = exvpp_url - if exvpp_url.empty? - @@check_mutex.synchronize do - updated_status[index] = BackendStatus::Working.to_i - updated_ends.push(index) - end - return - else - begin - exvpp_health = HTTP::Client.get "#{exvpp_url}/health" - if exvpp_health.status_code == 200 - @@check_mutex.synchronize do - updated_status[index] = BackendStatus::Working.to_i - updated_ends.push(index) - end - return exvpp_url - else - @@check_mutex.synchronize do - updated_status[index] = BackendStatus::Problems.to_i - end - end - rescue - @@check_mutex.synchronize do - updated_status[index] = BackendStatus::Problems.to_i - end - end - end - end - - private def generate_csp(companion_url : Array(String), exvpp_url : String? = nil, index : Int32? = nil) + private def generate_csp(companion_url : Array(String), index : Int32? = nil) @@csp_mutex.synchronize do @@csp[index] = "" companion_url.each do |url| @@csp[index] += " #{url}" end - @@csp[index] += " #{exvpp_url}" end end @@ -119,10 +79,6 @@ module BackendInfo return @@working_ends end - def get_exvpp - return @@exvpp_url - end - def get_csp(index : Int32) # A little mutex to prevent sending a partial CSP header # Not sure if this is necessary. But if the @@csp[index] is being assigned diff --git a/src/invidious/routes/before_all.cr b/src/invidious/routes/before_all.cr index 1cc7b04a..474917c7 100644 --- a/src/invidious/routes/before_all.cr +++ b/src/invidious/routes/before_all.cr @@ -32,50 +32,59 @@ module Invidious::Routes::BeforeAll extra_connect_csp = "" if CONFIG.invidious_companion.present? - current_companion_d = host.split(":")[0].split(".")[0] + if !{ + "/sb/", + "/vi/", + "/s_p/", + "/yts/", + "/ggpht/", + }.any? { |r| env.request.resource.starts_with? r } + current_companion_d = host.split(":")[0].split(".")[0] - if index = COMPANION_PREFIXES.index(current_companion_d) - env.set "using_domain", true - env.set "current_companion", index - env.set "companion_public_url", CONFIG.invidious_companion[index].public_url.to_s - else - if !env.request.cookies[CONFIG.server_id_cookie_name]? - env.response.cookies[CONFIG.server_id_cookie_name] = Invidious::User::Cookies.server_id(host) - end + if index = COMPANION_PREFIXES.index(current_companion_d) + env.set "using_domain", true + env.set "current_companion", index + env.set "companion_public_url", CONFIG.invidious_companion[index].public_url.to_s + else + if !env.request.cookies[CONFIG.server_id_cookie_name]? + env.response.cookies[CONFIG.server_id_cookie_name] = Invidious::User::Cookies.server_id(host) + end - begin - current_companion = env.request.cookies[CONFIG.server_id_cookie_name].value.try &.to_i - rescue - working_ends = BackendInfo.get_working_ends - current_companion = working_ends.sample - end + begin + current_companion = env.request.cookies[CONFIG.server_id_cookie_name].value.try &.to_i + rescue + working_ends = BackendInfo.get_working_ends + current_companion = working_ends.sample + end - if current_companion > CONFIG.invidious_companion.size - current_companion = current_companion % CONFIG.invidious_companion.size - env.response.cookies[CONFIG.server_id_cookie_name] = Invidious::User::Cookies.server_id(host, current_companion) - end - - companion_status = BackendInfo.get_status - - if companion_status[current_companion] != 2 - alive_companion = companion_status.index(2, offset: current_companion) - if alive_companion - env.set "companion_switched", true - current_companion = alive_companion + if current_companion > CONFIG.invidious_companion.size + current_companion = current_companion % CONFIG.invidious_companion.size env.response.cookies[CONFIG.server_id_cookie_name] = Invidious::User::Cookies.server_id(host, current_companion) end + + companion_status = BackendInfo.get_status + + if companion_status[current_companion] != BackendInfo::Status::Working.to_i + current_companion = 0 if current_companion == companion_status.size - 1 + alive_companion = companion_status.index(BackendInfo::Status::Working.to_i, offset: current_companion) + if alive_companion + env.set "companion_switched", true + current_companion = alive_companion + env.response.cookies[CONFIG.server_id_cookie_name] = Invidious::User::Cookies.server_id(host, current_companion) + end + end + + env.set "current_companion", current_companion + + if host.split(".").last == "i2p" + env.set "companion_public_url", CONFIG.invidious_companion[current_companion].i2p_public_url.to_s + else + env.set "companion_public_url", CONFIG.invidious_companion[current_companion].public_url.to_s + end end - env.set "current_companion", current_companion - - if host.split(".").last == "i2p" - env.set "companion_public_url", CONFIG.invidious_companion[current_companion].i2p_public_url.to_s - else - env.set "companion_public_url", CONFIG.invidious_companion[current_companion].public_url.to_s - end + extra_media_csp, extra_connect_csp = BackendInfo.get_csp(env.get("current_companion").as(Int32)) end - - extra_media_csp, extra_connect_csp = BackendInfo.get_csp(env.get("current_companion").as(Int32)) end # Only allow the pages at /embed/* to be embedded diff --git a/src/invidious/views/template.ecr b/src/invidious/views/template.ecr index e4c81238..6239902e 100644 --- a/src/invidious/views/template.ecr +++ b/src/invidious/views/template.ecr @@ -123,9 +123,8 @@ display: inline-block;"> <%= HTML.escape(CONFIG.backend_name_prefix + (index + 1).to_s) %> <%= HTML.escape(companion.note) %> <% if !(index == CONFIG.invidious_companion.size-1) %> @@ -138,9 +137,8 @@ display: inline-block;"> <%= HTML.escape(CONFIG.backend_name_prefix + (index + 1).to_s) %> <%= HTML.escape(companion.note) %> <% if !(index == CONFIG.invidious_companion.size-1) %>