forked from lopins/serv00-auto-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
login.js
84 lines (68 loc) · 2.74 KB
/
login.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
const fs = require('fs');
const puppeteer = require('puppeteer');
function formatToISO(date) {
return date.toISOString().replace('T', ' ').replace('Z', '').replace(/\.\d{3}Z/, '');
}
async function delayTime(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
(async () => {
// 读取 accounts.json 中的 JSON 字符串
const accountsJson = fs.readFileSync('accounts.json', 'utf-8');
const accounts = JSON.parse(accountsJson);
for (const account of accounts) {
const { username, password, panelnum } = account;
const browser = await puppeteer.launch({ headless: false });
const page = await browser.newPage();
let url = `https://panel${panelnum}.serv00.com/login/?next=/`;
try {
// 修改网址为新的登录页面
await page.goto(url);
// 清空用户名输入框的原有值
const usernameInput = await page.$('#id_username');
if (usernameInput) {
await usernameInput.click({ clickCount: 3 }); // 选中输入框的内容
await usernameInput.press('Backspace'); // 删除原来的值
}
// 输入实际的账号和密码
await page.type('#id_username', username);
await page.type('#id_password', password);
// 提交登录表单
const loginButton = await page.$('#submit');
if (loginButton) {
await loginButton.click();
} else {
throw new Error('无法找到登录按钮');
}
// 等待登录成功(如果有跳转页面的话)
await page.waitForNavigation();
// 判断是否登录成功
const isLoggedIn = await page.evaluate(() => {
const logoutButton = document.querySelector('a[href="/logout/"]');
return logoutButton !== null;
});
if (isLoggedIn) {
// 获取当前的UTC时间和北京时间
const nowUtc = formatToISO(new Date());// UTC时间
const nowBeijing = formatToISO(new Date(new Date().getTime() + 8 * 60 * 60 * 1000)); // 北京时间东8区,用算术来搞
console.log(`账号 ${username} 于北京时间 ${nowBeijing}(UTC时间 ${nowUtc})登录成功!`);
} else {
console.error(`账号 ${username} 登录失败,请检查账号和密码是否正确。`);
}
} catch (error) {
console.error(`账号 ${username} 登录时出现错误: ${error}`);
} finally {
// 关闭页面和浏览器
await page.close();
await browser.close();
// 用户之间添加随机延时
const delay = Math.floor(Math.random() * 8000) + 1000; // 随机延时1秒到8秒之间
await delayTime(delay);
}
}
console.log('所有账号登录完成!');
})();
// 自定义延时函数
function delayTime(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}