Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
getAgent(proxy, protocol) {
if (protocol == 'socks4' || protocol == 'socks5') {
const agent = new SocksProxyAgent(protocol + '://' + proxy);
agent.timeout = this.initialRequestConfig.timeout;
return { agent };
}
return { proxy: 'http://' + proxy };
}
const proxyAgent = (proxyConfig: ProxyConfig): HttpsProxyAgent | SocksProxyAgent => {
let auth = ''
if (proxyConfig.auth) {
auth = `${proxyConfig.auth.username}:${proxyConfig.auth.password}@`
}
switch (proxyConfig.protocol) {
case 'http':
case 'https':
const httpsAgent = new HttpsProxyAgent(`${proxyConfig.protocol}://${auth}${proxyConfig.host}:${proxyConfig.port}`)
return httpsAgent
case 'socks4':
case 'socks4a':
case 'socks5':
case 'socks5h':
case 'socks':
const socksAgent = new SocksProxyAgent(`${proxyConfig.protocol}://${auth}${proxyConfig.host}:${proxyConfig.port}`)
return socksAgent
default:
throw new ProxyProtocolError('protocol is not accepted')
}
}
export default proxyAgent