2022-12-19 07:26:38 +00:00
|
|
|
from urllib.parse import urlparse
|
|
|
|
|
2022-12-10 19:16:08 +00:00
|
|
|
import httpx
|
|
|
|
from django.conf import settings
|
|
|
|
from django.http import Http404, HttpResponse
|
|
|
|
from django.shortcuts import get_object_or_404
|
|
|
|
from django.views.generic import View
|
|
|
|
|
2022-12-15 07:50:54 +00:00
|
|
|
from activities.models import Emoji, PostAttachment
|
2022-12-10 19:16:08 +00:00
|
|
|
from users.models import Identity
|
|
|
|
|
|
|
|
|
2022-12-19 04:26:42 +00:00
|
|
|
class BaseProxyView(View):
|
2022-12-10 19:16:08 +00:00
|
|
|
"""
|
2022-12-19 04:26:42 +00:00
|
|
|
Base class for proxying remote content.
|
2022-12-10 19:16:08 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
def get(self, request, **kwargs):
|
|
|
|
self.kwargs = kwargs
|
|
|
|
remote_url = self.get_remote_url()
|
2022-12-19 07:26:38 +00:00
|
|
|
# See if we can do the nginx trick or a normal forward
|
2022-12-20 05:53:34 +00:00
|
|
|
if request.headers.get("x-takahe-accel") and not request.GET.get("no_accel"):
|
2022-12-19 07:26:38 +00:00
|
|
|
bits = urlparse(remote_url)
|
|
|
|
redirect_url = (
|
|
|
|
f"/__takahe_accel__/{bits.scheme}/{bits.hostname}/{bits.path}"
|
|
|
|
)
|
|
|
|
if bits.query:
|
|
|
|
redirect_url += f"?{bits.query}"
|
|
|
|
return HttpResponse(
|
|
|
|
"",
|
|
|
|
headers={
|
|
|
|
"X-Accel-Redirect": "/__takahe_accel__/",
|
|
|
|
"X-Takahe-RealUri": remote_url,
|
2022-12-28 18:39:40 +00:00
|
|
|
"Cache-Control": "public",
|
2022-12-19 07:26:38 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
remote_response = httpx.get(
|
|
|
|
remote_url,
|
|
|
|
headers={"User-Agent": settings.TAKAHE_USER_AGENT},
|
|
|
|
follow_redirects=True,
|
|
|
|
timeout=settings.SETUP.REMOTE_TIMEOUT,
|
|
|
|
)
|
|
|
|
except httpx.RequestError:
|
|
|
|
return HttpResponse(status=502)
|
|
|
|
if remote_response.status_code >= 400:
|
|
|
|
return HttpResponse(status=502)
|
|
|
|
return HttpResponse(
|
|
|
|
remote_response.content,
|
|
|
|
headers={
|
|
|
|
"Content-Type": remote_response.headers.get(
|
|
|
|
"Content-Type", "application/octet-stream"
|
|
|
|
),
|
2022-12-28 18:39:40 +00:00
|
|
|
"Cache-Control": remote_response.headers.get(
|
|
|
|
"Cache-Control", "public, max-age=3600"
|
|
|
|
),
|
2022-12-19 07:26:38 +00:00
|
|
|
},
|
2022-12-19 04:26:42 +00:00
|
|
|
)
|
2022-12-10 19:16:08 +00:00
|
|
|
|
|
|
|
def get_remote_url(self):
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
|
2022-12-19 04:26:42 +00:00
|
|
|
class EmojiCacheView(BaseProxyView):
|
2022-12-15 07:50:54 +00:00
|
|
|
"""
|
2022-12-19 04:26:42 +00:00
|
|
|
Proxies Emoji
|
2022-12-15 07:50:54 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
def get_remote_url(self):
|
|
|
|
self.emoji = get_object_or_404(Emoji, pk=self.kwargs["emoji_id"])
|
|
|
|
|
|
|
|
if not self.emoji.remote_url:
|
|
|
|
raise Http404()
|
|
|
|
return self.emoji.remote_url
|
|
|
|
|
|
|
|
|
2022-12-19 04:26:42 +00:00
|
|
|
class IdentityIconCacheView(BaseProxyView):
|
2022-12-10 19:16:08 +00:00
|
|
|
"""
|
2022-12-19 04:26:42 +00:00
|
|
|
Proxies identity icons (avatars)
|
2022-12-10 19:16:08 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
def get_remote_url(self):
|
|
|
|
self.identity = get_object_or_404(Identity, pk=self.kwargs["identity_id"])
|
2022-12-11 16:36:27 +00:00
|
|
|
if self.identity.local or not self.identity.icon_uri:
|
2022-12-10 19:16:08 +00:00
|
|
|
raise Http404()
|
|
|
|
return self.identity.icon_uri
|
|
|
|
|
|
|
|
|
2022-12-19 04:26:42 +00:00
|
|
|
class IdentityImageCacheView(BaseProxyView):
|
2022-12-10 19:16:08 +00:00
|
|
|
"""
|
2022-12-19 04:26:42 +00:00
|
|
|
Proxies identity profile header images
|
2022-12-10 19:16:08 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
def get_remote_url(self):
|
|
|
|
self.identity = get_object_or_404(Identity, pk=self.kwargs["identity_id"])
|
|
|
|
if self.identity.local or not self.identity.image_uri:
|
|
|
|
raise Http404()
|
|
|
|
return self.identity.image_uri
|
|
|
|
|
|
|
|
|
2022-12-19 04:26:42 +00:00
|
|
|
class PostAttachmentCacheView(BaseProxyView):
|
2022-12-10 19:16:08 +00:00
|
|
|
"""
|
2022-12-19 04:26:42 +00:00
|
|
|
Proxies post media (images only, videos should always be offloaded to remote)
|
2022-12-10 19:16:08 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
def get_remote_url(self):
|
|
|
|
self.post_attachment = get_object_or_404(
|
|
|
|
PostAttachment, pk=self.kwargs["attachment_id"]
|
|
|
|
)
|
|
|
|
if not self.post_attachment.is_image():
|
|
|
|
raise Http404()
|
|
|
|
return self.post_attachment.remote_url
|