From bdf76420705180d69377dae06f1621407fd12077 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A2=AB=E9=81=97=E5=BF=98=E7=9A=84=E8=AE=B0=E5=BF=86?= <32097237+YIWANG-sketch@users.noreply.github.com> Date: Fri, 1 Nov 2024 20:25:10 +0800 Subject: [PATCH] Add files via upload --- you_providers/garbledText.mjs | 49 +++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 you_providers/garbledText.mjs diff --git a/you_providers/garbledText.mjs b/you_providers/garbledText.mjs new file mode 100644 index 0000000..923a785 --- /dev/null +++ b/you_providers/garbledText.mjs @@ -0,0 +1,49 @@ +import crypto from 'crypto'; + +export function getRandomInt(min, max) { + min = Math.ceil(min); + max = Math.floor(max); + return Math.floor(Math.random() * (max - min + 1)) + min; +} + +// 插入乱码 +export function insertGarbledText(content) { + const enableGarbledStart = process.env.ENABLE_GARBLED_START === 'true'; + const enableGarbledEnd = process.env.ENABLE_GARBLED_END === 'true'; + + if (!enableGarbledStart && !enableGarbledEnd) { + return content; + } + + // 生成指定长度的随机乱码 + function generateGarbledText(length) { + return crypto.randomBytes(length).toString('hex'); + } + + let garbledContent = content; + + // 配置参数 + const startMinLength = parseInt(process.env.GARBLED_START_MIN_LENGTH) || 1000; + const startMaxLength = parseInt(process.env.GARBLED_START_MAX_LENGTH) || 5000; + + const endGarbledLength = parseInt(process.env.GARBLED_END_LENGTH) || 500; + + if (enableGarbledStart) { + const startGarbledLength = getRandomInt(startMinLength, startMaxLength); + + const byteLength = Math.ceil(startGarbledLength / 2); + + // 生成乱码 + const startPlaceholder = generateGarbledText(byteLength); + + garbledContent = startPlaceholder + '\n\n\n' + garbledContent.trim(); + } + + if (enableGarbledEnd) { + const byteLength = Math.ceil(endGarbledLength / 2); + const endPlaceholder = generateGarbledText(byteLength); + garbledContent = garbledContent.trim() + '\n\n\n' + endPlaceholder; + } + + return garbledContent; +} \ No newline at end of file