From f602e857eb57c472fc5aa660c2ce8b433c1d6246 Mon Sep 17 00:00:00 2001 From: TeamMasse Date: Thu, 30 Jan 2025 22:02:15 +0100 Subject: [PATCH 1/9] do it --- src/invidious/routes/errors.cr | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/invidious/routes/errors.cr b/src/invidious/routes/errors.cr index 1e9ab44e..0dd99716 100644 --- a/src/invidious/routes/errors.cr +++ b/src/invidious/routes/errors.cr @@ -15,10 +15,10 @@ module Invidious::Routes::ErrorRoutes response = YT_POOL.client &.get(URI.parse(response.headers["Location"]).request_target) end - if response.body.empty? - env.response.headers["Location"] = "/" - haltf env, status_code: 302 - end + #if response.body.empty? + # env.response.headers["Location"] = "/" + # haltf env, status_code: 302 + #end html = XML.parse_html(response.body) ucid = html.xpath_node(%q(//link[@rel="canonical"])).try &.["href"].split("/")[-1] From ba66d62d8eb43a3a46b35f4a15fdcec045ecda83 Mon Sep 17 00:00:00 2001 From: TeamMasse Date: Thu, 30 Jan 2025 22:11:27 +0100 Subject: [PATCH 2/9] changed hmac key --- docker-compose.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-compose.yml b/docker-compose.yml index afda8726..4b39a1a3 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -30,7 +30,7 @@ services: # domain: # https_only: false # statistics_enabled: false - hmac_key: "CHANGE_ME!!" + hmac_key: "Ahyie5aiz6ahCh2naex4" healthcheck: test: wget -nv --tries=1 --spider http://127.0.0.1:3000/api/v1/trending || exit 1 interval: 30s From bea747c3c0cb8c46b71b6780546cd40534e4b424 Mon Sep 17 00:00:00 2001 From: TeamMasse Date: Thu, 30 Jan 2025 22:22:31 +0100 Subject: [PATCH 3/9] comment out --- docker-compose.yml | 1 + src/invidious/routes/errors.cr | 12 ++++++------ 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 4b39a1a3..3092ac91 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -31,6 +31,7 @@ services: # https_only: false # statistics_enabled: false hmac_key: "Ahyie5aiz6ahCh2naex4" + healthcheck: test: wget -nv --tries=1 --spider http://127.0.0.1:3000/api/v1/trending || exit 1 interval: 30s diff --git a/src/invidious/routes/errors.cr b/src/invidious/routes/errors.cr index 0dd99716..5c8d7d06 100644 --- a/src/invidious/routes/errors.cr +++ b/src/invidious/routes/errors.cr @@ -20,13 +20,13 @@ module Invidious::Routes::ErrorRoutes # haltf env, status_code: 302 #end - html = XML.parse_html(response.body) - ucid = html.xpath_node(%q(//link[@rel="canonical"])).try &.["href"].split("/")[-1] + #html = XML.parse_html(response.body) + #ucid = html.xpath_node(%q(//link[@rel="canonical"])).try &.["href"].split("/")[-1] - if ucid - env.response.headers["Location"] = "/channel/#{ucid}" - haltf env, status_code: 302 - end + #if ucid + # env.response.headers["Location"] = "/channel/#{ucid}" + # haltf env, status_code: 302 + #end params = [] of String env.params.query.each do |k, v| From 3625ed36200769bf30bb803804de3c9edafae1a4 Mon Sep 17 00:00:00 2001 From: TeamMasse <106512658+TeamMasse@users.noreply.github.com> Date: Wed, 23 Apr 2025 22:56:29 +0200 Subject: [PATCH 4/9] fix #3933 swichted the order of checking for channel links and youtu.be/videoid short links to prevent redirecting to / because of empty response body for video --- src/invidious/routes/errors.cr | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/invidious/routes/errors.cr b/src/invidious/routes/errors.cr index 5c8d7d06..6d305578 100644 --- a/src/invidious/routes/errors.cr +++ b/src/invidious/routes/errors.cr @@ -15,19 +15,6 @@ module Invidious::Routes::ErrorRoutes response = YT_POOL.client &.get(URI.parse(response.headers["Location"]).request_target) end - #if response.body.empty? - # env.response.headers["Location"] = "/" - # haltf env, status_code: 302 - #end - - #html = XML.parse_html(response.body) - #ucid = html.xpath_node(%q(//link[@rel="canonical"])).try &.["href"].split("/")[-1] - - #if ucid - # env.response.headers["Location"] = "/channel/#{ucid}" - # haltf env, status_code: 302 - #end - params = [] of String env.params.query.each do |k, v| params << "#{k}=#{v}" @@ -44,6 +31,19 @@ module Invidious::Routes::ErrorRoutes env.response.headers["Location"] = url haltf env, status_code: 302 end + + if response.body.empty? + env.response.headers["Location"] = "/" + haltf env, status_code: 302 + end + + html = XML.parse_html(response.body) + ucid = html.xpath_node(%q(//link[@rel="canonical"])).try &.["href"].split("/")[-1] + + if ucid + env.response.headers["Location"] = "/channel/#{ucid}" + haltf env, status_code: 302 + end end env.response.headers["Location"] = "/" From 8984ad4c7d412281c843b8aaeb875e6b8cb31d7e Mon Sep 17 00:00:00 2001 From: TeamMasse <106512658+TeamMasse@users.noreply.github.com> Date: Wed, 23 Apr 2025 23:01:06 +0200 Subject: [PATCH 5/9] rechange hmac_key --- docker-compose.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-compose.yml b/docker-compose.yml index 3092ac91..20ce31c8 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -30,7 +30,7 @@ services: # domain: # https_only: false # statistics_enabled: false - hmac_key: "Ahyie5aiz6ahCh2naex4" + hmac_key: "CHANGE_ME!!" healthcheck: test: wget -nv --tries=1 --spider http://127.0.0.1:3000/api/v1/trending || exit 1 From 96d7186cc8ca0a111e4d7e0ddf48600a8481faa5 Mon Sep 17 00:00:00 2001 From: TeamMasse <106512658+TeamMasse@users.noreply.github.com> Date: Wed, 23 Apr 2025 23:01:55 +0200 Subject: [PATCH 6/9] Update docker-compose.yml --- docker-compose.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 20ce31c8..78fe159f 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -30,8 +30,7 @@ services: # domain: # https_only: false # statistics_enabled: false - hmac_key: "CHANGE_ME!!" - + hmac_key: "CHANGE_ME!!" healthcheck: test: wget -nv --tries=1 --spider http://127.0.0.1:3000/api/v1/trending || exit 1 interval: 30s From 247194c9945ebffa69428cd8302d52bab50583ca Mon Sep 17 00:00:00 2001 From: TeamMasse <106512658+TeamMasse@users.noreply.github.com> Date: Wed, 23 Apr 2025 23:03:02 +0200 Subject: [PATCH 7/9] Update docker-compose.yml --- docker-compose.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-compose.yml b/docker-compose.yml index 78fe159f..afda8726 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -30,7 +30,7 @@ services: # domain: # https_only: false # statistics_enabled: false - hmac_key: "CHANGE_ME!!" + hmac_key: "CHANGE_ME!!" healthcheck: test: wget -nv --tries=1 --spider http://127.0.0.1:3000/api/v1/trending || exit 1 interval: 30s From 08a53df7f4feeeee646388e352bcfcff39689cba Mon Sep 17 00:00:00 2001 From: Theo Fischer Date: Sun, 17 Aug 2025 11:56:49 +0200 Subject: [PATCH 8/9] fixed the redirection by adding a cookie so that the old flow works again --- src/invidious/routes/errors.cr | 44 ++++++++++++++++------------------ 1 file changed, 21 insertions(+), 23 deletions(-) diff --git a/src/invidious/routes/errors.cr b/src/invidious/routes/errors.cr index 6d305578..4b021ed6 100644 --- a/src/invidious/routes/errors.cr +++ b/src/invidious/routes/errors.cr @@ -4,32 +4,15 @@ module Invidious::Routes::ErrorRoutes if HOST_URL.empty? && env.request.path.starts_with?("/v1/storyboards/sb") return env.redirect "#{env.request.path[15..]}?#{env.params.query}" end - if md = env.request.path.match(/^\/(?([a-zA-Z0-9_-]{11})|(\w+))$/) item = md["id"] - # Check if item is branding URL e.g. https://youtube.com/gaming - response = YT_POOL.client &.get("/#{item}") - + headers = HTTP::Headers{ + "Cookie" => "SOCS=CAE" # Cookies to prevent redirects to Cookie Consent Page CAE~Reject all, CAA~showing the cookie banner, CAI~Accept all + } + response = YT_POOL.client &.get("/#{item}", headers: headers) if response.status_code == 301 - response = YT_POOL.client &.get(URI.parse(response.headers["Location"]).request_target) - end - - params = [] of String - env.params.query.each do |k, v| - params << "#{k}=#{v}" - end - params = params.join("&") - - url = "/watch?v=#{item}" - if !params.empty? - url += "&#{params}" - end - - # Check if item is video ID - if item.match(/^[a-zA-Z0-9_-]{11}$/) && YT_POOL.client &.head("/watch?v=#{item}").status_code != 404 - env.response.headers["Location"] = url - haltf env, status_code: 302 + response = YT_POOL.client &.get(URI.parse(response.headers["Location"]).request_target, headers: headers) end if response.body.empty? @@ -44,9 +27,24 @@ module Invidious::Routes::ErrorRoutes env.response.headers["Location"] = "/channel/#{ucid}" haltf env, status_code: 302 end + + params = [] of String + env.params.query.each do |k, v| + params << "#{k}=#{v}" + end + params = params.join("&") + url = "/watch?v=#{item}" + if !params.empty? + url += "&#{params}" + end + # Check if item is video ID + if item.match(/^[a-zA-Z0-9_-]{11}$/) && YT_POOL.client &.head("/watch?v=#{item}").status_code != 404 + env.response.headers["Location"] = url + haltf env, status_code: 302 + end end env.response.headers["Location"] = "/" haltf env, status_code: 302 end -end +end \ No newline at end of file From 4d1f10a30d531531611154ff9f95f05859e30503 Mon Sep 17 00:00:00 2001 From: Theo Fischer Date: Sun, 17 Aug 2025 12:01:32 +0200 Subject: [PATCH 9/9] improve readability --- src/invidious/routes/errors.cr | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/invidious/routes/errors.cr b/src/invidious/routes/errors.cr index 4b021ed6..2ac6b436 100644 --- a/src/invidious/routes/errors.cr +++ b/src/invidious/routes/errors.cr @@ -4,8 +4,10 @@ module Invidious::Routes::ErrorRoutes if HOST_URL.empty? && env.request.path.starts_with?("/v1/storyboards/sb") return env.redirect "#{env.request.path[15..]}?#{env.params.query}" end + if md = env.request.path.match(/^\/(?([a-zA-Z0-9_-]{11})|(\w+))$/) item = md["id"] + # Check if item is branding URL e.g. https://youtube.com/gaming headers = HTTP::Headers{ "Cookie" => "SOCS=CAE" # Cookies to prevent redirects to Cookie Consent Page CAE~Reject all, CAA~showing the cookie banner, CAI~Accept all @@ -33,10 +35,12 @@ module Invidious::Routes::ErrorRoutes params << "#{k}=#{v}" end params = params.join("&") + url = "/watch?v=#{item}" if !params.empty? url += "&#{params}" end + # Check if item is video ID if item.match(/^[a-zA-Z0-9_-]{11}$/) && YT_POOL.client &.head("/watch?v=#{item}").status_code != 404 env.response.headers["Location"] = url