From 288851b4913c9f020cb922e88fc9d15c710fa004 Mon Sep 17 00:00:00 2001 From: KirDeer <1215496644@qq.com> Date: Mon, 28 Aug 2023 09:34:29 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=89=AB=E7=A0=81=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E7=A7=BB=E5=8A=A8=E7=AB=AF=E7=9A=84CK?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E8=B4=A6=E5=8F=B7,=E5=B8=A6sid=E5=8F=AF?= =?UTF-8?q?=E5=87=8F=E5=B0=8F=E6=8E=89=E5=8F=B7=E9=A2=91=E7=8E=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + lib/index.js | 3 +- lib/login.js | 65 +++++++++++++++++++++++++++++++++++++++++ main.js | 8 ++++- package.json | 6 ++-- script/build/pkg.sh | 2 +- script/docker/init.sh | 22 ++++++++++++++ script/qinglong/init.sh | 1 + 8 files changed, 103 insertions(+), 5 deletions(-) create mode 100644 lib/login.js diff --git a/README.md b/README.md index 50b322b6f..b93a891aa 100644 --- a/README.md +++ b/README.md @@ -122,6 +122,7 @@ buvid3亦可不填 使用随机生成值 acount 查看帐号信息 clear 清理动态和关注 update 检查更新 + login 扫码登录自动更新CK help 帮助信息 ``` diff --git a/lib/index.js b/lib/index.js index 3e9a472c1..f345c4fd9 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,3 +1,4 @@ +const { login } = require('./login'); const { isMe } = require('./check'); const { clear } = require('./clear'); const { start } = require('./lottery'); @@ -24,4 +25,4 @@ async function checkCookie(num) { } -module.exports = { start, isMe, clear, checkCookie, account } \ No newline at end of file +module.exports = { login, start, isMe, clear, checkCookie, account } \ No newline at end of file diff --git a/lib/login.js b/lib/login.js new file mode 100644 index 000000000..8e3b83a1b --- /dev/null +++ b/lib/login.js @@ -0,0 +1,65 @@ +const { readFileSync, writeFileSync } = require('fs'); +const { log } = require("./utils"); +const path = require('path'); +const env_path = path.join(path.resolve(__dirname, '..'), "env.js") + +/* +* 扫码登陆 +* @param {string} num +*/ +async function login(num) { + try { + const { pcLogin } = await require('@catlair/blogin'); + const loginInfo = await pcLogin(); + if (!loginInfo) { + log.error("登录状态","失败/取消"); + return; + } + log.info("登录状态","登录成功"); + JSON.stringify(loginInfo, null, 2); + const uid = `${loginInfo.mid}`; + const cookie = `${loginInfo.cookie}`; + log.info("账号UID", uid); + log.info("cookie", cookie); + if (replaceCookie(env_path, uid, cookie)) { + log.info("说明",`账号${num}已进行cookie自动更新,如未能生效请手动复制在env.js内替换。路径:${env_path}`); + return; + } + } catch (error) { + if (error?.message?.includes('Cannot find module')) { + log.error('请先运行 yarn add @catlair/blogin'); + return; + } + log.error(error); + } +} + +/* +* 正则检索uid更新cookie +* @param {string} filePath +* @param {string} uid +* @param {string} oldCK +*/ +async function replaceCookie(filePath,uid,oldCK) { + try { + const content = readFileSync(filePath, 'utf-8'); + const DedeUserID = `DedeUserID=${uid}`; + const reg = new RegExp(`['"]?COOKIE['"]?:\\s?['"](.*${DedeUserID}.*)['"]`, 'g'); + const newCK = content.replaceAll(reg, substring => { + let quote = substring.at(0) || ''; + /['"]/.test(quote) || (quote = ''); + const quote2 = oldCK.includes("'") + ? '"' + : substring.match(/^['"]?COOKIE['"]?:\s?(['"])/)?.[1] || '"'; + return `${quote}COOKIE${quote}: ${quote2}${oldCK}${quote2}`; + }); + if (content === newCK) return false; + writeFileSync(filePath, newCK); + return true; + } catch (error) { + log.error("替换错误",error); + } + return false; +} + +module.exports = { login } \ No newline at end of file diff --git a/main.js b/main.js index 767101046..d6dbdfe8e 100644 --- a/main.js +++ b/main.js @@ -1,4 +1,5 @@ const { version: ve, env_file, config_file, log, hasEnv, delay, hasFileOrDir, clearLotteryInfo } = require("./lib/utils"); +const {lottery_loop_wait} = require("./lib/data/config"); const metainfo = [ ` _ _ _ _____ _ _ `, @@ -49,7 +50,7 @@ async function main() { await global_var.init(COOKIE, NUMBER); /**引入基础功能 */ - const { start, isMe, clear, account, checkCookie } = require("./lib/index"); + const { start, isMe, clear, account, checkCookie, login } = require("./lib/index"); log.info('main', '当前为第' + NUMBER + '个账号'); log._cache.length = 0 @@ -79,6 +80,11 @@ async function main() { await clear(); } break; + case 'login': + log.info('登陆', '开始扫码'); + loop_wait = lottery_loop_wait; + await login(NUMBER); + break; case 'help': return help_msg case 'account': diff --git a/package.json b/package.json index 84d5620a3..f42bf9082 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "description": "自动参与B站动态抽奖", "main": "main.js", "scripts": { + "login": "node main.js login", "start": "node main.js start", "test": "node test/index.js", "clear": "node main.js clear", @@ -46,6 +47,7 @@ "chalk": "^4.1.2", "http-proxy-agent": "^7.0.0", "https-proxy-agent": "^7.0.0", - "nodemailer": "^6.7.0" + "nodemailer": "^6.7.0", + "@catlair/blogin": "^0.0.8" } -} +} \ No newline at end of file diff --git a/script/build/pkg.sh b/script/build/pkg.sh index 543610953..6be4f0862 100755 --- a/script/build/pkg.sh +++ b/script/build/pkg.sh @@ -49,7 +49,7 @@ for file in "$TARGET_DIR/"*; do cp $TEMPLATE_CONFIG_FILE "$TMPDIR.d/$CONFIG_FILE" cp $TEMPLATE_ENV_FILE "$TMPDIR.d/$ENV_FILE" if [ "$(echo $file | grep '.exe')" ]; then - BATS=("start" "check" "clear" "account" "update") + BATS=("start" "check" "clear" "account" "update" "login") for item in "${BATS[@]}"; do create_win_bat "${item}" >"$TMPDIR.d/$item.bat" done diff --git a/script/docker/init.sh b/script/docker/init.sh index dc6df5eff..a6f1e7848 100755 --- a/script/docker/init.sh +++ b/script/docker/init.sh @@ -188,3 +188,25 @@ docker image rm -f shanmite/lottery_auto_docker echo "see you next time!" EOF chmod +x remove_all.sh + +echo "create login.sh" +cat >login.sh < docker logs \$NAME" + echo "close this -> docker stop \$NAME" + echo "login \$NAME" + docker login \$NAME +fi +EOF +chmod +x login.sh \ No newline at end of file diff --git a/script/qinglong/init.sh b/script/qinglong/init.sh index 76697c13e..6cc50c7a5 100644 --- a/script/qinglong/init.sh +++ b/script/qinglong/init.sh @@ -40,6 +40,7 @@ create start create check create clear create account +create login cd $NAME npm i