Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
{
"env": {
"browser": true,
"commonjs": true,
"es6": true,
"node": true,
"jest": true
},
"parserOptions": {
"ecmaVersion": 9,
"ecmaFeatures": {
"experimentalObjectRestSpread": true
}
},
"globals": {
"exinfo": false,
"exwarn": false,
"exerror": false
},
"extends": "eslint:recommended",
"rules": {
"array-callback-return": 2,
"arrow-body-style": ["error", "as-needed"],
"arrow-parens": ["error", "as-needed"],
"arrow-spacing": "error",
"callback-return": 2,
"camelcase": "error",
"comma-style": [2, "last"],
"comma-dangle": ["error", "always-multiline"],
"curly": [2, "multi-line", "consistent"],
"dot-location": [2, "object"],
"dot-notation": 2,
"eqeqeq": 2,
"generator-star-spacing": "error",
"id-length": [
"error",
{
"min": 3,
"exceptions": ["_", "ip", "IP"]
}
],
"indent": [2, 2],
"new-cap": "error",
"new-parens": 2,
"no-console": "off",
"no-dupe-keys": 2,
"no-mixed-operators": "error",
"no-multi-str": 2,
"no-multi-spaces": 2,
"no-param-reassign": 2,
"no-shadow-restricted-names": 2,
"no-unneeded-ternary": 2,
"no-unsafe-negation": 2,
"no-unused-vars": [
"error",
{
"argsIgnorePattern": "^_",
"varsIgnorePattern": "^_",
"ignoreRestSiblings": true
}
],
"no-useless-constructor": "error",
"no-useless-return": 2,
"object-shorthand": 2,
"prefer-arrow-callback": "error",
"prefer-const": "error",
"prefer-destructuring": 2,
"prefer-rest-params": 2,
"prefer-spread": "error",
"prefer-template": 2,
"quote-props": [2, "as-needed"],
"quotes": [
2,
"single",
{
"avoidEscape": true,
"allowTemplateLiterals": true
}
],
"semi": 2,
"space-before-blocks": "error",
"space-before-function-paren": ["error", "never"],
"space-infix-ops": [
"error",
{
"int32Hint": false
}
],
"spaced-comment": ["error", "always"],
"template-curly-spacing": [2, "always"],
"valid-jsdoc": 1,
"yoda": 2
}
}
62 changes: 4 additions & 58 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,61 +1,7 @@
'use strict';

const checker = module.exports;
const IP4 = require('./ip4');

checker.checkIP(ip, ips = [], iprs = []) {
// check if single ips includes ip
if ( ips.includes(ip) ) return true
if (iprs.some(ipr => {
const _range = ipr.split('-')
const _rangeLowerBound = _range[0].split('.').map(e => Number(e))
const _rangeUpperBound = _range[1].split('.').map(e => Number(e))

const _originIp4Arr = ip.split('.').map(e => Number(e))

return false
}))
}


function(origin_ip, allowed_ips = []) {
if (allowed_ips.length > 0) {
const ips = allowed_ips.filter(e => !e.includes('-'))
const iprs = allowed_ips.filter(e => e.includes('-'))
// check if any single ip matches the incoming req ip
if ( ips.includes(origin_ip)) {
return {success: true, ips: allowed_ips}
} else {
const _rangeResults = []
for (let i = 0; i < iprs.length; i++) {

const _range = iprs[i].split('-')
const _noip = origin_ip.split('.').map(e => Number(e))

const _rangeLowerBound = _range[0].split('.').map(e => Number(e))
const _rangeUpperBound = _range[1].split('.').map(e => Number(e))

for (let k0 = 0; k0 < _rangeLowerBound.length; k0++) {
if (_noip[k0] < _rangeLowerBound[k0]) {
_rangeResults.push(false)
}
}

for (let k1 = 0; k1 < _rangeUpperBound.length; k1++) {
if (_noip[k1] > _rangeUpperBound[k1]) {
_rangeResults.push(false)
}
}

_rangeResults.push(true)

}
if (_rangeResults.some(e => e === true)) {
return {success: true, ips: allowed_ips}
} else {
return {success: false, ips: allowed_ips, message: 'You have no access with your current IP'}
}
}
} else {
return {success: true, ips: []}
}
}
module.exports = {
IP4,
};
141 changes: 0 additions & 141 deletions ip.js

This file was deleted.

61 changes: 50 additions & 11 deletions ip.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,51 @@
const IP = require('./ip'),
IP4 = IP.IP4,
IP6 = IP.IP6;

test('', () => {
console.log(IP);
const ip4 = new IP4(1, 1, 1, 1)
console.log(ip4);
const ip6 = new IP6('2001','0db8','85a3','08d3','1319','8a2e','0370','7344')
console.log(ip6);
//expect(getOrElse()).toBe(undefined);
const { IP4 } = require('./index');

test('IP4 | VALID IP4 should construct a valid IP4 instance', () => {
const IP_4 = new IP4('192.168.178.10');
expect(IP_4.address).toBe('192.168.178.10');
expect(IP_4.subnet).toBe('32');
});

test('IP4 | INVALID IP4 should throw invalid IP4 error', () => {
expect(() => new IP4('192.168.178.A')).toThrow();
});

test('IP4 | IP4 with subnetinfo (eg.: /32) should throw no subnet information error', () => {
expect(() => new IP4('192.168.178.10/32')).toThrow();
});

test('IP4 | MATCH ON OCT 1 - 3 with IP EQUAL ON OCT 1 -3 MUST MATCH', () => {
const STATIC_IP_4 = new IP4('192.168.178.21');
const IP_4_SUBNET_TO_TEST = new IP4('192.168.178.0', true);
expect(STATIC_IP_4.isInSubnet(IP_4_SUBNET_TO_TEST)).toBe(true);
});

test('IP4 | MATCH ON OCT 1 - 3 with IP NON EQUAL ON OCT 1 - 3 SHOULD NOT MATCH', () => {
const STATIC_IP_4 = new IP4('192.168.178.21');
const IP_4_SUBNET_TO_TEST = new IP4('192.168.179.0', true);
expect(STATIC_IP_4.isInSubnet(IP_4_SUBNET_TO_TEST)).toBe(false);
});

test('IP4 | MATCH ON OCT 1 - 2 with IP EQUAL ON OCT 1 - 2 MUST MATCH', () => {
const STATIC_IP_4 = new IP4('192.168.178.21');
const IP_4_SUBNET_TO_TEST = new IP4('192.168.0.0', true);
expect(STATIC_IP_4.isInSubnet(IP_4_SUBNET_TO_TEST)).toBe(true);
});

test('IP4 | MATCH ON OCT 1 - 2 with IP NON EQUAL ON OCT 1 - 2 SHOULD NOT MATCH', () => {
const STATIC_IP_4 = new IP4('192.168.178.21');
const IP_4_SUBNET_TO_TEST = new IP4('192.169.0.0', true);
expect(STATIC_IP_4.isInSubnet(IP_4_SUBNET_TO_TEST)).toBe(false);
});

test('IP4 | MATCH ON OCT 1 with IP EQUAL ON OCT 1 MUST MATCH', () => {
const STATIC_IP_4 = new IP4('192.168.178.21');
const IP_4_SUBNET_TO_TEST = new IP4('192.0.0.0', true);
expect(STATIC_IP_4.isInSubnet(IP_4_SUBNET_TO_TEST)).toBe(true);
});

test('IP4 | MATCH ON OCT 1 with IP NON EQUAL ON OCT 1 SHOULD NOT MATCH', () => {
const STATIC_IP_4 = new IP4('192.168.178.21');
const IP_4_SUBNET_TO_TEST = new IP4('193.0.0.0', true);
expect(STATIC_IP_4.isInSubnet(IP_4_SUBNET_TO_TEST)).toBe(false);
});
Loading