Skip to content

Commit

Permalink
IP-DASH v1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
bbharathkumarreddy committed May 11, 2022
1 parent e412799 commit a0070e3
Show file tree
Hide file tree
Showing 6 changed files with 239 additions and 105 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
# For more information see: https://help.github.com/actions/language-and-framework-guides/publishing-nodejs-packages

name: Node.js Package

on:
release:
types: [created]
workflow_dispatch:
inputs:
home:
description: 'NPM Publish'
required: false
default: 'NPM Publish'

jobs:

publish-npm:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 12
registry-url: https://registry.npmjs.org/
- run: npm install
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{secrets.npm_token}}
105 changes: 2 additions & 103 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,104 +1,3 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.test

# parcel-bundler cache (https://parceljs.org/)
.cache

# Next.js build output
.next

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and *not* Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port
package-lock.json
.DS_Store
69 changes: 67 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,67 @@
# ip-dash
IP Utility Tools - IPv4 and IPv6
# IP-Dash

[![npm version](https://badge.fury.io/js/ip-dash.svg)](https://www.npmjs.com/package/ip-dash)

IP address utility tool for IPv4 and IPv6

## Installation

```shell
npm install ip-dash
```

https://www.npmjs.com/package/ip-dash

## Features

- Basic IP address validation and find the version of the IP address.
- Find the scope of IP address - Public, Private or Loopback.
- Get public IP address of the client in top matched order from `req`.
- Convert both IPv4 and IPv6 to IP decimal format.

## Usage

```js
const ipDash = require('ip-dash');

// Checks valid ip address and returns the ip version 4 or 6 and 0 if invalid ip.
ipDash.isIP('35.89.146.200');

// Checks ip address is ip address version 4 and returns true or false.
ipDash.isIPv4('35.89.146.200');

// Checks ip address is ip address version 6 and returns true or false.
ipDash.isIPv6('07b5:92e5:6f12:0315:6259:34bd:93ca:bbfd');

// Checks ip address is a private ip address and returns true or false.
ipDash.isPrivateIP('127.0.0.1');

// Checks ip address is a private ip address and returns true or false.
ipDash.isPublicIP('35.89.146.200');

// Checks ip address is a private ip address and returns true or false.
ipDash.isLoopbackIP('127.0.0.1');

// Return the ip address of user from request in the order of X-Client-IP,
// X-Forwarded-For, CF-Connecting-IP, connection, socket, info remoteAddresses.
ipDash.getClientIP(req);

// Returns the private ip address of the current running process.
ipDash.getMyPrivateIP(); // Returns 192.168.0.4

// Converts IP address to IP decimal.
ipDash.ipToDecimal('35.89.146.200'); // Returns 593072840

// Converts IP address to IP decimal.
ipDash.ipToDecimal('07b5:92e5:6f12:0315:6259:34bd:93ca:bbfd'); // Returns 10247381111315175137996068065712454653
```

## Maintainers

- B Bharath Kumar - [@bbharathkumarreddy](https://github.com/bbharathkumarreddy/)

Contributors are welcome

## License

The MIT License (MIT) - 2022
1 change: 1 addition & 0 deletions _config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
theme: jekyll-theme-cayman
116 changes: 116 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
const ipDash = exports;

const bigInt = require('big-integer');
const nodeIp = require('ip');
const requestIp = require('request-ip');
const net = require('net');

/**
* Returns randomly generated ip version 4 addresses.
* @returns {string} ip
*/
ipDash.getRandomIP = function () {
return `${Math.floor(Math.random() * 255) + 1}.${Math.floor(Math.random() * 255)}.${Math.floor(
Math.random() * 255
)}.${Math.floor(Math.random() * 255)}`;
};

/**
* Tests if input is an IP address.
* @param {string} ip
* @returns {number} `4` for IP version 4 addresses returns `6` for IP version 6 and `0` for invalid strings
*/
ipDash.isIP = function (ip) {
return net.isIP(ip);
};

/**
* Tests if input is an IP address version 4.
* @param {string} ip
* @returns {boolean}
*/
ipDash.isIPv4 = function (ip) {
return net.isIPv4(ip);
};
/**
* Tests if input is an IP address version 6.
* @param {string} ip
* @returns {boolean}
*/
ipDash.isIPv6 = function (ip) {
return net.isIPv6(ip);
};

/**
* Tests if input is an IP address is a private ip address.
* @param {string} ip
* @returns {boolean}
*/
ipDash.isPrivateIP = function (ip) {
return nodeIp.isPrivate(ip);
};

/**
* Tests if input is an IP address is a public ip address.
* @param {string} ip
* @returns {boolean}
*/
ipDash.isPublicIP = function (ip) {
return !nodeIp.isPrivate(ip);
};

/**
* Tests if input is an IP address is a loopback ip address.
* @param {string} ip
* @returns {boolean}
*/
ipDash.isLoopbackIP = function (ip) {
return nodeIp.isLoopback(ip);
};

/**
* Returns the private ip address of the current running process.
* @param {string} ip
* @returns {string} ip
*/
ipDash.getMyPrivateIP = function () {
nodeIp.address();
};

/**
* Returns the ip address of user from request in the order of X-Client-IP,
* X-Forwarded-For, CF-Connecting-IP, connection, socket, info remoteAddresses.
* @param {string} ip
* @returns {string} ip
*/
ipDash.getClientIP = function (req) {
requestIp.getClientIp(req);
};

/**
* Returns the decimal represetation ip address given in version 4 or version 6
* @param {string} ip
* @returns {number} ip-decimal
*/
ipDash.ipToDecimal = function (ip) {
if (net.isIPv4(ip)) {
let iplen = 0;
ip.split('.').forEach((it) => {
iplen <<= 8;
iplen += parseInt(it);
});
return iplen >>> 0;
} else if (net.isIPv6(ip)) {
const parts = [];
ip.split(':').forEach(function (it) {
let bin = parseInt(it, 16).toString(2);
while (bin.length < 16) {
bin = '0' + bin;
}
parts.push(bin);
});
return bigInt(parts.join(''), 2).toString();
}

return 0;
};
24 changes: 24 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "ip-dash",
"version": "1.0.0",
"description": "IP address utility tool for IPv4 and IPv6",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/bbharathkumarreddy/ip-dash.git"
},
"author": "B Bharath Kumar <bbharathkumarreddy96@gmail.com>",
"license": "MIT",
"bugs": {
"url": "https://github.com/bbharathkumarreddy/ip-dash/issues"
},
"homepage": "https://github.com/bbharathkumarreddy/ip-dash#readme",
"dependencies": {
"big-integer": "^1.6.51",
"ip": "^1.1.8",
"request-ip": "^2.1.3"
}
}

0 comments on commit a0070e3

Please sign in to comment.