| import { HttpsProxyAgent } from 'https-proxy-agent'
|
| import { getProxyUrl } from '../configs/config.js'
|
|
|
| let cachedAgent = null
|
| let cachedProxyUrl = null
|
|
|
| export function getNextProxyAgent(targetUrl) {
|
| const proxyUrl = getProxyUrl()
|
|
|
|
|
| if (!proxyUrl) {
|
| return null
|
| }
|
|
|
|
|
| if (proxyUrl !== cachedProxyUrl) {
|
| cachedAgent = null
|
| cachedProxyUrl = proxyUrl
|
| }
|
|
|
|
|
| if (cachedAgent) {
|
| return { agent: cachedAgent, proxy: { url: proxyUrl } }
|
| }
|
|
|
|
|
| try {
|
| cachedAgent = new HttpsProxyAgent(proxyUrl)
|
| console.log(`使用代理 ${proxyUrl} 请求 ${targetUrl}`)
|
| return { agent: cachedAgent, proxy: { url: proxyUrl } }
|
| } catch (error) {
|
| console.error(`为 ${proxyUrl} 创建代理失败:`, error)
|
| return null
|
| }
|
| }
|
|
|
|
|