support for numbered backends
All checks were successful
Invidious CI / build (push) Successful in 6m3s

This commit is contained in:
Fijxu 2025-03-31 18:41:17 -03:00
parent be9a3794e9
commit 015c9ec5d1
Signed by: Fijxu
GPG key ID: 32C1DDF333EDA6A4
3 changed files with 53 additions and 32 deletions

View file

@ -84,6 +84,7 @@ class Config
property public_url : URI = URI.parse("") property public_url : URI = URI.parse("")
property note : String = "" property note : String = ""
property domain : String = ""
end end
# Number of threads to use for crawling videos from channels (for updating subscriptions) # Number of threads to use for crawling videos from channels (for updating subscriptions)

View file

@ -24,24 +24,34 @@ module Invidious::Routes::BeforeAll
extra_connect_csp = "" extra_connect_csp = ""
if CONFIG.invidious_companion.present? if CONFIG.invidious_companion.present?
if env.request.cookies[CONFIG.server_id_cookie_name]?.nil? CONFIG.invidious_companion.each_with_index do |companion, index|
env.response.cookies[CONFIG.server_id_cookie_name] = Invidious::User::Cookies.server_id(env.request.headers["Host"]) if companion.domain == env.request.headers["Host"]
env.set "current_companion", index
env.set "domain", true
break
end
end end
begin if env.get?("current_companion").try &.as(Int32) == nil
current_companion = env.request.cookies[CONFIG.server_id_cookie_name].value.try &.to_i if !env.request.cookies[CONFIG.server_id_cookie_name]?
rescue env.response.cookies[CONFIG.server_id_cookie_name] = Invidious::User::Cookies.server_id(env.request.headers["Host"])
current_companion = rand(CONFIG.invidious_companion.size) end
begin
current_companion = env.request.cookies[CONFIG.server_id_cookie_name].value.try &.to_i
rescue
current_companion = rand(CONFIG.invidious_companion.size)
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(env.request.headers["Host"], current_companion)
end
env.set "current_companion", current_companion
end end
if current_companion > CONFIG.invidious_companion.size extra_media_csp, extra_connect_csp = BackendInfo.get_csp
current_companion = current_companion % CONFIG.invidious_companion.size
env.response.cookies[CONFIG.server_id_cookie_name] = Invidious::User::Cookies.server_id(env.request.headers["Host"], current_companion)
end
env.set "current_companion", current_companion
extra_media_csp, extra_connect_csp = BackendInfo.get_csp()
end end
if !CONFIG.external_videoplayback_proxy.empty? if !CONFIG.external_videoplayback_proxy.empty?

View file

@ -110,31 +110,41 @@
<% <%
if CONFIG.invidious_companion.present? if CONFIG.invidious_companion.present?
current_backend = env.get("current_companion").as(Int32) current_backend = env.get("current_companion").as(Int32)
domain = env.get?("domain").try &.as(Bool)
status = BackendInfo.get_status status = BackendInfo.get_status
%> %>
<div class="h-box" style="margin-bottom: 10px;"> <div class="h-box" style="margin-bottom: 10px;">
<b>Switch Backend:</b> <b>Switch Backend:</b>
<% CONFIG.invidious_companion.each_with_index do | backend, index | %> <% if domain %>
<% if current_backend == index %> <% CONFIG.invidious_companion.each_with_index do | companion, index | %>
<a href="/switchbackend?backend_id=<%= index.to_s %>" style="text-decoration-line: underline; display: inline-block;"> <% is_current_backend_host = companion.domain == env.request.headers["Host"] %>
Backend<%= HTML.escape((index+1).to_s) %> <%= HTML.escape(backend.note) %> <% scheme = env.request.headers["X-Forwarded-Proto"]? || ("https" if CONFIG.https_only) || "http" %>
<a href="<%= scheme %>://<%= companion.domain %><%= env.request.resource %>" style="<%= is_current_backend_host ? "text-decoration-line: underline;" : "" %> display: inline-block;">
Backend<%= HTML.escape((index + 1).to_s) %> <%= HTML.escape(companion.note) %>
<span style="color: <span style="color:
<% if status[index] == 0 %> #fd4848; <% end %> <% if status[index] == 0 %> #fd4848; <% end %>
<% if status[index] == 1 %> #d06925; <% end %> <% if status[index] == 1 %> #d06925; <% end %>
<% if status[index] == 2 %> #42ae3c; <% end %> <% if status[index] == 2 %> #42ae3c; <% end %>
">•</span>
<% else %>
<a href="/switchbackend?backend_id=<%= index.to_s %>" style="display: inline-block;">
Backend<%= HTML.escape((index+1).to_s) %> <%= HTML.escape(backend.note) %>
<span style="color:
<% if status[index] == 0 %> #fd4848; <% end %>
<% if status[index] == 1 %> #d06925; <% end %>
<% if status[index] == 2 %> #42ae3c; <% end %>
">•</span> ">•</span>
</a>
<% if !(index == CONFIG.invidious_companion.size-1) %>
<span> | </span>
<% end %>
<% end %> <% end %>
</a> <% else %>
<% if !(index == CONFIG.invidious_companion.size-1) %> <% CONFIG.invidious_companion.each_with_index do | companion, index | %>
<span> | </span> <% is_current_backend_index = current_backend == index %>
<a href="/switchbackend?backend_id=<%= index.to_s %>" style="<%= is_current_backend_index ? "text-decoration-line: underline;" : "" %> display: inline-block;">
Backend<%= HTML.escape((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 %>
">•</span>
</a>
<% if !(index == CONFIG.invidious_companion.size-1) %>
<span> | </span>
<% end %>
<% end %> <% end %>
<% end %> <% end %>
</div> </div>