123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- import axios from 'axios'
- import { HttpsProxyAgent } from 'https-proxy-agent'
- export async function sleep(ms: number) {
- return new Promise(resolve => setTimeout(resolve, ms))
- }
- export function generateRandomString(length: number) {
- const characters =
- 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
- let result = ''
- const charactersLength = characters.length
- for (let i = 0; i < length; i++) {
- result += characters.charAt(Math.floor(Math.random() * charactersLength))
- }
- return result
- }
- export function getProxyUrl() {
- const pass = `test1234_session-${generateRandomString(8)}_lifetime-24h`
- return `http://tuxla:${pass}@geo.iproyal.com:12321`
- }
- export function generate2CaptchaProxyAgent() {
- const username = `u3e4c2852572e05c5-zone-custom-session-${generateRandomString(
- 9,
- )}-sessTime-20`
- const password = 'u3e4c2852572e05c5'
- return new HttpsProxyAgent(
- `http://${username}:${password}@43.152.113.55:2334`,
- )
- }
- export function getProxyAgent() {
- const pass = `test1234_session-${generateRandomString(8)}_lifetime-1h`
- return new HttpsProxyAgent(`http://tuxla:${pass}@geo.iproyal.com:12321`)
- }
- export function getAxiosClient(useProxy: boolean) {
- const pass = `test1234_session-${generateRandomString(8)}_lifetime-1h`
- const proxyAgent = useProxy
- ? new HttpsProxyAgent(`http://tuxla:${pass}@geo.iproyal.com:12321`)
- : undefined
- // const proxyAgent = useProxy
- // ? generate2CaptchaProxyAgent()
- // : undefined
- return axios.create({
- httpsAgent: proxyAgent,
- headers: {
- 'User-Agent':
- 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3',
- 'Sec-Ch-Ua':
- '"Google Chrome";v="123", "Not:A-Brand";v="8", "Chromium";v="123"',
- 'Sec-Ch-Ua-Mobile': '?0',
- 'Sec-Ch-Ua-Platform': '"Windows"',
- // Origin: 'https://faucet.testnet.initia.xyz',
- // Referer: 'https://faucet.testnet.initia.xyz/',
- 'Sec-Fetch-Dest': 'empty',
- 'Sec-Fetch-Mode': 'cors',
- 'Sec-Fetch-Site': 'same-site',
- },
- withCredentials: true,
- })
- }
- export async function forEachAsync<T = any>(
- array: T[],
- concurrency: number,
- asyncFn: (item: any, index: number) => Promise<void>,
- timeout: number = 320000,
- ) {
- let running = 0 // 当前正在运行的任务数
- let completed = 0 // 完成的任务数
- let currentTaskIndex = 0 // 当前任务索引
- return new Promise<void>((resolve, reject) => {
- // 执行任务的函数
- const runTask = async () => {
- if (completed >= array.length) {
- // 所有任务都完成了
- resolve()
- }
- // 如果有可运行的任务且没有超过并发限制
- while (currentTaskIndex < array.length && running < concurrency) {
- const index = currentTaskIndex
- const item = array[currentTaskIndex]
- currentTaskIndex++ // 准备下一个任务的索引
- running++ // 增加正在运行的任务数
- // 立即执行异步函数
- ;(async () => {
- try {
- await Promise.race([
- asyncFn(item, index), // 等待异步任务完成
- new Promise<void>((_, reject) =>
- setTimeout(() => reject(new Error('任务超时')), timeout),
- ),
- ])
- } catch (error) {
- reject(error) // 任务超时或发生错误,直接拒绝主Promise
- return
- } finally {
- running-- // 任务完成或失败,减少正在运行的任务数
- completed++ // 增加完成的任务数
- runTask() // 尝试运行下一个任务
- }
- })()
- }
- }
- // 初始化并发任务
- runTask()
- })
- }
|