This repository was archived by the owner on Apr 26, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathaction.js
More file actions
623 lines (534 loc) · 22.8 KB
/
Copy pathaction.js
File metadata and controls
623 lines (534 loc) · 22.8 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
const { chromium } = require('playwright-extra');
const stealth = require('puppeteer-extra-plugin-stealth')();
const axios = require('axios');
const cheerio = require('cheerio');
const fs = require('fs');
const path = require('path');
const { spawn } = require('child_process');
const http = require('http');
const os = require('os');
// ==========================================
// Part 1: Configuration & Helpers
// ==========================================
// Enable stealth plugin
chromium.use(stealth);
const RENEW_DAYS = 10;
const INJECTED_SCRIPT = `
(function() {
if (window.self === window.top) return;
try {
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
let screenX = getRandomInt(800, 1200);
let screenY = getRandomInt(400, 600);
Object.defineProperty(MouseEvent.prototype, 'screenX', { value: screenX });
Object.defineProperty(MouseEvent.prototype, 'screenY', { value: screenY });
} catch (e) { }
try {
const originalAttachShadow = Element.prototype.attachShadow;
Element.prototype.attachShadow = function(init) {
const shadowRoot = originalAttachShadow.call(this, init);
if (shadowRoot) {
const checkAndReport = () => {
const checkbox = shadowRoot.querySelector('input[type="checkbox"]');
if (checkbox) {
const rect = checkbox.getBoundingClientRect();
if (rect.width > 0 && rect.height > 0 && window.innerWidth > 0 && window.innerHeight > 0) {
const xRatio = (rect.left + rect.width / 2) / window.innerWidth;
const yRatio = (rect.top + rect.height / 2) / window.innerHeight;
window.__turnstile_data = { xRatio, yRatio };
return true;
}
}
return false;
};
if (!checkAndReport()) {
const observer = new MutationObserver(() => {
if (checkAndReport()) observer.disconnect();
});
observer.observe(shadowRoot, { childList: true, subtree: true });
}
}
return shadowRoot;
};
} catch (e) { }
})();
`;
// Helper to sleep
const sleep = (min = 3000, max = 8000) => {
const delay = Math.floor(Math.random() * (max - min + 1)) + min;
return new Promise(resolve => setTimeout(resolve, delay));
};
function getUsers() {
try {
if (process.env.USERS_JSON) {
const parsed = JSON.parse(process.env.USERS_JSON);
return Array.isArray(parsed) ? parsed : (parsed.users || []);
}
// Fallback for local testing
const localUsersPath = path.join(__dirname, 'users.json');
if (fs.existsSync(localUsersPath)) {
console.log('正在从本地 users.json 文件加载用户...');
const fileContent = fs.readFileSync(localUsersPath, 'utf8');
const parsed = JSON.parse(fileContent);
return Array.isArray(parsed) ? parsed : (parsed.users || []);
}
} catch (e) {
console.error('解析 USERS_JSON 或 users.json 时出错:', e);
}
return [];
}
// ==========================================
// Part 2: Renewal Logic (HidenCloudBot)
// ==========================================
class HidenCloudBot {
constructor(page, username) {
this.page = page;
this.username = username;
this.services = [];
this.logMsg = [];
this.csrfToken = '';
}
log(msg) {
console.log(`[${this.username}] ${msg}`);
this.logMsg.push(msg);
}
// Wrap fetch inside the browser context
async request(method, url, data = null, extraHeaders = {}) {
// Construct full URL if needed
const targetUrl = url.startsWith('http') ? url : `https://dash.hidencloud.com${url.startsWith('/') ? '' : '/'}${url}`;
// Prepare Headers - Browser handles User-Agent, Cookie, Host, etc.
// We only add specific functional headers
const headers = { ...extraHeaders };
if (method === 'POST' && !headers['Content-Type']) {
headers['Content-Type'] = 'application/x-www-form-urlencoded';
}
// CSRF Token if we have it
if (this.csrfToken && !headers['X-CSRF-TOKEN']) {
headers['X-CSRF-TOKEN'] = this.csrfToken;
}
try {
// Execute fetch inside the browser
const result = await this.page.evaluate(async ({ url, method, data, headers }) => {
const options = {
method: method,
headers: headers,
redirect: 'follow' // Let browser verify redirects automatically
};
if (data) options.body = data;
const res = await fetch(url, options);
const text = await res.text();
return {
status: res.status,
url: res.url, // Final URL after redirects
headers: {}, // We can't iterate headers easily in all browsers, but usually not needed for logic if we trust auto-redirects
data: text
};
}, { url: targetUrl, method, data: data ? data.toString() : null, headers });
// Normalize result to match our previous axios structure
result.finalUrl = result.url;
return result;
} catch (err) {
throw new Error(`浏览器请求错误: ${err.message}`);
}
}
extractTokens($) {
const metaToken = $('meta[name="csrf-token"]').attr('content');
if (metaToken) this.csrfToken = metaToken;
}
async init() {
this.log('🔍 正在验证 API 登录状态 (Browser Mode)...');
try {
await sleep(2000); // Wait a bit
const res = await this.request('GET', '/dashboard');
// Check for login redirection
if (res.finalUrl.includes('/login') || res.finalUrl.includes('/auth')) {
this.log('❌ 浏览器似乎未保持登录状态');
return false;
}
const $ = cheerio.load(res.data);
const title = $('title').text().trim();
this.log(`调试: 页面标题 = "${title}"`);
if (title.includes('Just a moment') || title.includes('Attention Required')) {
this.log('⚠️ 依然检测到拦截页面,请检查 Turnstile');
return false;
}
this.extractTokens($);
// Parse Services
$('a[href*="/service/"]').each((i, el) => {
const href = $(el).attr('href');
const match = href.match(/\/service\/(\d+)\/manage/);
if (match) {
this.services.push({ id: match[1], url: href });
}
});
// deduplicate
this.services = this.services.filter((v, i, a) => a.findIndex(t => t.id === v.id) === i);
this.log(`✅ API 连接成功,发现 ${this.services.length} 个服务`);
return true;
} catch (e) {
this.log(`❌ 初始化异常: ${e.message}`);
return false;
}
}
async processService(service) {
await sleep(2000, 4000);
this.log(`>>> 处理服务 ID: ${service.id}`);
try {
const manageRes = await this.request('GET', `/service/${service.id}/manage`);
const $ = cheerio.load(manageRes.data);
const formToken = $('input[name="_token"]').val();
this.log(`📅 提交续期 (${RENEW_DAYS}天)...`);
await sleep(1000, 2000);
const params = new URLSearchParams();
params.append('_token', formToken);
params.append('days', RENEW_DAYS);
const res = await this.request('POST', `/service/${service.id}/renew`, params.toString());
if (res.finalUrl && res.finalUrl.includes('/invoice/')) {
this.log(`⚡️ 续期成功,前往支付`);
await this.performPayFromHtml(res.data, res.finalUrl);
} else {
this.log('⚠️ 续期后未跳转,检查账单列表...');
await this.checkAndPayInvoices(service.id);
}
} catch (e) {
this.log(`❌ 处理异常: ${e.message}`);
}
}
async checkAndPayInvoices(serviceId) {
await sleep(2000, 3000);
try {
const res = await this.request('GET', `/service/${serviceId}/invoices?where=unpaid`);
const $ = cheerio.load(res.data);
const invoiceLinks = [];
$('a[href*="/invoice/"]').each((i, el) => {
const href = $(el).attr('href');
if (href && !href.includes('download')) invoiceLinks.push(href);
});
const uniqueInvoices = [...new Set(invoiceLinks)];
if (uniqueInvoices.length === 0) {
this.log(`✅ 无未支付账单`);
return;
}
for (const url of uniqueInvoices) {
await this.paySingleInvoice(url);
await sleep(3000, 5000);
}
} catch (e) {
this.log(`❌ 查账单出错: ${e.message}`);
}
}
async paySingleInvoice(url) {
try {
this.log(`📄 打开账单: ${url}`);
const res = await this.request('GET', url);
await this.performPayFromHtml(res.data, url);
} catch (e) {
this.log(`❌ 访问失败: ${e.message}`);
}
}
async performPayFromHtml(html, currentUrl) {
const $ = cheerio.load(html);
let targetForm = null;
let targetAction = '';
$('form').each((i, form) => {
const btnText = $(form).find('button').text().trim().toLowerCase();
const action = $(form).attr('action');
if (btnText.includes('pay') && action && !action.includes('balance/add')) {
targetForm = $(form);
targetAction = action;
return false;
}
});
if (!targetForm) {
this.log(`⚪ 页面未找到支付表单 (可能已支付)`);
return;
}
const payParams = new URLSearchParams();
targetForm.find('input').each((i, el) => {
const name = $(el).attr('name');
const value = $(el).val();
if (name) payParams.append(name, value || '');
});
this.log(`💳 提交支付...`);
try {
// No Referer needed for Browser Fetch (it handles it, or we rely on standard behavior)
// But we can add it if needed
const res = await this.request('POST', targetAction, payParams.toString());
if (res.status === 200) {
this.log(`✅ 支付成功!`);
} else {
this.log(`⚠️ 支付响应: ${res.status}`);
}
} catch (e) {
this.log(`❌ 支付失败: ${e.message}`);
}
}
}
// ==========================================
// Part 3: Browser Login Logic (Integrated from login.js)
// ==========================================
const CHROME_PATH = process.env.CHROME_PATH || '/usr/bin/google-chrome';
const DEBUG_PORT = 9222;
function checkPort(port) {
return new Promise((resolve) => {
const req = http.get(`http://localhost:${port}/json/version`, (res) => {
resolve(true);
});
req.on('error', () => resolve(false));
req.end();
});
}
async function launchChrome() {
if (await checkPort(DEBUG_PORT)) {
console.log('Chrome 已启动。');
return;
}
console.log(`正在启动 Chrome (分离模式)...`);
// Use OS temp directory for user data or specific tmp path
const userDataDir = path.join(os.tmpdir(), 'chrome_user_data_' + Date.now());
const args = [
`--remote-debugging-port=${DEBUG_PORT}`,
'--no-first-run',
'--no-default-browser-check',
'--disable-gpu',
'--window-size=1280,720',
'--no-sandbox',
'--disable-setuid-sandbox',
'--disable-setuid-sandbox',
'--disable-setuid-sandbox',
`--user-data-dir=${userDataDir}`,
'--disable-dev-shm-usage'
];
const chrome = spawn(CHROME_PATH, args, {
detached: true,
stdio: 'ignore'
});
chrome.unref();
console.log('正在等待 Chrome 初始化...');
for (let i = 0; i < 20; i++) {
if (await checkPort(DEBUG_PORT)) break;
await new Promise(r => setTimeout(r, 1000));
}
if (!await checkPort(DEBUG_PORT)) {
throw new Error('Chrome 启动失败');
}
}
async function attemptTurnstileCdp(page) {
const frames = page.frames();
for (const frame of frames) {
try {
const data = await frame.evaluate(() => window.__turnstile_data).catch(() => null);
if (data) {
console.log('>> 在框架中发现 Turnstile。比例:', data);
const iframeElement = await frame.frameElement();
if (!iframeElement) continue;
const box = await iframeElement.boundingBox();
if (!box) continue;
const clickX = box.x + (box.width * data.xRatio);
const clickY = box.y + (box.height * data.yRatio);
const client = await page.context().newCDPSession(page);
await client.send('Input.dispatchMouseEvent', {
type: 'mousePressed',
x: clickX,
y: clickY,
button: 'left',
clickCount: 1
});
await new Promise(r => setTimeout(r, 50 + Math.random() * 100));
await client.send('Input.dispatchMouseEvent', {
type: 'mouseReleased',
x: clickX,
y: clickY,
button: 'left',
clickCount: 1
});
console.log('>> CDP 点击已发送。');
await client.detach();
return true;
}
} catch (e) { }
}
return false;
}
async function handleVerification(page) {
console.log('正在检查验证...');
for (let i = 0; i < 30; i++) {
if (await page.getByRole('textbox', { name: 'Email or Username' }).isVisible()) {
console.log('检测到登录表单。');
return;
}
await attemptTurnstileCdp(page);
await page.waitForTimeout(1000);
}
}
// ==========================================
// Main Execution
// ==========================================
async function sendTelegramNotification(summaryText) {
const tgToken = process.env.TG_BOT_TOKEN;
const tgChatId = process.env.TG_CHAT_ID;
if (!tgToken || !tgChatId) {
console.log('⚠️ 未检测到 TG_BOT_TOKEN 或 TG_CHAT_ID,跳过 Telegram 通知。');
return;
}
console.log('📨 正在发送 Telegram 通知...');
try {
const url = `https://api.telegram.org/bot${tgToken}/sendMessage`;
await axios.post(url, {
chat_id: tgChatId,
text: summaryText,
parse_mode: 'Markdown'
});
console.log('✅ Telegram 通知发送成功');
} catch (e) {
console.error('❌ Telegram 通知发送失败:', e.message);
}
}
(async () => {
const users = getUsers();
if (users.length === 0) {
console.log('未在 process.env.USERS_JSON 或本地 users.json 中找到用户');
process.exit(1);
}
console.log(`🚀 正在启动操作脚本,共 ${users.length} 个用户 (隔离环境)...`);
const summary = [];
for (let i = 0; i < users.length; i++) {
const user = users[i];
console.log(`\n=== 正在处理用户 ${i + 1}: ${user.username} ===`);
// 1. Prepare Isolated Environment
let browser;
let chromeProcess;
let page;
try {
// Launch specific Chrome for this user
// We use the launchChrome logic but inlined or adapted to return the process
if (await checkPort(DEBUG_PORT)) {
console.log('警告: Chrome 端口似乎繁忙。正在尝试清理孤立进程...');
try {
// Simple kill attempt for Linux/CI
require('child_process').execSync(`pkill -f "remote-debugging-port=${DEBUG_PORT}" || true`);
await sleep(2000);
} catch (e) { }
}
console.log(`正在启动 Chrome (隔离用户 ${user.username})...`);
const userDataDir = path.join(os.tmpdir(), `chrome_${Date.now()}_${i}`);
const args = [
`--remote-debugging-port=${DEBUG_PORT}`,
'--no-first-run',
'--no-default-browser-check',
'--disable-gpu',
'--window-size=1280,720',
'--no-sandbox',
'--disable-setuid-sandbox',
`--user-data-dir=${userDataDir}`,
'--disable-dev-shm-usage'
];
chromeProcess = spawn(CHROME_PATH, args, {
detached: true,
stdio: 'ignore'
});
chromeProcess.unref();
// Wait for Port
console.log('正在等待 Chrome...');
let portReady = false;
for (let k = 0; k < 20; k++) {
if (await checkPort(DEBUG_PORT)) {
portReady = true;
break;
}
await new Promise(r => setTimeout(r, 1000));
}
if (!portReady) throw new Error('Chrome 启动超时');
// Connect
console.log(`正在连接到 Chrome...`);
browser = await chromium.connectOverCDP(`http://localhost:${DEBUG_PORT}`);
const defaultContext = browser.contexts()[0];
page = await defaultContext.newPage();
await page.addInitScript(INJECTED_SCRIPT);
page.setDefaultTimeout(60000);
let loginSuccess = false;
// --- Part A: Login ---
console.log('--- 第一阶段: 浏览器登录 ---');
await page.goto('https://dash.hidencloud.com/auth/login');
await handleVerification(page);
await page.getByRole('textbox', { name: 'Email or Username' }).waitFor({ timeout: 20000 });
await page.getByRole('textbox', { name: 'Email or Username' }).fill(user.username);
await page.getByRole('textbox', { name: 'Password' }).click();
await page.getByRole('textbox', { name: 'Password' }).fill(user.password);
console.log('正在检查二次验证...');
for (let j = 0; j < 5; j++) {
if (await attemptTurnstileCdp(page)) await page.waitForTimeout(2000);
await page.waitForTimeout(500);
}
console.log('正在点击登录...');
await page.getByRole('button', { name: 'Sign in to your account' }).click();
try {
await page.waitForURL('**/dashboard', { timeout: 30000 });
console.log('浏览器登录成功!');
loginSuccess = true;
} catch (e) {
console.error('等待仪表盘失败。正在检查错误...');
if (await page.getByText('Incorrect password').isVisible()) {
console.error('登录失败: 密码错误。');
} else {
await page.screenshot({ path: `login_failed_${i}.png` });
}
}
// --- Part B: Renewal Logic ---
if (loginSuccess) {
console.log('\n--- 第二阶段: 续期操作 (浏览器模式) ---');
if (page.isClosed()) {
console.error('错误: 页面意外关闭。');
} else {
const bot = new HidenCloudBot(page, user.username);
if (await bot.init()) {
for (const svc of bot.services) {
await bot.processService(svc);
}
summary.push({ user: user.username, status: 'Success', services: bot.services.length });
} else {
summary.push({ user: user.username, status: 'Failed (API Init)', services: 0 });
}
}
} else {
summary.push({ user: user.username, status: 'Failed (Login)', services: 0 });
}
} catch (err) {
console.error(`处理用户 ${user.username} 时出错: ${err.message}`);
if (page) await page.screenshot({ path: `error_process_${i}.png` }).catch(() => { });
} finally {
// Cleanup Everything for this user
console.log('正在清理用户环境...');
try { if (browser) await browser.close(); } catch (e) { }
// Kill the chrome process we started
try {
if (process.platform === 'win32') {
require('child_process').execSync(`taskkill /F /IM chrome.exe /FI "WINDOWTITLE eq Chrome (Isolated*)" || taskkill /F /IM chrome.exe`); // Imprecise on Windows but best effort
} else {
if (chromeProcess && chromeProcess.pid) process.kill(-chromeProcess.pid, 'SIGKILL'); // If we could use pgid
require('child_process').execSync(`pkill -f "remote-debugging-port=${DEBUG_PORT}" || true`);
}
} catch (e) { }
// Wait for port close
await sleep(2000);
}
}
console.log('\n\n╔════════════════════════════════════════════╗');
console.log('║ Final Summary ║');
console.log('╚════════════════════════════════════════════╝');
let summaryText = `*HidenCloud 续期任务报告 (${new Date().toLocaleDateString()})*\n\n`;
summary.forEach(s => {
const line = `User: ${s.user} | Status: ${s.status} | Services: ${s.services}`;
console.log(line);
summaryText += `👤 用户: \`${s.user}\`\n状态: ${s.status === 'Success' ? '✅ 成功' : '❌ 失败'}\n服务数: ${s.services}\n\n`;
});
await sendTelegramNotification(summaryText);
// Exit code based on success
if (summary.some(s => s.status.includes('Failed'))) {
process.exit(1);
} else {
process.exit(0);
}
})();