index.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import axios from 'axios'
  2. import { HttpsProxyAgent } from 'https-proxy-agent'
  3. export async function sleep(ms: number) {
  4. return new Promise(resolve => setTimeout(resolve, ms))
  5. }
  6. export function generateRandomString(length: number) {
  7. const characters =
  8. 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
  9. let result = ''
  10. const charactersLength = characters.length
  11. for (let i = 0; i < length; i++) {
  12. result += characters.charAt(Math.floor(Math.random() * charactersLength))
  13. }
  14. return result
  15. }
  16. export function getProxyUrl() {
  17. const pass = `test1234_session-${generateRandomString(8)}_lifetime-24h`
  18. return `http://tuxla:${pass}@geo.iproyal.com:12321`
  19. }
  20. export function generate2CaptchaProxyAgent() {
  21. const username = `u3e4c2852572e05c5-zone-custom-session-${generateRandomString(
  22. 9,
  23. )}-sessTime-20`
  24. const password = 'u3e4c2852572e05c5'
  25. return new HttpsProxyAgent(
  26. `http://${username}:${password}@43.152.113.55:2334`,
  27. )
  28. }
  29. export function getProxyAgent() {
  30. const pass = `test1234_session-${generateRandomString(8)}_lifetime-1h`
  31. return new HttpsProxyAgent(`http://tuxla:${pass}@geo.iproyal.com:12321`)
  32. }
  33. export function getAxiosClient(useProxy: boolean) {
  34. const pass = `test1234_session-${generateRandomString(8)}_lifetime-1h`
  35. const proxyAgent = useProxy
  36. ? new HttpsProxyAgent(`http://tuxla:${pass}@geo.iproyal.com:12321`)
  37. : undefined
  38. // const proxyAgent = useProxy
  39. // ? generate2CaptchaProxyAgent()
  40. // : undefined
  41. return axios.create({
  42. httpsAgent: proxyAgent,
  43. headers: {
  44. 'User-Agent':
  45. 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3',
  46. 'Sec-Ch-Ua':
  47. '"Google Chrome";v="123", "Not:A-Brand";v="8", "Chromium";v="123"',
  48. 'Sec-Ch-Ua-Mobile': '?0',
  49. 'Sec-Ch-Ua-Platform': '"Windows"',
  50. // Origin: 'https://faucet.testnet.initia.xyz',
  51. // Referer: 'https://faucet.testnet.initia.xyz/',
  52. 'Sec-Fetch-Dest': 'empty',
  53. 'Sec-Fetch-Mode': 'cors',
  54. 'Sec-Fetch-Site': 'same-site',
  55. },
  56. withCredentials: true,
  57. })
  58. }
  59. export async function forEachAsync<T = any>(
  60. array: T[],
  61. concurrency: number,
  62. asyncFn: (item: any, index: number) => Promise<void>,
  63. timeout: number = 320000,
  64. ) {
  65. let running = 0 // 当前正在运行的任务数
  66. let completed = 0 // 完成的任务数
  67. let currentTaskIndex = 0 // 当前任务索引
  68. return new Promise<void>((resolve, reject) => {
  69. // 执行任务的函数
  70. const runTask = async () => {
  71. if (completed >= array.length) {
  72. // 所有任务都完成了
  73. resolve()
  74. }
  75. // 如果有可运行的任务且没有超过并发限制
  76. while (currentTaskIndex < array.length && running < concurrency) {
  77. const index = currentTaskIndex
  78. const item = array[currentTaskIndex]
  79. currentTaskIndex++ // 准备下一个任务的索引
  80. running++ // 增加正在运行的任务数
  81. // 立即执行异步函数
  82. ;(async () => {
  83. try {
  84. await Promise.race([
  85. asyncFn(item, index), // 等待异步任务完成
  86. new Promise<void>((_, reject) =>
  87. setTimeout(() => reject(new Error('任务超时')), timeout),
  88. ),
  89. ])
  90. } catch (error) {
  91. reject(error) // 任务超时或发生错误,直接拒绝主Promise
  92. return
  93. } finally {
  94. running-- // 任务完成或失败,减少正在运行的任务数
  95. completed++ // 增加完成的任务数
  96. runTask() // 尝试运行下一个任务
  97. }
  98. })()
  99. }
  100. }
  101. // 初始化并发任务
  102. runTask()
  103. })
  104. }