diff --git a/src/invidious.cr b/src/invidious.cr index b1f5264a..fa65f2dd 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -67,11 +67,11 @@ REDIS_DB = Redis::PooledClient.new(unixsocket: CONFIG.redis_socket || nil, url: if REDIS_DB.ping puts "Connected to redis" end -ARCHIVE_URL = URI.parse("https://archive.org") -PUBSUB_URL = URI.parse("https://pubsubhubbub.appspot.com") -REDDIT_URL = URI.parse("https://www.reddit.com") -YT_URL = URI.parse("https://www.youtube.com") -HOST_URL = make_host_url(Kemal.config) +ARCHIVE_URL = URI.parse("https://archive.org") +PUBSUB_URL = URI.parse("https://pubsubhubbub.appspot.com") +REDDIT_URL = URI.parse("https://www.reddit.com") +YT_URL = URI.parse("https://www.youtube.com") +HOST_URL = make_host_url(Kemal.config) EXT_VIDEOP_LIST = gen_videoplayback_proxy_list() CHARS_SAFE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_" @@ -189,7 +189,7 @@ Invidious::Jobs.register Invidious::Jobs::ClearExpiredItemsJob.new Invidious::Jobs.register Invidious::Jobs::InstanceListRefreshJob.new -if CONFIG.external_videoplayback_proxy +if !CONFIG.external_videoplayback_proxy.empty? Invidious::Jobs.register Invidious::Jobs::CheckExternalProxy.new end diff --git a/src/invidious/config.cr b/src/invidious/config.cr index b1e10684..2d11778d 100644 --- a/src/invidious/config.cr +++ b/src/invidious/config.cr @@ -180,7 +180,9 @@ class Config # of the backend property backends_delimiter : String = "|" - property external_videoplayback_proxy : String? + # External videoplayback proxies list. They should include `https://` + # at the start of the URI + property external_videoplayback_proxy : Array(String) = [] of String # Materialious redirects property materialious_domain : String? diff --git a/src/invidious/helpers/utils.cr b/src/invidious/helpers/utils.cr index d98c933b..3e4764c4 100644 --- a/src/invidious/helpers/utils.cr +++ b/src/invidious/helpers/utils.cr @@ -384,7 +384,7 @@ def parse_link_endpoint(endpoint : JSON::Any, text : String, video_id : String) return text end -# Generates a list of external videoplayback proxies for +# Generates a list of external videoplayback proxies for # CSP def gen_videoplayback_proxy_list if !CONFIG.external_videoplayback_proxy.empty? diff --git a/src/invidious/http_server/utils.cr b/src/invidious/http_server/utils.cr index a1348227..86494060 100644 --- a/src/invidious/http_server/utils.cr +++ b/src/invidious/http_server/utils.cr @@ -4,14 +4,20 @@ module Invidious::HttpServer module Utils extend self - @@proxy_alive : Bool = false + @@proxy_alive : String = "" def check_external_proxy - begin - response = HTTP::Client.get("#{CONFIG.external_videoplayback_proxy}") - @@proxy_alive = response.status_code == 200 - rescue - @@proxy_alive = false + CONFIG.external_videoplayback_proxy.each do |proxy| + begin + response = HTTP::Client.get(proxy) + if response.status_code == 200 + @@proxy_alive = proxy + LOGGER.debug("CheckExternalProxy: Proxy set to: '#{proxy}'") + break + end + rescue + LOGGER.debug("CheckExternalProxy: Proxy '#{proxy}' is not available") + end end end @@ -25,8 +31,8 @@ module Invidious::HttpServer url.query_params = params if absolute - if @@proxy_alive - return "#{CONFIG.external_videoplayback_proxy}#{url.request_target}" + if !@@proxy_alive.empty? + return "#{@@proxy_alive}#{url.request_target}" else return "#{HOST_URL}#{url.request_target}" end diff --git a/src/invidious/routes/before_all.cr b/src/invidious/routes/before_all.cr index 54c30fc5..41a2dd3c 100644 --- a/src/invidious/routes/before_all.cr +++ b/src/invidious/routes/before_all.cr @@ -28,12 +28,6 @@ module Invidious::Routes::BeforeAll extra_media_csp = "" end - if CONFIG.external_videoplayback_proxy - external_videoplayback_proxy = " #{CONFIG.external_videoplayback_proxy}" - else - external_videoplayback_proxy = "" - end - # Only allow the pages at /embed/* to be embedded if env.request.resource.starts_with?("/embed") frame_ancestors = "'self' file: http: https:" @@ -49,7 +43,7 @@ module Invidious::Routes::BeforeAll "style-src 'self' 'unsafe-inline'", "img-src 'self' data:", "font-src 'self' data:", - "connect-src 'self'" + external_videoplayback_proxy, + "connect-src 'self'" + EXT_VIDEOP_LIST, "manifest-src 'self'", "media-src 'self' blob:" + extra_media_csp, "child-src 'self' blob:",