mirror of
https://github.com/iv-org/invidious.git
synced 2025-06-28 01:28:30 +00:00
chore: add the suggestions
This commit is contained in:
parent
fd2404b85c
commit
a1d61e05ce
@ -63,6 +63,7 @@ module Invidious::Routes::BeforeAll
|
|||||||
"/videoplayback",
|
"/videoplayback",
|
||||||
"/latest_version",
|
"/latest_version",
|
||||||
"/download",
|
"/download",
|
||||||
|
"/companion/",
|
||||||
}.any? { |r| env.request.resource.starts_with? r }
|
}.any? { |r| env.request.resource.starts_with? r }
|
||||||
|
|
||||||
if env.request.cookies.has_key? "SID"
|
if env.request.cookies.has_key? "SID"
|
||||||
|
@ -28,10 +28,6 @@ module Invidious::Routes::Companion
|
|||||||
env.response.headers[key] = value
|
env.response.headers[key] = value
|
||||||
end
|
end
|
||||||
|
|
||||||
if response.status_code >= 300
|
return IO.copy response.body_io, env.response
|
||||||
return env.response.headers.delete("Transfer-Encoding")
|
|
||||||
end
|
|
||||||
|
|
||||||
return proxy_file(response, env)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -46,13 +46,18 @@ struct YoutubeConnectionPool
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class CompanionWrapper
|
# Packages a `HTTP::Client` to an Invidious companion instance alongside the configuration for that instance.
|
||||||
|
#
|
||||||
|
# This is used as the resource for the `CompanionPool` as to allow the ability to
|
||||||
|
# proxy the requests to Invidious companion from Invidious directly.
|
||||||
|
# Instead of setting up routes in a reverse proxy.
|
||||||
|
struct CompanionWrapper
|
||||||
property client : HTTP::Client
|
property client : HTTP::Client
|
||||||
property companion : Config::CompanionConfig
|
property companion : Config::CompanionConfig
|
||||||
|
|
||||||
def initialize(companion : Config::CompanionConfig)
|
def initialize(companion : Config::CompanionConfig)
|
||||||
@companion = companion
|
@companion = companion
|
||||||
@client = HTTP::Client.new(companion.private_url)
|
@client = make_client(companion.private_url, use_http_proxy: false)
|
||||||
end
|
end
|
||||||
|
|
||||||
def close
|
def close
|
||||||
@ -73,7 +78,7 @@ struct CompanionConnectionPool
|
|||||||
|
|
||||||
@pool = DB::Pool(CompanionWrapper).new(options) do
|
@pool = DB::Pool(CompanionWrapper).new(options) do
|
||||||
companion = CONFIG.invidious_companion.sample
|
companion = CONFIG.invidious_companion.sample
|
||||||
client = make_client(companion.private_url, use_http_proxy: false)
|
make_client(companion.private_url, use_http_proxy: false)
|
||||||
CompanionWrapper.new(companion: companion)
|
CompanionWrapper.new(companion: companion)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -84,10 +89,10 @@ struct CompanionConnectionPool
|
|||||||
begin
|
begin
|
||||||
response = yield wrapper
|
response = yield wrapper
|
||||||
rescue ex
|
rescue ex
|
||||||
wrapper.client.close
|
wrapper.close
|
||||||
|
|
||||||
companion = CONFIG.invidious_companion.sample
|
companion = CONFIG.invidious_companion.sample
|
||||||
client = make_client(companion.private_url, use_http_proxy: false)
|
make_client(companion.private_url, use_http_proxy: false)
|
||||||
wrapper = CompanionWrapper.new(companion: companion)
|
wrapper = CompanionWrapper.new(companion: companion)
|
||||||
|
|
||||||
response = yield wrapper
|
response = yield wrapper
|
||||||
|
@ -695,25 +695,17 @@ module YoutubeAPI
|
|||||||
# Send the POST request
|
# Send the POST request
|
||||||
|
|
||||||
begin
|
begin
|
||||||
response_body = ""
|
response_body = Hash(String, JSON::Any).new
|
||||||
|
|
||||||
COMPANION_POOL.client do |wrapper|
|
COMPANION_POOL.client do |wrapper|
|
||||||
companion_base_url = wrapper.companion.private_url.path
|
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)
|
wrapper.client.post("#{companion_base_url}#{endpoint}", headers: headers, body: data.to_json) do | response |
|
||||||
response_body = response.body
|
response_body = JSON.parse(response.body_io).as_h
|
||||||
|
|
||||||
if response.status_code != 200
|
|
||||||
raise Exception.new(
|
|
||||||
"Error while communicating with Invidious companion: " \
|
|
||||||
"status code: #{response.status_code} and body: #{response_body.dump}"
|
|
||||||
)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Convert result to Hash
|
return response_body
|
||||||
return JSON.parse(response_body).as_h
|
|
||||||
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
|
||||||
|
Loading…
Reference in New Issue
Block a user