| 3 | const tls = require('tls') |
| 4 | |
| 5 | function createReleaseHttpClient({ |
| 6 | env = process.env, |
| 7 | userAgent, |
| 8 | requestTimeout, |
| 9 | httpModule = http, |
| 10 | httpsModule = https, |
| 11 | tlsModule = tls, |
| 12 | }) { |
| 13 | function getProxyUrl() { |
| 14 | return ( |
| 15 | env.HTTPS_PROXY || |
| 16 | env.https_proxy || |
| 17 | env.HTTP_PROXY || |
| 18 | env.http_proxy || |
| 19 | null |
| 20 | ) |
| 21 | } |
| 22 | |
| 23 | function shouldBypassProxy(hostname) { |
| 24 | const noProxy = env.NO_PROXY || env.no_proxy || '' |
| 25 | if (!noProxy) return false |
| 26 | |
| 27 | const domains = noProxy |
| 28 | .split(',') |
| 29 | .map((domain) => domain.trim().toLowerCase().replace(/:\d+$/, '')) |
| 30 | const host = hostname.toLowerCase() |
| 31 | |
| 32 | return domains.some((domain) => { |
| 33 | if (domain === '*') return true |
| 34 | if (domain.startsWith('.')) { |
| 35 | return host.endsWith(domain) || host === domain.slice(1) |
| 36 | } |
| 37 | return host === domain || host.endsWith(`.${domain}`) |
| 38 | }) |
| 39 | } |
| 40 | |
| 41 | function connectThroughProxy(proxyUrl, targetHost, targetPort) { |
| 42 | return new Promise((resolve, reject) => { |
| 43 | const proxy = new URL(proxyUrl) |
| 44 | const isHttpsProxy = proxy.protocol === 'https:' |
| 45 | const connectOptions = { |
| 46 | hostname: proxy.hostname, |
| 47 | port: proxy.port || (isHttpsProxy ? 443 : 80), |
| 48 | method: 'CONNECT', |
| 49 | path: `${targetHost}:${targetPort}`, |
| 50 | headers: { |
| 51 | Host: `${targetHost}:${targetPort}`, |
| 52 | }, |
| 53 | } |
| 54 | |
| 55 | if (proxy.username || proxy.password) { |
| 56 | const auth = Buffer.from( |
| 57 | `${decodeURIComponent(proxy.username || '')}:${decodeURIComponent( |
| 58 | proxy.password || '', |
| 59 | )}`, |
| 60 | ).toString('base64') |
| 61 | connectOptions.headers['Proxy-Authorization'] = `Basic ${auth}` |
| 62 | } |