From 5788a49abaa6650652d1bb4317b66adb6fee5c30 Mon Sep 17 00:00:00 2001 From: Fijxu Date: Fri, 19 Dec 2025 17:08:47 -0300 Subject: [PATCH] lol --- config/config.example.yml | 9 -------- docker/config.yml | 6 ++---- src/invidious/config.cr | 4 ++-- src/invidious/routes/backend_switcher.cr | 8 +++++++- src/invidious/routes/before_all.cr | 24 ++++++++++++---------- src/invidious/user/cookies.cr | 26 ------------------------ 6 files changed, 24 insertions(+), 53 deletions(-) diff --git a/config/config.example.yml b/config/config.example.yml index ff146764..ffba2515 100644 --- a/config/config.example.yml +++ b/config/config.example.yml @@ -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. diff --git a/docker/config.yml b/docker/config.yml index c5620030..e7586bb1 100644 --- a/docker/config.yml +++ b/docker/config.yml @@ -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" \ No newline at end of file +hmac_key: "blahblahthisisnotarealkeyusedonproductionthisisjustfortesting123" diff --git a/src/invidious/config.cr b/src/invidious/config.cr index f2d52b2d..a30a7217 100644 --- a/src/invidious/config.cr +++ b/src/invidious/config.cr @@ -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 diff --git a/src/invidious/routes/backend_switcher.cr b/src/invidious/routes/backend_switcher.cr index e4702729..cec98b43 100644 --- a/src/invidious/routes/backend_switcher.cr +++ b/src/invidious/routes/backend_switcher.cr @@ -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 diff --git a/src/invidious/routes/before_all.cr b/src/invidious/routes/before_all.cr index 5e788b23..ac2eec93 100644 --- a/src/invidious/routes/before_all.cr +++ b/src/invidious/routes/before_all.cr @@ -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 diff --git a/src/invidious/user/cookies.cr b/src/invidious/user/cookies.cr index eee92085..48669493 100644 --- a/src/invidious/user/cookies.cr +++ b/src/invidious/user/cookies.cr @@ -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