mirror of
https://github.com/iv-org/invidious.git
synced 2025-06-28 01:28:30 +00:00
initial support for base_url with invidious companion + proxy invidious_companion
This commit is contained in:
parent
df8839d1f0
commit
fd2404b85c
@ -75,7 +75,7 @@ db:
|
|||||||
## If you are using a reverse proxy then you will probably need to
|
## If you are using a reverse proxy then you will probably need to
|
||||||
## configure the public_url to be the same as the domain used for Invidious.
|
## configure the public_url to be the same as the domain used for Invidious.
|
||||||
## Also apply when used from an external IP address (without a domain).
|
## Also apply when used from an external IP address (without a domain).
|
||||||
## Examples: https://MYINVIDIOUSDOMAIN or http://192.168.1.100:8282
|
## Examples: https://MYINVIDIOUSDOMAIN/companion or http://192.168.1.100:8282/companion
|
||||||
##
|
##
|
||||||
## Both parameter can have identical URL when Invidious is hosted in
|
## Both parameter can have identical URL when Invidious is hosted in
|
||||||
## an internal network or at home or locally (localhost).
|
## an internal network or at home or locally (localhost).
|
||||||
@ -84,8 +84,8 @@ db:
|
|||||||
## Default: <none>
|
## Default: <none>
|
||||||
##
|
##
|
||||||
#invidious_companion:
|
#invidious_companion:
|
||||||
# - private_url: "http://localhost:8282"
|
# - private_url: "http://localhost:8282/companion"
|
||||||
# public_url: "http://localhost:8282"
|
# public_url: "http://localhost:8282/companion"
|
||||||
|
|
||||||
##
|
##
|
||||||
## API key for Invidious companion, used for securing the communication
|
## API key for Invidious companion, used for securing the communication
|
||||||
|
37
src/invidious/routes/companion.cr
Normal file
37
src/invidious/routes/companion.cr
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
module Invidious::Routes::Companion
|
||||||
|
# /companion
|
||||||
|
def self.get_companion(env)
|
||||||
|
url = env.request.path.lchop("/companion")
|
||||||
|
|
||||||
|
begin
|
||||||
|
COMPANION_POOL.client &.get(url, env.request.header) do |resp|
|
||||||
|
return self.proxy_companion(env, resp)
|
||||||
|
end
|
||||||
|
rescue ex
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.options_companion(env)
|
||||||
|
url = env.request.path.lchop("/companion")
|
||||||
|
|
||||||
|
begin
|
||||||
|
COMPANION_POOL.client &.options(url, env.request.header) do |resp|
|
||||||
|
return self.proxy_companion(env, resp)
|
||||||
|
end
|
||||||
|
rescue ex
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private def self.proxy_companion(env, response)
|
||||||
|
env.response.status_code = response.status_code
|
||||||
|
response.headers.each do |key, value|
|
||||||
|
env.response.headers[key] = value
|
||||||
|
end
|
||||||
|
|
||||||
|
if response.status_code >= 300
|
||||||
|
return env.response.headers.delete("Transfer-Encoding")
|
||||||
|
end
|
||||||
|
|
||||||
|
return proxy_file(response, env)
|
||||||
|
end
|
||||||
|
end
|
@ -209,10 +209,14 @@ module Invidious::Routes::Embed
|
|||||||
|
|
||||||
if CONFIG.invidious_companion.present?
|
if CONFIG.invidious_companion.present?
|
||||||
invidious_companion = CONFIG.invidious_companion.sample
|
invidious_companion = CONFIG.invidious_companion.sample
|
||||||
|
invidious_companion_urls = CONFIG.invidious_companion.map do |companion|
|
||||||
|
uri =
|
||||||
|
"#{companion.public_url.scheme}://#{companion.public_url.host}#{companion.public_url.port ? ":#{companion.public_url.port}" : ""}"
|
||||||
|
end.join(" ")
|
||||||
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 #{invidious_companion.public_url}")
|
.gsub("media-src", "media-src #{invidious_companion_urls}")
|
||||||
.gsub("connect-src", "connect-src #{invidious_companion.public_url}")
|
.gsub("connect-src", "connect-src #{invidious_companion_urls}")
|
||||||
end
|
end
|
||||||
|
|
||||||
rendered "embed"
|
rendered "embed"
|
||||||
|
@ -194,10 +194,14 @@ module Invidious::Routes::Watch
|
|||||||
|
|
||||||
if CONFIG.invidious_companion.present?
|
if CONFIG.invidious_companion.present?
|
||||||
invidious_companion = CONFIG.invidious_companion.sample
|
invidious_companion = CONFIG.invidious_companion.sample
|
||||||
|
invidious_companion_urls = CONFIG.invidious_companion.map do |companion|
|
||||||
|
uri =
|
||||||
|
"#{companion.public_url.scheme}://#{companion.public_url.host}#{companion.public_url.port ? ":#{companion.public_url.port}" : ""}"
|
||||||
|
end.join(" ")
|
||||||
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 #{invidious_companion.public_url}")
|
.gsub("media-src", "media-src #{invidious_companion_urls}")
|
||||||
.gsub("connect-src", "connect-src #{invidious_companion.public_url}")
|
.gsub("connect-src", "connect-src #{invidious_companion_urls}")
|
||||||
end
|
end
|
||||||
|
|
||||||
templated "watch"
|
templated "watch"
|
||||||
|
@ -188,7 +188,7 @@ module Invidious::Routing
|
|||||||
end
|
end
|
||||||
|
|
||||||
# -------------------
|
# -------------------
|
||||||
# Media proxy routes
|
# Proxy routes
|
||||||
# -------------------
|
# -------------------
|
||||||
|
|
||||||
def register_api_manifest_routes
|
def register_api_manifest_routes
|
||||||
@ -223,6 +223,13 @@ module Invidious::Routing
|
|||||||
get "/vi/:id/:name", Routes::Images, :thumbnails
|
get "/vi/:id/:name", Routes::Images, :thumbnails
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def register_companion_routes
|
||||||
|
if CONFIG.invidious_companion.present?
|
||||||
|
get "/companion/*", Routes::Companion, :get_companion
|
||||||
|
options "/companion/*", Routes::Companion, :options_companion
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# -------------------
|
# -------------------
|
||||||
# API routes
|
# API routes
|
||||||
# -------------------
|
# -------------------
|
||||||
|
@ -46,8 +46,22 @@ struct YoutubeConnectionPool
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class CompanionWrapper
|
||||||
|
property client : HTTP::Client
|
||||||
|
property companion : Config::CompanionConfig
|
||||||
|
|
||||||
|
def initialize(companion : Config::CompanionConfig)
|
||||||
|
@companion = companion
|
||||||
|
@client = HTTP::Client.new(companion.private_url)
|
||||||
|
end
|
||||||
|
|
||||||
|
def close
|
||||||
|
@client.close
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
struct CompanionConnectionPool
|
struct CompanionConnectionPool
|
||||||
property pool : DB::Pool(HTTP::Client)
|
property pool : DB::Pool(CompanionWrapper)
|
||||||
|
|
||||||
def initialize(capacity = 5, timeout = 5.0)
|
def initialize(capacity = 5, timeout = 5.0)
|
||||||
options = DB::Pool::Options.new(
|
options = DB::Pool::Options.new(
|
||||||
@ -57,26 +71,28 @@ struct CompanionConnectionPool
|
|||||||
checkout_timeout: timeout
|
checkout_timeout: timeout
|
||||||
)
|
)
|
||||||
|
|
||||||
@pool = DB::Pool(HTTP::Client).new(options) do
|
@pool = DB::Pool(CompanionWrapper).new(options) do
|
||||||
companion = CONFIG.invidious_companion.sample
|
companion = CONFIG.invidious_companion.sample
|
||||||
next make_client(companion.private_url, use_http_proxy: false)
|
client = make_client(companion.private_url, use_http_proxy: false)
|
||||||
|
CompanionWrapper.new(companion: companion)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def client(&)
|
def client(&)
|
||||||
conn = pool.checkout
|
wrapper = pool.checkout
|
||||||
|
|
||||||
begin
|
begin
|
||||||
response = yield conn
|
response = yield wrapper
|
||||||
rescue ex
|
rescue ex
|
||||||
conn.close
|
wrapper.client.close
|
||||||
|
|
||||||
companion = CONFIG.invidious_companion.sample
|
companion = CONFIG.invidious_companion.sample
|
||||||
conn = make_client(companion.private_url, use_http_proxy: false)
|
client = make_client(companion.private_url, use_http_proxy: false)
|
||||||
|
wrapper = CompanionWrapper.new(companion: companion)
|
||||||
|
|
||||||
response = yield conn
|
response = yield wrapper
|
||||||
ensure
|
ensure
|
||||||
pool.release(conn)
|
pool.release(wrapper)
|
||||||
end
|
end
|
||||||
|
|
||||||
response
|
response
|
||||||
|
@ -695,22 +695,28 @@ module YoutubeAPI
|
|||||||
# Send the POST request
|
# Send the POST request
|
||||||
|
|
||||||
begin
|
begin
|
||||||
response = COMPANION_POOL.client &.post(endpoint, headers: headers, body: data.to_json)
|
response_body = ""
|
||||||
body = response.body
|
|
||||||
if (response.status_code != 200)
|
COMPANION_POOL.client do |wrapper|
|
||||||
|
companion_base_url = wrapper.companion.private_url.path
|
||||||
|
puts "Using companion: #{wrapper.companion.private_url}"
|
||||||
|
|
||||||
|
response = wrapper.client.post(companion_base_url + endpoint, headers: headers, body: data.to_json)
|
||||||
|
response_body = response.body
|
||||||
|
|
||||||
|
if response.status_code != 200
|
||||||
raise Exception.new(
|
raise Exception.new(
|
||||||
"Error while communicating with Invidious companion: \
|
"Error while communicating with Invidious companion: " \
|
||||||
status code: #{response.status_code} and body: #{body.dump}"
|
"status code: #{response.status_code} and body: #{response_body.dump}"
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
rescue ex
|
|
||||||
raise InfoException.new("Error while communicating with Invidious companion: " + (ex.message || "no extra info found"))
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Convert result to Hash
|
# Convert result to Hash
|
||||||
initial_data = JSON.parse(body).as_h
|
return JSON.parse(response_body).as_h
|
||||||
|
rescue ex
|
||||||
return initial_data
|
raise InfoException.new("Error while communicating with Invidious companion: " + (ex.message || "no extra info found"))
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
####################################################################
|
####################################################################
|
||||||
|
Loading…
Reference in New Issue
Block a user