CoolFace
Apppublic

devme/droid

sourceHugging Faceupdated 10mo agoView on Hugging Face
0likes
proxy.js37 linesDownload Raw Back to managers
1import { HttpsProxyAgent } from 'https-proxy-agent'
2import { getProxyUrl } from '../configs/config.js'
3
4let cachedAgent = null
5let cachedProxyUrl = null
6
7export function getNextProxyAgent(targetUrl) {
8  const proxyUrl = getProxyUrl()
9
10  // 如果没有配置代理,返回 null(直连)
11  if (!proxyUrl) {
12    return null
13  }
14
15  // 如果代理 URL 改变,清除缓存
16  if (proxyUrl !== cachedProxyUrl) {
17    cachedAgent = null
18    cachedProxyUrl = proxyUrl
19  }
20
21  // 如果已有缓存的代理,直接返回
22  if (cachedAgent) {
23    return { agent: cachedAgent, proxy: { url: proxyUrl } }
24  }
25
26  // 创建新的代理
27  try {
28    cachedAgent = new HttpsProxyAgent(proxyUrl)
29    console.log(`使用代理 ${proxyUrl} 请求 ${targetUrl}`)
30    return { agent: cachedAgent, proxy: { url: proxyUrl } }
31  } catch (error) {
32    console.error(`为 ${proxyUrl} 创建代理失败:`, error)
33    return null
34  }
35}
36
37