Replace static asset requests with QUIC

This commit is contained in:
Omar Roth 2020-03-06 13:53:35 -05:00
parent d96dee3aa6
commit bb72672dd9
No known key found for this signature in database
GPG key ID: B8254FB7EC3D37F2

View file

@ -99,8 +99,7 @@ LOCALES = {
"zh-TW" => load_locale("zh-TW"), "zh-TW" => load_locale("zh-TW"),
} }
YT_POOL = QUICPool.new(YT_URL, capacity: CONFIG.pool_size, timeout: 0.05) YT_POOL = QUICPool.new(YT_URL, capacity: CONFIG.pool_size, timeout: 0.1)
YT_IMG_POOL = QUICPool.new(YT_IMG_URL, capacity: CONFIG.pool_size, timeout: 0.05)
config = CONFIG config = CONFIG
logger = Invidious::LogHandler.new logger = Invidious::LogHandler.new
@ -5646,11 +5645,10 @@ get "/videoplayback" do |env|
end end
get "/ggpht/*" do |env| get "/ggpht/*" do |env|
host = "https://yt3.ggpht.com"
client = make_client(URI.parse(host))
url = env.request.path.lchop("/ggpht") url = env.request.path.lchop("/ggpht")
headers = HTTP::Headers.new headers = HTTP::Headers.new
headers[":authority"] = "yt3.ggpht.com"
REQUEST_HEADERS_WHITELIST.each do |header| REQUEST_HEADERS_WHITELIST.each do |header|
if env.request.headers[header]? if env.request.headers[header]?
headers[header] = env.request.headers[header] headers[header] = env.request.headers[header]
@ -5658,7 +5656,7 @@ get "/ggpht/*" do |env|
end end
begin begin
client.get(url, headers) do |response| YT_POOL.client &.get(url, headers) do |response|
env.response.status_code = response.status_code env.response.status_code = response.status_code
response.headers.each do |key, value| response.headers.each do |key, value|
if !RESPONSE_HEADERS_BLACKLIST.includes?(key.downcase) if !RESPONSE_HEADERS_BLACKLIST.includes?(key.downcase)
@ -5691,16 +5689,16 @@ get "/sb/:id/:storyboard/:index" do |env|
storyboard = env.params.url["storyboard"] storyboard = env.params.url["storyboard"]
index = env.params.url["index"] index = env.params.url["index"]
if storyboard.starts_with? "storyboard_live"
host = "https://i.ytimg.com"
else
host = "https://i9.ytimg.com"
end
client = make_client(URI.parse(host))
url = "/sb/#{id}/#{storyboard}/#{index}?#{env.params.query}" url = "/sb/#{id}/#{storyboard}/#{index}?#{env.params.query}"
headers = HTTP::Headers.new headers = HTTP::Headers.new
if storyboard.starts_with? "storyboard_live"
headers[":authority"] = "i.ytimg.com"
else
headers[":authority"] = "i9.ytimg.com"
end
REQUEST_HEADERS_WHITELIST.each do |header| REQUEST_HEADERS_WHITELIST.each do |header|
if env.request.headers[header]? if env.request.headers[header]?
headers[header] = env.request.headers[header] headers[header] = env.request.headers[header]
@ -5708,7 +5706,7 @@ get "/sb/:id/:storyboard/:index" do |env|
end end
begin begin
client.get(url, headers) do |response| YT_POOL.client &.get(url, headers) do |response|
env.response.status_code = response.status_code env.response.status_code = response.status_code
response.headers.each do |key, value| response.headers.each do |key, value|
if !RESPONSE_HEADERS_BLACKLIST.includes?(key.downcase) if !RESPONSE_HEADERS_BLACKLIST.includes?(key.downcase)
@ -5734,11 +5732,10 @@ get "/s_p/:id/:name" do |env|
id = env.params.url["id"] id = env.params.url["id"]
name = env.params.url["name"] name = env.params.url["name"]
host = "https://i9.ytimg.com"
client = make_client(URI.parse(host))
url = env.request.resource url = env.request.resource
headers = HTTP::Headers.new headers = HTTP::Headers.new
headers[":authority"] = "i9.ytimg.com"
REQUEST_HEADERS_WHITELIST.each do |header| REQUEST_HEADERS_WHITELIST.each do |header|
if env.request.headers[header]? if env.request.headers[header]?
headers[header] = env.request.headers[header] headers[header] = env.request.headers[header]
@ -5746,7 +5743,7 @@ get "/s_p/:id/:name" do |env|
end end
begin begin
client.get(url, headers) do |response| YT_POOL.client &.get(url, headers) do |response|
env.response.status_code = response.status_code env.response.status_code = response.status_code
response.headers.each do |key, value| response.headers.each do |key, value|
if !RESPONSE_HEADERS_BLACKLIST.includes?(key.downcase) if !RESPONSE_HEADERS_BLACKLIST.includes?(key.downcase)
@ -5801,9 +5798,12 @@ get "/vi/:id/:name" do |env|
id = env.params.url["id"] id = env.params.url["id"]
name = env.params.url["name"] name = env.params.url["name"]
headers = HTTP::Headers.new
headers[":authority"] = "i.ytimg.com"
if name == "maxres.jpg" if name == "maxres.jpg"
build_thumbnails(id, config, Kemal.config).each do |thumb| build_thumbnails(id, config, Kemal.config).each do |thumb|
if YT_IMG_POOL.client &.head("/vi/#{id}/#{thumb[:url]}.jpg").status_code == 200 if YT_POOL.client &.head("/vi/#{id}/#{thumb[:url]}.jpg", headers).status_code == 200
name = thumb[:url] + ".jpg" name = thumb[:url] + ".jpg"
break break
end end
@ -5811,7 +5811,6 @@ get "/vi/:id/:name" do |env|
end end
url = "/vi/#{id}/#{name}" url = "/vi/#{id}/#{name}"
headers = HTTP::Headers.new
REQUEST_HEADERS_WHITELIST.each do |header| REQUEST_HEADERS_WHITELIST.each do |header|
if env.request.headers[header]? if env.request.headers[header]?
headers[header] = env.request.headers[header] headers[header] = env.request.headers[header]
@ -5819,7 +5818,7 @@ get "/vi/:id/:name" do |env|
end end
begin begin
YT_IMG_POOL.client &.get(url, headers) do |response| YT_POOL.client &.get(url, headers) do |response|
env.response.status_code = response.status_code env.response.status_code = response.status_code
response.headers.each do |key, value| response.headers.each do |key, value|
if !RESPONSE_HEADERS_BLACKLIST.includes?(key.downcase) if !RESPONSE_HEADERS_BLACKLIST.includes?(key.downcase)