Skip to content

Commit

Permalink
feat: 支持多个邮箱密码(#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
zb81 committed Jun 7, 2024
1 parent 235cb1b commit 9a41bac
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 41 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@

### 第一步:添加邮箱、密码到环境变量

打开青龙面板,创建 `IKUUU_EMAIL``IKUUU_PWD` 变量。
打开青龙面板,创建 `IKUUU_EMAIL``IKUUU_PWD` 变量,多个邮箱或密码请换行。

> 注意:邮箱和密码数量必须一致且一一对应。
<img alt="picture 1" src="https://cdn.zb81.icu/95635412513591a0f93dd1b6c96662a8fc1a6b5dc86a13a24ef0d17660af73d1.png" />

<img alt="picture 2" src="https://cdn.zb81.icu/8ebbee42e5fe546ae7d18efcb83ab4f6411b2317aa141db671eec74290d1dd38.png" />

<img alt="picture 0" src="https://cdn.zb81.icu/163bc6f247b4f29418ee9b07ac584934d9fd476304a6661240531aec98e1903d.png" />


### 第三步:添加依赖项
Expand All @@ -27,7 +32,8 @@ ql repo https://github.com/zb81/ql-ikuuu.git "checkin|traffic" "" "qlApi|utils"

## 通知效果

<img alt="picture 0" src="https://cdn.zb81.icu/8027e8dbec62f99119e65edf308998762bf0697bda34cc6d561320961e5dd7bb.jpg" />
<img alt="picture 3" src="https://cdn.zb81.icu/cdcb0c957db045c6b5b383d78814f8ce8a4a8ce7775911b4a3450abfeafc3b1e.jpg" />


## 原理

Expand Down
32 changes: 23 additions & 9 deletions checkin.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const axios = require('axios')
const { getCookie, getTraffic } = require('./utils')
const { getCookie, getTraffic, getEmailAndPwdList } = require('./utils')
const notify = require('./sendNotify')

const checkinURL = 'https://ikuuu.pw/user/checkin'
Expand All @@ -21,13 +21,27 @@ async function checkin(cookie) {
}
}

(async () => {
const cookie = await getCookie()
async function run() {
const [emailList, pwdList] = await getEmailAndPwdList()
const messages = []
for (let i = 0; i < emailList.length; i++) {
const email = emailList[i]
const pwd = pwdList[i]
let msg = `邮箱:${emailList[i]}`
const cookie = await getCookie(email, pwd)
if (cookie.includes('登录失败')) {
msg += `\n${cookie}`
messages.push(msg)
continue
}
const checkinRes = await checkin(cookie)
msg += `\n${checkinRes}`
const arr = await getTraffic(cookie)
msg += `\n${arr.join('\n')}`
messages.push(msg)
}

let message = await checkin(cookie)
message += '\n\n'
const traffic = await getTraffic(cookie)
message += traffic.join('\n')
await notify.sendNotify(`iKuuu VPN 签到通知`, messages.join('\n\n========================\n\n'))
}

await notify.sendNotify(`iKuuu VPN 签到通知`, message)
})()
run()
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ql-ikuuu",
"version": "1.2.0",
"version": "1.3.0",
"private": "true",
"description": "iKuuu VPN 签到、今日流量",
"author": "Zhu Bei <[email protected]>",
Expand Down
3 changes: 0 additions & 3 deletions test/auth-test.js

This file was deleted.

29 changes: 23 additions & 6 deletions traffic.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
const { getCookie, getTraffic } = require('./utils')
const { getCookie, getTraffic, getEmailAndPwdList } = require('./utils')
const notify = require('./sendNotify');

(async () => {
const cookie = await getCookie()
const arr = await getTraffic(cookie)
await notify.sendNotify(`iKuuu VPN 今日流量统计`, arr.join('\n'))
})()
async function run() {
const [emailList, pwdList] = await getEmailAndPwdList()
const messages = []
for (let i = 0; i < emailList.length; i++) {
const email = emailList[i]
const pwd = pwdList[i]
let msg = `邮箱:${emailList[i]}`
const cookie = await getCookie(email, pwd)
if (cookie.includes('登录失败')) {
msg += `\n${cookie}`
messages.push(msg)
continue
}
const arr = await getTraffic(cookie)
msg += `\n${arr.join('\n')}`
messages.push(msg)
}

await notify.sendNotify(`iKuuu VPN 今日流量统计`, messages.join('\n\n========================\n\n'))
}

run()
66 changes: 47 additions & 19 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,72 @@ const infoURL = 'https://ikuuu.pw/user'
const todayTrafficReg = /\n.*\s(\d+\.?\d*)([M|G]B)/
const restTrafficReg = /[\s\S]*<span class="counter">(\d+\.?\d*)<\/span> ([M|G]B)/

/** 获取邮箱和密码 */
async function getEmailAndPwd() {
function extractArr(envStr) {
if (typeof envStr === 'string') {
envStr = envStr.trim()
}

if (Array.isArray(envStr)) {
return envStr
} else if (envStr.includes('\n')) {
return envStr.split('\n').map(v => v.trim()).filter(Boolean)
}
return [envStr]
}

/** 获取邮箱和密码数组 */
async function getEmailAndPwdList() {
let instance = null
try {
instance = await initInstance()
} catch (e) { }

let email = process.env.IKUUU_EMAIL
let pwd = process.env.IKUUU_PWD
let emailEnv = process.env.IKUUU_EMAIL || []
let pwdEnv = process.env.IKUUU_PWD || []

try {
if (instance) {
email = await getEnv(instance, 'IKUUU_EMAIL')
pwd = await getEnv(instance, 'IKUUU_PWD')
emailEnv = await getEnv(instance, 'IKUUU_EMAIL')
pwdEnv = await getEnv(instance, 'IKUUU_PWD')
}
} catch (e) { }
} catch { }

const emailList = extractArr(emailEnv)
const pwdList = extractArr(pwdEnv)

if (!email || !pwd) {
const emailLen = emailList.length
const pwdLen = pwdList.length

if (!emailLen || !pwdLen) {
console.log('未获取到邮箱和密码, 程序终止')
process.exit(1)
}

console.log('✅ 成功读取环境变量')
if (emailLen !== pwdLen) {
console.log('邮箱和密码数量不一致, 程序终止')
process.exit(1)
}

console.log(`✅ 成功读取 ${emailLen} 对邮箱和密码`)

return [email, pwd]
return [emailList, pwdList]
}

/** 登录获取 cookie */
async function getCookie() {
const [email, pwd] = await getEmailAndPwd()
async function getCookie(email, pwd) {
const formData = new FormData()
formData.append('email', email)
formData.append('passwd', pwd)
const res = await axios(loginURL, {
method: 'POST',
data: formData
})
console.log('✅ 登录成功!')
return res.headers['set-cookie'].join('; ')
try {
const res = await axios(loginURL, {
method: 'POST',
data: formData
})
if (res.data.ret === 0) {
return `❌ 登录失败:${res.data.msg}`
}
return res.headers['set-cookie'].join('; ')
} catch { }
}

/** 获取流量 */
Expand All @@ -70,4 +98,4 @@ async function getTraffic(cookie) {
}
}

module.exports = { getCookie, getTraffic }
module.exports = { getCookie, getTraffic, getEmailAndPwdList }

0 comments on commit 9a41bac

Please sign in to comment.