get-ip-range
Advanced tools
Comparing version 1.1.7 to 2.1.0
96
index.js
@@ -1,51 +0,79 @@ | ||
'use strict'; | ||
const ip = require('ip'); | ||
const cidrv4 = require('cidr-regex').cidrv4; | ||
const ipAddress = require('ip-address') | ||
const { cidrv4, cidrv6 } = require('cidr-regex'); | ||
const errorMessage = new Error('IP supplied is not valid'); | ||
module.exports = convert; | ||
const getRangev4 = (ip1, ip2) => { | ||
const ips = []; | ||
function convert(cidrIp, ip2) { | ||
/* | ||
If second IP address is supplied, ensure that both are IPv4 format | ||
*/ | ||
if (ip2) { | ||
if (ip.isV4Format(cidrIp) && ip.isV4Format(ip2)) | ||
return getRange(cidrIp, ip2); | ||
let firstAddressLong = ip.toLong(ip1); | ||
const lastAddressLong = ip.toLong(ip2); | ||
return buildResponse(errorMessage); | ||
for (firstAddressLong; firstAddressLong <= lastAddressLong; firstAddressLong++) | ||
ips.push(ip.fromLong(firstAddressLong)); | ||
return ips; | ||
} | ||
const getRangev6 = (ip1, ip2) => { | ||
const ips = []; | ||
const firstAddress = new ipAddress.Address6(ip1); | ||
const lastAddress = new ipAddress.Address6(ip2); | ||
for (let i = firstAddress.bigInteger(); i <= lastAddress.bigInteger(); i++) { | ||
ips.push(ipAddress.Address6.fromBigInteger(i).correctForm()); | ||
} | ||
/* | ||
Ensure IP is valid and in CIDR format | ||
*/ | ||
if (!cidrv4.test(cidrIp)) | ||
return buildResponse(errorMessage); | ||
return ips; | ||
} | ||
const subnet = ip.cidrSubnet(cidrIp); | ||
const firstAddress = subnet.firstAddress; | ||
const lastAddress = subnet.lastAddress; | ||
const isCIDR = ip => { | ||
return ip.indexOf('/') !== -1 | ||
} | ||
return getRange(firstAddress, lastAddress); | ||
const isRange = ip => { | ||
return ip.indexOf('-') !== -1 | ||
} | ||
function getRange(ip1, ip2) { | ||
const ips = []; | ||
const convert = (cidrIp, ip2) => { | ||
const ip1v4 = new ipAddress.Address4(cidrIp); | ||
const ip1v6 = new ipAddress.Address6(cidrIp); | ||
let firstAddressLong = ip.toLong(ip1); | ||
const lastAddressLong = ip.toLong(ip2); | ||
if (ip2) { | ||
const ip2v4 = new ipAddress.Address4(ip2); | ||
const ip2v6 = new ipAddress.Address6(ip2); | ||
for (firstAddressLong; firstAddressLong <= lastAddressLong; firstAddressLong++) | ||
ips.push(ip.fromLong(firstAddressLong)); | ||
if (ip1v4.valid && ip2v4.valid && !isCIDR(cidrIp) && !isCIDR(ip2)) { | ||
return getRangev4(cidrIp, ip2); | ||
} | ||
return buildResponse(null, ips); | ||
if (ip1v6.valid && ip2v6.valid) { | ||
return getRangev6(cidrIp, ip2); | ||
} | ||
} else { | ||
if (ip1v4.valid) { | ||
const subnet = ip.cidrSubnet(cidrIp); | ||
const firstAddress = subnet.firstAddress; | ||
const lastAddress = subnet.lastAddress; | ||
return getRangev4(firstAddress, lastAddress); | ||
} | ||
if (ip1v6.valid) { | ||
const IPv6 = new ipAddress.Address6(cidrIp); | ||
return getRangev6(IPv6.startAddress().correctForm(), IPv6.endAddress().correctForm()); | ||
} | ||
if (isRange(cidrIp)) { | ||
const [ firstAddress, lastAddress ] = cidrIp.split('-'); | ||
return convert(firstAddress, lastAddress); | ||
} | ||
} | ||
throw err; | ||
} | ||
function buildResponse(err, value) { | ||
return { | ||
error: err, | ||
value: value, | ||
}; | ||
} | ||
module.exports = convert; |
{ | ||
"name": "get-ip-range", | ||
"version": "1.1.7", | ||
"version": "2.1.0", | ||
"description": "Simple utility to convert either CIDR notation or two IP addresses to an array of the range of IP addresses", | ||
@@ -42,3 +42,3 @@ "main": "index.js", | ||
"mocha": "^5.2.0", | ||
"nyc": "^11.9.0" | ||
"nyc": "^14.1.1" | ||
}, | ||
@@ -67,4 +67,5 @@ "pre-commit": [ | ||
"cidr-regex": "^1.0.7", | ||
"ip": "^1.1.5" | ||
"ip": "^1.1.5", | ||
"ip-address": "^6.1.0" | ||
} | ||
} |
@@ -1,4 +0,4 @@ | ||
# get-ip-range | ||
# get-ip-range | ||
Simple utility to convert either CIDR notation or two IP addresses to an array of the range of IP addresses | ||
Simple utility to convert either CIDR notation, a hyphenated IP range, or two IP addresses to an array of the range of IP addresses. | ||
@@ -9,38 +9,72 @@ ---- | ||
![Build Status](https://travis-ci.org/JoeScho/getIPRange.png?branch=master)[![Coverage Status](https://coveralls.io/repos/github/JoeScho/getIPRange/badge.svg?branch=master)](https://coveralls.io/github/JoeScho/getIPRange?branch=master)[![ISC License](https://img.shields.io/badge/license-ISC-blue.svg?style=flat-square)](https://github.com/JoeScho/getIPRange/blob/master/LICENSE) | ||
![Build Status](https://travis-ci.org/JoeScho/get-ip-range.svg?branch=master)[![Coverage Status](https://coveralls.io/repos/github/JoeScho/getIPRange/badge.svg?branch=master)](https://coveralls.io/github/JoeScho/getIPRange?branch=master)[![ISC License](https://img.shields.io/badge/license-ISC-blue.svg?style=flat-square)](https://github.com/JoeScho/getIPRange/blob/master/LICENSE) | ||
### Installation | ||
---- | ||
`npm install --save get-ip-range` | ||
## Installation | ||
### Usage | ||
```sh | ||
$ npm i get-ip-range | ||
``` | ||
**For CIDR notation**: | ||
---- | ||
``` | ||
const getIPRange = require('get-ip-range'); | ||
## Accepted formats | ||
### IPv4 | ||
* CIDR `"x.x.x.x/x"` | ||
* Range `"x.x.x.x-x.x.x.x"` | ||
* Two IPs `"x.x.x.x, x.x.x.x"` | ||
getIPRange('192.168.1.134/29'); | ||
// Returns an object: | ||
{ | ||
error: null, | ||
value: array of 6 IP addresses | ||
} | ||
``` | ||
### IPv6 | ||
* CIDR `"x:x:x:x:x:x:x:x/x"` | ||
* Range `"::x:x:x-::x:x:x"` | ||
* Two IPs `"::x:x:x", "::x:x:x"` | ||
**For two IP addresses**: | ||
**N.B. Shorthand IPv6 is supported** | ||
``` | ||
---- | ||
## Usage | ||
```js | ||
const getIPRange = require('get-ip-range'); | ||
getIPRange('192.168.1.129', '192.168.1.134'); | ||
// Returns an object: | ||
{ | ||
error: null, | ||
value: array of 6 IP addresses | ||
} | ||
const ipv4CIDR = getIPRange('192.168.1.134/29'); | ||
const ipv4Range = getIPRange('192.168.1.128-192.168.1.135'); | ||
const twoIPv4 = getIPRange('192.168.1.128', '192.168.1.135'); | ||
// All return: | ||
// | ||
// [ | ||
// '192.168.1.128', | ||
// '192.168.1.129', | ||
// '192.168.1.130', | ||
// '192.168.1.131', | ||
// '192.168.1.132', | ||
// '192.168.1.133', | ||
// '192.168.1.134', | ||
// '192.168.1.135', | ||
// ] | ||
// | ||
const ipv6CIDR = getIPRange('0:0:0:0:0:ffff:102:304/126'); | ||
const ipv6Range = getIPRange('::ffff:102:304-::ffff:102:307'); | ||
const twoIPv6 = getIPRange('::ffff:102:304', '::ffff:102:307'); | ||
// All return: | ||
// | ||
// [ | ||
// '::ffff:102:304', | ||
// '::ffff:102:305', | ||
// '::ffff:102:306', | ||
// '::ffff:102:307', | ||
// ] | ||
// | ||
``` | ||
**Error scenario** | ||
---- | ||
If the supplied IP address(es) are invalid, the response.error value will be an [Error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error) | ||
**Errors** | ||
If the supplied IP address(es) are invalid, the request will **throw an [error](https://nodejs.org/api/errors.html#errors_class_error)**. Please handle errors appropriately. | ||
---- |
6339
5
58
80
3
+ Addedip-address@^6.1.0
+ Addedip-address@6.4.0(transitive)
+ Addedjsbn@1.1.0(transitive)
+ Addedlodash.find@4.6.0(transitive)
+ Addedlodash.max@4.0.1(transitive)
+ Addedlodash.merge@4.6.2(transitive)
+ Addedlodash.padstart@4.6.1(transitive)
+ Addedlodash.repeat@4.1.0(transitive)
+ Addedsprintf-js@1.1.2(transitive)