apply all the suggestions + rework invidious_companion parameter

This commit is contained in:
Emilien 2024-12-08 22:21:10 +01:00
parent 7a070fa710
commit f710dd37bf
9 changed files with 48 additions and 51 deletions

View File

@ -59,16 +59,27 @@ db:
## for loading the video streams from YouTube servers. ## for loading the video streams from YouTube servers.
## ##
## When this setting is commented out, Invidious companion is not used. ## 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 ## The parameter private_url needs to be configured for the internal
## to Invidious companion. ## communication between the companion and Invidious.
## Or randomly choose one if multiple Invidious companion are configured. ## 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)://<IP-HOSTNAME>:<Port>" ## Accepted values: "http(s)://<IP-HOSTNAME>:<Port>"
## Default: <none> ## Default: <none>
## ##
#invidious_companion: #invidious_companion:
# - http://127.0.0.1:8282 # - private_url: "http://localhost:8282"
# public_url: "http://localhost:8282"
## ##
## API key for Invidious companion ## API key for Invidious companion

View File

@ -67,26 +67,14 @@ end
class Config class Config
include YAML::Serializable include YAML::Serializable
module URIArrayConverter class CompanionConfig
def self.to_yaml(values : Array(URI), yaml : YAML::Nodes::Builder) include YAML::Serializable
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) @[YAML::Field(converter: Preferences::URIConverter)]
if node.is_a?(YAML::Nodes::Sequence) property private_url : URI = URI.parse("")
node.map do |child|
unless child.is_a?(YAML::Nodes::Scalar)
node.raise "Expected scalar, not #{child.class}"
end
URI.parse(child.value) @[YAML::Field(converter: Preferences::URIConverter)]
end property public_url : URI = URI.parse("")
else
node.raise "Expected sequence, not #{node.class}"
end
end
end end
# Number of threads to use for crawling videos from channels (for updating subscriptions) # Number of threads to use for crawling videos from channels (for updating subscriptions)
@ -174,8 +162,7 @@ class Config
property po_token : String? = nil property po_token : String? = nil
# Invidious companion # Invidious companion
@[YAML::Field(converter: Config::URIArrayConverter)] property invidious_companion : Array(CompanionConfig) = [] of CompanionConfig
property invidious_companion : Array(URI) = [] of URI
# Invidious companion API key # Invidious companion API key
property invidious_companion_key : String = "" property invidious_companion_key : String = ""

View File

@ -1,10 +1,6 @@
module Invidious::Routes::API::Manifest module Invidious::Routes::API::Manifest
# /api/manifest/dash/id/:id # /api/manifest/dash/id/:id
def self.get_dash_video_id(env) 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.headers.add("Access-Control-Allow-Origin", "*")
env.response.content_type = "application/dash+xml" env.response.content_type = "application/dash+xml"
@ -12,6 +8,11 @@ module Invidious::Routes::API::Manifest
id = env.params.url["id"] id = env.params.url["id"]
region = env.params.query["region"]? 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, # 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 # 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 } unique_res = env.params.query["unique_res"]?.try { |q| (q == "true" || q == "1").to_unsafe }

View File

@ -201,11 +201,11 @@ module Invidious::Routes::Embed
return env.redirect url return env.redirect url
end 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"] =
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("media-src", "media-src #{companion_base_url}")
.gsub("connect-src", "connect-src " + video.invidious_companion.not_nil!["baseUrl"].as_s) .gsub("connect-src", "connect-src #{companion_base_url}")
end end
rendered "embed" rendered "embed"

View File

@ -254,7 +254,8 @@ module Invidious::Routes::VideoPlayback
# so we have a mechanism here to redirect to the latest version # so we have a mechanism here to redirect to the latest version
def self.latest_version(env) def self.latest_version(env)
if !CONFIG.invidious_companion.empty? 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 end
id = env.params.query["id"]? id = env.params.query["id"]?

View File

@ -190,11 +190,11 @@ module Invidious::Routes::Watch
captions: video.captions 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"] =
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("media-src", "media-src #{companion_base_url}")
.gsub("connect-src", "connect-src " + video.invidious_companion.not_nil!["baseUrl"].as_s) .gsub("connect-src", "connect-src #{companion_base_url}")
end end
templated "watch" templated "watch"
@ -327,18 +327,16 @@ module Invidious::Routes::Watch
env.params.query["label"] = URI.decode_www_form(label.as_s) env.params.query["label"] = URI.decode_www_form(label.as_s)
return Invidious::Routes::API::V1::Videos.captions(env) return Invidious::Routes::API::V1::Videos.captions(env)
elsif itag = download_widget["itag"]?.try &.as_i elsif itag = download_widget["itag"]?.try &.as_i.to_s
itag = itag.to_s
# URL params specific to /latest_version # URL params specific to /latest_version
env.params.query["id"] = video_id env.params.query["id"] = video_id
env.params.query["itag"] = itag
env.params.query["title"] = filename env.params.query["title"] = filename
env.params.query["local"] = "true" env.params.query["local"] = "true"
if (!CONFIG.invidious_companion.empty?) if (!CONFIG.invidious_companion.empty?)
video = get_video(video_id) 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 else
return Invidious::Routes::VideoPlayback.latest_version(env) return Invidious::Routes::VideoPlayback.latest_version(env)
end end

View File

@ -193,7 +193,7 @@ struct Video
end end
def invidious_companion : Hash(String, JSON::Any)? def invidious_companion : Hash(String, JSON::Any)?
info["invidiousCompanion"]?.try &.as_h info["invidiousCompanion"]?.try &.as_h || {} of String => JSON::Any
end end
# Macros defining getters/setters for various types of data # Macros defining getters/setters for various types of data

View File

@ -22,7 +22,7 @@
audio_streams.each_with_index do |fmt, i| audio_streams.each_with_index do |fmt, i|
src_url = "/latest_version?id=#{video.id}&itag=#{fmt["itag"]}" src_url = "/latest_version?id=#{video.id}&itag=#{fmt["itag"]}"
src_url += "&local=true" if params.local 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"] bitrate = fmt["bitrate"]
mimetype = HTML.escape(fmt["mimeType"].as_s) mimetype = HTML.escape(fmt["mimeType"].as_s)
@ -37,7 +37,7 @@
<% else %> <% else %>
<% if params.quality == "dash" <% if params.quality == "dash"
src_url = "/api/manifest/dash/id/" + video.id + "?local=true&unique_res=1" 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?)
%> %>
<source src="<%= src_url %>" type='application/dash+xml' label="dash"> <source src="<%= src_url %>" type='application/dash+xml' label="dash">
<% end %> <% end %>
@ -48,7 +48,7 @@
fmt_stream.each_with_index do |fmt, i| fmt_stream.each_with_index do |fmt, i|
src_url = "/latest_version?id=#{video.id}&itag=#{fmt["itag"]}" src_url = "/latest_version?id=#{video.id}&itag=#{fmt["itag"]}"
src_url += "&local=true" if params.local 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"] quality = fmt["quality"]
mimetype = HTML.escape(fmt["mimeType"].as_s) mimetype = HTML.escape(fmt["mimeType"].as_s)

View File

@ -685,7 +685,7 @@ module YoutubeAPI
) : Hash(String, JSON::Any) ) : Hash(String, JSON::Any)
headers = HTTP::Headers{ headers = HTTP::Headers{
"Content-Type" => "application/json; charset=UTF-8", "Content-Type" => "application/json; charset=UTF-8",
"Authorization" => "Bearer " + CONFIG.invidious_companion_key, "Authorization" => "Bearer #{CONFIG.invidious_companion_key}",
} }
# Logging # Logging
@ -695,21 +695,20 @@ module YoutubeAPI
# Send the POST request # Send the POST request
begin 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)) &.post(endpoint, headers: headers, body: data.to_json))
body = response.body body = response.body
if (response.status_code != 200) if (response.status_code != 200)
raise Exception.new("Error while communicating with Invidious companion: \ raise Exception.new(
status code: " + response.status_code.to_s + " and body: " + body) "Error while communicating with Invidious companion: \
status code: #{response.status_code} and body: #{body.dump}"
)
end end
rescue ex rescue ex
raise InfoException.new("Error while communicating with Invidious companion: " + (ex.message || "no extra info found")) raise InfoException.new("Error while communicating with Invidious companion: " + (ex.message || "no extra info found"))
end end
if body.nil?
raise InfoException.new("Error while communicating with Invidious companion: no response data.")
end
# Convert result to Hash # Convert result to Hash
initial_data = JSON.parse(body).as_h initial_data = JSON.parse(body).as_h