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 #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 ## Checks if the companions in the `invidious_companion` list
## are alive using their `/healthz` endpoint. ## are alive using their `/healthz` endpoint.

View File

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

View File

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

View File

@@ -2,14 +2,20 @@
module Invidious::Routes::BackendSwitcher module Invidious::Routes::BackendSwitcher
def self.switch(env) def self.switch(env)
preferences = env.get("preferences").as(Preferences)
referer = get_referer(env, unroll: false) referer = get_referer(env, unroll: false)
saved_backend = preferences.backend_number
backend_id = env.params.query["backend_id"]?.try &.to_i backend_id = env.params.query["backend_id"]?.try &.to_i
if backend_id.nil? if backend_id.nil?
return error_template(400, "Backend ID is required") return error_template(400, "Backend ID is required")
end 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 env.redirect referer
end end

View File

@@ -46,12 +46,12 @@ module Invidious::Routes::BeforeAll
env.set "current_companion", index env.set "current_companion", index
env.set "companion_public_url", CONFIG.invidious_companion[index].public_url.to_s env.set "companion_public_url", CONFIG.invidious_companion[index].public_url.to_s
else else
if !env.request.cookies[CONFIG.server_id_cookie_name]? if preferences.backend_number.nil?
env.response.cookies[CONFIG.server_id_cookie_name] = Invidious::User::Cookies.server_id(host) preferences.backend_number = rand(CONFIG.invidious_companion.size)
end end
begin begin
current_companion = env.request.cookies[CONFIG.server_id_cookie_name].value.try &.to_i current_companion = preferences.backend_number
rescue rescue
working_ends = BackendInfo.get_working_ends working_ends = BackendInfo.get_working_ends
if !working_ends.empty? if !working_ends.empty?
@@ -61,29 +61,31 @@ module Invidious::Routes::BeforeAll
end end
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 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 end
companion_status = BackendInfo.get_status 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 current_companion = 0 if current_companion == companion_status.size - 1
alive_companion = companion_status.index(BackendInfo::Status::Working.to_i, offset: current_companion) alive_companion = companion_status.index(BackendInfo::Status::Working.to_i, offset: current_companion)
if alive_companion if alive_companion
env.set "companion_switched", true env.set "companion_switched", true
current_companion = alive_companion 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
end end
env.set "current_companion", current_companion env.set "current_companion", current_companion
if host.split(".").last == "i2p" if current_companion
env.set "companion_public_url", CONFIG.invidious_companion[current_companion].i2p_public_url.to_s if host.split(".").last == "i2p"
else env.set "companion_public_url", CONFIG.invidious_companion[current_companion].i2p_public_url.to_s
env.set "companion_public_url", CONFIG.invidious_companion[current_companion].public_url.to_s else
env.set "companion_public_url", CONFIG.invidious_companion[current_companion].public_url.to_s
end
end end
end end

View File

@@ -53,31 +53,5 @@ struct Invidious::User
samesite: HTTP::Cookie::SameSite::Lax samesite: HTTP::Cookie::SameSite::Lax
) )
end 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
end end