mirror of
https://github.com/wq-h/qinglong.git
synced 2025-12-17 15:25:15 +08:00
优化脚本
This commit is contained in:
@@ -8,127 +8,177 @@ export Ikuuu_HOST="ikuuu.one"
|
||||
cron: 33 08 * * *
|
||||
const $ = new Env("ikuuu 机场签到");
|
||||
*/
|
||||
const { sendNotify } = require("./sendNotify"); // 引入 sendNotify.js 模块
|
||||
const { sendNotify } = require("./sendNotify");
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
const host = process.env.Ikuuu_HOST || "ikuuu.one";
|
||||
|
||||
const protocolPrefix = "https://";
|
||||
const logInUrl = `${protocolPrefix}${host}/auth/login`;
|
||||
const checkInUrl = `${protocolPrefix}${host}/user/checkin`;
|
||||
|
||||
function parseCookie(rawCookie) {
|
||||
let cookieSets = rawCookie.split("path=/,");
|
||||
|
||||
const cookies = {};
|
||||
|
||||
cookieSets.forEach((cookie) => {
|
||||
const match = cookie.match(/^([^=]+)=(.*?);/);
|
||||
if (match) {
|
||||
const fieldName = match[1].trim();
|
||||
let fieldValue = match[2].trim();
|
||||
|
||||
fieldValue = decodeURIComponent(fieldValue);
|
||||
|
||||
if (!cookies[fieldName]) {
|
||||
cookies[fieldName] = fieldValue;
|
||||
}
|
||||
// 配置类
|
||||
class Config {
|
||||
static get HOST() {
|
||||
return process.env.Ikuuu_HOST || "ikuuu.one";
|
||||
}
|
||||
});
|
||||
|
||||
return cookies;
|
||||
static get PROTOCOL_PREFIX() {
|
||||
return "https://";
|
||||
}
|
||||
|
||||
static get LOGIN_URL() {
|
||||
return `${Config.PROTOCOL_PREFIX}${Config.HOST}/auth/login`;
|
||||
}
|
||||
|
||||
static get CHECKIN_URL() {
|
||||
return `${Config.PROTOCOL_PREFIX}${Config.HOST}/user/checkin`;
|
||||
}
|
||||
}
|
||||
|
||||
function generateCookieStr(cookieObject) {
|
||||
return Object.entries(cookieObject)
|
||||
.map(([key, value]) => `${key}=${encodeURIComponent(value)}`)
|
||||
.join("; ");
|
||||
// 日志配置
|
||||
const logStream = fs.createWriteStream(path.join(__dirname, 'ikuuu.log'), { flags: 'a' });
|
||||
|
||||
function log(level, message) {
|
||||
const timestamp = new Date().toISOString();
|
||||
const logMessage = `[${timestamp}] [${level}] ${message}\n`;
|
||||
logStream.write(logMessage);
|
||||
console.log(logMessage);
|
||||
}
|
||||
|
||||
async function logIn(email, passwd) {
|
||||
console.log(`Logging in with email: ${email}...`);
|
||||
// Cookie 工具类
|
||||
class CookieUtil {
|
||||
static parseCookie(rawCookie) {
|
||||
let cookieSets = rawCookie.split("path=/,");
|
||||
const cookies = {};
|
||||
|
||||
let formData = new FormData();
|
||||
formData.append("host", host);
|
||||
formData.append("email", email);
|
||||
formData.append("passwd", passwd);
|
||||
formData.append("code", "");
|
||||
formData.append("remember_me", "off");
|
||||
cookieSets.forEach((cookie) => {
|
||||
const match = cookie.match(/^([^=]+)=(.*?);/);
|
||||
if (match) {
|
||||
const fieldName = match[1].trim();
|
||||
let fieldValue = match[2].trim();
|
||||
fieldValue = decodeURIComponent(fieldValue);
|
||||
|
||||
let response = await fetch(logInUrl, {
|
||||
method: "POST",
|
||||
body: formData,
|
||||
});
|
||||
if (!cookies[fieldName]) {
|
||||
cookies[fieldName] = fieldValue;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
let rawCookie = response.headers.get("set-cookie");
|
||||
return cookies;
|
||||
}
|
||||
|
||||
let responseJson = await response.json();
|
||||
|
||||
if (responseJson) {
|
||||
console.log(responseJson.msg);
|
||||
}
|
||||
|
||||
return parseCookie(rawCookie);
|
||||
static generateCookieStr(cookieObject) {
|
||||
return Object.entries(cookieObject)
|
||||
.map(([key, value]) => `${key}=${encodeURIComponent(value)}`)
|
||||
.join("; ");
|
||||
}
|
||||
}
|
||||
|
||||
function checkIn(cookie) {
|
||||
return fetch(checkInUrl, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
Cookie: generateCookieStr(cookie),
|
||||
},
|
||||
})
|
||||
.then((res) => res.json())
|
||||
.then((resJson) => {
|
||||
if (resJson) {
|
||||
console.log(resJson.msg);
|
||||
}
|
||||
});
|
||||
// Ikuuu 客户端类
|
||||
class IkuuuClient {
|
||||
constructor(email, password) {
|
||||
this.email = email;
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
async login() {
|
||||
log('INFO', `Logging in with email: ${this.email}...`);
|
||||
|
||||
let formData = new FormData();
|
||||
formData.append("host", Config.HOST);
|
||||
formData.append("email", this.email);
|
||||
formData.append("passwd", this.password);
|
||||
formData.append("code", "");
|
||||
formData.append("remember_me", "off");
|
||||
|
||||
try {
|
||||
let response = await fetch(Config.LOGIN_URL, {
|
||||
method: "POST",
|
||||
body: formData,
|
||||
});
|
||||
|
||||
let rawCookie = response.headers.get("set-cookie");
|
||||
let responseJson = await response.json();
|
||||
|
||||
if (responseJson) {
|
||||
log('INFO', responseJson.msg);
|
||||
}
|
||||
|
||||
return CookieUtil.parseCookie(rawCookie);
|
||||
} catch (error) {
|
||||
log('ERROR', `Login failed for ${this.email}: ${error.message}`);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async checkIn(cookie) {
|
||||
try {
|
||||
let response = await fetch(Config.CHECKIN_URL, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
Cookie: CookieUtil.generateCookieStr(cookie),
|
||||
},
|
||||
});
|
||||
|
||||
let responseJson = await response.json();
|
||||
if (responseJson) {
|
||||
log('INFO', responseJson.msg);
|
||||
}
|
||||
} catch (error) {
|
||||
log('ERROR', `Check-in failed for ${this.email}: ${error.message}`);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 延迟函数,单位为毫秒
|
||||
function delay(ms) {
|
||||
return new Promise(resolve => setTimeout(resolve, ms));
|
||||
return new Promise(resolve => setTimeout(resolve, ms));
|
||||
}
|
||||
|
||||
async function main() {
|
||||
let emails = process.env.Ikuuu_EMAIL;
|
||||
let passwords = process.env.Ikuuu_PASSWD;
|
||||
let emails = process.env.Ikuuu_EMAIL;
|
||||
let passwords = process.env.Ikuuu_PASSWD;
|
||||
|
||||
if (!emails || !passwords) {
|
||||
console.log("ENV ERROR: Please set both Ikuuu_EMAIL and Ikuuu_PASSWD.");
|
||||
process.exit(1);
|
||||
}
|
||||
if (!emails || !passwords) {
|
||||
log('ERROR', "ENV ERROR: Please set both Ikuuu_EMAIL and Ikuuu_PASSWD.");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
let emailList = emails.split(";");
|
||||
let passwdList = passwords.split(";");
|
||||
let emailList = emails.split(";");
|
||||
let passwdList = passwords.split(";");
|
||||
|
||||
if (emailList.length !== passwdList.length) {
|
||||
console.log("Error: The number of emails does not match the number of passwords.");
|
||||
process.exit(1);
|
||||
}
|
||||
if (emailList.length !== passwdList.length) {
|
||||
log('ERROR', "Error: The number of emails does not match the number of passwords.");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// 创建一个通知数组,避免多次发送相同的消息
|
||||
let notifications = [];
|
||||
let notifications = [];
|
||||
|
||||
// 遍历每个账号执行登录并签到
|
||||
for (let i = 0; i < emailList.length; i++) {
|
||||
let email = emailList[i];
|
||||
let passwd = passwdList[i];
|
||||
let cookie = await logIn(email, passwd);
|
||||
await checkIn(cookie);
|
||||
for (let i = 0; i < emailList.length; i++) {
|
||||
let email = emailList[i];
|
||||
let passwd = passwdList[i];
|
||||
let client = new IkuuuClient(email, passwd);
|
||||
|
||||
// 每个账号的操作只添加一条通知
|
||||
notifications.push(`账号 ${email} 登录成功,签到完成`);
|
||||
try {
|
||||
let cookie = await client.login();
|
||||
await client.checkIn(cookie);
|
||||
notifications.push(`账号 ${email} 登录成功,签到完成`);
|
||||
} catch (error) {
|
||||
notifications.push(`账号 ${email} 操作失败: ${error.message}`);
|
||||
}
|
||||
|
||||
// 每次登录后延迟 2 秒
|
||||
await delay(2000); // 延迟 2 秒
|
||||
}
|
||||
await delay(2000); // 延迟 2 秒
|
||||
}
|
||||
|
||||
// 合并所有通知为一条消息,避免多次发送
|
||||
const notificationMessage = notifications.join("\n");
|
||||
// 过滤掉 undefined 值
|
||||
const notificationMessage = notifications
|
||||
.filter(msg => msg !== undefined)
|
||||
.join("\n");
|
||||
|
||||
// 发送合并后的消息通知
|
||||
sendNotify(`多个账号操作完成:\n${notificationMessage}`);
|
||||
// 调试:打印通知数组
|
||||
console.log("通知数组内容:", notifications);
|
||||
|
||||
sendNotify(`多个账号操作完成:\n${notificationMessage}`);
|
||||
}
|
||||
|
||||
main();
|
||||
main().catch(error => {
|
||||
log('ERROR', `Main function failed: ${error.message}`);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user