Pick a different instance upon redirect (#5154)

The automatic instance redirection has the potential to pick
the same instance the user is currently on. This is especially
prevalent when the instance list is limited in number like how it is
today.

This PR checks the domain of the instance and ensures that it is not
the same as the current instane before redirecting the user to it.
Otherwise, it just sends the user to rediret.invidious.io
This commit is contained in:
syeopite 2025-02-26 14:05:21 -08:00
commit c5e9447f41
No known key found for this signature in database
GPG Key ID: A73C186DA3955A1A

View File

@ -42,12 +42,17 @@ module Invidious::Routes::Misc
referer = get_referer(env)
instance_list = Invidious::Jobs::InstanceListRefreshJob::INSTANCES["INSTANCES"]
if instance_list.empty?
# Filter out the current instance
other_available_instances = instance_list.reject { |_, domain| domain == CONFIG.domain }
if other_available_instances.empty?
# If the current instance is the only one, use the redirect URL as fallback
instance_url = "redirect.invidious.io"
else
# Select other random instance
# Sample returns an array
# Instances are packaged as {region, domain} in the instance list
instance_url = instance_list.sample(1)[0][1]
instance_url = other_available_instances.sample(1)[0][1]
end
env.redirect "https://#{instance_url}#{referer}"