This commit is contained in:
Fijxu
2025-12-19 17:08:47 -03:00
parent 1d699d537c
commit 5788a49aba
6 changed files with 24 additions and 53 deletions

View File

@@ -1163,15 +1163,6 @@ video_cache:
##
#max_dash_resolution: null
##
## The name of the cookie to hold the backend id that
## the client is using.
##
## Accepted values: a string
## Default: "COMPANION_ID"
##
#server_id_cookie_name: "COMPANION_ID"
##
## Checks if the companions in the `invidious_companion` list
## are alive using their `/healthz` endpoint.

View File

@@ -1,7 +1,7 @@
channel_threads: 0
log_level: Info
colorize_logs: true
database_url: postgres://kemal:kemal@pgbouncer:6432/invidious
database_url: postgres://kemal:kemal@postgres:5432/invidious
redis_url: tcp://valkey:6379
https_only: true
domain: inv.nadeko.net
@@ -48,8 +48,6 @@ default_user_preferences:
extend_desc: true
#local: false
server_id_cookie_name: "COMPANION_IDD"
video_cache:
enabled: true
backend: 1
@@ -59,4 +57,4 @@ check_backends_interval: 3
max_popular_results: 100
disable_video_downloads: true
hmac_key: "blahblahthisisnotarealkeyusedonproductionthisisjustfortesting123"
hmac_key: "blahblahthisisnotarealkeyusedonproductionthisisjustfortesting123"

View File

@@ -59,6 +59,8 @@ struct ConfigPreferences
property hidden_channels : Array(String)? = nil
@[YAML::Field(ignore: true)]
property default_trending_type : Invidious::Routes::Feeds::TrendingTypes = Invidious::Routes::Feeds::TrendingTypes::Default
@[YAML::Field(ignore: true)]
property backend_number : Int32? = nil
def to_tuple
{% begin %}
@@ -221,8 +223,6 @@ class Config
property pubsub_domain : String = ""
property server_id_cookie_name : String = "COMPANION_ID"
property video_cache : VideoCacheConfig = VideoCacheConfig.from_yaml("")
class VideoCacheConfig

View File

@@ -2,14 +2,20 @@
module Invidious::Routes::BackendSwitcher
def self.switch(env)
preferences = env.get("preferences").as(Preferences)
referer = get_referer(env, unroll: false)
saved_backend = preferences.backend_number
backend_id = env.params.query["backend_id"]?.try &.to_i
if backend_id.nil?
return error_template(400, "Backend ID is required")
end
env.response.cookies[CONFIG.server_id_cookie_name] = Invidious::User::Cookies.server_id(env.request.headers["Host"], backend_id)
if saved_backend.nil?
saved_backend = rand(CONFIG.invidious_companion.size)
end
env.response.cookies["PREFS"] = Invidious::User::Cookies.prefs(env.request.headers["Host"], preferences)
env.redirect referer
end

View File

@@ -46,12 +46,12 @@ module Invidious::Routes::BeforeAll
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)
if preferences.backend_number.nil?
preferences.backend_number = rand(CONFIG.invidious_companion.size)
end
begin
current_companion = env.request.cookies[CONFIG.server_id_cookie_name].value.try &.to_i
current_companion = preferences.backend_number
rescue
working_ends = BackendInfo.get_working_ends
if !working_ends.empty?
@@ -61,29 +61,31 @@ module Invidious::Routes::BeforeAll
end
end
if current_companion > CONFIG.invidious_companion.size
if current_companion && (current_companion > CONFIG.invidious_companion.size)
current_companion = current_companion % CONFIG.invidious_companion.size - 1
env.response.cookies[CONFIG.server_id_cookie_name] = Invidious::User::Cookies.server_id(host, current_companion)
preferences.backend_number = current_companion
end
companion_status = BackendInfo.get_status
if companion_status[current_companion] != BackendInfo::Status::Working.to_i
if current_companion && (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)
preferences.backend_number = 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
if 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
end

View File

@@ -53,31 +53,5 @@ struct Invidious::User
samesite: HTTP::Cookie::SameSite::Lax
)
end
# Backend (CONFIG.server_id_cookie_name) cookie
# Parameter "domain" comes from the global config
def server_id(domain : String?, server_id : Int32? = nil) : HTTP::Cookie
if server_id.nil?
server_id = rand(CONFIG.invidious_companion.size)
end
# Strip the port from the domain if it's being accessed from another port
# Browsers will reject the cookie if it contains the port number. This is
# because `example.com:3000` is not the same as `example.com` on a cookie.
domain = domain.split(":")[0]
# Not secure if it's being accessed from I2P
# Browsers expect the domain to include https. On I2P there is no HTTPS
if domain.not_nil!.split(".").last == "i2p"
@@secure = false
end
return HTTP::Cookie.new(
name: CONFIG.server_id_cookie_name,
domain: domain,
path: "/",
value: server_id.to_s,
secure: @@secure,
http_only: true,
samesite: HTTP::Cookie::SameSite::Lax
)
end
end
end