0%

HTTPS 请求使用 HTTP 代理

Nodejs HTTP 包使用代理并不是很方便, 主要记录下一种平时试验用的方法, 原理就是通过 HTTP 连接代理后, HTTPS 使用上面代理的隧道进行通信

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
const req = http.get({
host: '127.0.0.1',
port: 8081,
method: 'CONNECT',
timeout: 10000,
path: 'baidu.com:443'
}).on('connect', (response, socket) => {
log.log(response.headers, response.statusCode, response.statusMessage)
if (response.statusCode === 200) {
https.request({
method: 'GET',
host: 'baidu.com',
socket,
agent: false,
timeout: 10000,
headers: {
'User-Agent': 'Mozilla/5.0'
},
rejectUnauthorized: false
}, (res) => {
log.log(res.headers, res.statusCode, res.statusMessage)
let chunks = []
res.on('data', chunk => chunks.push(chunk))
res.on('end', () => {
log.log('DONE', Buffer.concat(chunks).toString('utf8'))
resolve(Buffer.concat(chunks).toString('utf8'))
})
}).on('error', err => {
log.error(err)
}).end()
}
}).on('timeout', error => {
log.error(error)
reject(error)
}).on('error', error => {
log.error(error)
reject(error)
}).end()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
client:log {} 200 Connection established +0ms
client:log { server: 'bfe/1.0.8.18',
client:log date: 'Wed, 25 Mar 2020 02:17:21 GMT',
client:log 'content-type': 'text/html',
client:log 'content-length': '161',
client:log connection: 'close',
client:log location: 'http://www.baidu.com/' } 302 Moved Temporarily +238ms
client:log DONE <html>
<head><title>302 Found</title></head>
<body bgcolor="white">
<center><h1>302 Found</h1></center>
<hr><center>bfe/1.0.8.18</center>
</body>
</html>
+6ms

注意: rejectUnauthorized: false 是不规范的行为, 正常是需要验证 CA 证书的, 可以使用 ssl-root-cas/latest 模块

最简单的还是使用现成的模块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
const HttpsProxyAgent = require('https-proxy-agent')
const options = url.parse(endpoint)
options.timeout = 10000
options.headers = {
'User-Agent': 'Mozilla/5.0'
}
options.agent = new HttpsProxyAgent({
host: '172.17.0.1',
port: 8081,
headers: {
'User-Agent': 'Mozilla/5.0'
},
timeout: 10000
})
https.get(options, (res) => {}).end()