From dcf2092e931e622b68eb572ae8a0a54ed83b1f5a Mon Sep 17 00:00:00 2001 From: Faker <Faker@apple.com> Date: Thu, 12 May 2022 11:24:09 +0800 Subject: [PATCH] Revert "Update jdCookie.js" This reverts commit 339348956c5b58cd6631fe5c66c838ca783ce046. --- jdCookie.js | 80 ++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 73 insertions(+), 7 deletions(-) diff --git a/jdCookie.js b/jdCookie.js index e097004..a344b17 100644 --- a/jdCookie.js +++ b/jdCookie.js @@ -1,4 +1,13 @@ /* +================================================================================ +魔改自 https://github.com/shufflewzc/faker2/blob/main/jdCookie.js +修改内容:与task_before.sh配合,由task_before.sh设置要设置要做互助的活动的 ShareCodeConfigName 和 ShareCodeEnvName 环境变量, + 然后在这里实际解析/ql/log/.ShareCode中该活动对应的配置信息(由code.sh生成和维护),注入到nodejs的环境变量中 +修改原因:原先的task_before.sh直接将互助信息注入到shell的env中,在ck超过45以上时,互助码环境变量过大会导致调用一些系统命令 + (如date/cat)时报 Argument list too long,而在node中修改环境变量不会受这个限制,也不会影响外部shell环境,确保脚本可以正常运行 +魔改作者:风之凌殇 +================================================================================ + 此文件为Node.js专用。其他用户请忽略 */ //此处填写京东账号cookie。 @@ -14,7 +23,7 @@ if (process.env.JD_COOKIE) { CookieJDs = [process.env.JD_COOKIE]; } } -if (JSON.stringify(process.env).indexOf('GITHUB') > -1) { +if (JSON.stringify(process.env).indexOf('GITHUB')>-1) { console.log(`请勿使用github action运行此脚本,无论你是从你自己的私库还是其他哪里拉取的源代码,都会导致我被封号\n`); !(async () => { await require('./sendNotify').sendNotify('提醒', `请勿使用github action、滥用github资源会封我仓库以及账号`) @@ -22,14 +31,71 @@ if (JSON.stringify(process.env).indexOf('GITHUB') > -1) { })() } CookieJDs = [...new Set(CookieJDs.filter(item => !!item))] -if (!require.main.filename.includes('.ts')) { - console.log(`\n====================共${CookieJDs.length}个京东账号Cookie=========\n`); - console.log(`================== ${new Date().toLocaleString('zh-CN', {timeZone: 'Asia/Shanghai'})} =====================\n`) -} -if (process.env.JD_DEBUG && process.env.JD_DEBUG === 'false') console.log = () => { -}; +console.log(`\n====================共${CookieJDs.length}个京东账号Cookie=========\n`); +console.log(`==================脚本执行- 北京时间(UTC+8):${new Date(new Date().getTime() + new Date().getTimezoneOffset()*60*1000 + 8*60*60*1000).toLocaleString('zh', {hour12: false}).replace(' 24:',' 00:')}=====================\n`) +if (process.env.JD_DEBUG && process.env.JD_DEBUG === 'false') console.log = () => {}; for (let i = 0; i < CookieJDs.length; i++) { if (!CookieJDs[i].match(/pt_pin=(.+?);/) || !CookieJDs[i].match(/pt_key=(.+?);/)) console.log(`\n提示:京东cookie 【${CookieJDs[i]}】填写不规范,可能会影响部分脚本正常使用。正确格式为: pt_key=xxx;pt_pin=xxx;(分号;不可少)\n`); const index = (i + 1 === 1) ? '' : (i + 1); exports['CookieJD' + index] = CookieJDs[i].trim(); } + +// 以下为注入互助码环境变量(仅nodejs内起效)的代码 +function SetShareCodesEnv(nameConfig = "", envName = "") { + let rawCodeConfig = {} + + // 读取互助码 + let shareCodeLogPath = `${process.env.QL_DIR}/log/.ShareCode/${nameConfig}.log` + let fs = require('fs') + if (fs.existsSync(shareCodeLogPath)) { + // 因为faker2目前没有自带ini,改用已有的dotenv来解析 + // // 利用ini模块读取原始互助码和互助组信息 + // let ini = require('ini') + // rawCodeConfig = ini.parse(fs.readFileSync(shareCodeLogPath, 'utf-8')) + + // 使用env模块 + require('dotenv').config({path: shareCodeLogPath}) + rawCodeConfig = process.env + } + + // 解析每个用户的互助码 + let codes = {} + Object.keys(rawCodeConfig).forEach(function (key) { + if (key.startsWith(`My${nameConfig}`)) { + codes[key] = rawCodeConfig[key] + } + }); + + // 解析每个用户要帮助的互助码组,将用户实际的互助码填充进去 + let helpOtherCodes = {} + Object.keys(rawCodeConfig).forEach(function (key) { + if (key.startsWith(`ForOther${nameConfig}`)) { + helpCode = rawCodeConfig[key] + for (const [codeEnv, codeVal] of Object.entries(codes)) { + helpCode = helpCode.replace("${" + codeEnv + "}", codeVal) + } + + helpOtherCodes[key] = helpCode + } + }); + + // 按顺序用&拼凑到一起,并放入环境变量,供目标脚本使用 + let shareCodes = [] + let totalCodeCount = Object.keys(helpOtherCodes).length + for (let idx = 1; idx <= totalCodeCount; idx++) { + shareCodes.push(helpOtherCodes[`ForOther${nameConfig}${idx}`]) + } + let shareCodesStr = shareCodes.join('&') + process.env[envName] = shareCodesStr + + console.info(`【风之凌殇】 友情提示:为避免ck超过45以上时,互助码环境变量过大而导致调用一些系统命令(如date/cat)时报 Argument list too long,改为在nodejs中设置 ${nameConfig} 的 互助码环境变量 ${envName},共计 ${totalCodeCount} 组互助码,总大小为 ${shareCodesStr.length}`) +} + +// 若在task_before.sh 中设置了要设置互助码环境变量的活动名称和环境变量名称信息,则在nodejs中处理,供活动使用 +let nameConfig = process.env.ShareCodeConfigName +let envName = process.env.ShareCodeEnvName +if (nameConfig && envName) { + SetShareCodesEnv(nameConfig, envName) +} else { + console.debug(`faker 友情提示:您的脚本正常运行中`) +}