diff --git a/src/invidious/config.cr b/src/invidious/config.cr index a3112fb5..eb6967e3 100644 --- a/src/invidious/config.cr +++ b/src/invidious/config.cr @@ -84,6 +84,7 @@ class Config property public_url : URI = URI.parse("") property note : String = "" + property domain : String = "" end # Number of threads to use for crawling videos from channels (for updating subscriptions) diff --git a/src/invidious/routes/before_all.cr b/src/invidious/routes/before_all.cr index 72606929..2d5f1da3 100644 --- a/src/invidious/routes/before_all.cr +++ b/src/invidious/routes/before_all.cr @@ -24,24 +24,34 @@ module Invidious::Routes::BeforeAll extra_connect_csp = "" if CONFIG.invidious_companion.present? - if env.request.cookies[CONFIG.server_id_cookie_name]?.nil? - env.response.cookies[CONFIG.server_id_cookie_name] = Invidious::User::Cookies.server_id(env.request.headers["Host"]) + CONFIG.invidious_companion.each_with_index do |companion, index| + if companion.domain == env.request.headers["Host"] + env.set "current_companion", index + env.set "domain", true + break + end end - begin - current_companion = env.request.cookies[CONFIG.server_id_cookie_name].value.try &.to_i - rescue - current_companion = rand(CONFIG.invidious_companion.size) + if env.get?("current_companion").try &.as(Int32) == nil + 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"]) + 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 - 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 - - extra_media_csp, extra_connect_csp = BackendInfo.get_csp() + extra_media_csp, extra_connect_csp = BackendInfo.get_csp end if !CONFIG.external_videoplayback_proxy.empty? diff --git a/src/invidious/views/template.ecr b/src/invidious/views/template.ecr index 6334f19c..9144a78c 100644 --- a/src/invidious/views/template.ecr +++ b/src/invidious/views/template.ecr @@ -110,31 +110,41 @@ <% if CONFIG.invidious_companion.present? current_backend = env.get("current_companion").as(Int32) + domain = env.get?("domain").try &.as(Bool) status = BackendInfo.get_status %>
Switch Backend: - <% CONFIG.invidious_companion.each_with_index do | backend, index | %> - <% if current_backend == index %> - - Backend<%= HTML.escape((index+1).to_s) %> <%= HTML.escape(backend.note) %> + <% if domain %> + <% CONFIG.invidious_companion.each_with_index do | companion, index | %> + <% is_current_backend_host = companion.domain == env.request.headers["Host"] %> + <% scheme = env.request.headers["X-Forwarded-Proto"]? || ("https" if CONFIG.https_only) || "http" %> + display: inline-block;"> + Backend<%= HTML.escape((index + 1).to_s) %> <%= HTML.escape(companion.note) %> - <% else %> - - Backend<%= HTML.escape((index+1).to_s) %> <%= HTML.escape(backend.note) %> - + + <% if !(index == CONFIG.invidious_companion.size-1) %> + | + <% end %> <% end %> - - <% if !(index == CONFIG.invidious_companion.size-1) %> - | + <% else %> + <% CONFIG.invidious_companion.each_with_index do | companion, index | %> + <% is_current_backend_index = current_backend == index %> + display: inline-block;"> + Backend<%= HTML.escape((index + 1).to_s) %> <%= HTML.escape(companion.note) %> + + + <% if !(index == CONFIG.invidious_companion.size-1) %> + | + <% end %> <% end %> <% end %>