From 35bee987f60a12d3c246789e79c6012756577034 Mon Sep 17 00:00:00 2001 From: Omar Roth Date: Mon, 17 Sep 2018 18:39:28 -0500 Subject: [PATCH] Proxy profile pictures --- src/invidious.cr | 49 ++++++++++++++++++++++++++++++++ src/invidious/comments.cr | 4 ++- src/invidious/helpers/helpers.cr | 2 +- 3 files changed, 53 insertions(+), 2 deletions(-) diff --git a/src/invidious.cr b/src/invidious.cr index d1509437..ff1f2579 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -3024,6 +3024,55 @@ get "/videoplayback" do |env| end end +get "/ggpht*" do |env| +end + +get "/ggpht/*" do |env| + host = "https://yt3.ggpht.com" + client = make_client(URI.parse(host)) + url = env.request.path.lchop("/ggpht") + + headers = env.request.headers + headers.delete("Host") + headers.delete("Cookie") + headers.delete("User-Agent") + headers.delete("Referer") + + client.get(url, headers) do |response| + env.response.status_code = response.status_code + response.headers.each do |key, value| + env.response.headers[key] = value + end + + if response.status_code == 304 + break + end + + chunk_size = 4096 + size = 1 + if response.headers.includes_word?("Content-Encoding", "gzip") + Gzip::Writer.open(env.response) do |deflate| + until size == 0 + size = IO.copy(response.body_io, deflate) + env.response.flush + end + end + elsif response.headers.includes_word?("Content-Encoding", "deflate") + Flate::Writer.open(env.response) do |deflate| + until size == 0 + size = IO.copy(response.body_io, deflate) + env.response.flush + end + end + else + until size == 0 + size = IO.copy(response.body_io, env.response, chunk_size) + env.response.flush + end + end + end +end + get "/vi/:id/:name" do |env| id = env.params.url["id"] name = env.params.url["name"] diff --git a/src/invidious/comments.cr b/src/invidious/comments.cr index 70746499..20716dcc 100644 --- a/src/invidious/comments.cr +++ b/src/invidious/comments.cr @@ -100,10 +100,12 @@ def template_youtube_comments(comments) END_HTML end + author_thumbnail = "/ggpht#{URI.parse(child["authorThumbnails"][-1]["url"].as_s).full_path}" + html += <<-END_HTML
- +

diff --git a/src/invidious/helpers/helpers.cr b/src/invidious/helpers/helpers.cr index 68c5cc5b..aeb275d3 100644 --- a/src/invidious/helpers/helpers.cr +++ b/src/invidious/helpers/helpers.cr @@ -18,7 +18,7 @@ class Config end class FilteredCompressHandler < Kemal::Handler - exclude ["/videoplayback", "/videoplayback/*", "/vi/*", "/api/*"] + exclude ["/videoplayback", "/videoplayback/*", "/vi/*", "/api/*", "/ggpht/*"] def call(env) return call_next env if exclude_match? env