Skip to content

Commit

Permalink
Add configs to use a proxy for viewing attachments
Browse files Browse the repository at this point in the history
  • Loading branch information
Taaku18 committed Apr 22, 2024
1 parent 23dbdcf commit 1ba069d
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 20 deletions.
13 changes: 11 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
MONGO_URI=mongodb+srv://urihere
LOG_URL_PREFIX=/logs
# Your MongoDB Connection string, same as your bot's.
CONNECTION_URI=mongodb+srv://urihere
# Where should the logviewer serve your logs. Default: https://example.com/logs/LOGKEY
LOG_URL_PREFIX=/logs
# Listen address and port. Don't change them if you don't know what they do.
HOST=0.0.0.0
PORT=8000
# Whether if the logviewer should use a proxy to view attachments.
# If set to "no" (default), attachments will expire after 1 day and the logviewer won't be able to show the attachment.
# Please be aware that this may violate Discord TOS and the proxy will have full access to your attachments.
# Modmail/Logviewer is not affiliated with the proxy in any way. USE AT YOUR OWN RISK.
USE_ATTACHMENT_PROXY=no
ATTACHMENT_PROXY_URL=https://cdn.discordapp.xyz
24 changes: 17 additions & 7 deletions app.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
{
"name": "Modmail Log Viewer",
"description": "A simple webserver to view self-hosted logs",
"repository": "https://github.com/kyb3r/logviewer",
"env": {
"MONGO_URI": {
"description": "MongoDB connection URI that contains your modmail logs.",
"required": true
"name": "Modmail Log Viewer",
"description": "A simple webserver to view self-hosted logs",
"repository": "https://github.com/kyb3r/logviewer",
"env": {
"MONGO_URI": {
"description": "MongoDB connection URI that contains your modmail logs.",
"required": true
}
},
"USE_ATTACHMENT_PROXY": {
"description": "Whether if the logviewer should use a proxy to view attachments. If set to 'no', attachments will expire after 1 day. USE AT YOUR OWN RISK.",
"required": false,
"value": "no"
},
"ATTACHMENT_PROXY_URL": {
"description": "Proxy URL for viewing attachments.",
"required": false,
"value": "https://cdn.discordapp.xyz"
}
}
29 changes: 27 additions & 2 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
__version__ = "1.1.1"
__version__ = "1.1.2"

import html
import os

from dotenv import load_dotenv
Expand Down Expand Up @@ -41,10 +42,34 @@ def render_template(name, *args, **kwargs):
app.ctx.render_template = render_template


def strtobool(val):
"""
Copied from distutils.strtobool.
Convert a string representation of truth to true (1) or false (0).
True values are 'y', 'yes', 't', 'true', 'on', and '1'; false values
are 'n', 'no', 'f', 'false', 'off', and '0'. Raises ValueError if
'val' is anything else.
"""
val = val.lower()
if val in ('y', 'yes', 't', 'true', 'on', '1'):
return 1
elif val in ('n', 'no', 'f', 'false', 'off', '0'):
return 0
else:
raise ValueError("invalid truth value %r" % (val,))


@app.listener("before_server_start")
async def init(app, loop):
app.ctx.db = AsyncIOMotorClient(MONGO_URI).modmail_bot

use_attachment_proxy = strtobool(os.getenv("USE_ATTACHMENT_PROXY", "https://cdn.discordapp.xyz"))
if use_attachment_proxy:
app.ctx.attachment_proxy_url = os.environ["ATTACHMENT_PROXY_URL"]
app.ctx.attachment_proxy_url = html.escape(app.ctx.attachment_proxy_url).rstrip("/")
else:
app.ctx.attachment_proxy_url = None

@app.exception(NotFound)
async def not_found(request, exc):
Expand Down
25 changes: 16 additions & 9 deletions core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ def __init__(self, app, data):
)
self.channel_id = int(data["channel_id"])
self.guild_id = int(data["guild_id"])
self.creator = User(data["creator"])
self.recipient = User(data["recipient"])
self.closer = User(data["closer"]) if not self.open else None
self.creator = User(app, data["creator"])
self.recipient = User(app, data["recipient"])
self.closer = User(app, data["closer"]) if not self.open else None
self.close_message = format_content_html(data.get("close_message") or "")
self.messages = [Message(m) for m in data["messages"]]
self.messages = [Message(app, m) for m in data["messages"]]
self.internal_messages = [m for m in self.messages if m.type == "internal"]
self.thread_messages = [
m for m in self.messages if m.type not in ("internal", "system")
Expand Down Expand Up @@ -112,7 +112,8 @@ def render_plain_text(self):


class User:
def __init__(self, data):
def __init__(self, app, data):
self.app = app
self.id = int(data.get("id"))
self.name = data["name"]
self.discriminator = data["discriminator"]
Expand Down Expand Up @@ -147,7 +148,8 @@ def type(self):


class Attachment:
def __init__(self, data):
def __init__(self, app, data):
self.app = app
if isinstance(data, str): # Backwards compatibility
self.id = 0
self.filename = "attachment"
Expand All @@ -160,17 +162,22 @@ def __init__(self, data):
self.url = data["url"]
self.is_image = data["is_image"]
self.size = data["size"]
if self.app.ctx.attachment_proxy_url is not None:
self.url = self.url.replace("https://cdn.discordapp.com", self.app.ctx.attachment_proxy_url)
self.url = self.url.replace("https://media.discordapp.net", self.app.ctx.attachment_proxy_url)
print(self.url)


class Message:
def __init__(self, data):
def __init__(self, app, data):
self.app = app
self.id = int(data["message_id"])
self.created_at = dateutil.parser.parse(data["timestamp"]).astimezone(timezone.utc)
self.human_created_at = duration(self.created_at, now=datetime.now(timezone.utc))
self.raw_content = data["content"]
self.content = self.format_html_content(self.raw_content)
self.attachments = [Attachment(a) for a in data["attachments"]]
self.author = User(data["author"])
self.attachments = [Attachment(app, a) for a in data["attachments"]]
self.author = User(app, data["author"])
self.type = data.get("type", "thread_message")
self.edited = data.get("edited", False)

Expand Down

0 comments on commit 1ba069d

Please sign in to comment.