Socket
Socket
Sign inDemoInstall

ip

Package Overview
Dependencies
0
Maintainers
3
Versions
26
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.0.1 to 1.0.2

89

lib/ip.js

@@ -12,7 +12,3 @@ 'use strict';

if (/^::ffff:(\d{1,3}\.){3,3}\d{1,3}$/.test(ip)) {
ip = ip.replace(/^::ffff:/, '');
}
if (/^(\d{1,3}\.){3,3}\d{1,3}$/.test(ip)) {
if (this.isV4Format(ip)) {
result = buff || new Buffer(offset + 4);

@@ -22,25 +18,42 @@ ip.split(/\./g).map(function(byte) {

});
} else if (/^[a-f0-9:]+$/.test(ip)) {
var s = ip.split(/::/g, 2);
var head = (s[0] || '').split(/:/g, 8);
var tail = (s[1] || '').split(/:/g, 8);
} else if (this.isV6Format(ip)) {
var sections = ip.split(':', 8);
if (tail.length === 0) {
// xxxx::
while (head.length < 8) head.push('0000');
} else if (head.length === 0) {
// ::xxxx
while (tail.length < 8) tail.unshift('0000');
} else {
// xxxx::xxxx
while (head.length + tail.length < 8) head.push('0000');
var i;
for (i = 0; i < sections.length; i++) {
var isv4 = this.isV4Format(sections[i]);
var v4Buffer;
if (isv4) {
v4Buffer = this.toBuffer(sections[i]);
sections[i] = v4Buffer.slice(0, 2).toString('hex');
}
if (v4Buffer && ++i < 8) {
sections.splice(i, 0, v4Buffer.slice(2, 4).toString('hex'));
}
}
if (sections[0] === '') {
while (sections.length < 8) sections.unshift('0');
} else if (sections[sections.length - 1] === '') {
while (sections.length < 8) sections.push('0');
} else if (sections.length < 8) {
for (i = 0; i < sections.length && sections[i] !== ''; i++);
var argv = [ i, 1 ];
for (i = 9 - sections.length; i > 0; i--) {
argv.push('0');
}
sections.splice.apply(sections, argv);
}
result = buff || new Buffer(offset + 16);
head.concat(tail).map(function(word) {
word = parseInt(word, 16);
for (i = 0; i < sections.length; i++) {
var word = parseInt(sections[i], 16);
result[offset++] = (word >> 8) & 0xff;
result[offset++] = word & 0xff;
});
} else {
}
}
if (!result) {
throw Error('Invalid ip address: ' + ip);

@@ -76,2 +89,13 @@ }

var ipv4Regex = /^(\d{1,3}\.){3,3}\d{1,3}$/;
var ipv6Regex =
/^(::)?(((\d{1,3}\.){3}(\d{1,3}){1})?([0-9a-f]){0,4}:{0,2}){1,8}(::)?$/i;
ip.isV4Format = function isV4Format(ip) {
return ipv4Regex.test(ip);
};
ip.isV6Format = function isV6Format(ip) {
return ipv6Regex.test(ip);
};
function _normalizeFamily(family) {

@@ -275,9 +299,11 @@ return family ? family.toLowerCase() : 'ipv4';

ip.isPrivate = function isPrivate(addr) {
return /^10\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})/.test(addr) ||
/^192\.168\.([0-9]{1,3})\.([0-9]{1,3})/.test(addr) ||
/^172\.(1[6-9]|2\d|30|31)\.([0-9]{1,3})\.([0-9]{1,3})/.test(addr) ||
/^127\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})/.test(addr) ||
/^169\.254\.([0-9]{1,3})\.([0-9]{1,3})/.test(addr) ||
/^fc00:/.test(addr) ||
/^fe80:/.test(addr) ||
return /^(::f{4}:)?10\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$/
.test(addr) ||
/^(::f{4}:)?192\.168\.([0-9]{1,3})\.([0-9]{1,3})$/.test(addr) ||
/^(::f{4}:)?172\.(1[6-9]|2\d|30|31)\.([0-9]{1,3})\.([0-9]{1,3})$/
.test(addr) ||
/^(::f{4}:)?127\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$/.test(addr) ||
/^(::f{4}:)?169\.254\.([0-9]{1,3})\.([0-9]{1,3})$/.test(addr) ||
/^fc00:/i.test(addr) ||
/^fe80:/i.test(addr) ||
/^::1$/.test(addr) ||

@@ -292,3 +318,4 @@ /^::$/.test(addr);

ip.isLoopback = function isLoopback(addr) {
return /^127\.\d+\.\d+\.\d+$/.test(addr) ||
return /^(::f{4}:)?127\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})/
.test(addr) ||
/^fe80::1$/.test(addr) ||

@@ -325,3 +352,3 @@ /^::1$/.test(addr) ||

// * 'private': the first private ip address of family.
// * undefined: First address with `ipv4` or loopback addres `127.0.0.1`.
// * undefined: First address with `ipv4` or loopback address `127.0.0.1`.
//

@@ -328,0 +355,0 @@ ip.address = function address(name, family) {

{
"name": "ip",
"version": "1.0.1",
"version": "1.0.2",
"author": "Fedor Indutny <fedor@indutny.com>",

@@ -5,0 +5,0 @@ "homepage": "https://github.com/indutny/node-ip",

@@ -1,4 +0,19 @@

# IP
# IP
[![](https://badge.fury.io/js/ip.svg)](https://www.npmjs.com/package/ip)
IP address utilities for node.js
## Installation
### npm
```shell
npm install ip
```
### git
```shell
git clone https://github.com/indutny/node-ip.git
```
## Usage

@@ -20,2 +35,4 @@ Get your ip address, compare ip addresses, validate ip addresses, etc.

ip.isPrivate('127.0.0.1') // true
ip.isV4Format('127.0.0.1'); // true
ip.isV6Format('::ffff:127.0.0.1'); // true

@@ -22,0 +39,0 @@ // operate on buffers in-place

@@ -46,4 +46,12 @@ 'use strict';

var buf = ip.toBuffer('::ffff:127.0.0.1');
assert.equal(buf.toString('hex'), '7f000001');
assert.equal(ip.toString(buf), '127.0.0.1');
assert.equal(buf.toString('hex'), '00000000000000000000ffff7f000001');
assert.equal(ip.toString(buf), '::ffff:7f00:1');
buf = ip.toBuffer('ffff::127.0.0.1');
assert.equal(buf.toString('hex'), 'ffff000000000000000000007f000001');
assert.equal(ip.toString(buf), 'ffff::7f00:1');
buf = ip.toBuffer('0:0:0:0:0:ffff:127.0.0.1');
assert.equal(buf.toString('hex'), '00000000000000000000ffff7f000001');
assert.equal(ip.toString(buf), '::ffff:7f00:1');
});

@@ -50,0 +58,0 @@ });

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc