Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add redirects for at.tumblr.com links #17

Merged
merged 2 commits into from
Jan 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions locales/en_US/LC_MESSAGES/priviblur.po
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,6 @@ msgstr "Error: Unable to fetch HTTP client in time"

msgid "priviblur_error_pool_timeout_error_description"
msgstr "Priviblur was unable to fetch a HTTP client to request Tumblr in time. Please try again later"

msgid "priviblur_error_invalid_internal_tumblr_redirect"
msgstr "Error: Tumblr HTTP 301 redirect points to foreign URL"
13 changes: 13 additions & 0 deletions src/helpers/error_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ async def tumblr_error_restricted_tag(request, exception):
status=403
)


async def tumblr_error_unknown_blog(request, exception):
return await sanic_ext.render(
"misc/generic_error.jinja",
Expand Down Expand Up @@ -81,3 +82,15 @@ async def error_404(request, exception):
},
status=404
)


async def invalid_redirect(request, exception):
return await sanic_ext.render(
"misc/generic_error.jinja",
context={
"app": request.app,
"exception": exception,
"error_heading": request.app.ctx.translate("en", "priviblur_error_invalid_internal_tumblr_redirect"),
},
status=502
)
2 changes: 2 additions & 0 deletions src/helpers/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class TumblrInvalidRedirect(Exception):
pass
4 changes: 2 additions & 2 deletions src/helpers/helpers.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import sanic

import copy
import urllib.parse
from typing import Sequence

import sanic

def url_handler(raw_url):
"""Change URLs found in posts to privacy-friendly alternatives"""
url = urllib.parse.urlparse(raw_url)
Expand Down
5 changes: 3 additions & 2 deletions src/routes/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from . import explore, media, search, tagged, blogs, assets, priviblur
from . import explore, media, search, tagged, blogs, assets, priviblur, miscellaneous

BLUEPRINTS = (
explore.explore,
Expand All @@ -7,5 +7,6 @@
tagged.tagged,
blogs.blogs,
assets.assets,
priviblur.priviblur
priviblur.priviblur,
miscellaneous.miscellaneous
)
4 changes: 3 additions & 1 deletion src/routes/media.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import sanic

from ..helpers import exceptions

media = sanic.Blueprint("TumblrMedia", url_prefix="/tblr")


Expand All @@ -15,7 +17,7 @@ async def get_media(request, client, path_to_request):
if location := priviblur_response_headers.get("location"):
location = request.app.ctx.URL_HANDLER(location)
if not location.startswith("/"):
return sanic.response.text("Media is redirecting to foreign URL", status=500)
raise exceptions.TumblrInvalidRedirect()

return sanic.redirect(location)
elif tumblr_response.status_code == 429:
Expand Down
17 changes: 17 additions & 0 deletions src/routes/miscellaneous.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import urllib.parse

import sanic
from ..helpers import exceptions

miscellaneous = sanic.Blueprint("miscellaneous", url_prefix="/")

@miscellaneous.get(r"/at/<path:path>")
async def _at_links(request: sanic.Request, path : str):
"""Redirects for at.tumblr.com links"""
response = await request.app.ctx.TumblrAtClient.head(path)
if response.status_code == 301:
location = urllib.parse.urlparse(response.headers["location"])
if location.path.startswith("/"):
return sanic.redirect(location.path)

raise exceptions.TumblrInvalidRedirect()
11 changes: 9 additions & 2 deletions src/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from . import routes, priviblur_extractor
from . import priviblur_extractor
from .config import load_config
from .helpers import setup_logging, helpers, error_handlers, pool_timeout_tracker
from .helpers import setup_logging, helpers, error_handlers, exceptions, pool_timeout_tracker
from .version import VERSION, CURRENT_COMMIT
from .jobs import refresh_pool

Expand Down Expand Up @@ -64,7 +64,6 @@

@app.listener("before_server_start")
async def initialize(app):

priviblur_backend = app.ctx.PRIVIBLUR_CONFIG.backend

app.ctx.TumblrAPI = await priviblur_extractor.TumblrAPI.create(
Expand Down Expand Up @@ -100,6 +99,13 @@ def create_image_client(url):

tasks.append(app.add_task(refresh_pool.refresh_pool_task(app, create_image_client)))

app.ctx.TumblrAtClient = httpx.AsyncClient(
base_url="https://at.tumblr.com",
headers={"user-agent": priviblur_extractor.TumblrAPI.DEFAULT_HEADERS["user-agent"]},
http2=True,
timeout=priviblur_backend.main_response_timeout
)

# Add additional jinja filters and functions

app.ext.environment.filters["encodepathsegment"] = functools.partial(urllib.parse.quote, safe="")
Expand Down Expand Up @@ -182,6 +188,7 @@ async def reload_shutdown(app):

app.error_handler.add(httpx.PoolTimeout, error_handlers.pool_timeout_error)
app.error_handler.add(sanic.exceptions.NotFound, error_handlers.error_404)
app.error_handler.add(exceptions.TumblrInvalidRedirect, error_handlers.invalid_redirect)

# Register all routes:
for route in routes.BLUEPRINTS:
Expand Down