From 8f318b156c42079abc020c2627aa880d92e6258f Mon Sep 17 00:00:00 2001 From: yzhh <31803608+OasisAkari@users.noreply.github.com> Date: Wed, 18 Sep 2024 15:13:25 +0800 Subject: [PATCH] #1317 --- config/__init__.py | 17 ++++++++++++++++- config/config.toml.example | 3 ++- core/utils/text.py | 24 ++++++++++++++++++++++++ modules/core/su_utils.py | 13 ++++++++++++- poetry.lock | 16 +++++++++++++++- pyproject.toml | 1 + 6 files changed, 70 insertions(+), 4 deletions(-) diff --git a/config/__init__.py b/config/__init__.py index a1492a3bc7..4fee7ebe41 100644 --- a/config/__init__.py +++ b/config/__init__.py @@ -6,7 +6,6 @@ import toml from core.exceptions import ConfigFileNotFound, ConfigValueError -from core.utils.text import isfloat, isint config_filename = 'config.toml' @@ -15,6 +14,22 @@ old_cfg_file_path = abspath('./config/config.cfg') +def isfloat(num_str: str) -> bool: + try: + float(num_str) + return True + except Exception: + return False + + +def isint(num_str: str) -> bool: + try: + int(num_str) + return True + except Exception: + return False + + def convert_cfg_to_toml(): import configparser config = configparser.ConfigParser() diff --git a/config/config.toml.example b/config/config.toml.example index 65d72820f4..63b41149ec 100644 --- a/config/config.toml.example +++ b/config/config.toml.example @@ -22,7 +22,8 @@ openai_api_key = osu_api_key = wolfram_alpha_appid = jwt_secret = - +ff3_key = 0AF6C4788B86F4E2FB97215E6E4A5EB0 +ff3_tweak = 6520BD033E2928 [cfg] api_port = 5000 base_superuser = ["QQ|2596322644",] diff --git a/core/utils/text.py b/core/utils/text.py index e2384c2a3b..4d071c7b88 100644 --- a/core/utils/text.py +++ b/core/utils/text.py @@ -1,6 +1,12 @@ +import random +import re import sys from datetime import timedelta from typing import TypeVar +from ff3 import FF3Cipher + +from config import Config + T = TypeVar("T", str, bytes, bytearray) @@ -53,3 +59,21 @@ def parse_time_string(time_str: str) -> timedelta: return timedelta(hours=hour, minutes=minute) except Exception: return timedelta() + + +def random_string(length: int) -> str: + return ''.join(random.choices("0123456789ABCDEF", k=length)) + + +def decrypt_string(text): + key = Config('ff3_key', random_string(32)) + tweak = Config('ff3_tweak', random_string(14)) + c = FF3Cipher.withCustomAlphabet(key, tweak, + "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~") + d = [] + for i in range(0, len(text), 28): + d.append(text[i:i+28]) + dec_text = ''.join([c.decrypt(i) for i in d]) + if m :=re.match(r'^t:(.*?):e.*?$', dec_text): + return m.group(1) + return False diff --git a/modules/core/su_utils.py b/modules/core/su_utils.py index a3acf5f494..099676d00c 100644 --- a/modules/core/su_utils.py +++ b/modules/core/su_utils.py @@ -17,7 +17,7 @@ from core.tos import pardon_user, warn_user from core.utils.info import Info from core.utils.storedata import get_stored_list, update_stored_list -from core.utils.text import isfloat, isint +from core.utils.text import isfloat, isint, decrypt_string from database import BotDBUtil @@ -562,3 +562,14 @@ async def _(msg: Bot.MessageSession, sender: str = None): async def stop_playing_maimai(msg: Bot.MessageSession): BotDBUtil.JobQueue.clear(0) await msg.finish(msg.locale.t("success")) + + +decry = module('decrypt', required_superuser=True, base=True, doc=True) + +@decry.command('') +async def _(msg: Bot.MessageSession): + dec = decrypt_string(msg.as_display().split(' ', 1)[1]) + if dec: + await msg.finish(dec) + else: + await msg.finish(msg.locale.t("failed")) diff --git a/poetry.lock b/poetry.lock index 2a475b9167..7a624abbaf 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1021,6 +1021,20 @@ files = [ [package.dependencies] sgmllib3k = "*" +[[package]] +name = "ff3" +version = "1.0.2" +description = "Format Preserving Encryption (FPE) with FF3" +optional = false +python-versions = ">=3.8" +files = [ + {file = "ff3-1.0.2-py3-none-any.whl", hash = "sha256:d289644085037c1357497bae0bace18e31e13b25f60e1eadffa71b9eeed4779d"}, + {file = "ff3-1.0.2.tar.gz", hash = "sha256:ff892c7472509c17f68cb4907f8af7fc91835cb68c260e2ab7597b57ad44bc7a"}, +] + +[package.dependencies] +pycryptodome = "*" + [[package]] name = "ffmpy" version = "0.3.3" @@ -4589,4 +4603,4 @@ multidict = ">=4.0" [metadata] lock-version = "2.0" python-versions = "^3.12.0" -content-hash = "712667ea460dcf0b839545e4ecdfde0756c84ca4d83075b0d4c1acec7800216c" +content-hash = "56692c1eee88aa6c27e9195f5821dc56ed0f446208dffc9ed705f835c8d2bc8c" diff --git a/pyproject.toml b/pyproject.toml index f064aa8c61..cfcb503153 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -64,6 +64,7 @@ inputimeout = "^1.0.4" prompt-toolkit = "^3.0.47" emoji = "^2.12.1" langconv = {git = "https://github.com/OasisAkari/langconv.py.git"} +ff3 = "^1.0.2" [tool.poetry.group.dev.dependencies]