forked from teralomaniac/miaomiaomiao
-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathsessionManager.mjs
More file actions
509 lines (446 loc) · 18.7 KB
/
sessionManager.mjs
File metadata and controls
509 lines (446 loc) · 18.7 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
import fs from 'fs';
import path from 'path';
import {Mutex} from 'async-mutex';
import {detectBrowser} from './utils/browserDetector.mjs';
import {createDirectoryIfNotExists} from './utils/cookieUtils.mjs';
import {fileURLToPath} from 'url';
import {optimizeBrowserDisplay} from './utils/browserDisplayFixer.mjs';
import {launchEdgeBrowser} from './utils/edgeLauncher.mjs';
import {setupBrowserFingerprint} from './utils/browserFingerprint.mjs';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const isHeadless = process.env.HEADLESS_BROWSER === 'true' && process.env.USE_MANUAL_LOGIN !== 'true';
let puppeteerModule;
let connect;
if (isHeadless === false) {
puppeteerModule = await import('puppeteer-real-browser');
connect = puppeteerModule.connect;
} else {
puppeteerModule = await import('puppeteer-core');
}
// 会话自动释放时间(秒)
const SESSION_LOCK_TIMEOUT = parseInt(process.env.SESSION_LOCK_TIMEOUT || '0', 10);
// 存储已达请求上限的账号(格式: "timestamp | username")
const cooldownFilePath = path.join(__dirname, 'cooldownAccounts.log');
// 冷却时长(默认24小时)
const COOLDOWN_DURATION = 24 * 60 * 60 * 1000;
class SessionManager {
constructor(provider) {
this.provider = provider;
this.isCustomModeEnabled = process.env.USE_CUSTOM_MODE === 'true';
this.isRotationEnabled = process.env.ENABLE_MODE_ROTATION === 'true';
this.isHeadless = isHeadless; // 是否隐藏浏览器
this.currentIndex = 0;
this.usernameList = []; // 缓存用户名列表
this.browserInstances = []; // 浏览器实例数组
this.browserMutex = new Mutex(); // 浏览器互斥锁
this.browserIndex = 0;
this.sessionAutoUnlockTimers = {}; // 自动解锁计时器
this.cooldownList = this.loadCooldownList(); // 加载并清理 cooldown 文件
this.cleanupCooldownList();
}
setSessions(sessions) {
this.sessions = sessions;
this.usernameList = Object.keys(this.sessions);
// 为每个 session 初始化相关属性
for (const username in this.sessions) {
const session = this.sessions[username];
session.locked = false; // 标记会话是否被锁定
session.requestCount = 0; // 请求计数
session.valid = true; // 标记会话是否有效
session.mutex = new Mutex(); // 创建互斥锁
if (session.currentMode === undefined) {
session.currentMode = this.isCustomModeEnabled ? 'custom' : 'default';
}
if (!session.modeStatus) {
session.modeStatus = {
default: true,
custom: true,
};
}
session.rotationEnabled = true; // 是否启用模式轮换
session.switchCounter = 0; // 模式切换计数器
session.requestsInCurrentMode = 0; // 当前模式下的请求次数
session.lastDefaultThreshold = 0; // 上次默认模式阈值
session.switchThreshold = this.provider.getRandomSwitchThreshold(session);
// 记录请求次数
session.youTotalRequests = 0;
// 权重
if (typeof session.weight !== 'number') {
session.weight = 1;
}
}
}
loadCooldownList() {
try {
if (!fs.existsSync(cooldownFilePath)) {
fs.writeFileSync(cooldownFilePath, '', 'utf8');
return [];
}
const lines = fs.readFileSync(cooldownFilePath, 'utf8')
.split('\n')
.map(line => line.trim())
.filter(line => line.length > 0);
const arr = [];
for (const line of lines) {
const parts = line.split('|').map(x => x.trim());
if (parts.length === 2) {
const timestamp = parseInt(parts[0], 10);
const name = parts[1];
if (!isNaN(timestamp) && name) {
arr.push({time: timestamp, username: name});
}
}
}
return arr;
} catch (err) {
console.error(`读取 ${cooldownFilePath} 出错:`, err);
return [];
}
}
saveCooldownList() {
try {
const lines = this.cooldownList.map(item => `${item.time} | ${item.username}`);
fs.writeFileSync(cooldownFilePath, lines.join('\n') + '\n', 'utf8');
} catch (err) {
console.error(`写入 ${cooldownFilePath} 出错:`, err);
}
}
// 清理过期(超过指定冷却时长)
cleanupCooldownList() {
const now = Date.now();
let changed = false;
this.cooldownList = this.cooldownList.filter(item => {
const expired = (now - item.time) >= COOLDOWN_DURATION;
if (expired) changed = true;
return !expired;
});
if (changed) {
this.saveCooldownList();
}
}
recordLimitedAccount(username) {
const now = Date.now();
const already = this.cooldownList.find(x => x.username === username);
if (!already) {
this.cooldownList.push({time: now, username});
this.saveCooldownList();
console.log(`写入冷却列表:${new Date(now).toLocaleString()} | ${username}`);
}
}
// 是否在冷却期(24小时内)
isInCooldown(username) {
this.cleanupCooldownList();
return this.cooldownList.some(item => item.username === username);
}
// 批量初始化浏览器实例
async initBrowserInstancesInBatch() {
const browserCount = parseInt(process.env.BROWSER_INSTANCE_COUNT) || 1;
// 可以是 'chrome', 'edge', 或 'auto'
const browserPath = detectBrowser(process.env.BROWSER_TYPE || 'auto');
const sharedProfilePath = path.join(__dirname, 'browser_profiles');
createDirectoryIfNotExists(sharedProfilePath);
const tasks = [];
for (let i = 0; i < browserCount; i++) {
const browserId = `browser_${i}`;
const userDataDir = path.join(sharedProfilePath, browserId);
createDirectoryIfNotExists(userDataDir);
tasks.push(this.launchSingleBrowser(browserId, userDataDir, browserPath));
}
// 并行执行
const results = await Promise.all(tasks);
for (const instanceInfo of results) {
this.browserInstances.push(instanceInfo);
console.log(`创建浏览器实例: ${instanceInfo.id}`);
}
}
async launchSingleBrowser(browserId, userDataDir, browserPath) {
let browser, page;
const isEdge = browserPath.toLowerCase().includes('msedge') ||
process.env.BROWSER_TYPE === 'edge';
if (isEdge) {
try {
const debugPort = 9222 + parseInt(browserId.replace('browser_', ''), 10);
const result = await launchEdgeBrowser(userDataDir, browserPath, debugPort);
browser = result.browser;
page = result.page;
console.log(`Edge浏览器启动成功 (browserId=${browserId})`);
} catch (error) {
console.error(`原生启动Edge失败:`, error);
console.log(`回退标准方式启动浏览器...`);
}
}
if (!browser) {
if (isHeadless === false) {
// 使用 puppeteer-real-browser
const response = await connect({
headless: 'auto',
turnstile: true,
customConfig: {
userDataDir: userDataDir,
executablePath: browserPath,
args: [
'--no-sandbox',
'--disable-setuid-sandbox',
'--remote-debugging-address=::',
'--window-size=1280,850',
'--force-device-scale-factor=1',
],
},
});
browser = response.browser;
page = response.page;
} else {
// 使用 puppeteer-core
browser = await puppeteerModule.launch({
headless: this.isHeadless,
executablePath: browserPath,
userDataDir: userDataDir,
args: [
'--no-sandbox',
'--disable-setuid-sandbox',
'--disable-gpu',
'--disable-dev-shm-usage',
'--remote-debugging-port=0',
'--window-size=1280,850',
'--force-device-scale-factor=1',
],
});
page = await browser.newPage();
}
}
const originalUserAgent = await page.evaluate(() => navigator.userAgent);
// console.log(`浏览器 ${browserId} 原始用户代理: ${originalUserAgent}`);
const browserType = isEdge ? 'edge' : 'chrome';
const fingerprint = await setupBrowserFingerprint(page, browserType);
try {
const newUserAgent = await page.evaluate(() => navigator.userAgent);
const newPlatform = await page.evaluate(() => navigator.platform);
const newCores = await page.evaluate(() => navigator.hardwareConcurrency);
// console.log(`浏览器 ${browserId} 应用指纹后:`);
// console.log(`- 用户代理: ${newUserAgent}`);
// console.log(`- 平台: ${newPlatform}`);
// console.log(`- CPU核心: ${newCores}`);
// console.log(`- 内存: ${fingerprint.ram}GB`);
// console.log(`- 设备名称: ${fingerprint.deviceName}`);
const isActuallyEdge = newUserAgent.includes('Edg');
// 应用显示优化
try {
await optimizeBrowserDisplay(page, {
width: 1280,
height: 850,
deviceScaleFactor: 1,
cssScale: 1,
fixHighDpi: true,
isHeadless: this.isHeadless
});
} catch (error) {
console.warn(`显示优化失败:`, error);
}
return {
id: browserId,
browser: browser,
page: page,
locked: false,
isEdgeBrowser: isActuallyEdge,
fingerprint: fingerprint // 存储指纹信息
};
} catch (error) {
console.error(`验证指纹时出错:`, error);
try {
await optimizeBrowserDisplay(page, {
width: 1280,
height: 850,
deviceScaleFactor: 1,
cssScale: 1,
fixHighDpi: true,
isHeadless: this.isHeadless
});
} catch (displayError) {
console.warn(`显示优化失败:`, displayError);
}
const isActuallyEdge = originalUserAgent.includes('Edg');
return {
id: browserId,
browser: browser,
page: page,
locked: false,
isEdgeBrowser: isActuallyEdge
};
}
}
async getAvailableBrowser() {
return await this.browserMutex.runExclusive(async () => {
const totalBrowsers = this.browserInstances.length;
for (let i = 0; i < totalBrowsers; i++) {
const index = (this.browserIndex + i) % totalBrowsers;
const browserInstance = this.browserInstances[index];
if (!browserInstance.locked) {
browserInstance.locked = true;
this.browserIndex = (index + 1) % totalBrowsers;
return browserInstance;
}
}
throw new Error('当前负载已饱和,请稍后再试(以达到最大并发)');
});
}
async releaseBrowser(browserId) {
await this.browserMutex.runExclusive(async () => {
const browserInstance = this.browserInstances.find(b => b.id === browserId);
if (browserInstance) {
browserInstance.locked = false;
}
});
}
async getAvailableSessions() {
const allSessionsLocked = this.usernameList.every(username => this.sessions[username].locked);
if (allSessionsLocked) {
throw new Error('所有会话处于饱和状态,请稍后再试(无可用账号)');
}
// 收集所有valid && !locked && (不在冷却期)
let candidates = [];
for (const username of this.usernameList) {
const session = this.sessions[username];
// 如果没被锁 并且 session.valid
if (session.valid && !session.locked) {
if (this.provider.enableRequestLimit && this.isInCooldown(username)) {
// console.log(`账号 ${username} 处于 24 小时冷却中,跳过`);
continue;
}
candidates.push(username);
}
}
if (candidates.length === 0) {
throw new Error('没有可用的会话');
}
// 随机洗牌
shuffleArray(candidates);
// 加权抽签
let weightSum = 0;
for (const uname of candidates) {
weightSum += this.sessions[uname].weight;
}
// 生成随机
const randValue = Math.floor(Math.random() * weightSum) + 1;
// 遍历并扣减
let cumulative = 0;
let selectedUsername = null;
for (const uname of candidates) {
cumulative += this.sessions[uname].weight;
if (randValue <= cumulative) {
selectedUsername = uname;
break;
}
}
if (!selectedUsername) {
selectedUsername = candidates[0];
}
const selectedSession = this.sessions[selectedUsername];
// 再尝试锁定账号
const result = await selectedSession.mutex.runExclusive(async () => {
if (selectedSession.locked) {
return null;
}
// 判断是否可用
if (selectedSession.modeStatus && selectedSession.modeStatus[selectedSession.currentMode]) {
// 锁定
selectedSession.locked = true;
selectedSession.requestCount++;
// 获取可用浏览器
const browserInstance = await this.getAvailableBrowser();
// 启动自动解锁计时器
if (SESSION_LOCK_TIMEOUT > 0) {
this.startAutoUnlockTimer(selectedUsername, browserInstance.id);
}
return {
selectedUsername,
modeSwitched: false,
browserInstance
};
} else if (
this.isCustomModeEnabled &&
this.isRotationEnabled &&
this.provider &&
typeof this.provider.switchMode === 'function'
) {
console.warn(`尝试为账号 ${selectedUsername} 切换模式...`);
this.provider.switchMode(selectedSession);
selectedSession.rotationEnabled = false;
if (selectedSession.modeStatus && selectedSession.modeStatus[selectedSession.currentMode]) {
selectedSession.locked = true;
selectedSession.requestCount++;
const browserInstance = await this.getAvailableBrowser();
if (SESSION_LOCK_TIMEOUT > 0) {
this.startAutoUnlockTimer(selectedUsername, browserInstance.id);
}
return {
selectedUsername,
modeSwitched: true,
browserInstance
};
}
}
return null;
});
if (result) {
return result;
} else {
throw new Error('会话刚被占用或模式不可用!');
}
}
startAutoUnlockTimer(username, browserId) {
// 清除可能残留计时器
if (this.sessionAutoUnlockTimers[username]) {
clearTimeout(this.sessionAutoUnlockTimers[username]);
}
const lockDurationMs = SESSION_LOCK_TIMEOUT * 1000;
this.sessionAutoUnlockTimers[username] = setTimeout(async () => {
const session = this.sessions[username];
if (session && session.locked) {
console.warn(
`会话 "${username}" 已自动解锁`
);
await session.mutex.runExclusive(async () => {
session.locked = false;
});
}
}, lockDurationMs);
}
async releaseSession(username, browserId) {
const session = this.sessions[username];
if (session) {
await session.mutex.runExclusive(() => {
session.locked = false;
});
}
// 存在相应计时器清除
if (this.sessionAutoUnlockTimers[username]) {
clearTimeout(this.sessionAutoUnlockTimers[username]);
delete this.sessionAutoUnlockTimers[username];
}
if (browserId) {
await this.releaseBrowser(browserId);
}
}
// 返回会话
// getBrowserInstances() {
// return this.browserInstances;
// }
// 策略
async getSessionByStrategy(strategy = 'round_robin') {
if (strategy === 'round_robin') {
return await this.getAvailableSessions();
}
throw new Error(`未实现的策略: ${strategy}`);
}
}
/**
* Fisher–Yates 洗牌
*/
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
}
export default SessionManager;