forked from CodebuffAI/codebuff
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp.js
More file actions
176 lines (150 loc) · 4.54 KB
/
Copy pathhttp.js
File metadata and controls
176 lines (150 loc) · 4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
const http = require('http')
const https = require('https')
const tls = require('tls')
function createReleaseHttpClient({
env = process.env,
userAgent,
requestTimeout,
httpModule = http,
httpsModule = https,
tlsModule = tls,
}) {
function getProxyUrl() {
return (
env.HTTPS_PROXY ||
env.https_proxy ||
env.HTTP_PROXY ||
env.http_proxy ||
null
)
}
function shouldBypassProxy(hostname) {
const noProxy = env.NO_PROXY || env.no_proxy || ''
if (!noProxy) return false
const domains = noProxy
.split(',')
.map((domain) => domain.trim().toLowerCase().replace(/:\d+$/, ''))
const host = hostname.toLowerCase()
return domains.some((domain) => {
if (domain === '*') return true
if (domain.startsWith('.')) {
return host.endsWith(domain) || host === domain.slice(1)
}
return host === domain || host.endsWith(`.${domain}`)
})
}
function connectThroughProxy(proxyUrl, targetHost, targetPort) {
return new Promise((resolve, reject) => {
const proxy = new URL(proxyUrl)
const isHttpsProxy = proxy.protocol === 'https:'
const connectOptions = {
hostname: proxy.hostname,
port: proxy.port || (isHttpsProxy ? 443 : 80),
method: 'CONNECT',
path: `${targetHost}:${targetPort}`,
headers: {
Host: `${targetHost}:${targetPort}`,
},
}
if (proxy.username || proxy.password) {
const auth = Buffer.from(
`${decodeURIComponent(proxy.username || '')}:${decodeURIComponent(
proxy.password || '',
)}`,
).toString('base64')
connectOptions.headers['Proxy-Authorization'] = `Basic ${auth}`
}
const transport = isHttpsProxy ? httpsModule : httpModule
const req = transport.request(connectOptions)
req.on('connect', (res, socket) => {
if (res.statusCode === 200) {
resolve(socket)
return
}
socket.destroy()
reject(new Error(`Proxy CONNECT failed with status ${res.statusCode}`))
})
req.on('error', (error) => {
reject(new Error(`Proxy connection failed: ${error.message}`))
})
req.setTimeout(requestTimeout, () => {
req.destroy()
reject(new Error('Proxy connection timeout.'))
})
req.end()
})
}
async function buildRequestOptions(url, options = {}) {
const parsedUrl = new URL(url)
const reqOptions = {
hostname: parsedUrl.hostname,
port: parsedUrl.port || 443,
path: parsedUrl.pathname + parsedUrl.search,
headers: {
'User-Agent': userAgent,
...options.headers,
},
}
const proxyUrl = getProxyUrl()
if (!proxyUrl || shouldBypassProxy(parsedUrl.hostname)) {
return reqOptions
}
const tunnelSocket = await connectThroughProxy(
proxyUrl,
parsedUrl.hostname,
parsedUrl.port || 443,
)
class TunnelAgent extends httpsModule.Agent {
createConnection(_options, callback) {
const secureSocket = tlsModule.connect({
socket: tunnelSocket,
servername: parsedUrl.hostname,
})
if (typeof callback === 'function') {
if (typeof secureSocket.once === 'function') {
let settled = false
const finish = (error) => {
if (settled) return
settled = true
callback(error || null, error ? undefined : secureSocket)
}
secureSocket.once('secureConnect', () => finish(null))
secureSocket.once('error', (error) => finish(error))
} else {
callback(null, secureSocket)
}
}
return secureSocket
}
}
reqOptions.agent = new TunnelAgent({ keepAlive: false })
return reqOptions
}
async function httpGet(url, options = {}) {
const reqOptions = await buildRequestOptions(url, options)
return new Promise((resolve, reject) => {
const req = httpsModule.get(reqOptions, (res) => {
if (res.statusCode === 301 || res.statusCode === 302) {
res.resume()
httpGet(new URL(res.headers.location, url).href, options)
.then(resolve)
.catch(reject)
return
}
resolve(res)
})
req.on('error', reject)
req.setTimeout(options.timeout || requestTimeout, () => {
req.destroy()
reject(new Error('Request timeout.'))
})
})
}
return {
getProxyUrl,
httpGet,
}
}
module.exports = {
createReleaseHttpClient,
}