| |
| |
| |
| |
|
|
| |
| export const ProxyTypes = { |
| HTTP: 'http', |
| HTTPS: 'https', |
| SOCKS4: 'socks4', |
| SOCKS5: 'socks5' |
| }; |
|
|
| |
| |
| |
| |
| |
| export function parseProxyUrl(proxyUrl) { |
| if (!proxyUrl) return null; |
| |
| try { |
| const url = new URL(proxyUrl); |
| const config = { |
| type: url.protocol.replace(':', ''), |
| host: url.hostname, |
| port: parseInt(url.port), |
| }; |
| |
| if (url.username) { |
| config.username = url.username; |
| } |
| if (url.password) { |
| config.password = url.password; |
| } |
| |
| return config; |
| } catch (error) { |
| console.error('代理URL解析错误:', error.message); |
| return null; |
| } |
| } |
|
|
| |
| |
| |
| |
| export function getProxyConfig() { |
| const proxyUrl = process.env.PROXY_URL; |
| const proxyHost = process.env.PROXY_HOST; |
| const proxyPort = process.env.PROXY_PORT; |
| const proxyType = process.env.PROXY_TYPE || 'http'; |
| const proxyUsername = process.env.PROXY_USERNAME; |
| const proxyPassword = process.env.PROXY_PASSWORD; |
| |
| |
| if (proxyUrl) { |
| return parseProxyUrl(proxyUrl); |
| } |
| |
| |
| if (proxyHost && proxyPort) { |
| const config = { |
| type: proxyType, |
| host: proxyHost, |
| port: parseInt(proxyPort), |
| }; |
| |
| if (proxyUsername) { |
| config.username = proxyUsername; |
| } |
| if (proxyPassword) { |
| config.password = proxyPassword; |
| } |
| |
| return config; |
| } |
| |
| return null; |
| } |
|
|
| |
| |
| |
| |
| |
| export function getPuppeteerProxyArgs(proxyConfig) { |
| if (!proxyConfig) return []; |
| |
| const args = []; |
| |
| if (proxyConfig.type === 'http' || proxyConfig.type === 'https') { |
| args.push(`--proxy-server=${proxyConfig.type}://${proxyConfig.host}:${proxyConfig.port}`); |
| } else if (proxyConfig.type === 'socks4' || proxyConfig.type === 'socks5') { |
| args.push(`--proxy-server=${proxyConfig.type}://${proxyConfig.host}:${proxyConfig.port}`); |
| } |
| |
| return args; |
| } |
|
|
| |
| |
| |
| |
| |
| export function getPlaywrightProxyConfig(proxyConfig) { |
| if (!proxyConfig) return null; |
| |
| const config = { |
| server: `${proxyConfig.type}://${proxyConfig.host}:${proxyConfig.port}`, |
| }; |
| |
| if (proxyConfig.username && proxyConfig.password) { |
| config.username = proxyConfig.username; |
| config.password = proxyConfig.password; |
| } |
| |
| return config; |
| } |
|
|
| |
| |
| |
| |
| |
| export async function testProxyConnection(proxyConfig) { |
| if (!proxyConfig) return false; |
| |
| try { |
| const { default: fetch } = await import('node-fetch'); |
| const { HttpsProxyAgent } = await import('https-proxy-agent'); |
| const { SocksProxyAgent } = await import('socks-proxy-agent'); |
| |
| let agent; |
| |
| if (proxyConfig.type === 'http' || proxyConfig.type === 'https') { |
| const proxyUrl = proxyConfig.username && proxyConfig.password |
| ? `${proxyConfig.type}://${proxyConfig.username}:${proxyConfig.password}@${proxyConfig.host}:${proxyConfig.port}` |
| : `${proxyConfig.type}://${proxyConfig.host}:${proxyConfig.port}`; |
| agent = new HttpsProxyAgent(proxyUrl); |
| } else if (proxyConfig.type === 'socks4' || proxyConfig.type === 'socks5') { |
| const proxyUrl = proxyConfig.username && proxyConfig.password |
| ? `${proxyConfig.type}://${proxyConfig.username}:${proxyConfig.password}@${proxyConfig.host}:${proxyConfig.port}` |
| : `${proxyConfig.type}://${proxyConfig.host}:${proxyConfig.port}`; |
| agent = new SocksProxyAgent(proxyUrl); |
| } |
| |
| const response = await fetch('https://httpbin.org/ip', { |
| agent, |
| timeout: 10000 |
| }); |
| |
| if (response.ok) { |
| const data = await response.json(); |
| console.log('代理测试成功,IP地址:', data.origin); |
| return true; |
| } |
| |
| return false; |
| } catch (error) { |
| console.error('代理测试失败:', error.message); |
| return false; |
| } |
| } |
|
|
| |
| |
| |
| export async function getCurrentIP() { |
| try { |
| const { default: fetch } = await import('node-fetch'); |
| const response = await fetch('https://httpbin.org/ip', { timeout: 10000 }); |
| if (response.ok) { |
| const data = await response.json(); |
| return data.origin; |
| } |
| } catch (error) { |
| console.error('获取IP地址失败:', error.message); |
| } |
| return null; |
| } |
|
|
| export default { |
| ProxyTypes, |
| parseProxyUrl, |
| getProxyConfig, |
| getPuppeteerProxyArgs, |
| getPlaywrightProxyConfig, |
| testProxyConnection, |
| getCurrentIP |
| }; |
|
|