Add compile-time flag to remove code for QUIC

This commit is contained in:
syeopite 2021-08-30 14:55:35 -07:00
parent a12571e748
commit d379a36c0e
No known key found for this signature in database
GPG key ID: 6FA616E5A5294A82
2 changed files with 33 additions and 8 deletions

View file

@ -53,7 +53,13 @@ module Invidious::Routes::Login
# See https://github.com/ytdl-org/youtube-dl/blob/2019.04.07/youtube_dl/extractor/youtube.py#L82 # See https://github.com/ytdl-org/youtube-dl/blob/2019.04.07/youtube_dl/extractor/youtube.py#L82
begin begin
client = QUIC::Client.new(LOGIN_URL) client = nil # Declare variable
{% unless flag?(:disable_quic) %}
client = QUIC::Client.new(LOGIN_URL)
{% else %}
client = HTTP::Client.new(LOGIN_URL)
{% end %}
headers = HTTP::Headers.new headers = HTTP::Headers.new
login_page = client.get("/ServiceLogin") login_page = client.get("/ServiceLogin")

View file

@ -1,5 +1,13 @@
require "lsquic" require "lsquic"
{% unless flag?(:disable_quic) %}
require "lsquic"
alias ConnectonClientType = QUIC::Client | HTTP::Client
{% else %}
alias ConnectonClientType = HTTP::Client
{% end %}
def add_yt_headers(request) def add_yt_headers(request)
request.headers["user-agent"] ||= "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36" request.headers["user-agent"] ||= "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36"
request.headers["accept-charset"] ||= "ISO-8859-1,utf-8;q=0.7,*;q=0.7" request.headers["accept-charset"] ||= "ISO-8859-1,utf-8;q=0.7,*;q=0.7"
@ -19,7 +27,7 @@ struct YoutubeConnectionPool
property! url : URI property! url : URI
property! capacity : Int32 property! capacity : Int32
property! timeout : Float64 property! timeout : Float64
property pool : DB::Pool(QUIC::Client | HTTP::Client) property pool : DB::Pool(ConnectonClientType)
def initialize(url : URI, @capacity = 5, @timeout = 5.0, use_quic = true) def initialize(url : URI, @capacity = 5, @timeout = 5.0, use_quic = true)
@url = url @url = url
@ -36,7 +44,12 @@ struct YoutubeConnectionPool
response = yield conn response = yield conn
rescue ex rescue ex
conn.close conn.close
conn = QUIC::Client.new(url) {% unless flag?(:disable_quic) %}
conn = QUIC::Client.new(url)
{% else %}
conn = HTTP::Client.new(url)
{% end %}
conn.family = (url.host == "www.youtube.com") ? CONFIG.force_resolve : Socket::Family::INET conn.family = (url.host == "www.youtube.com") ? CONFIG.force_resolve : Socket::Family::INET
conn.family = Socket::Family::INET if conn.family == Socket::Family::UNSPEC conn.family = Socket::Family::INET if conn.family == Socket::Family::UNSPEC
conn.before_request { |r| add_yt_headers(r) } if url.host == "www.youtube.com" conn.before_request { |r| add_yt_headers(r) } if url.host == "www.youtube.com"
@ -50,12 +63,18 @@ struct YoutubeConnectionPool
end end
private def build_pool(use_quic) private def build_pool(use_quic)
DB::Pool(QUIC::Client | HTTP::Client).new(initial_pool_size: 0, max_pool_size: capacity, max_idle_pool_size: capacity, checkout_timeout: timeout) do DB::Pool(ConnectonClientType).new(initial_pool_size: 0, max_pool_size: capacity, max_idle_pool_size: capacity, checkout_timeout: timeout) do
if use_quic conn = nil # Declare
conn = QUIC::Client.new(url) {% unless flag?(:disable_quic) %}
else if use_quic
conn = QUIC::Client.new(url)
else
conn = HTTP::Client.new(url)
end
{% else %}
conn = HTTP::Client.new(url) conn = HTTP::Client.new(url)
end {% end %}
conn.family = (url.host == "www.youtube.com") ? CONFIG.force_resolve : Socket::Family::INET conn.family = (url.host == "www.youtube.com") ? CONFIG.force_resolve : Socket::Family::INET
conn.family = Socket::Family::INET if conn.family == Socket::Family::UNSPEC conn.family = Socket::Family::INET if conn.family == Socket::Family::UNSPEC
conn.before_request { |r| add_yt_headers(r) } if url.host == "www.youtube.com" conn.before_request { |r| add_yt_headers(r) } if url.host == "www.youtube.com"