add support for i2p backends and onion numbered backends
Some checks failed
Invidious CI / build (push) Failing after 22s

This commit is contained in:
Fijxu 2025-04-05 20:24:01 -03:00
parent 470fee2777
commit 8a644bf916
Signed by: Fijxu
GPG key ID: 32C1DDF333EDA6A4
8 changed files with 65 additions and 44 deletions

View file

@ -83,8 +83,11 @@ class Config
@[YAML::Field(converter: Preferences::URIConverter)] @[YAML::Field(converter: Preferences::URIConverter)]
property public_url : URI = URI.parse("") property public_url : URI = URI.parse("")
@[YAML::Field(converter: Preferences::URIConverter)]
property i2p_public_url : URI = URI.parse("")
property note : String = "" property note : String = ""
property domain : String = "" property domain : Array(String) = [] of String
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)

View file

@ -16,7 +16,7 @@ module BackendInfo
response = HTTP::Client.get "#{companion.private_url}/healthz" response = HTTP::Client.get "#{companion.private_url}/healthz"
if response.status_code == 200 if response.status_code == 200
check_videoplayback_proxy(companion, index) check_videoplayback_proxy(companion, index)
generate_csp(companion.public_url.to_s, @@exvpp_url[index], index) generate_csp([companion.public_url.to_s, companion.i2p_public_url.to_s], @@exvpp_url[index], index)
else else
@@status[index] = 0 @@status[index] = 0
end end
@ -31,31 +31,38 @@ module BackendInfo
begin begin
info = HTTP::Client.get "#{companion.private_url}/info" info = HTTP::Client.get "#{companion.private_url}/info"
exvpp_url = JSON.parse(info.body)["external_videoplayback_proxy"]?.try &.to_s exvpp_url = JSON.parse(info.body)["external_videoplayback_proxy"]?.try &.to_s
exvpp_url = "" if exvpp_url.nil? rescue JSON::ParseException
@@exvpp_url[index] = exvpp_url @@status[index] = 2
if exvpp_url.empty? return
@@status[index] = 2 end
return
else exvpp_url = "" if exvpp_url.nil?
begin @@exvpp_url[index] = exvpp_url
exvpp_health = HTTP::Client.get "#{exvpp_url}/health" if exvpp_url.empty?
if exvpp_health.status_code == 200 @@status[index] = 2
@@status[index] = 2 return
return exvpp_url else
else begin
@@status[index] = 1 exvpp_health = HTTP::Client.get "#{exvpp_url}/health"
end if exvpp_health.status_code == 200
rescue @@status[index] = 2
return exvpp_url
else
@@status[index] = 1 @@status[index] = 1
end end
rescue
@@status[index] = 1
end end
rescue
end end
end end
private def generate_csp(companion_url : String, exvpp_url : String, index : Int32) private def generate_csp(companion_url : Array(String), exvpp_url : String? = nil, index : Int32? = nil)
@@mutex.synchronize do @@mutex.synchronize do
@@csp[index] = " #{companion_url} #{exvpp_url}" @@csp[index] = ""
companion_url.each do |url|
@@csp[index] += " #{url}"
end
@@csp[index] += " #{exvpp_url}"
end end
end end

View file

@ -9,9 +9,8 @@ module Invidious::Routes::API::Manifest
region = env.params.query["region"]? region = env.params.query["region"]?
if CONFIG.invidious_companion.present? if CONFIG.invidious_companion.present?
current_companion = env.get("current_companion").as(Int32) companion_public_url = env.get("companion_public_url").as(String)
invidious_companion = CONFIG.invidious_companion[current_companion] return env.redirect "#{companion_public_url}/api/manifest/dash/id/#{id}?#{env.params.query}"
return env.redirect "#{invidious_companion.public_url}/api/manifest/dash/id/#{id}?#{env.params.query}"
end 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,

View file

@ -25,11 +25,22 @@ module Invidious::Routes::BeforeAll
if CONFIG.invidious_companion.present? if CONFIG.invidious_companion.present?
CONFIG.invidious_companion.each_with_index do |companion, index| CONFIG.invidious_companion.each_with_index do |companion, index|
if companion.domain == env.request.headers["Host"] puts "inv-companion " + index.to_s
env.set "current_companion", index if companion.domain.each_with_index do |domain, domain_index|
env.set "domain", true puts "domain " + domain_index.to_s
break if domain == env.request.headers["Host"]
env.set "current_companion", index
env.set "companion_public_url", companion.public_url.to_s
env.set "domain_index", domain_index
if domain_index == 2
env.set "companion_public_url", companion.i2p_public_url.to_s
end
puts "exit_inner"
break
end
end
end end
break if env.get?("current_companion")
end end
if env.get?("current_companion").try &.as(Int32) == nil if env.get?("current_companion").try &.as(Int32) == nil
@ -49,6 +60,7 @@ module Invidious::Routes::BeforeAll
end end
env.set "current_companion", current_companion env.set "current_companion", current_companion
env.set "companion_public_url", CONFIG.invidious_companion[current_companion].public_url.to_s
end end
extra_media_csp, extra_connect_csp = BackendInfo.get_csp(env.get("current_companion").as(Int32)) extra_media_csp, extra_connect_csp = BackendInfo.get_csp(env.get("current_companion").as(Int32))

View file

@ -267,9 +267,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.present? if CONFIG.invidious_companion.present?
current_companion = env.get("current_companion").as(Int32) companion_public_url = env.get("companion_public_url").as(String)
invidious_companion = CONFIG.invidious_companion[current_companion] return env.redirect "#{companion_public_url}/latest_version?#{env.params.query}"
return env.redirect "#{invidious_companion.public_url}/latest_version?#{env.params.query}"
end end
id = env.params.query["id"]? id = env.params.query["id"]?

View file

@ -348,9 +348,8 @@ module Invidious::Routes::Watch
if (CONFIG.invidious_companion.present?) if (CONFIG.invidious_companion.present?)
video = get_video(video_id, env: env) video = get_video(video_id, env: env)
current_companion = env.get("current_companion").as(Int32) companion_public_url = env.get("companion_public_url").as(String)
invidious_companion = CONFIG.invidious_companion[current_companion] return env.redirect "#{companion_public_url}/latest_version?#{env.params.query}"
return env.redirect "#{invidious_companion.public_url}/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

@ -22,7 +22,8 @@
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 = invidious_companion.public_url.to_s + src_url + companion_public_url = env.get("companion_public_url").as(String)
src_url = companion_public_url + src_url +
"&check=#{invidious_companion_encrypt(video.id)}" if (invidious_companion) "&check=#{invidious_companion_encrypt(video.id)}" if (invidious_companion)
bitrate = fmt["bitrate"] bitrate = fmt["bitrate"]
@ -38,7 +39,8 @@
<% 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 = invidious_companion.public_url.to_s + src_url + companion_public_url = env.get("companion_public_url").as(String)
src_url = companion_public_url + src_url +
"&check=#{invidious_companion_encrypt(video.id)}" if (invidious_companion) "&check=#{invidious_companion_encrypt(video.id)}" if (invidious_companion)
%> %>
<source src="<%= src_url %>" type='application/dash+xml' label="dash"> <source src="<%= src_url %>" type='application/dash+xml' label="dash">
@ -50,7 +52,8 @@
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 = invidious_companion.public_url.to_s + src_url + companion_public_url = env.get("companion_public_url").as(String)
src_url = companion_public_url + src_url +
"&check=#{invidious_companion_encrypt(video.id)}" if (invidious_companion) "&check=#{invidious_companion_encrypt(video.id)}" if (invidious_companion)
quality = fmt["quality"] quality = fmt["quality"]

View file

@ -109,16 +109,16 @@
<% <%
if CONFIG.invidious_companion.present? if CONFIG.invidious_companion.present?
current_backend = env.get?("current_companion").try &.as(Int32) current_backend = env.get?("current_companion").try &.as(Int32)
domain = env.get?("domain").try &.as(Bool) domain_index = env.get?("domain_index").try &.as(Int32)
scheme = env.get("scheme") scheme = env.get("scheme")
status = BackendInfo.get_status status = BackendInfo.get_status
%> %>
<div class="h-box" style="margin-bottom: 10px;"> <div class="h-box" style="margin-bottom: 10px;">
<b>Switch Backend:</b> <b>Switch Backend:</b>
<% if domain %> <% if domain_index %>
<% CONFIG.invidious_companion.each_with_index do | companion, index | %> <% CONFIG.invidious_companion.each_with_index do | companion, index | %>
<% is_current_backend_host = companion.domain == env.request.headers["Host"] %> <% is_current_backend_host = companion.domain[domain_index] == env.request.headers["Host"] %>
<a href="<%= scheme %>://<%= companion.domain %><%= env.request.resource %>" style="<%= is_current_backend_host ? "text-decoration-line: underline;" : "" %> display: inline-block;"> <a href="<%= scheme %>://<%= companion.domain[domain_index] %><%= env.request.resource %>" style="<%= is_current_backend_host ? "text-decoration-line: underline;" : "" %> display: inline-block;">
Backend<%= HTML.escape((index + 1).to_s) %> <%= HTML.escape(companion.note) %> Backend<%= HTML.escape((index + 1).to_s) %> <%= HTML.escape(companion.note) %>
<span style="color: <span style="color:
<% if status[index] == 0 %> #fd4848; <% end %> <% if status[index] == 0 %> #fd4848; <% end %>
@ -132,8 +132,7 @@
<% end %> <% end %>
<% else %> <% else %>
<% CONFIG.invidious_companion.each_with_index do | companion, index | %> <% CONFIG.invidious_companion.each_with_index do | companion, index | %>
<% is_current_backend_index = current_backend == index %> <a href="/switchbackend?backend_id=<%= index.to_s %>" style="<%= current_backend == index ? "text-decoration-line: underline;" : "" %> display: inline-block;">
<a href="/switchbackend?backend_id=<%= index.to_s %>" style="<%= is_current_backend_index ? "text-decoration-line: underline;" : "" %> display: inline-block;">
Backend<%= HTML.escape((index + 1).to_s) %> <%= HTML.escape(companion.note) %> Backend<%= HTML.escape((index + 1).to_s) %> <%= HTML.escape(companion.note) %>
<span style="color: <span style="color:
<% if status[index] == 0 %> #fd4848; <% end %> <% if status[index] == 0 %> #fd4848; <% end %>
@ -332,9 +331,9 @@
<div class="footer-footer"> <div class="footer-footer">
<% <%
if CONFIG.invidious_companion.present? if CONFIG.invidious_companion.present?
current_backend = env.get?("current_companion").try &.as(Int32) companion_public_url = env.get("companion_public_url").as(String)
%> %>
<div class="box">You are currently using Backend: <%= current_backend ? CONFIG.invidious_companion[current_backend].public_url : "Unable to get backend, this is bug, please report it!" %></div> <div class="box">You are currently using Backend: <%= current_backend ? companion_public_url : "Unable to get backend, this is bug, please report it!" %></div>
<% end %> <% end %>
<span class="left"> <span class="left">
<% if CONFIG.modified_source_code_url %> <% if CONFIG.modified_source_code_url %>