From 3dff7a76cf9f64ec70aac0a057a3b0bfa1edfc82 Mon Sep 17 00:00:00 2001 From: Emilien Devos <4016501+unixfox@users.noreply.github.com> Date: Sun, 20 Oct 2024 02:10:55 +0200 Subject: [PATCH 01/20] add support for invidious companion --- config/config.example.yml | 16 ++++++++++ src/invidious/config.cr | 3 ++ src/invidious/videos/parser.cr | 39 +++++++++++++----------- src/invidious/yt_backend/youtube_api.cr | 40 +++++++++++++++++++------ 4 files changed, 71 insertions(+), 27 deletions(-) diff --git a/config/config.example.yml b/config/config.example.yml index a3a2eeb7..f715b356 100644 --- a/config/config.example.yml +++ b/config/config.example.yml @@ -54,6 +54,22 @@ db: ## #signature_server: +## +## Path to the Invidious companion. +## An external program for loading the video streams from YouTube servers. +## +## When this setting is commented out, Invidious companion is not used. +## +## When this setting is configured and "external_port" is used then +## you need to configure Invidious companion routes into your reverse proxy. +## If "external_port" is not configured then Invidious will proxy the requests +## to Invidious companion. +## +## Accepted values: "http(s)://:" +## Default: +## +#invidious_companion: + ######################################### # diff --git a/src/invidious/config.cr b/src/invidious/config.cr index c4ca622f..54b3a799 100644 --- a/src/invidious/config.cr +++ b/src/invidious/config.cr @@ -151,6 +151,9 @@ class Config # poToken for passing bot attestation property po_token : String? = nil + # Invidious companion + property invidious_companion : String? = nil + # Saved cookies in "name1=value1; name2=value2..." format @[YAML::Field(converter: Preferences::StringToCookies)] property cookies : HTTP::Cookies = HTTP::Cookies.new diff --git a/src/invidious/videos/parser.cr b/src/invidious/videos/parser.cr index 915c9baf..b8e9de20 100644 --- a/src/invidious/videos/parser.cr +++ b/src/invidious/videos/parser.cr @@ -100,27 +100,30 @@ def extract_video_info(video_id : String) params = parse_video_info(video_id, player_response) params["reason"] = JSON::Any.new(reason) if reason - new_player_response = nil + if CONFIG.invidious_companion.nil? + new_player_response = nil - # Don't use Android test suite client if po_token is passed because po_token doesn't - # work for Android test suite client. - if reason.nil? && CONFIG.po_token.nil? - # Fetch the video streams using an Android client in order to get the - # decrypted URLs and maybe fix throttling issues (#2194). See the - # following issue for an explanation about decrypted URLs: - # https://github.com/TeamNewPipe/NewPipeExtractor/issues/562 - client_config.client_type = YoutubeAPI::ClientType::AndroidTestSuite - new_player_response = try_fetch_streaming_data(video_id, client_config) - end + # Don't use Android test suite client if po_token is passed because po_token doesn't + # work for Android test suite client. + if reason.nil? && CONFIG.po_token.nil? + # Fetch the video streams using an Android client in order to get the + # decrypted URLs and maybe fix throttling issues (#2194). See the + # following issue for an explanation about decrypted URLs: + # https://github.com/TeamNewPipe/NewPipeExtractor/issues/562 + client_config.client_type = YoutubeAPI::ClientType::AndroidTestSuite + new_player_response = try_fetch_streaming_data(video_id, client_config) + end - # Replace player response and reset reason - if !new_player_response.nil? - # Preserve captions & storyboard data before replacement - new_player_response["storyboards"] = player_response["storyboards"] if player_response["storyboards"]? - new_player_response["captions"] = player_response["captions"] if player_response["captions"]? + # Replace player response and reset reason + if !new_player_response.nil? + # Preserve captions & storyboard data before replacement + new_player_response["storyboards"] = player_response["storyboards"] if player_response["storyboards"]? + new_player_response["captions"] = player_response["captions"] if player_response["captions"]? - player_response = new_player_response - params.delete("reason") + player_response = new_player_response + params.delete("reason") + end + end end {"captions", "playabilityStatus", "playerConfig", "storyboards"}.each do |f| diff --git a/src/invidious/yt_backend/youtube_api.cr b/src/invidious/yt_backend/youtube_api.cr index 8f5aa61d..8a90a76e 100644 --- a/src/invidious/yt_backend/youtube_api.cr +++ b/src/invidious/yt_backend/youtube_api.cr @@ -615,12 +615,19 @@ module YoutubeAPI headers = HTTP::Headers{ "Content-Type" => "application/json; charset=UTF-8", - "Accept-Encoding" => "gzip, deflate", "x-goog-api-format-version" => "2", "x-youtube-client-name" => client_config.name_proto, "x-youtube-client-version" => client_config.version, } + if CONFIG.invidious_companion && endpoint == "/youtubei/v1/player" + headers["Authorization"] = "Bearer " + CONFIG.hmac_key + end + + if !CONFIG.invidious_companion + headers["Accept-Encoding"] = "gzip, deflate" + end + if user_agent = client_config.user_agent headers["User-Agent"] = user_agent end @@ -634,16 +641,31 @@ module YoutubeAPI LOGGER.trace("YoutubeAPI: ClientConfig: #{client_config}") LOGGER.trace("YoutubeAPI: POST data: #{data}") + invidious_companion_url = CONFIG.invidious_companion + # Send the POST request - body = YT_POOL.client() do |client| - client.post(url, headers: headers, body: data.to_json) do |response| - if response.status_code != 200 - raise InfoException.new("Error: non 200 status code. Youtube API returned \ - status code #{response.status_code}. See \ - https://docs.invidious.io/youtube-errors-explained/ for troubleshooting.") - end - self._decompress(response.body_io, response.headers["Content-Encoding"]?) + if invidious_companion_url && endpoint == "/youtubei/v1/player" + begin + body = make_client(URI.parse(invidious_companion_url), + &.post(endpoint, headers: headers, body: data.to_json).body) + rescue + raise InfoException.new("Unable to communicate with Invidious companion.") end + else + body = YT_POOL.client() do |client| + client.post(url, headers: headers, body: data.to_json) do |response| + if response.status_code != 200 + raise InfoException.new("Error: non 200 status code. Youtube API returned \ + status code #{response.status_code}. See \ + https://docs.invidious.io/youtube-errors-explained/ for troubleshooting.") + end + self._decompress(response.body_io, response.headers["Content-Encoding"]?) + end + end + end + + if body.nil? && invidious_companion_url + raise InfoException.new("Unable to communicate with Invidious companion.") end # Convert result to Hash From 73c84baf9fa6eaf9c5d4981bc199f81306ebe5a2 Mon Sep 17 00:00:00 2001 From: Emilien Devos <4016501+unixfox@users.noreply.github.com> Date: Sun, 20 Oct 2024 23:51:00 +0200 Subject: [PATCH 02/20] redirect latest_version and dash manifest to invidious companion --- config/config.example.yml | 3 ++- src/invidious/config.cr | 2 +- src/invidious/routes/api/manifest.cr | 4 ++++ src/invidious/routes/video_playback.cr | 3 +++ src/invidious/routes/watch.cr | 7 +++++++ src/invidious/videos.cr | 4 ++++ src/invidious/videos/parser.cr | 15 ++++++++++----- src/invidious/views/components/player.ecr | 9 +++++++-- src/invidious/yt_backend/youtube_api.cr | 22 ++++++++++++++-------- 9 files changed, 52 insertions(+), 17 deletions(-) diff --git a/config/config.example.yml b/config/config.example.yml index f715b356..1e342382 100644 --- a/config/config.example.yml +++ b/config/config.example.yml @@ -68,7 +68,8 @@ db: ## Accepted values: "http(s)://:" ## Default: ## -#invidious_companion: +# invidious_companion: +# - http://127.0.0.1:8282 ######################################### diff --git a/src/invidious/config.cr b/src/invidious/config.cr index 54b3a799..ba4224ce 100644 --- a/src/invidious/config.cr +++ b/src/invidious/config.cr @@ -152,7 +152,7 @@ class Config property po_token : String? = nil # Invidious companion - property invidious_companion : String? = nil + property invidious_companion : Array(String)? = nil # Saved cookies in "name1=value1; name2=value2..." format @[YAML::Field(converter: Preferences::StringToCookies)] diff --git a/src/invidious/routes/api/manifest.cr b/src/invidious/routes/api/manifest.cr index d89e752c..43d0eb2f 100644 --- a/src/invidious/routes/api/manifest.cr +++ b/src/invidious/routes/api/manifest.cr @@ -20,6 +20,10 @@ module Invidious::Routes::API::Manifest haltf env, status_code: 403 end + if local && CONFIG.invidious_companion + return env.redirect "#{video.invidious_companion["baseUrl"].as_s}#{env.request.path}?#{env.request.query}" + end + if dashmpd = video.dash_manifest_url response = YT_POOL.client &.get(URI.parse(dashmpd).request_target) diff --git a/src/invidious/routes/video_playback.cr b/src/invidious/routes/video_playback.cr index 26852d06..5fda7084 100644 --- a/src/invidious/routes/video_playback.cr +++ b/src/invidious/routes/video_playback.cr @@ -294,6 +294,9 @@ module Invidious::Routes::VideoPlayback end if local + if (CONFIG.invidious_companion) + return env.redirect "#{video.invidious_companion["baseUrl"].as_s}#{env.request.path}?#{env.request.query}" + end url = URI.parse(url).request_target.not_nil! url += "&title=#{URI.encode_www_form(title, space_to_plus: false)}" if title end diff --git a/src/invidious/routes/watch.cr b/src/invidious/routes/watch.cr index aabe8dfc..961d7b31 100644 --- a/src/invidious/routes/watch.cr +++ b/src/invidious/routes/watch.cr @@ -190,6 +190,13 @@ module Invidious::Routes::Watch captions: video.captions ) + if (CONFIG.invidious_companion && env.params.query["local"] == true) + env.response.headers["Content-Security-Policy"] = + env.response.headers["Content-Security-Policy"] + .gsub("media-src", "media-src " + video.invidious_companion["baseUrl"].as_s) + .gsub("connect-src", "connect-src " + video.invidious_companion["baseUrl"].as_s) + end + templated "watch" end diff --git a/src/invidious/videos.cr b/src/invidious/videos.cr index ae09e736..131549d0 100644 --- a/src/invidious/videos.cr +++ b/src/invidious/videos.cr @@ -192,6 +192,10 @@ struct Video } end + def invidious_companion : Hash(String, JSON::Any) + info["invidiousCompanion"].try &.as_h + end + # Macros defining getters/setters for various types of data private macro getset_string(name) diff --git a/src/invidious/videos/parser.cr b/src/invidious/videos/parser.cr index b8e9de20..28f3ea41 100644 --- a/src/invidious/videos/parser.cr +++ b/src/invidious/videos/parser.cr @@ -141,6 +141,10 @@ def extract_video_info(video_id : String) params["streamingData"] = streaming_data end + if CONFIG.invidious_companion + params["invidiousCompanion"] = player_response["invidiousCompanion"] + end + # Data structure version, for cache control params["version"] = JSON::Any.new(Video::SCHEMA_VERSION.to_i64) @@ -449,11 +453,12 @@ def parse_video_info(video_id : String, player_response : Hash(String, JSON::Any # Music section "music" => JSON.parse(music_list.to_json), # Author infos - "author" => JSON::Any.new(author || ""), - "ucid" => JSON::Any.new(ucid || ""), - "authorThumbnail" => JSON::Any.new(author_thumbnail.try &.as_s || ""), - "authorVerified" => JSON::Any.new(author_verified || false), - "subCountText" => JSON::Any.new(subs_text || "-"), + "author" => JSON::Any.new(author || ""), + "ucid" => JSON::Any.new(ucid || ""), + "authorThumbnail" => JSON::Any.new(author_thumbnail.try &.as_s || ""), + "authorVerified" => JSON::Any.new(author_verified || false), + "subCountText" => JSON::Any.new(subs_text || "-"), + "invidiousCompanion" => JSON::Any.new(subs_text), } return params diff --git a/src/invidious/views/components/player.ecr b/src/invidious/views/components/player.ecr index 5c28358b..017b3462 100644 --- a/src/invidious/views/components/player.ecr +++ b/src/invidious/views/components/player.ecr @@ -22,6 +22,7 @@ audio_streams.each_with_index do |fmt, i| src_url = "/latest_version?id=#{video.id}&itag=#{fmt["itag"]}" src_url += "&local=true" if params.local + src_url = video.invidious_companion["baseUrl"].as_s + src_url if (CONFIG.invidious_companion && params.local) bitrate = fmt["bitrate"] mimetype = HTML.escape(fmt["mimeType"].as_s) @@ -34,8 +35,11 @@ <% end %> <% end %> <% else %> - <% if params.quality == "dash" %> - + <% if params.quality == "dash" + src_url = "/api/manifest/dash/id/" + video.id + "?local=true&unique_res=1" + src_url = video.invidious_companion["baseUrl"].as_s + src_url if (CONFIG.invidious_companion) + %> + <% end %> <% @@ -44,6 +48,7 @@ fmt_stream.each_with_index do |fmt, i| src_url = "/latest_version?id=#{video.id}&itag=#{fmt["itag"]}" src_url += "&local=true" if params.local + src_url = video.invidious_companion["baseUrl"].as_s + src_url if (CONFIG.invidious_companion && params.local) quality = fmt["quality"] mimetype = HTML.escape(fmt["mimeType"].as_s) diff --git a/src/invidious/yt_backend/youtube_api.cr b/src/invidious/yt_backend/youtube_api.cr index 8a90a76e..de205034 100644 --- a/src/invidious/yt_backend/youtube_api.cr +++ b/src/invidious/yt_backend/youtube_api.cr @@ -641,15 +641,21 @@ module YoutubeAPI LOGGER.trace("YoutubeAPI: ClientConfig: #{client_config}") LOGGER.trace("YoutubeAPI: POST data: #{data}") - invidious_companion_url = CONFIG.invidious_companion + invidious_companion_urls = CONFIG.invidious_companion # Send the POST request - if invidious_companion_url && endpoint == "/youtubei/v1/player" + if invidious_companion_urls && endpoint == "/youtubei/v1/player" + puts "invidious companion section" + puts invidious_companion_urls[Random.rand(invidious_companion_urls.size)] begin - body = make_client(URI.parse(invidious_companion_url), - &.post(endpoint, headers: headers, body: data.to_json).body) - rescue - raise InfoException.new("Unable to communicate with Invidious companion.") + response = make_client(URI.parse(invidious_companion_urls[Random.rand(invidious_companion_urls.size)]), + &.post(endpoint, headers: headers, body: data.to_json)) + body = response.body + if (response.status_code != 200) + raise Exception.new("status code: " + response.status_code.to_s + " and body: " + body) + end + rescue ex + raise InfoException.new("Error while communicating with Invidious companion: " + (ex.message || "no extra info found")) end else body = YT_POOL.client() do |client| @@ -664,8 +670,8 @@ module YoutubeAPI end end - if body.nil? && invidious_companion_url - raise InfoException.new("Unable to communicate with Invidious companion.") + if body.nil? && CONFIG.invidious_companion + raise InfoException.new("Error while communicating with Invidious companion: no response data.") end # Convert result to Hash From 195446337159d2cb92b48510af7311fe0cc0f5bb Mon Sep 17 00:00:00 2001 From: Emilien Devos <4016501+unixfox@users.noreply.github.com> Date: Sun, 20 Oct 2024 23:53:08 +0200 Subject: [PATCH 03/20] fix Shadowing outer local variable `response` --- src/invidious/yt_backend/youtube_api.cr | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/invidious/yt_backend/youtube_api.cr b/src/invidious/yt_backend/youtube_api.cr index de205034..b0494203 100644 --- a/src/invidious/yt_backend/youtube_api.cr +++ b/src/invidious/yt_backend/youtube_api.cr @@ -648,11 +648,11 @@ module YoutubeAPI puts "invidious companion section" puts invidious_companion_urls[Random.rand(invidious_companion_urls.size)] begin - response = make_client(URI.parse(invidious_companion_urls[Random.rand(invidious_companion_urls.size)]), + invidious_companion_response = make_client(URI.parse(invidious_companion_urls[Random.rand(invidious_companion_urls.size)]), &.post(endpoint, headers: headers, body: data.to_json)) - body = response.body - if (response.status_code != 200) - raise Exception.new("status code: " + response.status_code.to_s + " and body: " + body) + body = invidious_companion_response.body + if (invidious_companion_response.status_code != 200) + raise Exception.new("status code: " + invidious_companion_response.status_code.to_s + " and body: " + body) end rescue ex raise InfoException.new("Error while communicating with Invidious companion: " + (ex.message || "no extra info found")) From c612423a4d64f0adbef135074fc55dcc1c362f84 Mon Sep 17 00:00:00 2001 From: Emilien Devos <4016501+unixfox@users.noreply.github.com> Date: Mon, 21 Oct 2024 01:20:16 +0200 Subject: [PATCH 04/20] fixing condition for Content-Security-Policy --- src/invidious/routes/watch.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/invidious/routes/watch.cr b/src/invidious/routes/watch.cr index 961d7b31..2cf8a725 100644 --- a/src/invidious/routes/watch.cr +++ b/src/invidious/routes/watch.cr @@ -190,7 +190,7 @@ module Invidious::Routes::Watch captions: video.captions ) - if (CONFIG.invidious_companion && env.params.query["local"] == true) + if (CONFIG.invidious_companion && (preferences.local || preferences.quality == "dash")) env.response.headers["Content-Security-Policy"] = env.response.headers["Content-Security-Policy"] .gsub("media-src", "media-src " + video.invidious_companion["baseUrl"].as_s) From 2cc204a0457665f8e334970d7e54b1843a667ab6 Mon Sep 17 00:00:00 2001 From: Emilien <4016501+unixfox@users.noreply.github.com> Date: Fri, 1 Nov 2024 21:30:58 +0100 Subject: [PATCH 05/20] throw error if inv_sig_helper and invidious_companion used same time --- src/invidious.cr | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/invidious.cr b/src/invidious.cr index b422dcbb..2fd9cdd8 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -161,6 +161,12 @@ Invidious::Database.check_integrity(CONFIG) {% puts "\nDone checking player dependencies, now compiling Invidious...\n" %} {% end %} +# invidious_companion and signature_server can't work together +if CONFIG.signature_server && CONFIG.invidious_companion + puts "You can not run inv_sig_helper and invidious_companion at the same time." + exit(1) +end + # Misc DECRYPT_FUNCTION = From 1c9f5b0a2b38ad94fb8972764ffae98df1e41dc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milien=20=28perso=29?= <4016501+unixfox@users.noreply.github.com> Date: Tue, 5 Nov 2024 15:31:21 +0100 Subject: [PATCH 06/20] Use sample instead of Random.rand Co-authored-by: syeopite <70992037+syeopite@users.noreply.github.com> --- src/invidious/yt_backend/youtube_api.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/invidious/yt_backend/youtube_api.cr b/src/invidious/yt_backend/youtube_api.cr index b0494203..1bd236d2 100644 --- a/src/invidious/yt_backend/youtube_api.cr +++ b/src/invidious/yt_backend/youtube_api.cr @@ -648,7 +648,7 @@ module YoutubeAPI puts "invidious companion section" puts invidious_companion_urls[Random.rand(invidious_companion_urls.size)] begin - invidious_companion_response = make_client(URI.parse(invidious_companion_urls[Random.rand(invidious_companion_urls.size)]), + invidious_companion_response = make_client(URI.parse(invidious_companion_urls.sample), &.post(endpoint, headers: headers, body: data.to_json)) body = invidious_companion_response.body if (invidious_companion_response.status_code != 200) From 27b24f51abcccd1c68f4dc1c29c0c62ca26e604c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milien=20=28perso=29?= <4016501+unixfox@users.noreply.github.com> Date: Tue, 5 Nov 2024 15:31:45 +0100 Subject: [PATCH 07/20] Remove debug puts functions Co-authored-by: syeopite <70992037+syeopite@users.noreply.github.com> --- src/invidious/yt_backend/youtube_api.cr | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/invidious/yt_backend/youtube_api.cr b/src/invidious/yt_backend/youtube_api.cr index 1bd236d2..68abcf48 100644 --- a/src/invidious/yt_backend/youtube_api.cr +++ b/src/invidious/yt_backend/youtube_api.cr @@ -645,8 +645,6 @@ module YoutubeAPI # Send the POST request if invidious_companion_urls && endpoint == "/youtubei/v1/player" - puts "invidious companion section" - puts invidious_companion_urls[Random.rand(invidious_companion_urls.size)] begin invidious_companion_response = make_client(URI.parse(invidious_companion_urls.sample), &.post(endpoint, headers: headers, body: data.to_json)) From 409df4cff3cc69c5565a12feb307441eed36f937 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milien=20=28perso=29?= <4016501+unixfox@users.noreply.github.com> Date: Tue, 5 Nov 2024 15:50:59 +0100 Subject: [PATCH 08/20] modify the description for config.example.yaml about invidious companion --- config/config.example.yml | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/config/config.example.yml b/config/config.example.yml index 1e342382..76a9a077 100644 --- a/config/config.example.yml +++ b/config/config.example.yml @@ -55,15 +55,14 @@ db: #signature_server: ## -## Path to the Invidious companion. -## An external program for loading the video streams from YouTube servers. +## Invidious companion is an external program +## for loading the video streams from YouTube servers. ## ## When this setting is commented out, Invidious companion is not used. ## -## When this setting is configured and "external_port" is used then -## you need to configure Invidious companion routes into your reverse proxy. -## If "external_port" is not configured then Invidious will proxy the requests +## When this setting is configured, then Invidious will proxy the requests ## to Invidious companion. +## Or randomly choose one if multiple Invidious companion are configured. ## ## Accepted values: "http(s)://:" ## Default: From ff3305d52175c517b035d79b3c0c6a84809cbd0f Mon Sep 17 00:00:00 2001 From: Emilien <4016501+unixfox@users.noreply.github.com> Date: Fri, 8 Nov 2024 21:05:17 +0100 Subject: [PATCH 09/20] move config checks for invidious companion --- config/config.example.yml | 12 +++++++++++- src/invidious.cr | 6 ------ src/invidious/config.cr | 18 ++++++++++++++++++ 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/config/config.example.yml b/config/config.example.yml index 76a9a077..06e53541 100644 --- a/config/config.example.yml +++ b/config/config.example.yml @@ -67,9 +67,19 @@ db: ## Accepted values: "http(s)://:" ## Default: ## -# invidious_companion: +#invidious_companion: # - http://127.0.0.1:8282 +## +## API key for Invidious companion +## +## Needed when invidious_companion is configured +## +## Accepted values: "http(s)://:" +## Default: +## + +#invidious_companion_key: "CHANGE_ME!!" ######################################### # diff --git a/src/invidious.cr b/src/invidious.cr index 2fd9cdd8..b422dcbb 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -161,12 +161,6 @@ Invidious::Database.check_integrity(CONFIG) {% puts "\nDone checking player dependencies, now compiling Invidious...\n" %} {% end %} -# invidious_companion and signature_server can't work together -if CONFIG.signature_server && CONFIG.invidious_companion - puts "You can not run inv_sig_helper and invidious_companion at the same time." - exit(1) -end - # Misc DECRYPT_FUNCTION = diff --git a/src/invidious/config.cr b/src/invidious/config.cr index ba4224ce..7cd4bbdc 100644 --- a/src/invidious/config.cr +++ b/src/invidious/config.cr @@ -154,6 +154,9 @@ class Config # Invidious companion property invidious_companion : Array(String)? = nil + # Invidious companion API key + property invidious_companion_key : String? = nil + # Saved cookies in "name1=value1; name2=value2..." format @[YAML::Field(converter: Preferences::StringToCookies)] property cookies : HTTP::Cookies = HTTP::Cookies.new @@ -225,6 +228,21 @@ class Config end {% end %} + if CONFIG.invidious_companion + # invidious_companion and signature_server can't work together + if CONFIG.signature_server + puts "Config: You can not run inv_sig_helper and invidious_companion at the same time." + exit(1) + end + if !CONFIG.invidious_companion_key + puts "Config: Please configure a key if you are using invidious companion." + exit(1) + elsif CONFIG.invidious_companion_key == "CHANGE_ME!!" + puts "Config: The value of 'invidious_companion_key' needs to be changed!!" + exit(1) + end + end + # HMAC_key is mandatory # See: https://github.com/iv-org/invidious/issues/3854 if config.hmac_key.empty? From 1aa154b9787eddcdee960d06aed4c1c91f17c1c3 Mon Sep 17 00:00:00 2001 From: Emilien <4016501+unixfox@users.noreply.github.com> Date: Sat, 16 Nov 2024 22:33:28 +0100 Subject: [PATCH 10/20] separate invidious_companion logic + better config.yaml config --- src/invidious/config.cr | 29 ++++++- src/invidious/routes/api/manifest.cr | 8 +- src/invidious/routes/embed.cr | 7 ++ src/invidious/routes/video_playback.cr | 8 +- src/invidious/routes/watch.cr | 6 +- src/invidious/videos.cr | 6 +- src/invidious/videos/parser.cr | 19 ++--- src/invidious/views/components/player.ecr | 6 +- src/invidious/yt_backend/youtube_api.cr | 95 ++++++++++++++--------- 9 files changed, 119 insertions(+), 65 deletions(-) diff --git a/src/invidious/config.cr b/src/invidious/config.cr index 7cd4bbdc..29f4b60c 100644 --- a/src/invidious/config.cr +++ b/src/invidious/config.cr @@ -67,6 +67,28 @@ end class Config include YAML::Serializable + module URIArrayConverter + def self.to_yaml(values : Array(URI), yaml : YAML::Nodes::Builder) + yaml.sequence do + values.each { |v| yaml.scalar v.to_s } + end + end + + def self.from_yaml(ctx : YAML::ParseContext, node : YAML::Nodes::Node) : Array(URI) + if node.is_a?(YAML::Nodes::Sequence) + node.map do |child| + unless child.is_a?(YAML::Nodes::Scalar) + node.raise "Expected scalar, not #{child.class}" + end + + URI.parse(child.value) + end + else + node.raise "Expected sequence, not #{node.class}" + end + end + end + # Number of threads to use for crawling videos from channels (for updating subscriptions) property channel_threads : Int32 = 1 # Time interval between two executions of the job that crawls channel videos (subscriptions update). @@ -152,10 +174,11 @@ class Config property po_token : String? = nil # Invidious companion - property invidious_companion : Array(String)? = nil + @[YAML::Field(converter: Config::URIArrayConverter)] + property invidious_companion : Array(URI) = [] of URI # Invidious companion API key - property invidious_companion_key : String? = nil + property invidious_companion_key : String = "" # Saved cookies in "name1=value1; name2=value2..." format @[YAML::Field(converter: Preferences::StringToCookies)] @@ -228,7 +251,7 @@ class Config end {% end %} - if CONFIG.invidious_companion + if !CONFIG.invidious_companion.empty? # invidious_companion and signature_server can't work together if CONFIG.signature_server puts "Config: You can not run inv_sig_helper and invidious_companion at the same time." diff --git a/src/invidious/routes/api/manifest.cr b/src/invidious/routes/api/manifest.cr index 43d0eb2f..2892d1b4 100644 --- a/src/invidious/routes/api/manifest.cr +++ b/src/invidious/routes/api/manifest.cr @@ -1,6 +1,10 @@ module Invidious::Routes::API::Manifest # /api/manifest/dash/id/:id def self.get_dash_video_id(env) + if !CONFIG.invidious_companion.empty? + return error_template(403, "This endpoint is not permitted because it is handled by Invidious companion.") + end + env.response.headers.add("Access-Control-Allow-Origin", "*") env.response.content_type = "application/dash+xml" @@ -20,10 +24,6 @@ module Invidious::Routes::API::Manifest haltf env, status_code: 403 end - if local && CONFIG.invidious_companion - return env.redirect "#{video.invidious_companion["baseUrl"].as_s}#{env.request.path}?#{env.request.query}" - end - if dashmpd = video.dash_manifest_url response = YT_POOL.client &.get(URI.parse(dashmpd).request_target) diff --git a/src/invidious/routes/embed.cr b/src/invidious/routes/embed.cr index 266f7ba4..23f2bbe1 100644 --- a/src/invidious/routes/embed.cr +++ b/src/invidious/routes/embed.cr @@ -201,6 +201,13 @@ module Invidious::Routes::Embed return env.redirect url end + if (!CONFIG.invidious_companion.empty? && (preferences.local || preferences.quality == "dash")) + env.response.headers["Content-Security-Policy"] = + env.response.headers["Content-Security-Policy"] + .gsub("media-src", "media-src " + video.invidious_companion.not_nil!["baseUrl"].as_s) + .gsub("connect-src", "connect-src " + video.invidious_companion.not_nil!["baseUrl"].as_s) + end + rendered "embed" end end diff --git a/src/invidious/routes/video_playback.cr b/src/invidious/routes/video_playback.cr index 5fda7084..17b1022a 100644 --- a/src/invidious/routes/video_playback.cr +++ b/src/invidious/routes/video_playback.cr @@ -253,6 +253,10 @@ module Invidious::Routes::VideoPlayback # YouTube /videoplayback links expire after 6 hours, # so we have a mechanism here to redirect to the latest version def self.latest_version(env) + if !CONFIG.invidious_companion.empty? && CONFIG.disabled?("downloads") + return error_template(403, "This endpoint is not permitted because it is handled by Invidious companion.") + end + id = env.params.query["id"]? itag = env.params.query["itag"]?.try &.to_i? @@ -294,8 +298,8 @@ module Invidious::Routes::VideoPlayback end if local - if (CONFIG.invidious_companion) - return env.redirect "#{video.invidious_companion["baseUrl"].as_s}#{env.request.path}?#{env.request.query}" + if (!CONFIG.invidious_companion.empty?) + return env.redirect "#{video.invidious_companion.not_nil!["baseUrl"].as_s}#{env.request.path}?#{env.request.query}" end url = URI.parse(url).request_target.not_nil! url += "&title=#{URI.encode_www_form(title, space_to_plus: false)}" if title diff --git a/src/invidious/routes/watch.cr b/src/invidious/routes/watch.cr index 2cf8a725..69780d78 100644 --- a/src/invidious/routes/watch.cr +++ b/src/invidious/routes/watch.cr @@ -190,11 +190,11 @@ module Invidious::Routes::Watch captions: video.captions ) - if (CONFIG.invidious_companion && (preferences.local || preferences.quality == "dash")) + if (!CONFIG.invidious_companion.empty? && (preferences.local || preferences.quality == "dash")) env.response.headers["Content-Security-Policy"] = env.response.headers["Content-Security-Policy"] - .gsub("media-src", "media-src " + video.invidious_companion["baseUrl"].as_s) - .gsub("connect-src", "connect-src " + video.invidious_companion["baseUrl"].as_s) + .gsub("media-src", "media-src " + video.invidious_companion.not_nil!["baseUrl"].as_s) + .gsub("connect-src", "connect-src " + video.invidious_companion.not_nil!["baseUrl"].as_s) end templated "watch" diff --git a/src/invidious/videos.cr b/src/invidious/videos.cr index 131549d0..7670e9a9 100644 --- a/src/invidious/videos.cr +++ b/src/invidious/videos.cr @@ -15,7 +15,7 @@ struct Video # NOTE: don't forget to bump this number if any change is made to # the `params` structure in videos/parser.cr!!! # - SCHEMA_VERSION = 2 + SCHEMA_VERSION = 3 property id : String @@ -192,8 +192,8 @@ struct Video } end - def invidious_companion : Hash(String, JSON::Any) - info["invidiousCompanion"].try &.as_h + def invidious_companion : Hash(String, JSON::Any)? + info["invidiousCompanion"]?.try &.as_h end # Macros defining getters/setters for various types of data diff --git a/src/invidious/videos/parser.cr b/src/invidious/videos/parser.cr index 28f3ea41..24729beb 100644 --- a/src/invidious/videos/parser.cr +++ b/src/invidious/videos/parser.cr @@ -100,7 +100,7 @@ def extract_video_info(video_id : String) params = parse_video_info(video_id, player_response) params["reason"] = JSON::Any.new(reason) if reason - if CONFIG.invidious_companion.nil? + if !CONFIG.invidious_companion.empty? new_player_response = nil # Don't use Android test suite client if po_token is passed because po_token doesn't @@ -126,7 +126,7 @@ def extract_video_info(video_id : String) end end - {"captions", "playabilityStatus", "playerConfig", "storyboards"}.each do |f| + {"captions", "playabilityStatus", "playerConfig", "storyboards", "invidiousCompanion"}.each do |f| params[f] = player_response[f] if player_response[f]? end @@ -141,10 +141,6 @@ def extract_video_info(video_id : String) params["streamingData"] = streaming_data end - if CONFIG.invidious_companion - params["invidiousCompanion"] = player_response["invidiousCompanion"] - end - # Data structure version, for cache control params["version"] = JSON::Any.new(Video::SCHEMA_VERSION.to_i64) @@ -453,12 +449,11 @@ def parse_video_info(video_id : String, player_response : Hash(String, JSON::Any # Music section "music" => JSON.parse(music_list.to_json), # Author infos - "author" => JSON::Any.new(author || ""), - "ucid" => JSON::Any.new(ucid || ""), - "authorThumbnail" => JSON::Any.new(author_thumbnail.try &.as_s || ""), - "authorVerified" => JSON::Any.new(author_verified || false), - "subCountText" => JSON::Any.new(subs_text || "-"), - "invidiousCompanion" => JSON::Any.new(subs_text), + "author" => JSON::Any.new(author || ""), + "ucid" => JSON::Any.new(ucid || ""), + "authorThumbnail" => JSON::Any.new(author_thumbnail.try &.as_s || ""), + "authorVerified" => JSON::Any.new(author_verified || false), + "subCountText" => JSON::Any.new(subs_text || "-"), } return params diff --git a/src/invidious/views/components/player.ecr b/src/invidious/views/components/player.ecr index 017b3462..59f43239 100644 --- a/src/invidious/views/components/player.ecr +++ b/src/invidious/views/components/player.ecr @@ -22,7 +22,7 @@ audio_streams.each_with_index do |fmt, i| src_url = "/latest_version?id=#{video.id}&itag=#{fmt["itag"]}" src_url += "&local=true" if params.local - src_url = video.invidious_companion["baseUrl"].as_s + src_url if (CONFIG.invidious_companion && params.local) + src_url = video.invidious_companion.not_nil!["baseUrl"].as_s + src_url if (!CONFIG.invidious_companion.empty? && params.local) bitrate = fmt["bitrate"] mimetype = HTML.escape(fmt["mimeType"].as_s) @@ -37,7 +37,7 @@ <% else %> <% if params.quality == "dash" src_url = "/api/manifest/dash/id/" + video.id + "?local=true&unique_res=1" - src_url = video.invidious_companion["baseUrl"].as_s + src_url if (CONFIG.invidious_companion) + src_url = video.invidious_companion.not_nil!["baseUrl"].as_s + src_url if (!CONFIG.invidious_companion.empty?) %> <% end %> @@ -48,7 +48,7 @@ fmt_stream.each_with_index do |fmt, i| src_url = "/latest_version?id=#{video.id}&itag=#{fmt["itag"]}" src_url += "&local=true" if params.local - src_url = video.invidious_companion["baseUrl"].as_s + src_url if (CONFIG.invidious_companion && params.local) + src_url = video.invidious_companion.not_nil!["baseUrl"].as_s + src_url if (!CONFIG.invidious_companion.empty? && params.local) quality = fmt["quality"] mimetype = HTML.escape(fmt["mimeType"].as_s) diff --git a/src/invidious/yt_backend/youtube_api.cr b/src/invidious/yt_backend/youtube_api.cr index 68abcf48..6a0e50a1 100644 --- a/src/invidious/yt_backend/youtube_api.cr +++ b/src/invidious/yt_backend/youtube_api.cr @@ -500,7 +500,11 @@ module YoutubeAPI data["params"] = params end - return self._post_json("/youtubei/v1/player", data, client_config) + if !CONFIG.invidious_companion.empty? + return self._post_invidious_companion("/youtubei/v1/player", data) + else + return self._post_json("/youtubei/v1/player", data, client_config) + end end #################################################################### @@ -615,19 +619,12 @@ module YoutubeAPI headers = HTTP::Headers{ "Content-Type" => "application/json; charset=UTF-8", + "Accept-Encoding" => "gzip, deflate", "x-goog-api-format-version" => "2", "x-youtube-client-name" => client_config.name_proto, "x-youtube-client-version" => client_config.version, } - if CONFIG.invidious_companion && endpoint == "/youtubei/v1/player" - headers["Authorization"] = "Bearer " + CONFIG.hmac_key - end - - if !CONFIG.invidious_companion - headers["Accept-Encoding"] = "gzip, deflate" - end - if user_agent = client_config.user_agent headers["User-Agent"] = user_agent end @@ -641,35 +638,16 @@ module YoutubeAPI LOGGER.trace("YoutubeAPI: ClientConfig: #{client_config}") LOGGER.trace("YoutubeAPI: POST data: #{data}") - invidious_companion_urls = CONFIG.invidious_companion - # Send the POST request - if invidious_companion_urls && endpoint == "/youtubei/v1/player" - begin - invidious_companion_response = make_client(URI.parse(invidious_companion_urls.sample), - &.post(endpoint, headers: headers, body: data.to_json)) - body = invidious_companion_response.body - if (invidious_companion_response.status_code != 200) - raise Exception.new("status code: " + invidious_companion_response.status_code.to_s + " and body: " + body) + body = YT_POOL.client() do |client| + client.post(url, headers: headers, body: data.to_json) do |response| + if response.status_code != 200 + raise InfoException.new("Error: non 200 status code. Youtube API returned \ + status code #{response.status_code}. See \ + https://docs.invidious.io/youtube-errors-explained/ for troubleshooting.") end - rescue ex - raise InfoException.new("Error while communicating with Invidious companion: " + (ex.message || "no extra info found")) + self._decompress(response.body_io, response.headers["Content-Encoding"]?) end - else - body = YT_POOL.client() do |client| - client.post(url, headers: headers, body: data.to_json) do |response| - if response.status_code != 200 - raise InfoException.new("Error: non 200 status code. Youtube API returned \ - status code #{response.status_code}. See \ - https://docs.invidious.io/youtube-errors-explained/ for troubleshooting.") - end - self._decompress(response.body_io, response.headers["Content-Encoding"]?) - end - end - end - - if body.nil? && CONFIG.invidious_companion - raise InfoException.new("Error while communicating with Invidious companion: no response data.") end # Convert result to Hash @@ -692,6 +670,53 @@ module YoutubeAPI return initial_data end + #################################################################### + # _post_invidious_companion(endpoint, data) + # + # Internal function that does the actual request to Invidious companion + # and handles errors. + # + # The requested data is an endpoint (URL without the domain part) + # and the data as a Hash object. + # + def _post_invidious_companion( + endpoint : String, + data : Hash + ) : Hash(String, JSON::Any) + headers = HTTP::Headers{ + "Content-Type" => "application/json; charset=UTF-8", + "Accept-Encoding" => "gzip", + "Authorization" => "Bearer " + CONFIG.invidious_companion_key, + } + + # Logging + LOGGER.debug("Invidious companion: Using endpoint: \"#{endpoint}\"") + LOGGER.trace("Invidious companion: POST data: #{data}") + + # Send the POST request + + begin + response = make_client(CONFIG.invidious_companion.sample, + &.post(endpoint, headers: headers, body: data.to_json)) + body = self._decompress(response.body_io, response.headers["Content-Encoding"]?) + if (response.status_code != 200) + raise Exception.new("Error while communicating with Invidious companion: \ + status code: " + response.status_code.to_s + " and body: " + body) + end + rescue ex + raise InfoException.new("Error while communicating with Invidious companion: " + (ex.message || "no extra info found")) + end + + if body.nil? + raise InfoException.new("Error while communicating with Invidious companion: no response data.") + end + + # Convert result to Hash + initial_data = JSON.parse(body).as_h + + return initial_data + end + #################################################################### # _decompress(body_io, headers) # From 9f846127aea9b4f392acb062d662fff2cc58d1d0 Mon Sep 17 00:00:00 2001 From: Emilien <4016501+unixfox@users.noreply.github.com> Date: Sat, 16 Nov 2024 22:38:00 +0100 Subject: [PATCH 11/20] fixing "end" misplacement --- src/invidious/videos/parser.cr | 1 - 1 file changed, 1 deletion(-) diff --git a/src/invidious/videos/parser.cr b/src/invidious/videos/parser.cr index 24729beb..7a73be68 100644 --- a/src/invidious/videos/parser.cr +++ b/src/invidious/videos/parser.cr @@ -123,7 +123,6 @@ def extract_video_info(video_id : String) player_response = new_player_response params.delete("reason") end - end end {"captions", "playabilityStatus", "playerConfig", "storyboards", "invidiousCompanion"}.each do |f| From b51770dbdbdcca04d04849d37e5f11ce20948c73 Mon Sep 17 00:00:00 2001 From: Emilien <4016501+unixfox@users.noreply.github.com> Date: Sat, 16 Nov 2024 23:00:48 +0100 Subject: [PATCH 12/20] fix linting + use .empty? --- src/invidious/config.cr | 2 +- src/invidious/videos/parser.cr | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/invidious/config.cr b/src/invidious/config.cr index 29f4b60c..4b06ff83 100644 --- a/src/invidious/config.cr +++ b/src/invidious/config.cr @@ -257,7 +257,7 @@ class Config puts "Config: You can not run inv_sig_helper and invidious_companion at the same time." exit(1) end - if !CONFIG.invidious_companion_key + if CONFIG.invidious_companion_key.empty? puts "Config: Please configure a key if you are using invidious companion." exit(1) elsif CONFIG.invidious_companion_key == "CHANGE_ME!!" diff --git a/src/invidious/videos/parser.cr b/src/invidious/videos/parser.cr index 7a73be68..4d0e0378 100644 --- a/src/invidious/videos/parser.cr +++ b/src/invidious/videos/parser.cr @@ -120,9 +120,9 @@ def extract_video_info(video_id : String) new_player_response["storyboards"] = player_response["storyboards"] if player_response["storyboards"]? new_player_response["captions"] = player_response["captions"] if player_response["captions"]? - player_response = new_player_response - params.delete("reason") - end + player_response = new_player_response + params.delete("reason") + end end {"captions", "playabilityStatus", "playerConfig", "storyboards", "invidiousCompanion"}.each do |f| From bb2e3b2a3e5f53610b9dd602f8507303ec641450 Mon Sep 17 00:00:00 2001 From: Emilien <4016501+unixfox@users.noreply.github.com> Date: Sun, 17 Nov 2024 12:26:35 +0100 Subject: [PATCH 13/20] crystal handle decompression already by itself --- src/invidious/config.cr | 8 ++++---- src/invidious/yt_backend/youtube_api.cr | 3 +-- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/invidious/config.cr b/src/invidious/config.cr index 4b06ff83..20268ed1 100644 --- a/src/invidious/config.cr +++ b/src/invidious/config.cr @@ -251,16 +251,16 @@ class Config end {% end %} - if !CONFIG.invidious_companion.empty? + if !config.invidious_companion.empty? # invidious_companion and signature_server can't work together - if CONFIG.signature_server + if config.signature_server puts "Config: You can not run inv_sig_helper and invidious_companion at the same time." exit(1) end - if CONFIG.invidious_companion_key.empty? + if config.invidious_companion_key.empty? puts "Config: Please configure a key if you are using invidious companion." exit(1) - elsif CONFIG.invidious_companion_key == "CHANGE_ME!!" + elsif config.invidious_companion_key == "CHANGE_ME!!" puts "Config: The value of 'invidious_companion_key' needs to be changed!!" exit(1) end diff --git a/src/invidious/yt_backend/youtube_api.cr b/src/invidious/yt_backend/youtube_api.cr index 6a0e50a1..8fef8472 100644 --- a/src/invidious/yt_backend/youtube_api.cr +++ b/src/invidious/yt_backend/youtube_api.cr @@ -685,7 +685,6 @@ module YoutubeAPI ) : Hash(String, JSON::Any) headers = HTTP::Headers{ "Content-Type" => "application/json; charset=UTF-8", - "Accept-Encoding" => "gzip", "Authorization" => "Bearer " + CONFIG.invidious_companion_key, } @@ -698,7 +697,7 @@ module YoutubeAPI begin response = make_client(CONFIG.invidious_companion.sample, &.post(endpoint, headers: headers, body: data.to_json)) - body = self._decompress(response.body_io, response.headers["Content-Encoding"]?) + body = response.body if (response.status_code != 200) raise Exception.new("Error while communicating with Invidious companion: \ status code: " + response.status_code.to_s + " and body: " + body) From 734e72503f88f9741279ab385e86f5d2b340c71b Mon Sep 17 00:00:00 2001 From: Emilien <4016501+unixfox@users.noreply.github.com> Date: Sun, 17 Nov 2024 19:18:29 +0100 Subject: [PATCH 14/20] fix download function when invidious companion used --- src/invidious/routes/video_playback.cr | 5 +---- src/invidious/routes/watch.cr | 11 +++++++++-- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/invidious/routes/video_playback.cr b/src/invidious/routes/video_playback.cr index 17b1022a..405e733b 100644 --- a/src/invidious/routes/video_playback.cr +++ b/src/invidious/routes/video_playback.cr @@ -253,7 +253,7 @@ module Invidious::Routes::VideoPlayback # YouTube /videoplayback links expire after 6 hours, # so we have a mechanism here to redirect to the latest version def self.latest_version(env) - if !CONFIG.invidious_companion.empty? && CONFIG.disabled?("downloads") + if !CONFIG.invidious_companion.empty? return error_template(403, "This endpoint is not permitted because it is handled by Invidious companion.") end @@ -298,9 +298,6 @@ module Invidious::Routes::VideoPlayback end if local - if (!CONFIG.invidious_companion.empty?) - return env.redirect "#{video.invidious_companion.not_nil!["baseUrl"].as_s}#{env.request.path}?#{env.request.query}" - end url = URI.parse(url).request_target.not_nil! url += "&title=#{URI.encode_www_form(title, space_to_plus: false)}" if title end diff --git a/src/invidious/routes/watch.cr b/src/invidious/routes/watch.cr index 69780d78..740a15b2 100644 --- a/src/invidious/routes/watch.cr +++ b/src/invidious/routes/watch.cr @@ -328,13 +328,20 @@ module Invidious::Routes::Watch return Invidious::Routes::API::V1::Videos.captions(env) elsif itag = download_widget["itag"]?.try &.as_i + itag = itag.to_s + # URL params specific to /latest_version env.params.query["id"] = video_id - env.params.query["itag"] = itag.to_s + env.params.query["itag"] = itag env.params.query["title"] = filename env.params.query["local"] = "true" - return Invidious::Routes::VideoPlayback.latest_version(env) + if (!CONFIG.invidious_companion.empty?) + video = get_video(video_id) + return env.redirect "#{video.invidious_companion.not_nil!["baseUrl"].as_s}/latest_version?id=#{video_id}&itag=#{itag}&local=true" + else + return Invidious::Routes::VideoPlayback.latest_version(env) + end else return error_template(400, "Invalid label or itag") end From 1f51edd0b915ca64df7f195aa271f74c7ef093cb Mon Sep 17 00:00:00 2001 From: Emilien <4016501+unixfox@users.noreply.github.com> Date: Mon, 18 Nov 2024 12:22:23 +0100 Subject: [PATCH 15/20] fix linting --- src/invidious/yt_backend/youtube_api.cr | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/invidious/yt_backend/youtube_api.cr b/src/invidious/yt_backend/youtube_api.cr index 8fef8472..09f0936f 100644 --- a/src/invidious/yt_backend/youtube_api.cr +++ b/src/invidious/yt_backend/youtube_api.cr @@ -684,8 +684,8 @@ module YoutubeAPI data : Hash ) : Hash(String, JSON::Any) headers = HTTP::Headers{ - "Content-Type" => "application/json; charset=UTF-8", - "Authorization" => "Bearer " + CONFIG.invidious_companion_key, + "Content-Type" => "application/json; charset=UTF-8", + "Authorization" => "Bearer " + CONFIG.invidious_companion_key, } # Logging From 7a070fa710b7807cdda061d413ca9369a0962353 Mon Sep 17 00:00:00 2001 From: Emilien <4016501+unixfox@users.noreply.github.com> Date: Mon, 18 Nov 2024 12:30:37 +0100 Subject: [PATCH 16/20] invidious companion always used so always add CSP and redirect latest_version --- src/invidious/routes/embed.cr | 2 +- src/invidious/routes/watch.cr | 2 +- src/invidious/views/components/player.ecr | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/invidious/routes/embed.cr b/src/invidious/routes/embed.cr index 23f2bbe1..df697001 100644 --- a/src/invidious/routes/embed.cr +++ b/src/invidious/routes/embed.cr @@ -201,7 +201,7 @@ module Invidious::Routes::Embed return env.redirect url end - if (!CONFIG.invidious_companion.empty? && (preferences.local || preferences.quality == "dash")) + if (!CONFIG.invidious_companion.empty?) env.response.headers["Content-Security-Policy"] = env.response.headers["Content-Security-Policy"] .gsub("media-src", "media-src " + video.invidious_companion.not_nil!["baseUrl"].as_s) diff --git a/src/invidious/routes/watch.cr b/src/invidious/routes/watch.cr index 740a15b2..b42ee12d 100644 --- a/src/invidious/routes/watch.cr +++ b/src/invidious/routes/watch.cr @@ -190,7 +190,7 @@ module Invidious::Routes::Watch captions: video.captions ) - if (!CONFIG.invidious_companion.empty? && (preferences.local || preferences.quality == "dash")) + if (!CONFIG.invidious_companion.empty?) env.response.headers["Content-Security-Policy"] = env.response.headers["Content-Security-Policy"] .gsub("media-src", "media-src " + video.invidious_companion.not_nil!["baseUrl"].as_s) diff --git a/src/invidious/views/components/player.ecr b/src/invidious/views/components/player.ecr index 59f43239..46c00137 100644 --- a/src/invidious/views/components/player.ecr +++ b/src/invidious/views/components/player.ecr @@ -22,7 +22,7 @@ audio_streams.each_with_index do |fmt, i| src_url = "/latest_version?id=#{video.id}&itag=#{fmt["itag"]}" src_url += "&local=true" if params.local - src_url = video.invidious_companion.not_nil!["baseUrl"].as_s + src_url if (!CONFIG.invidious_companion.empty? && params.local) + src_url = video.invidious_companion.not_nil!["baseUrl"].as_s + src_url if (!CONFIG.invidious_companion.empty?) bitrate = fmt["bitrate"] mimetype = HTML.escape(fmt["mimeType"].as_s) @@ -48,7 +48,7 @@ fmt_stream.each_with_index do |fmt, i| src_url = "/latest_version?id=#{video.id}&itag=#{fmt["itag"]}" src_url += "&local=true" if params.local - src_url = video.invidious_companion.not_nil!["baseUrl"].as_s + src_url if (!CONFIG.invidious_companion.empty? && params.local) + src_url = video.invidious_companion.not_nil!["baseUrl"].as_s + src_url if (!CONFIG.invidious_companion.empty?) quality = fmt["quality"] mimetype = HTML.escape(fmt["mimeType"].as_s) From f710dd37bf4327748b43067d75025cc915b5639c Mon Sep 17 00:00:00 2001 From: Emilien <4016501+unixfox@users.noreply.github.com> Date: Sun, 8 Dec 2024 22:21:10 +0100 Subject: [PATCH 17/20] apply all the suggestions + rework invidious_companion parameter --- config/config.example.yml | 19 ++++++++++++---- src/invidious/config.cr | 27 ++++++----------------- src/invidious/routes/api/manifest.cr | 9 ++++---- src/invidious/routes/embed.cr | 6 ++--- src/invidious/routes/video_playback.cr | 3 ++- src/invidious/routes/watch.cr | 12 +++++----- src/invidious/videos.cr | 2 +- src/invidious/views/components/player.ecr | 6 ++--- src/invidious/yt_backend/youtube_api.cr | 15 ++++++------- 9 files changed, 48 insertions(+), 51 deletions(-) diff --git a/config/config.example.yml b/config/config.example.yml index 06e53541..2273c839 100644 --- a/config/config.example.yml +++ b/config/config.example.yml @@ -59,16 +59,27 @@ db: ## for loading the video streams from YouTube servers. ## ## When this setting is commented out, Invidious companion is not used. +## Otherwise, Invidious will proxy the requests to Invidious companion. +## +## Note: multiple URL can be configured. In this case, invidious will +## randomly pick one every time video data needs to be retrieved. This +## URL is then kept in the video metadata cache to allow video playback +## to work. Once said cache has expired, requesting that video's data +## again will cause a new companion URL to be picked. ## -## When this setting is configured, then Invidious will proxy the requests -## to Invidious companion. -## Or randomly choose one if multiple Invidious companion are configured. +## The parameter private_url needs to be configured for the internal +## communication between the companion and Invidious. +## And public_url is the public URL from which companion is listening +## to the requests from the user(s). +## Both parameter can have identical URL when Invidious is hosted in +## an internal network or at home. ## ## Accepted values: "http(s)://:" ## Default: ## #invidious_companion: -# - http://127.0.0.1:8282 +# - private_url: "http://localhost:8282" +# public_url: "http://localhost:8282" ## ## API key for Invidious companion diff --git a/src/invidious/config.cr b/src/invidious/config.cr index 20268ed1..91622576 100644 --- a/src/invidious/config.cr +++ b/src/invidious/config.cr @@ -67,26 +67,14 @@ end class Config include YAML::Serializable - module URIArrayConverter - def self.to_yaml(values : Array(URI), yaml : YAML::Nodes::Builder) - yaml.sequence do - values.each { |v| yaml.scalar v.to_s } - end - end + class CompanionConfig + include YAML::Serializable - def self.from_yaml(ctx : YAML::ParseContext, node : YAML::Nodes::Node) : Array(URI) - if node.is_a?(YAML::Nodes::Sequence) - node.map do |child| - unless child.is_a?(YAML::Nodes::Scalar) - node.raise "Expected scalar, not #{child.class}" - end + @[YAML::Field(converter: Preferences::URIConverter)] + property private_url : URI = URI.parse("") - URI.parse(child.value) - end - else - node.raise "Expected sequence, not #{node.class}" - end - end + @[YAML::Field(converter: Preferences::URIConverter)] + property public_url : URI = URI.parse("") end # Number of threads to use for crawling videos from channels (for updating subscriptions) @@ -174,8 +162,7 @@ class Config property po_token : String? = nil # Invidious companion - @[YAML::Field(converter: Config::URIArrayConverter)] - property invidious_companion : Array(URI) = [] of URI + property invidious_companion : Array(CompanionConfig) = [] of CompanionConfig # Invidious companion API key property invidious_companion_key : String = "" diff --git a/src/invidious/routes/api/manifest.cr b/src/invidious/routes/api/manifest.cr index 2892d1b4..b6c5cbf3 100644 --- a/src/invidious/routes/api/manifest.cr +++ b/src/invidious/routes/api/manifest.cr @@ -1,10 +1,6 @@ module Invidious::Routes::API::Manifest # /api/manifest/dash/id/:id def self.get_dash_video_id(env) - if !CONFIG.invidious_companion.empty? - return error_template(403, "This endpoint is not permitted because it is handled by Invidious companion.") - end - env.response.headers.add("Access-Control-Allow-Origin", "*") env.response.content_type = "application/dash+xml" @@ -12,6 +8,11 @@ module Invidious::Routes::API::Manifest id = env.params.url["id"] region = env.params.query["region"]? + if !CONFIG.invidious_companion.empty? + invidious_companion = CONFIG.invidious_companion.sample + return env.redirect "#{invidious_companion.public_url.to_s}/api/manifest/dash/id/#{id}?#{env.params.query}" + end + # Since some implementations create playlists based on resolution regardless of different codecs, # we can opt to only add a source to a representation if it has a unique height within that representation unique_res = env.params.query["unique_res"]?.try { |q| (q == "true" || q == "1").to_unsafe } diff --git a/src/invidious/routes/embed.cr b/src/invidious/routes/embed.cr index df697001..d023f61e 100644 --- a/src/invidious/routes/embed.cr +++ b/src/invidious/routes/embed.cr @@ -201,11 +201,11 @@ module Invidious::Routes::Embed return env.redirect url end - if (!CONFIG.invidious_companion.empty?) + if companion_base_url = video.invidious_companion.try &.["baseUrl"].as_s env.response.headers["Content-Security-Policy"] = env.response.headers["Content-Security-Policy"] - .gsub("media-src", "media-src " + video.invidious_companion.not_nil!["baseUrl"].as_s) - .gsub("connect-src", "connect-src " + video.invidious_companion.not_nil!["baseUrl"].as_s) + .gsub("media-src", "media-src #{companion_base_url}") + .gsub("connect-src", "connect-src #{companion_base_url}") end rendered "embed" diff --git a/src/invidious/routes/video_playback.cr b/src/invidious/routes/video_playback.cr index 405e733b..bac79b5c 100644 --- a/src/invidious/routes/video_playback.cr +++ b/src/invidious/routes/video_playback.cr @@ -254,7 +254,8 @@ module Invidious::Routes::VideoPlayback # so we have a mechanism here to redirect to the latest version def self.latest_version(env) if !CONFIG.invidious_companion.empty? - return error_template(403, "This endpoint is not permitted because it is handled by Invidious companion.") + invidious_companion = CONFIG.invidious_companion.sample + return env.redirect "#{invidious_companion.public_url.to_s}/latest_version?#{env.params.query}" end id = env.params.query["id"]? diff --git a/src/invidious/routes/watch.cr b/src/invidious/routes/watch.cr index b42ee12d..e9a076f3 100644 --- a/src/invidious/routes/watch.cr +++ b/src/invidious/routes/watch.cr @@ -190,11 +190,11 @@ module Invidious::Routes::Watch captions: video.captions ) - if (!CONFIG.invidious_companion.empty?) + if companion_base_url = video.invidious_companion.try &.["baseUrl"].as_s env.response.headers["Content-Security-Policy"] = env.response.headers["Content-Security-Policy"] - .gsub("media-src", "media-src " + video.invidious_companion.not_nil!["baseUrl"].as_s) - .gsub("connect-src", "connect-src " + video.invidious_companion.not_nil!["baseUrl"].as_s) + .gsub("media-src", "media-src #{companion_base_url}") + .gsub("connect-src", "connect-src #{companion_base_url}") end templated "watch" @@ -327,18 +327,16 @@ module Invidious::Routes::Watch env.params.query["label"] = URI.decode_www_form(label.as_s) return Invidious::Routes::API::V1::Videos.captions(env) - elsif itag = download_widget["itag"]?.try &.as_i - itag = itag.to_s + elsif itag = download_widget["itag"]?.try &.as_i.to_s # URL params specific to /latest_version env.params.query["id"] = video_id - env.params.query["itag"] = itag env.params.query["title"] = filename env.params.query["local"] = "true" if (!CONFIG.invidious_companion.empty?) video = get_video(video_id) - return env.redirect "#{video.invidious_companion.not_nil!["baseUrl"].as_s}/latest_version?id=#{video_id}&itag=#{itag}&local=true" + return env.redirect "#{video.invidious_companion["baseUrl"].as_s}/latest_version?#{env.params.query}" else return Invidious::Routes::VideoPlayback.latest_version(env) end diff --git a/src/invidious/videos.cr b/src/invidious/videos.cr index 7670e9a9..8cbbbf7a 100644 --- a/src/invidious/videos.cr +++ b/src/invidious/videos.cr @@ -193,7 +193,7 @@ struct Video end def invidious_companion : Hash(String, JSON::Any)? - info["invidiousCompanion"]?.try &.as_h + info["invidiousCompanion"]?.try &.as_h || {} of String => JSON::Any end # Macros defining getters/setters for various types of data diff --git a/src/invidious/views/components/player.ecr b/src/invidious/views/components/player.ecr index 46c00137..975a25be 100644 --- a/src/invidious/views/components/player.ecr +++ b/src/invidious/views/components/player.ecr @@ -22,7 +22,7 @@ audio_streams.each_with_index do |fmt, i| src_url = "/latest_version?id=#{video.id}&itag=#{fmt["itag"]}" src_url += "&local=true" if params.local - src_url = video.invidious_companion.not_nil!["baseUrl"].as_s + src_url if (!CONFIG.invidious_companion.empty?) + src_url = video.invidious_companion["baseUrl"].as_s + src_url if (!CONFIG.invidious_companion.empty?) bitrate = fmt["bitrate"] mimetype = HTML.escape(fmt["mimeType"].as_s) @@ -37,7 +37,7 @@ <% else %> <% if params.quality == "dash" src_url = "/api/manifest/dash/id/" + video.id + "?local=true&unique_res=1" - src_url = video.invidious_companion.not_nil!["baseUrl"].as_s + src_url if (!CONFIG.invidious_companion.empty?) + src_url = video.invidious_companion["baseUrl"].as_s + src_url if (!CONFIG.invidious_companion.empty?) %> <% end %> @@ -48,7 +48,7 @@ fmt_stream.each_with_index do |fmt, i| src_url = "/latest_version?id=#{video.id}&itag=#{fmt["itag"]}" src_url += "&local=true" if params.local - src_url = video.invidious_companion.not_nil!["baseUrl"].as_s + src_url if (!CONFIG.invidious_companion.empty?) + src_url = video.invidious_companion["baseUrl"].as_s + src_url if (!CONFIG.invidious_companion.empty?) quality = fmt["quality"] mimetype = HTML.escape(fmt["mimeType"].as_s) diff --git a/src/invidious/yt_backend/youtube_api.cr b/src/invidious/yt_backend/youtube_api.cr index 09f0936f..74f65449 100644 --- a/src/invidious/yt_backend/youtube_api.cr +++ b/src/invidious/yt_backend/youtube_api.cr @@ -685,7 +685,7 @@ module YoutubeAPI ) : Hash(String, JSON::Any) headers = HTTP::Headers{ "Content-Type" => "application/json; charset=UTF-8", - "Authorization" => "Bearer " + CONFIG.invidious_companion_key, + "Authorization" => "Bearer #{CONFIG.invidious_companion_key}", } # Logging @@ -695,21 +695,20 @@ module YoutubeAPI # Send the POST request begin - response = make_client(CONFIG.invidious_companion.sample, + invidious_companion = CONFIG.invidious_companion.sample + response = make_client(invidious_companion.private_url, &.post(endpoint, headers: headers, body: data.to_json)) body = response.body if (response.status_code != 200) - raise Exception.new("Error while communicating with Invidious companion: \ - status code: " + response.status_code.to_s + " and body: " + body) + raise Exception.new( + "Error while communicating with Invidious companion: \ + status code: #{response.status_code} and body: #{body.dump}" + ) end rescue ex raise InfoException.new("Error while communicating with Invidious companion: " + (ex.message || "no extra info found")) end - if body.nil? - raise InfoException.new("Error while communicating with Invidious companion: no response data.") - end - # Convert result to Hash initial_data = JSON.parse(body).as_h From a571eeaa381523f5efb29dea0f5fe097f4f1252c Mon Sep 17 00:00:00 2001 From: Emilien <4016501+unixfox@users.noreply.github.com> Date: Sun, 8 Dec 2024 22:22:08 +0100 Subject: [PATCH 18/20] format watch.cr --- src/invidious/routes/watch.cr | 1 - 1 file changed, 1 deletion(-) diff --git a/src/invidious/routes/watch.cr b/src/invidious/routes/watch.cr index e9a076f3..91aca03e 100644 --- a/src/invidious/routes/watch.cr +++ b/src/invidious/routes/watch.cr @@ -328,7 +328,6 @@ module Invidious::Routes::Watch return Invidious::Routes::API::V1::Videos.captions(env) elsif itag = download_widget["itag"]?.try &.as_i.to_s - # URL params specific to /latest_version env.params.query["id"] = video_id env.params.query["title"] = filename From ab72bbad7afb7d143883a7d0610145f68c06bac8 Mon Sep 17 00:00:00 2001 From: Emilien <4016501+unixfox@users.noreply.github.com> Date: Sun, 8 Dec 2024 22:24:57 +0100 Subject: [PATCH 19/20] fix ameba Redundant use of `Object#to_s` in interpolation --- src/invidious/routes/api/manifest.cr | 2 +- src/invidious/routes/video_playback.cr | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/invidious/routes/api/manifest.cr b/src/invidious/routes/api/manifest.cr index b6c5cbf3..b2085666 100644 --- a/src/invidious/routes/api/manifest.cr +++ b/src/invidious/routes/api/manifest.cr @@ -10,7 +10,7 @@ module Invidious::Routes::API::Manifest if !CONFIG.invidious_companion.empty? invidious_companion = CONFIG.invidious_companion.sample - return env.redirect "#{invidious_companion.public_url.to_s}/api/manifest/dash/id/#{id}?#{env.params.query}" + return env.redirect "#{invidious_companion.public_url}/api/manifest/dash/id/#{id}?#{env.params.query}" end # Since some implementations create playlists based on resolution regardless of different codecs, diff --git a/src/invidious/routes/video_playback.cr b/src/invidious/routes/video_playback.cr index bac79b5c..391aa139 100644 --- a/src/invidious/routes/video_playback.cr +++ b/src/invidious/routes/video_playback.cr @@ -255,7 +255,7 @@ module Invidious::Routes::VideoPlayback def self.latest_version(env) if !CONFIG.invidious_companion.empty? invidious_companion = CONFIG.invidious_companion.sample - return env.redirect "#{invidious_companion.public_url.to_s}/latest_version?#{env.params.query}" + return env.redirect "#{invidious_companion.public_url}/latest_version?#{env.params.query}" end id = env.params.query["id"]? From 1de20546182421e1280ec2b68c6d347abead7c54 Mon Sep 17 00:00:00 2001 From: Emilien <4016501+unixfox@users.noreply.github.com> Date: Fri, 13 Dec 2024 20:08:57 +0100 Subject: [PATCH 20/20] add ability for invidious companion to check request from invidious --- config/config.example.yml | 1 + src/invidious/config.cr | 3 +++ src/invidious/helpers/utils.cr | 19 +++++++++++++++++++ src/invidious/views/components/player.ecr | 9 ++++++--- 4 files changed, 29 insertions(+), 3 deletions(-) diff --git a/config/config.example.yml b/config/config.example.yml index 2273c839..44b7c806 100644 --- a/config/config.example.yml +++ b/config/config.example.yml @@ -83,6 +83,7 @@ db: ## ## API key for Invidious companion +## The size of the key needs to be more or equal to 16. ## ## Needed when invidious_companion is configured ## diff --git a/src/invidious/config.cr b/src/invidious/config.cr index 91622576..6508c847 100644 --- a/src/invidious/config.cr +++ b/src/invidious/config.cr @@ -250,6 +250,9 @@ class Config elsif config.invidious_companion_key == "CHANGE_ME!!" puts "Config: The value of 'invidious_companion_key' needs to be changed!!" exit(1) + elsif config.invidious_companion_key.size < 16 + puts "Config: The value of 'invidious_companion_key' needs to be a size of 16 or more." + exit(1) end end diff --git a/src/invidious/helpers/utils.cr b/src/invidious/helpers/utils.cr index 4d9bb28d..85462eb8 100644 --- a/src/invidious/helpers/utils.cr +++ b/src/invidious/helpers/utils.cr @@ -383,3 +383,22 @@ def parse_link_endpoint(endpoint : JSON::Any, text : String, video_id : String) end return text end + +def encrypt_ecb_without_salt(data, key) + cipher = OpenSSL::Cipher.new("aes-128-ecb") + cipher.encrypt + cipher.key = key + + io = IO::Memory.new + io.write(cipher.update(data)) + io.write(cipher.final) + io.rewind + + return io +end + +def invidious_companion_encrypt(data) + timestamp = Time.utc.to_unix + encrypted_data = encrypt_ecb_without_salt("#{timestamp}|#{data}", CONFIG.invidious_companion_key) + return Base64.urlsafe_encode(encrypted_data) +end diff --git a/src/invidious/views/components/player.ecr b/src/invidious/views/components/player.ecr index 975a25be..37f5f586 100644 --- a/src/invidious/views/components/player.ecr +++ b/src/invidious/views/components/player.ecr @@ -22,7 +22,8 @@ audio_streams.each_with_index do |fmt, i| src_url = "/latest_version?id=#{video.id}&itag=#{fmt["itag"]}" src_url += "&local=true" if params.local - src_url = video.invidious_companion["baseUrl"].as_s + src_url if (!CONFIG.invidious_companion.empty?) + src_url = video.invidious_companion["baseUrl"].as_s + src_url + + "&check=#{invidious_companion_encrypt(video.id)}" if (!CONFIG.invidious_companion.empty?) bitrate = fmt["bitrate"] mimetype = HTML.escape(fmt["mimeType"].as_s) @@ -37,7 +38,8 @@ <% else %> <% if params.quality == "dash" src_url = "/api/manifest/dash/id/" + video.id + "?local=true&unique_res=1" - src_url = video.invidious_companion["baseUrl"].as_s + src_url if (!CONFIG.invidious_companion.empty?) + src_url = video.invidious_companion["baseUrl"].as_s + src_url + + "&check=#{invidious_companion_encrypt(video.id)}" if (!CONFIG.invidious_companion.empty?) %> <% end %> @@ -48,7 +50,8 @@ fmt_stream.each_with_index do |fmt, i| src_url = "/latest_version?id=#{video.id}&itag=#{fmt["itag"]}" src_url += "&local=true" if params.local - src_url = video.invidious_companion["baseUrl"].as_s + src_url if (!CONFIG.invidious_companion.empty?) + src_url = video.invidious_companion["baseUrl"].as_s + src_url + + "&check=#{invidious_companion_encrypt(video.id)}" if (!CONFIG.invidious_companion.empty?) quality = fmt["quality"] mimetype = HTML.escape(fmt["mimeType"].as_s)