-
Notifications
You must be signed in to change notification settings - Fork 2
/
net.js
61 lines (52 loc) · 1.59 KB
/
net.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
'use strict';
const http = require('http');
const https = require('https');
async function request(endpoint, method, body) {
const url = new URL(endpoint);
const opts = {
'auth': url.username + ':' + url.password,
'host': url.host,
'hostname': url.hostname,
'port': url.port,
'href': url.href,
'protocol': url.protocol,
'path': url.pathname + url.search,
'method': method
};
const server = opts.protocol === 'https:' ? https : http;
return new Promise((resolve, reject) => {
const req = server.request(opts, (res) => {
let strData = '';
res.setEncoding('utf8');
res.on('data', d => {
// Concatenate response chunks into one string
strData += d;
});
res.on('end', () => {
// Return the full string
if (res.statusCode === 200) {
resolve(strData);
} else {
reject(Error('Status Code: ' + res.statusCode + ' (' +
res.statusMessage + ')\nResult: ' + strData));
}
});
});
req.on('error', error => {
reject(error);
});
req.setHeader('User-Agent', 'SCPscan');
if (body) {
req.write(body);
}
req.end();
});
}
async function get(endpoint) {
return await request(endpoint, 'GET');
}
async function post(endpoint, body) {
return await request(endpoint, 'POST', body);
}
exports.get = get;
exports.post = post;