From d379a36c0e89c66d47fe7faa005371e8b633a74a Mon Sep 17 00:00:00 2001 From: syeopite Date: Mon, 30 Aug 2021 14:55:35 -0700 Subject: [PATCH] Add compile-time flag to remove code for QUIC --- src/invidious/routes/login.cr | 8 ++++- src/invidious/yt_backend/connection_pool.cr | 33 ++++++++++++++++----- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/src/invidious/routes/login.cr b/src/invidious/routes/login.cr index b719b571..2d303713 100644 --- a/src/invidious/routes/login.cr +++ b/src/invidious/routes/login.cr @@ -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 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 login_page = client.get("/ServiceLogin") diff --git a/src/invidious/yt_backend/connection_pool.cr b/src/invidious/yt_backend/connection_pool.cr index 5ba2d73c..4bf200cc 100644 --- a/src/invidious/yt_backend/connection_pool.cr +++ b/src/invidious/yt_backend/connection_pool.cr @@ -1,5 +1,13 @@ 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) 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" @@ -19,7 +27,7 @@ struct YoutubeConnectionPool property! url : URI property! capacity : Int32 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) @url = url @@ -36,7 +44,12 @@ struct YoutubeConnectionPool response = yield conn rescue ex 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 = Socket::Family::INET if conn.family == Socket::Family::UNSPEC conn.before_request { |r| add_yt_headers(r) } if url.host == "www.youtube.com" @@ -50,12 +63,18 @@ struct YoutubeConnectionPool end 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 - if use_quic - conn = QUIC::Client.new(url) - else + DB::Pool(ConnectonClientType).new(initial_pool_size: 0, max_pool_size: capacity, max_idle_pool_size: capacity, checkout_timeout: timeout) do + conn = nil # Declare + {% unless flag?(:disable_quic) %} + if use_quic + conn = QUIC::Client.new(url) + else + conn = HTTP::Client.new(url) + end + {% else %} conn = HTTP::Client.new(url) - end + {% end %} + 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.before_request { |r| add_yt_headers(r) } if url.host == "www.youtube.com"