chore+fix(backends): Remove external videoplayback proxy support, fix
Some checks failed
Build and release container directly from master / release (push) Has been cancelled
Invidious CI / build - crystal: ${{ matrix.crystal }}, stable: ${{ matrix.stable }} (1.12.2, true) (push) Has been cancelled
Invidious CI / build - crystal: ${{ matrix.crystal }}, stable: ${{ matrix.stable }} (1.13.3, true) (push) Has been cancelled
Invidious CI / build - crystal: ${{ matrix.crystal }}, stable: ${{ matrix.stable }} (1.14.1, true) (push) Has been cancelled
Invidious CI / build - crystal: ${{ matrix.crystal }}, stable: ${{ matrix.stable }} (1.15.1, true) (push) Has been cancelled
Invidious CI / build - crystal: ${{ matrix.crystal }}, stable: ${{ matrix.stable }} (1.16.3, true) (push) Has been cancelled
Invidious CI / build - crystal: ${{ matrix.crystal }}, stable: ${{ matrix.stable }} (nightly, false) (push) Has been cancelled
Invidious CI / build-docker (push) Has been cancelled
Invidious CI / build-docker-arm64 (push) Has been cancelled
Invidious CI / lint (push) Has been cancelled

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.
This commit is contained in:
Fijxu 2025-06-06 11:58:57 -04:00
parent fc1e2fc221
commit e8e7a01dfb
No known key found for this signature in database
GPG Key ID: 32C1DDF333EDA6A4
3 changed files with 67 additions and 104 deletions

View File

@ -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

View File

@ -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

View File

@ -123,9 +123,8 @@
<a href="<%= scheme %>://<%= host_backend %><%= env.request.resource %>" style="<%= is_current_backend_host ? "text-decoration-line: underline;" : "" %> display: inline-block;">
<%= HTML.escape(CONFIG.backend_name_prefix + (index + 1).to_s) %> <%= HTML.escape(companion.note) %>
<span style="color:
<% if status[index] == 0 %> #fd4848; <% end %>
<% if status[index] == 1 %> #d06925; <% end %>
<% if status[index] == 2 %> #42ae3c; <% end %>
<% if status[index] == BackendInfo::Status::Dead.to_i %> #fd4848; <% end %>
<% if status[index] == BackendInfo::Status::Working.to_i %> #42ae3c; <% end %>
">•</span>
</a>
<% if !(index == CONFIG.invidious_companion.size-1) %>
@ -138,9 +137,8 @@
<a href="/switchbackend?backend_id=<%= index.to_s %>&referer=<%= current_page %>" style="<%= current_backend == index ? "text-decoration-line: underline;" : "" %> display: inline-block;">
<%= HTML.escape(CONFIG.backend_name_prefix + (index + 1).to_s) %> <%= HTML.escape(companion.note) %>
<span style="color:
<% if status[index] == 0 %> #fd4848; <% end %>
<% if status[index] == 1 %> #d06925; <% end %>
<% if status[index] == 2 %> #42ae3c; <% end %>
<% if status[index] == BackendInfo::Status::Dead.to_i %> #fd4848; <% end %>
<% if status[index] == BackendInfo::Status::Working.to_i %> #42ae3c; <% end %>
">•</span>
</a>
<% if !(index == CONFIG.invidious_companion.size-1) %>