-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
70 lines (59 loc) · 2.18 KB
/
index.js
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
const http = require('http');
const http2 = require('http2');
function request({
method = 'GET',
url = '',
body = null,
headers = {},
proxy
}) {
return new Promise((resolve, reject) => {
var parseUrl = new URL(url),
TARGET_HOST = `${parseUrl.host}:${url.startsWith('https') ? 443 : 80}`,
pathAndQuery = parseUrl.pathname + parseUrl.search,
response = { headers: {}, body: '', statusCode: 1001 }
method = String(method).toUpperCase()
const request = http.request({
method: 'CONNECT',
host: proxy.host,
port: proxy.port,
path: TARGET_HOST,
headers: {
'Host': TARGET_HOST,
...((proxy && proxy.username && proxy.password) ? ({ 'Proxy-Authorization': `Basic ${Buffer.from(`${proxy.username}:${proxy.password}`).toString('base64')}` }) : {})
}
})
request.on('connect', (res, socket) => {
if (res.statusCode !== 200) throw new Error('Connection rejected by the proxy')
const client = http2.connect(parseUrl.origin, { socket })
const req = client.request({
':path': pathAndQuery,
':method': method,
":authority": parseUrl.host,
":scheme": String(parseUrl.protocol).replace(':', ''),
...headers,
})
req.on('response', (headers) => {
response.headers = headers
response.statusCode = headers[':status'];
})
const buffers = []
req.on('data', (buffer) => {
buffers.push(buffer)
})
req.on('end', () => {
response.body = Buffer.concat(buffers).toString('utf-8')
try { response.body = JSON.parse(response.body) } catch (e) { }
resolve(response)
client.close()
})
if (body && typeof body == 'object') try { body = JSON.stringify(body) } catch (e) { console.log(e); }
if (body) req.write(body)
req.end()
})
request.end()
})
}
module.exports = {
request
}