-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·45 lines (37 loc) · 1.08 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
#!/usr/bin/env node
const https = require('https');
const program = require('commander');
const pkg = require('./package.json');
const URL = 'https://1.1.1.1/cdn-cgi/trace';
function ProcessVerboseResponse(response) {
const properties = response.split('\n');
const data = properties.reduce((obj, item) => {
var itemsplit = item.split('=');
itemsplit[0] && (obj[itemsplit[0]] = itemsplit[1]);
return obj;
}, {});
return data;
}
function promisifyResponseData(res) {
return new Promise((resolve) => {
var data = [];
res.setEncoding('utf8');
res.on('data', d => {
data.push(d);
});
res.on('end', () => {
resolve(data.join(''));
});
});
}
program
.version(pkg.version)
.description('`getmyip` command prints your current IP')
.usage('[options]')
.parse(process.argv);
https.get(URL, (res) => {
promisifyResponseData(res)
.then(text => ProcessVerboseResponse(text))
.then(ipObj => console.log(ipObj.ip))
.catch(err => console.log(err));
});