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 getAxiosClient(useProxy: boolean) { const pass = `test1234_session-${generateRandomString(8)}_lifetime-1h` const proxyAgent = useProxy ? new HttpsProxyAgent(`http://tuxla:${pass}@geo.iproyal.com:12321`) : 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( array: any[], concurrency: number, asyncFn: (item: any, index: number) => Promise, ) { let running = 0 // 当前正在运行的任务数 let completed = 0 // 完成的任务数 let currentTaskIndex = 0 // 当前任务索引 return new Promise(resolve => { // 执行任务的函数 const runTask = async () => { if (completed >= array.length) { // 所有任务都完成了 resolve() return } // 如果有可运行的任务且没有超过并发限制 while (currentTaskIndex < array.length && running < concurrency) { const index = currentTaskIndex const item = array[currentTaskIndex] currentTaskIndex++ // 准备下一个任务的索引 running++ // 增加正在运行的任务数 // 立即执行异步函数 ;(async () => { await asyncFn(item, index) // 等待异步任务完成 running-- // 任务完成,减少正在运行的任务数 completed++ // 增加完成的任务数 runTask() // 尝试运行下一个任务 })() } } // 初始化并发任务 runTask() }) }