From d4d460a2a073c2f6b2ea0e71e3e033b39c00b969 Mon Sep 17 00:00:00 2001 From: mctaylors Date: Wed, 21 May 2025 05:06:45 +0300 Subject: [PATCH] feat: add PreferSecureProtocol in config.ini --- commands.py | 19 ++++++++++--------- config.ini | 10 +++++++--- config.py | 3 +++ inline_query.py | 3 ++- 4 files changed, 22 insertions(+), 13 deletions(-) diff --git a/commands.py b/commands.py index cc8f001..f131356 100644 --- a/commands.py +++ b/commands.py @@ -71,7 +71,8 @@ async def info_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No keyboard = [ [ InlineKeyboardButton( - f"Open in {app.name}", url=f"http://{app.hostname}/posts/{post_id}" + f"Open in {app.name}", + url=f"{app.protocol}://{app.hostname}/posts/{post_id}", ) ] ] @@ -82,28 +83,28 @@ async def info_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No m.append(f"ID: {html_parser.code(post_data['id'])}") m.append( f"Uploader: {html_parser.hyperlink(uploader_data['name'], - f"http://{app.hostname}/users/{post_data['uploader_id']}")} " - f"{html_parser.hyperlink("»", f"http://{app.hostname}/posts?tags=user:{uploader_data['name']}")}" + f"{app.protocol}://{app.hostname}/users/{post_data['uploader_id']}")} " + f"{html_parser.hyperlink("»", f"{app.protocol}://{app.hostname}/posts?tags=user:{uploader_data['name']}")}" ) created_at = datetime.fromisoformat(post_data["created_at"]) m.append( f"Date: {html_parser.hyperlink( f"{created_at.strftime("%Y-%m-%d %X (%z)")}", - f"http://{app.hostname}/posts?tags=date:{created_at.strftime("%Y-%m-%d")}")}" + f"{app.protocol}://{app.hostname}/posts?tags=date:{created_at.strftime("%Y-%m-%d")}")}" ) if post_data["approver_id"] is not None: approver_data = get_json(f"users/{post_data['approver_id']}") m.append( f"Approver: {html_parser.hyperlink(approver_data['name'], - f"http://{app.hostname}/users/{post_data['approver_id']}")} " - f"{html_parser.hyperlink("»", f"http://{app.hostname}/posts?tags=approver:{approver_data['name']}")}" + f"{app.protocol}://{app.hostname}/users/{post_data['approver_id']}")} " + f"{html_parser.hyperlink("»", f"{app.protocol}://{app.hostname}/posts?tags=approver:{approver_data['name']}")}" ) m.append( f"Size: {html_parser.hyperlink( f"{humanize_filesize(post_data['media_asset']['file_size'])} .{post_data['media_asset']['file_ext']}", "" if post_data['is_banned'] else post_data['file_url'])} " f"({post_data['media_asset']['image_width']}x{post_data['media_asset']['image_height']}) " - f"{html_parser.hyperlink("»", f"http://{app.hostname}/media_assets/{post_data['media_asset']['id']}")}" + f"{html_parser.hyperlink("»", f"{app.protocol}://{app.hostname}/media_assets/{post_data['media_asset']['id']}")}" ) m.append( f"Source: {post_data['source'] if post_data['source'] != "" else "🚫"}" @@ -111,12 +112,12 @@ async def info_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No m.append(f"Rating: {format_rating(post_data['rating'])}") m.append( f"Score: {html_parser.hyperlink(post_data['score'], - f"http://{app.hostname}/post_votes?search[post_id]={post_data['id']}&variant=compact")} " + f"{app.protocol}://{app.hostname}/post_votes?search[post_id]={post_data['id']}&variant=compact")} " f"(+{post_data['up_score']} / -{post_data['down_score']})" ) m.append( f"Favorites: {html_parser.hyperlink(post_data['fav_count'], - f"http://{app.hostname}/posts/{post_data['id']}/favorites")}" + f"{app.protocol}://{app.hostname}/posts/{post_data['id']}/favorites")}" ) m.append(f"Status: {format_status(post_data)}") diff --git a/config.ini b/config.ini index 67c053b..44b06d3 100644 --- a/config.ini +++ b/config.ini @@ -10,8 +10,12 @@ SourceUrl = git.mctaylors.ru ; Application name (e.g. Danbooru). Name = Danbooru ; Host (without http://) used to access the application API. -Host = stashboo.ru -; (Optional) Public host name (without http://) for the “Open in [App.Name]” button. +Host = danbooru.donmai.us +; (Optional) Public hostname (without http://) for example, the “Open in [App.Name]” button. ; Useful if the bot is hosted on the same server as the application. ; If not set, [App.Host] will be used instead. -HostName = \ No newline at end of file +HostName = +; Prefer the secure HTTPS protocol for generating links that use a public hostname. +; If True, bot will generate HTTPS links. +; If False, bot will generate HTTP links. +PreferSecureProtocol = True \ No newline at end of file diff --git a/config.py b/config.py index 021c5e9..2d528f0 100644 --- a/config.py +++ b/config.py @@ -39,6 +39,9 @@ class _App: self.hostname = _c.get("App", "HostName") if not self.hostname: self.hostname = self.host + self.protocol = "http" + if eval(_c.get("App", "PreferSecureProtocol")): + self.protocol = "https" general = _General() diff --git a/inline_query.py b/inline_query.py index 6244e59..dfd06d2 100644 --- a/inline_query.py +++ b/inline_query.py @@ -67,7 +67,8 @@ async def answer_query(update: Update, query: str, data) -> None: keyboard = [ [ InlineKeyboardButton( - f"Open in {app.name}", url=f"http://{app.hostname}/posts/{query}" + f"Open in {app.name}", + url=f"{app.protocol}://{app.hostname}/posts/{query}", ), InlineKeyboardButton("View original", url=data["file_url"]), ]