|
| 1 | +# SSL Contexts are designed to be reused and supports infinite connections. |
| 2 | +# |
| 3 | +# This *significantly* improves the performance of spinning up new clients/reconnections |
| 4 | +# |
| 5 | +# For more information see https://github.com/crystal-lang/crystal/issues/15419 |
| 6 | +private GLOBAL_SSL_CONTEXT = OpenSSL::SSL::Context::Client.new |
| 7 | + |
1 | 8 | module Invidious
|
2 | 9 | class IVTCPSocket < TCPSocket
|
3 | 10 | def initialize(host : String, port, dns_timeout = nil, connect_timeout = nil, blocking = false, family = Socket::Family::UNSPEC)
|
@@ -89,13 +96,19 @@ def make_client(
|
89 | 96 | use_http_proxy : Bool = true,
|
90 | 97 | allow_auto_reconnect : Bool = true,
|
91 | 98 | )
|
| 99 | + tls = if url.scheme == "https" |
| 100 | + GLOBAL_SSL_CONTEXT |
| 101 | + else |
| 102 | + nil |
| 103 | + end |
| 104 | + |
92 | 105 | if CONFIG.http_proxy && use_http_proxy
|
93 |
| - client = Invidious::HTTPClient.new(url) |
| 106 | + client = Invidious::HTTPClient.new(url, tls: tls) |
94 | 107 | client.proxy = make_configured_http_proxy_client() if CONFIG.http_proxy && use_http_proxy
|
95 | 108 | elsif force_resolve
|
96 |
| - client = Invidious::HTTPClient.new(url, force_resolve: CONFIG.force_resolve) |
| 109 | + client = Invidious::HTTPClient.new(url, tls: tls, force_resolve: CONFIG.force_resolve) |
97 | 110 | else
|
98 |
| - client = Invidious::HTTPClient.new(url, allow_auto_reconnect: allow_auto_reconnect) |
| 111 | + client = Invidious::HTTPClient.new(url, tls: tls, allow_auto_reconnect: allow_auto_reconnect) |
99 | 112 | end
|
100 | 113 |
|
101 | 114 | client.before_request { |r| add_yt_headers(r) } if url.host.try &.ends_with?("youtube.com") || force_youtube_headers
|
|
0 commit comments