mirror of
https://gitea.invidious.io/iv-org/invidious-copy-2023-06-08.git
synced 2024-08-15 00:53:38 +00:00
Proxy: pass range as URL parameter to avoid throttling
This commit is contained in:
parent
e62d61abc1
commit
3474744f57
3 changed files with 86 additions and 33 deletions
|
@ -43,6 +43,14 @@ embed_url = location.origin + '/embed/' + video_data.id + embed_url.search;
|
||||||
var save_player_pos_key = 'save_player_pos';
|
var save_player_pos_key = 'save_player_pos';
|
||||||
|
|
||||||
videojs.Vhs.xhr.beforeRequest = function(options) {
|
videojs.Vhs.xhr.beforeRequest = function(options) {
|
||||||
|
// Pass range as an URL parameter, not as a header
|
||||||
|
if (options.headers){
|
||||||
|
if (options.headers.Range) {
|
||||||
|
options.uri = options.uri + '&range=' + options.headers.Range.split("=")[1];
|
||||||
|
delete options.headers.Range;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// set local if requested not videoplayback
|
// set local if requested not videoplayback
|
||||||
if (!options.uri.includes('videoplayback')) {
|
if (!options.uri.includes('videoplayback')) {
|
||||||
if (!options.uri.includes('local=true'))
|
if (!options.uri.includes('local=true'))
|
||||||
|
|
|
@ -8,13 +8,17 @@ module Invidious::Routes::VideoPlayback
|
||||||
mns = query_params["mn"]?.try &.split(",")
|
mns = query_params["mn"]?.try &.split(",")
|
||||||
mns ||= [] of String
|
mns ||= [] of String
|
||||||
|
|
||||||
if query_params["region"]?
|
# Extract some invidious-specific parameters
|
||||||
region = query_params["region"]
|
|
||||||
|
if region = query_params["region"]?
|
||||||
query_params.delete("region")
|
query_params.delete("region")
|
||||||
end
|
end
|
||||||
|
|
||||||
if query_params["host"]? && !query_params["host"].empty?
|
if title = query_params["title"]?
|
||||||
host = query_params["host"]
|
query_params.delete("title")
|
||||||
|
end
|
||||||
|
|
||||||
|
if host = query_params["host"]?
|
||||||
query_params.delete("host")
|
query_params.delete("host")
|
||||||
else
|
else
|
||||||
host = "r#{fvip}---#{mns.pop}.googlevideo.com"
|
host = "r#{fvip}---#{mns.pop}.googlevideo.com"
|
||||||
|
@ -25,17 +29,34 @@ module Invidious::Routes::VideoPlayback
|
||||||
return error_template(400, "Invalid \"host\" parameter.")
|
return error_template(400, "Invalid \"host\" parameter.")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Range manipulation
|
||||||
|
|
||||||
|
has_range_param = false
|
||||||
|
has_range_header = false
|
||||||
|
|
||||||
|
if range = query_params["range"]?
|
||||||
|
query_params.delete("range")
|
||||||
|
has_range_param = true
|
||||||
|
end
|
||||||
|
|
||||||
|
if range_header = env.request.headers["Range"]?
|
||||||
|
env.request.headers.delete("Range")
|
||||||
|
range ||= range_header.split('=')[1]
|
||||||
|
has_range_header = true if !has_range_param
|
||||||
|
end
|
||||||
|
|
||||||
|
# Skip redirections
|
||||||
|
|
||||||
host = "https://#{host}"
|
host = "https://#{host}"
|
||||||
url = "/videoplayback?#{query_params}"
|
url = "/videoplayback?#{query_params}"
|
||||||
|
|
||||||
headers = HTTP::Headers.new
|
headers = HTTP::Headers.new
|
||||||
MediaProxy.copy_request_headers(from: env.request.headers, to: headers)
|
MediaProxy.copy_request_headers(from: env.request.headers, to: headers)
|
||||||
|
|
||||||
# See: https://github.com/iv-org/invidious/issues/3302
|
if has_range_param
|
||||||
range_header = env.request.headers["Range"]?
|
url += "&range=#{range}"
|
||||||
if range_header.nil?
|
else
|
||||||
range_for_head = query_params["range"]? || "0-640"
|
headers["Range"] = "bytes=#{range || "0-"}"
|
||||||
headers["Range"] = "bytes=#{range_for_head}"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
client = make_client(URI.parse(host), region)
|
client = make_client(URI.parse(host), region)
|
||||||
|
@ -74,7 +95,7 @@ module Invidious::Routes::VideoPlayback
|
||||||
end
|
end
|
||||||
|
|
||||||
# Remove the Range header added previously.
|
# Remove the Range header added previously.
|
||||||
headers.delete("Range") if range_header.nil?
|
headers.delete("Range")
|
||||||
|
|
||||||
if response.status_code >= 400
|
if response.status_code >= 400
|
||||||
env.response.content_type = "text/plain"
|
env.response.content_type = "text/plain"
|
||||||
|
@ -86,29 +107,21 @@ module Invidious::Routes::VideoPlayback
|
||||||
return error_template(403, "Administrator has disabled this endpoint.")
|
return error_template(403, "Administrator has disabled this endpoint.")
|
||||||
end
|
end
|
||||||
|
|
||||||
begin
|
MediaProxy.proxy_dash_chunk(env, client, url, region)
|
||||||
client.get(url, headers) do |resp|
|
elsif has_range_param
|
||||||
MediaProxy.copy_response_headers(from: resp.headers, to: env.response.headers)
|
if CONFIG.disabled?("dash")
|
||||||
env.response.headers["Access-Control-Allow-Origin"] = "*"
|
return error_template(403, "Administrator has disabled this endpoint.")
|
||||||
|
|
||||||
if location = resp.headers["Location"]?
|
|
||||||
url = Invidious::HttpServer::Utils.proxy_video_url(location, region: region)
|
|
||||||
return env.redirect url
|
|
||||||
end
|
|
||||||
|
|
||||||
IO.copy(resp.body_io, env.response)
|
|
||||||
end
|
|
||||||
rescue ex
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
MediaProxy.proxy_dash_chunk(env, client, url, region)
|
||||||
else
|
else
|
||||||
if query_params["title"]? && CONFIG.disabled?("downloads") ||
|
if (title && CONFIG.disabled?("downloads")) || (title.nil? && CONFIG.disabled?("local"))
|
||||||
CONFIG.disabled?("dash")
|
|
||||||
return error_template(403, "Administrator has disabled this endpoint.")
|
return error_template(403, "Administrator has disabled this endpoint.")
|
||||||
end
|
end
|
||||||
|
|
||||||
content_length = nil
|
content_length = nil
|
||||||
first_chunk = true
|
first_chunk = true
|
||||||
range_start, range_end = parse_range(env.request.headers["Range"]?)
|
range_start, range_end = parse_range(range)
|
||||||
chunk_start = range_start
|
chunk_start = range_start
|
||||||
chunk_end = range_end
|
chunk_end = range_end
|
||||||
|
|
||||||
|
@ -135,16 +148,12 @@ module Invidious::Routes::VideoPlayback
|
||||||
begin
|
begin
|
||||||
client.get(url, headers) do |resp|
|
client.get(url, headers) do |resp|
|
||||||
if first_chunk
|
if first_chunk
|
||||||
if !env.request.headers["Range"]? && resp.status_code == 206
|
if !has_range_header && resp.status_code == 206
|
||||||
env.response.status_code = 200
|
env.response.status_code = 200
|
||||||
else
|
else
|
||||||
env.response.status_code = resp.status_code
|
env.response.status_code = resp.status_code
|
||||||
end
|
end
|
||||||
|
|
||||||
MediaProxy.copy_response_headers(from: resp.headers, to: env.response.headers)
|
|
||||||
env.response.headers.delete("Content-Range") # Important!
|
|
||||||
env.response.headers["Access-Control-Allow-Origin"] = "*"
|
|
||||||
|
|
||||||
if location = resp.headers["Location"]?
|
if location = resp.headers["Location"]?
|
||||||
location = URI.parse(location)
|
location = URI.parse(location)
|
||||||
location = "#{location.request_target}&host=#{location.host}#{region ? "®ion=#{region}" : ""}"
|
location = "#{location.request_target}&host=#{location.host}#{region ? "®ion=#{region}" : ""}"
|
||||||
|
@ -153,7 +162,11 @@ module Invidious::Routes::VideoPlayback
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
|
|
||||||
if title = query_params["title"]?
|
MediaProxy.copy_response_headers(from: resp.headers, to: env.response.headers)
|
||||||
|
env.response.headers.delete("Content-Range") # Important!
|
||||||
|
env.response.headers["Access-Control-Allow-Origin"] = "*"
|
||||||
|
|
||||||
|
if title
|
||||||
# https://blog.fastmail.com/2011/06/24/download-non-english-filenames/
|
# https://blog.fastmail.com/2011/06/24/download-non-english-filenames/
|
||||||
filename = URI.encode_www_form(title, space_to_plus: false)
|
filename = URI.encode_www_form(title, space_to_plus: false)
|
||||||
header = "attachment; filename=\"#{filename}\"; filename*=UTF-8''#{filename}"
|
header = "attachment; filename=\"#{filename}\"; filename*=UTF-8''#{filename}"
|
||||||
|
@ -162,7 +175,7 @@ module Invidious::Routes::VideoPlayback
|
||||||
|
|
||||||
if !resp.headers.includes_word?("Transfer-Encoding", "chunked")
|
if !resp.headers.includes_word?("Transfer-Encoding", "chunked")
|
||||||
content_length = resp.headers["Content-Range"].split("/")[-1].to_i64
|
content_length = resp.headers["Content-Range"].split("/")[-1].to_i64
|
||||||
if env.request.headers["Range"]?
|
if has_range_header
|
||||||
env.response.headers["Content-Range"] = "bytes #{range_start}-#{range_end || (content_length - 1)}/#{content_length}"
|
env.response.headers["Content-Range"] = "bytes #{range_start}-#{range_end || (content_length - 1)}/#{content_length}"
|
||||||
env.response.content_length = ((range_end.try &.+ 1) || content_length) - range_start
|
env.response.content_length = ((range_end.try &.+ 1) || content_length) - range_start
|
||||||
else
|
else
|
||||||
|
|
|
@ -35,4 +35,36 @@ module Invidious::MediaProxy
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# -------------------
|
||||||
|
# Proxy functions
|
||||||
|
# -------------------
|
||||||
|
|
||||||
|
def proxy_dash_chunk(
|
||||||
|
env : HTTP::Server::Context,
|
||||||
|
client : HTTP::Client,
|
||||||
|
url : URI | String,
|
||||||
|
region : String?
|
||||||
|
)
|
||||||
|
headers = HTTP::Headers.new
|
||||||
|
self.copy_request_headers(from: env.request.headers, to: headers)
|
||||||
|
|
||||||
|
# Make sure to remove a potential range header, to avoid throttling
|
||||||
|
headers.delete("Range")
|
||||||
|
|
||||||
|
client.get(url, headers) do |resp|
|
||||||
|
env.response.status_code = resp.status_code
|
||||||
|
|
||||||
|
self.copy_response_headers(from: resp.headers, to: env.response.headers)
|
||||||
|
env.response.headers["Access-Control-Allow-Origin"] = "*"
|
||||||
|
|
||||||
|
if location = resp.headers["Location"]?
|
||||||
|
url = HttpServer::Utils.proxy_video_url(location, region: region)
|
||||||
|
env.redirect url
|
||||||
|
else
|
||||||
|
IO.copy(resp.body_io, env.response)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
rescue ex
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue