Skip to content

Commit a0070e3

Browse files
IP-DASH v1.0.0
1 parent e412799 commit a0070e3

File tree

6 files changed

+239
-105
lines changed

6 files changed

+239
-105
lines changed

.github/workflows/npm-publish.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
2+
# For more information see: https://help.github.com/actions/language-and-framework-guides/publishing-nodejs-packages
3+
4+
name: Node.js Package
5+
6+
on:
7+
release:
8+
types: [created]
9+
workflow_dispatch:
10+
inputs:
11+
home:
12+
description: 'NPM Publish'
13+
required: false
14+
default: 'NPM Publish'
15+
16+
jobs:
17+
18+
publish-npm:
19+
runs-on: ubuntu-latest
20+
steps:
21+
- uses: actions/checkout@v2
22+
- uses: actions/setup-node@v1
23+
with:
24+
node-version: 12
25+
registry-url: https://registry.npmjs.org/
26+
- run: npm install
27+
- run: npm publish
28+
env:
29+
NODE_AUTH_TOKEN: ${{secrets.npm_token}}

.gitignore

Lines changed: 2 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -1,104 +1,3 @@
1-
# Logs
2-
logs
3-
*.log
4-
npm-debug.log*
5-
yarn-debug.log*
6-
yarn-error.log*
7-
lerna-debug.log*
8-
9-
# Diagnostic reports (https://nodejs.org/api/report.html)
10-
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
11-
12-
# Runtime data
13-
pids
14-
*.pid
15-
*.seed
16-
*.pid.lock
17-
18-
# Directory for instrumented libs generated by jscoverage/JSCover
19-
lib-cov
20-
21-
# Coverage directory used by tools like istanbul
22-
coverage
23-
*.lcov
24-
25-
# nyc test coverage
26-
.nyc_output
27-
28-
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
29-
.grunt
30-
31-
# Bower dependency directory (https://bower.io/)
32-
bower_components
33-
34-
# node-waf configuration
35-
.lock-wscript
36-
37-
# Compiled binary addons (https://nodejs.org/api/addons.html)
38-
build/Release
39-
40-
# Dependency directories
411
node_modules/
42-
jspm_packages/
43-
44-
# TypeScript v1 declaration files
45-
typings/
46-
47-
# TypeScript cache
48-
*.tsbuildinfo
49-
50-
# Optional npm cache directory
51-
.npm
52-
53-
# Optional eslint cache
54-
.eslintcache
55-
56-
# Microbundle cache
57-
.rpt2_cache/
58-
.rts2_cache_cjs/
59-
.rts2_cache_es/
60-
.rts2_cache_umd/
61-
62-
# Optional REPL history
63-
.node_repl_history
64-
65-
# Output of 'npm pack'
66-
*.tgz
67-
68-
# Yarn Integrity file
69-
.yarn-integrity
70-
71-
# dotenv environment variables file
72-
.env
73-
.env.test
74-
75-
# parcel-bundler cache (https://parceljs.org/)
76-
.cache
77-
78-
# Next.js build output
79-
.next
80-
81-
# Nuxt.js build / generate output
82-
.nuxt
83-
dist
84-
85-
# Gatsby files
86-
.cache/
87-
# Comment in the public line in if your project uses Gatsby and *not* Next.js
88-
# https://nextjs.org/blog/next-9-1#public-directory-support
89-
# public
90-
91-
# vuepress build output
92-
.vuepress/dist
93-
94-
# Serverless directories
95-
.serverless/
96-
97-
# FuseBox cache
98-
.fusebox/
99-
100-
# DynamoDB Local files
101-
.dynamodb/
102-
103-
# TernJS port file
104-
.tern-port
2+
package-lock.json
3+
.DS_Store

README.md

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,67 @@
1-
# ip-dash
2-
IP Utility Tools - IPv4 and IPv6
1+
# IP-Dash
2+
3+
[![npm version](https://badge.fury.io/js/ip-dash.svg)](https://www.npmjs.com/package/ip-dash)
4+
5+
IP address utility tool for IPv4 and IPv6
6+
7+
## Installation
8+
9+
```shell
10+
npm install ip-dash
11+
```
12+
13+
https://www.npmjs.com/package/ip-dash
14+
15+
## Features
16+
17+
- Basic IP address validation and find the version of the IP address.
18+
- Find the scope of IP address - Public, Private or Loopback.
19+
- Get public IP address of the client in top matched order from `req`.
20+
- Convert both IPv4 and IPv6 to IP decimal format.
21+
22+
## Usage
23+
24+
```js
25+
const ipDash = require('ip-dash');
26+
27+
// Checks valid ip address and returns the ip version 4 or 6 and 0 if invalid ip.
28+
ipDash.isIP('35.89.146.200');
29+
30+
// Checks ip address is ip address version 4 and returns true or false.
31+
ipDash.isIPv4('35.89.146.200');
32+
33+
// Checks ip address is ip address version 6 and returns true or false.
34+
ipDash.isIPv6('07b5:92e5:6f12:0315:6259:34bd:93ca:bbfd');
35+
36+
// Checks ip address is a private ip address and returns true or false.
37+
ipDash.isPrivateIP('127.0.0.1');
38+
39+
// Checks ip address is a private ip address and returns true or false.
40+
ipDash.isPublicIP('35.89.146.200');
41+
42+
// Checks ip address is a private ip address and returns true or false.
43+
ipDash.isLoopbackIP('127.0.0.1');
44+
45+
// Return the ip address of user from request in the order of X-Client-IP,
46+
// X-Forwarded-For, CF-Connecting-IP, connection, socket, info remoteAddresses.
47+
ipDash.getClientIP(req);
48+
49+
// Returns the private ip address of the current running process.
50+
ipDash.getMyPrivateIP(); // Returns 192.168.0.4
51+
52+
// Converts IP address to IP decimal.
53+
ipDash.ipToDecimal('35.89.146.200'); // Returns 593072840
54+
55+
// Converts IP address to IP decimal.
56+
ipDash.ipToDecimal('07b5:92e5:6f12:0315:6259:34bd:93ca:bbfd'); // Returns 10247381111315175137996068065712454653
57+
```
58+
59+
## Maintainers
60+
61+
- B Bharath Kumar - [@bbharathkumarreddy](https://github.com/bbharathkumarreddy/)
62+
63+
Contributors are welcome
64+
65+
## License
66+
67+
The MIT License (MIT) - 2022

_config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
theme: jekyll-theme-cayman

index.js

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
const ipDash = exports;
2+
3+
const bigInt = require('big-integer');
4+
const nodeIp = require('ip');
5+
const requestIp = require('request-ip');
6+
const net = require('net');
7+
8+
/**
9+
* Returns randomly generated ip version 4 addresses.
10+
* @returns {string} ip
11+
*/
12+
ipDash.getRandomIP = function () {
13+
return `${Math.floor(Math.random() * 255) + 1}.${Math.floor(Math.random() * 255)}.${Math.floor(
14+
Math.random() * 255
15+
)}.${Math.floor(Math.random() * 255)}`;
16+
};
17+
18+
/**
19+
* Tests if input is an IP address.
20+
* @param {string} ip
21+
* @returns {number} `4` for IP version 4 addresses returns `6` for IP version 6 and `0` for invalid strings
22+
*/
23+
ipDash.isIP = function (ip) {
24+
return net.isIP(ip);
25+
};
26+
27+
/**
28+
* Tests if input is an IP address version 4.
29+
* @param {string} ip
30+
* @returns {boolean}
31+
*/
32+
ipDash.isIPv4 = function (ip) {
33+
return net.isIPv4(ip);
34+
};
35+
/**
36+
* Tests if input is an IP address version 6.
37+
* @param {string} ip
38+
* @returns {boolean}
39+
*/
40+
ipDash.isIPv6 = function (ip) {
41+
return net.isIPv6(ip);
42+
};
43+
44+
/**
45+
* Tests if input is an IP address is a private ip address.
46+
* @param {string} ip
47+
* @returns {boolean}
48+
*/
49+
ipDash.isPrivateIP = function (ip) {
50+
return nodeIp.isPrivate(ip);
51+
};
52+
53+
/**
54+
* Tests if input is an IP address is a public ip address.
55+
* @param {string} ip
56+
* @returns {boolean}
57+
*/
58+
ipDash.isPublicIP = function (ip) {
59+
return !nodeIp.isPrivate(ip);
60+
};
61+
62+
/**
63+
* Tests if input is an IP address is a loopback ip address.
64+
* @param {string} ip
65+
* @returns {boolean}
66+
*/
67+
ipDash.isLoopbackIP = function (ip) {
68+
return nodeIp.isLoopback(ip);
69+
};
70+
71+
/**
72+
* Returns the private ip address of the current running process.
73+
* @param {string} ip
74+
* @returns {string} ip
75+
*/
76+
ipDash.getMyPrivateIP = function () {
77+
nodeIp.address();
78+
};
79+
80+
/**
81+
* Returns the ip address of user from request in the order of X-Client-IP,
82+
* X-Forwarded-For, CF-Connecting-IP, connection, socket, info remoteAddresses.
83+
* @param {string} ip
84+
* @returns {string} ip
85+
*/
86+
ipDash.getClientIP = function (req) {
87+
requestIp.getClientIp(req);
88+
};
89+
90+
/**
91+
* Returns the decimal represetation ip address given in version 4 or version 6
92+
* @param {string} ip
93+
* @returns {number} ip-decimal
94+
*/
95+
ipDash.ipToDecimal = function (ip) {
96+
if (net.isIPv4(ip)) {
97+
let iplen = 0;
98+
ip.split('.').forEach((it) => {
99+
iplen <<= 8;
100+
iplen += parseInt(it);
101+
});
102+
return iplen >>> 0;
103+
} else if (net.isIPv6(ip)) {
104+
const parts = [];
105+
ip.split(':').forEach(function (it) {
106+
let bin = parseInt(it, 16).toString(2);
107+
while (bin.length < 16) {
108+
bin = '0' + bin;
109+
}
110+
parts.push(bin);
111+
});
112+
return bigInt(parts.join(''), 2).toString();
113+
}
114+
115+
return 0;
116+
};

package.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "ip-dash",
3+
"version": "1.0.0",
4+
"description": "IP address utility tool for IPv4 and IPv6",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/bbharathkumarreddy/ip-dash.git"
12+
},
13+
"author": "B Bharath Kumar <bbharathkumarreddy96@gmail.com>",
14+
"license": "MIT",
15+
"bugs": {
16+
"url": "https://github.com/bbharathkumarreddy/ip-dash/issues"
17+
},
18+
"homepage": "https://github.com/bbharathkumarreddy/ip-dash#readme",
19+
"dependencies": {
20+
"big-integer": "^1.6.51",
21+
"ip": "^1.1.8",
22+
"request-ip": "^2.1.3"
23+
}
24+
}

0 commit comments

Comments
 (0)