chore(backends): change logic used to detect if the user accessed via a numbered backend or main domain
All checks were successful
Invidious CI / build (push) Successful in 6m4s

Numbered backends will only work if the domain has a number at the end, for example:

inv1.nadeko.net will redirect to backend 1
inv2.nadeko.net will redirect to backend 2
2inv.nadeko.net will use cookies
nadeko1.net will redirect to backend 1

The number of the backend in the domain should be always on the end of it to be able to use numbered backends.
This commit is contained in:
Fijxu 2025-04-13 18:18:06 -04:00
parent 3e33c9b70f
commit 69e351770d
Signed by: Fijxu
GPG key ID: 32C1DDF333EDA6A4
2 changed files with 15 additions and 22 deletions

View file

@ -1,6 +1,7 @@
module Invidious::Routes::BeforeAll module Invidious::Routes::BeforeAll
def self.handle(env) def self.handle(env)
preferences = Preferences.from_json("{}") preferences = Preferences.from_json("{}")
host = env.request.headers["Host"]
begin begin
if prefs_cookie = env.request.cookies["PREFS"]? if prefs_cookie = env.request.cookies["PREFS"]?
@ -24,25 +25,16 @@ module Invidious::Routes::BeforeAll
extra_connect_csp = "" extra_connect_csp = ""
if CONFIG.invidious_companion.present? if CONFIG.invidious_companion.present?
CONFIG.invidious_companion.each_with_index do |companion, index| current_companion_d = host.split(".")[0].scan(/(\d+)$/).last?.try &.[0].to_i
if companion.domain.each_with_index do |domain, domain_index|
if domain == env.request.headers["Host"]
env.set "current_companion", index
env.set "companion_public_url", companion.public_url.to_s
env.set "domain_index", domain_index
if domain_index == 2
env.set "companion_public_url", companion.i2p_public_url.to_s
end
break
end
end
end
break if env.get?("current_companion")
end
if env.get?("current_companion").try &.as(Int32) == nil if current_companion_d
current_companion_d = current_companion_d - 1
env.set "using_domain", true
env.set "current_companion", current_companion_d
env.set "companion_public_url", CONFIG.invidious_companion[current_companion_d].public_url.to_s
else
if !env.request.cookies[CONFIG.server_id_cookie_name]? if !env.request.cookies[CONFIG.server_id_cookie_name]?
env.response.cookies[CONFIG.server_id_cookie_name] = Invidious::User::Cookies.server_id(env.request.headers["Host"]) env.response.cookies[CONFIG.server_id_cookie_name] = Invidious::User::Cookies.server_id(host)
end end
begin begin
@ -53,7 +45,7 @@ module Invidious::Routes::BeforeAll
if current_companion > CONFIG.invidious_companion.size if current_companion > CONFIG.invidious_companion.size
current_companion = 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) env.response.cookies[CONFIG.server_id_cookie_name] = Invidious::User::Cookies.server_id(host, current_companion)
end end
env.set "current_companion", current_companion env.set "current_companion", current_companion

View file

@ -109,16 +109,17 @@
<% <%
if CONFIG.invidious_companion.present? if CONFIG.invidious_companion.present?
current_backend = env.get?("current_companion").try &.as(Int32) current_backend = env.get?("current_companion").try &.as(Int32)
domain_index = env.get?("domain_index").try &.as(Int32) domain = env.get?("using_domain")
scheme = env.get("scheme") scheme = env.get("scheme")
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>
<% if domain_index && domain_index != 2 %> <% if domain %>
<% CONFIG.invidious_companion.each_with_index do | companion, index | %> <% CONFIG.invidious_companion.each_with_index do | companion, index | %>
<% is_current_backend_host = companion.domain[domain_index] == env.request.headers["Host"] %> <% host_backend = env.request.headers["Host"].sub(/([^.]+)(\d+)/, "\\1#{index+1}") %>
<a href="<%= scheme %>://<%= companion.domain[domain_index] %><%= env.request.resource %>" style="<%= is_current_backend_host ? "text-decoration-line: underline;" : "" %> display: inline-block;"> <% is_current_backend_host = host_backend == env.request.headers["Host"] %>
<a href="<%= scheme %>://<%= host_backend %><%= 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) %> 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 %>