Upload manager.js
Browse files- src/lib/manager.js +62 -0
src/lib/manager.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* Mammouth账号管理器
|
| 3 |
+
* 提供Cookie轮换功能
|
| 4 |
+
*/
|
| 5 |
+
|
| 6 |
+
class AccountManager {
|
| 7 |
+
constructor(cookies) {
|
| 8 |
+
this.cookies = this.initCookies(cookies)
|
| 9 |
+
this.unavailableCookies = new Set()
|
| 10 |
+
this.currentIndex = 0
|
| 11 |
+
this.unlimitedIndex = 0
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
markAsUnavailable(cookie) {
|
| 15 |
+
this.unavailableCookies.add(cookie)
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
getNextAvailableCookie() {
|
| 19 |
+
if (this.cookies.length === 0) {
|
| 20 |
+
return null
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
// 如果所有Cookie都不可用,则返回第一个
|
| 24 |
+
if (this.unavailableCookies.size >= this.cookies.length) {
|
| 25 |
+
return this.cookies[0]
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
// 尝试查找可用账号
|
| 29 |
+
for (let i = 0; i < this.cookies.length; i++) {
|
| 30 |
+
// 轮换索引
|
| 31 |
+
this.currentIndex = (this.currentIndex + 1) % this.cookies.length
|
| 32 |
+
const cookie = this.cookies[this.currentIndex]
|
| 33 |
+
|
| 34 |
+
// 如果这个Cookie可用,就返回它
|
| 35 |
+
if (!this.unavailableCookies.has(cookie)) {
|
| 36 |
+
return cookie
|
| 37 |
+
}
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
// 如果循环结束还没找到,返回第一个Cookie
|
| 41 |
+
return this.cookies[0]
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
getAnyCookie() {
|
| 45 |
+
if (this.cookies.length === 0) {
|
| 46 |
+
return null
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
this.unlimitedIndex = (this.unlimitedIndex + 1) % this.cookies.length
|
| 50 |
+
return this.cookies[this.unlimitedIndex]
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
initCookies(cookies) {
|
| 54 |
+
return cookies.split(',').map(cookie => cookie.trim()).filter(cookie => cookie)
|
| 55 |
+
}
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
const COOKIES = process.env.COOKIES || ""
|
| 60 |
+
const accountManager = new AccountManager(COOKIES)
|
| 61 |
+
|
| 62 |
+
module.exports = accountManager
|