twenty 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -80
- package/package.json +9 -22
- package/.npmignore +0 -2
- package/bin/twenty +0 -2
- package/index.js +0 -118
package/README.md
CHANGED
|
@@ -1,82 +1,6 @@
|
|
|
1
|
-
# Twenty
|
|
1
|
+
# Twenty CLI
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
Twenty is a small package to help you get location (city/region and lat/lon coordinates) and ISP data from an IP address or URL. Other utilities are provided, like a method that provides the approximate distance between two IP addresses. All of this can also be done by specifying the FQDN, like "stackabuse.com" or "google.com".
|
|
3
|
+
The new command-line interface for [Twenty](https://twenty.com) is coming soon.
|
|
5
4
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
Twenty was created in conjunction with [this article](http://stackabuse.com/how-to-create-a-node-js-cli-application/).
|
|
9
|
-
|
|
10
|
-
## Installation
|
|
11
|
-
Twenty can be installed as a project dependency or global CLI tool:
|
|
12
|
-
|
|
13
|
-
```bash
|
|
14
|
-
$ [sudo] npm install twenty [-g]
|
|
15
|
-
```
|
|
16
|
-
|
|
17
|
-
## Usage
|
|
18
|
-
There are two ways to use this package: through the command line or by within your code.
|
|
19
|
-
|
|
20
|
-
### Command Line
|
|
21
|
-
```bash
|
|
22
|
-
$ twenty [command] [ip | url] [-j]
|
|
23
|
-
```
|
|
24
|
-
|
|
25
|
-
Twenty takes an optional IP (or URL) address as an argument. If one isn't given, then your own IP address is used. If you're using the `distance` command, then you must provide at least one IP/URL, and optionally two.
|
|
26
|
-
|
|
27
|
-
The `-j` flag will print out all of the IP location info JSON format.
|
|
28
|
-
|
|
29
|
-
```bash
|
|
30
|
-
$ twenty stackabuse.com -j
|
|
31
|
-
{
|
|
32
|
-
"ip": "54.173.122.231",
|
|
33
|
-
"hostname": "ec2-54-173-122-231.compute-1.amazonaws.com",
|
|
34
|
-
"city": "Ashburn",
|
|
35
|
-
"region": "Virginia",
|
|
36
|
-
"country": "US",
|
|
37
|
-
"loc": "39.0437,-77.4875",
|
|
38
|
-
"org": "AS14618 Amazon.com, Inc.",
|
|
39
|
-
"postal": "20147"
|
|
40
|
-
}
|
|
41
|
-
```
|
|
42
|
-
|
|
43
|
-
### In Your Code
|
|
44
|
-
Twenty can also be used programmatically. Currently, the functions provided are `info()` and `distance()`.
|
|
45
|
-
|
|
46
|
-
- `info()`: Provides you with all the IP info as a JavaScript object
|
|
47
|
-
- `distance()`: Calculates the distance between two IPs/URLs in kilometers
|
|
48
|
-
|
|
49
|
-
#### Example Code
|
|
50
|
-
|
|
51
|
-
```javascript
|
|
52
|
-
var twenty = require('twenty');
|
|
53
|
-
|
|
54
|
-
twenty.info('8.8.8.8', function(err, data) {
|
|
55
|
-
console.log('Google\'s DNS server is located at:', data.loc);
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
twenty.distance('8.8.8.8', '54.173.122.231', function(err, distance) {
|
|
59
|
-
console.log('Distance between Google\'s DNS and stackabuse.com server (km):', distance);
|
|
60
|
-
});
|
|
61
|
-
```
|
|
62
|
-
|
|
63
|
-
## Note
|
|
64
|
-
Since Twenty uses the ipinfo.io REST service for getting location information, you'll need to be aware of your usage. The daily limit for free usage is 1,000 requests per day. Check the [ipinfo.io](http://ipinfo.io/about) About page for more information.
|
|
65
|
-
|
|
66
|
-
## Why 'Twenty'?
|
|
67
|
-
The name 'Twenty' came from the phrase "What's your 20?", which came from the police [10-code](https://en.wikipedia.org/wiki/Ten-code) "10-20", which means "identify your position" or "where are you?".
|
|
68
|
-
|
|
69
|
-
## Copyright & License
|
|
70
|
-
Copyright (c) 2016 Scott Robinson
|
|
71
|
-
|
|
72
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
73
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
74
|
-
in the Software without restriction, including without limitation the rights
|
|
75
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
76
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
77
|
-
furnished to do so, subject to the following conditions:
|
|
78
|
-
|
|
79
|
-
The above copyright notice and this permission notice shall be included in
|
|
80
|
-
all copies or substantial portions of the Software.
|
|
81
|
-
|
|
82
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
5
|
+
This package does not provide an executable yet. For the currently available CLI,
|
|
6
|
+
use [twenty-sdk](https://www.npmjs.com/package/twenty-sdk).
|
package/package.json
CHANGED
|
@@ -1,28 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "twenty",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "
|
|
5
|
-
"
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "The new command-line interface for Twenty is coming soon.",
|
|
5
|
+
"homepage": "https://twenty.com",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
8
|
-
"url": "https://github.com/
|
|
8
|
+
"url": "git+https://github.com/twentyhq/twenty.git",
|
|
9
|
+
"directory": "packages/twenty-cli"
|
|
9
10
|
},
|
|
10
|
-
"
|
|
11
|
-
"
|
|
12
|
-
"location",
|
|
13
|
-
"ipinfo",
|
|
14
|
-
"tools"
|
|
15
|
-
],
|
|
16
|
-
"scripts": {
|
|
17
|
-
"test": "echo \"Error: no test specified\" && exit 1"
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/twentyhq/twenty/issues"
|
|
18
13
|
},
|
|
19
|
-
"
|
|
20
|
-
|
|
21
|
-
"dependencies": {
|
|
22
|
-
"request": "^2.67.0",
|
|
23
|
-
"yargs": "^4.2.0"
|
|
24
|
-
},
|
|
25
|
-
"bin": {
|
|
26
|
-
"twenty": "./bin/twenty"
|
|
27
|
-
}
|
|
28
|
-
}
|
|
14
|
+
"license": "AGPL-3.0"
|
|
15
|
+
}
|
package/.npmignore
DELETED
package/bin/twenty
DELETED
package/index.js
DELETED
|
@@ -1,118 +0,0 @@
|
|
|
1
|
-
var dns = require('dns');
|
|
2
|
-
var request = require('request');
|
|
3
|
-
|
|
4
|
-
var ipRegexStr = '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.' +
|
|
5
|
-
'(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.' +
|
|
6
|
-
'(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.' +
|
|
7
|
-
'(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)';
|
|
8
|
-
var ipRegex = new RegExp(ipRegexStr);
|
|
9
|
-
|
|
10
|
-
var toRad = function(num) {
|
|
11
|
-
return num * (Math.PI / 180);
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
var getIpInfo = function(server, callback) {
|
|
15
|
-
var ipinfo = function(p, cb) {
|
|
16
|
-
request('http://ipinfo.io/' + p, function(err, response, body) {
|
|
17
|
-
var json;
|
|
18
|
-
try {
|
|
19
|
-
json = JSON.parse(body);
|
|
20
|
-
} catch(error) {
|
|
21
|
-
return cb(error, null);
|
|
22
|
-
}
|
|
23
|
-
cb(err, json);
|
|
24
|
-
});
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
if (!server) {
|
|
28
|
-
return ipinfo('json', callback);
|
|
29
|
-
} else if (!server.match(ipRegex)) {
|
|
30
|
-
return dns.lookup(server, function(err, data) {
|
|
31
|
-
ipinfo(data, callback);
|
|
32
|
-
});
|
|
33
|
-
} else {
|
|
34
|
-
return ipinfo(server, callback);
|
|
35
|
-
}
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
var ipDistance = function(lat1, lon1, lat2, lon2) {
|
|
39
|
-
// Earth radius in km
|
|
40
|
-
var r = 6371;
|
|
41
|
-
|
|
42
|
-
var dLat = toRad(lat2 - lat1);
|
|
43
|
-
var dLon = toRad(lon2 - lon1);
|
|
44
|
-
lat1 = toRad(lat1);
|
|
45
|
-
lat2 = toRad(lat2);
|
|
46
|
-
|
|
47
|
-
var a = Math.sin(dLat / 2.0) * Math.sin(dLat / 2.0) +
|
|
48
|
-
Math.sin(dLon / 2.0) * Math.sin(dLon / 2.0) * Math.cos(lat1) * Math.cos(lat2);
|
|
49
|
-
var c = 2.0 * Math.atan2(Math.sqrt(a), Math.sqrt(1.0 - a));
|
|
50
|
-
return r * c;
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
var findDistance = function(ip1, ip2, callback) {
|
|
54
|
-
var lat1, lon1, lat2, lon2;
|
|
55
|
-
|
|
56
|
-
getIpInfo(ip1, function(err, data1) {
|
|
57
|
-
var coords1 = data1.loc.split(',');
|
|
58
|
-
lat1 = Number(coords1[0]);
|
|
59
|
-
lon1 = Number(coords1[1]);
|
|
60
|
-
getIpInfo(ip2, function(err, data2) {
|
|
61
|
-
var coords2 = data2.loc.split(',');
|
|
62
|
-
lat2 = Number(coords2[0]);
|
|
63
|
-
lon2 = Number(coords2[1]);
|
|
64
|
-
|
|
65
|
-
var dist = ipDistance(lat1, lon1, lat2, lon2);
|
|
66
|
-
callback(null, dist);
|
|
67
|
-
});
|
|
68
|
-
});
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
var cli = function() {
|
|
72
|
-
var yargs = require('yargs')
|
|
73
|
-
.usage('Usage: $0 [command] [options]')
|
|
74
|
-
.command('distance <loc1> [loc2]', 'Compute the approx. distance between IPs/URLs')
|
|
75
|
-
.option('json', {
|
|
76
|
-
alias: 'j',
|
|
77
|
-
describe: 'Return data as json',
|
|
78
|
-
})
|
|
79
|
-
.example('$0', 'Get location info for your IP address')
|
|
80
|
-
.example('$0 -j', 'Get location info for your IP as json')
|
|
81
|
-
.example('$0 distance 8.8.8.8', 'Get distance from your IP to given IP')
|
|
82
|
-
.example('$0 distance stackabuse.com google.com', 'Get the distance between two given URLs')
|
|
83
|
-
.help('help')
|
|
84
|
-
.alias('h', 'help')
|
|
85
|
-
.epilog('Copyright 2016 Scott Robinson');
|
|
86
|
-
|
|
87
|
-
var argv = yargs.argv;
|
|
88
|
-
|
|
89
|
-
if (argv.loc1) {
|
|
90
|
-
findDistance(argv.loc1, argv.loc2, function(err, distance) {
|
|
91
|
-
console.log(String(distance.toFixed(2)) + ' km');
|
|
92
|
-
});
|
|
93
|
-
} else if (argv.json) {
|
|
94
|
-
getIpInfo(argv._[0], function(err, data) {
|
|
95
|
-
console.log(JSON.stringify(data, null, 4));
|
|
96
|
-
});
|
|
97
|
-
} else {
|
|
98
|
-
getIpInfo(argv._[0], function(err, data) {
|
|
99
|
-
if (err) {
|
|
100
|
-
console.log('Error:', err.message);
|
|
101
|
-
return;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
console.log('IP:', data.ip);
|
|
105
|
-
console.log('Hostname:', data.hostname || '-');
|
|
106
|
-
console.log('City:', data.city || '-');
|
|
107
|
-
console.log('Region:', data.region || '-');
|
|
108
|
-
console.log('Postal:', data.postal || '-');
|
|
109
|
-
console.log('Country:', data.country || '-');
|
|
110
|
-
console.log('Coordinates:', data.loc || '-');
|
|
111
|
-
console.log('ISP:', data.org || '-');
|
|
112
|
-
});
|
|
113
|
-
}
|
|
114
|
-
};
|
|
115
|
-
|
|
116
|
-
exports.info = getIpInfo;
|
|
117
|
-
exports.distance = findDistance;
|
|
118
|
-
exports.cli = cli;
|