From 8c14aef81ab491e43068576160a273e924acefcb Mon Sep 17 00:00:00 2001 From: petruki <31597636+petruki@users.noreply.github.com> Date: Sun, 31 Jul 2022 16:56:43 -0700 Subject: [PATCH] Closes #118 - Removed ip-cidr --- README.md | 1 + package.json | 12 ++++++------ src/lib/snapshot.js | 10 +++++----- src/lib/utils/ipcidr.js | 19 +++++++++++++++++++ 4 files changed, 31 insertions(+), 11 deletions(-) create mode 100644 src/lib/utils/ipcidr.js diff --git a/README.md b/README.md index 4a65690..78bf72a 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,7 @@ A JavaScript SDK for Switcher API [![Master CI](https://github.com/switcherapi/switcher-client-master/actions/workflows/master.yml/badge.svg)](https://github.com/switcherapi/switcher-client-master/actions/workflows/master.yml) [![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=switcherapi_switcher-client-master&metric=alert_status)](https://sonarcloud.io/dashboard?id=switcherapi_switcher-client-master) [![npm version](https://badge.fury.io/js/switcher-client.svg)](https://badge.fury.io/js/switcher-client) +[![install size](https://packagephobia.com/badge?p=switcher-client)](https://packagephobia.com/result?p=switcher-client) [![Known Vulnerabilities](https://snyk.io/test/github/switcherapi/switcher-client-master/badge.svg?targetFile=package.json)](https://snyk.io/test/github/switcherapi/switcher-client-master?targetFile=package.json) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) [![Slack: Switcher-HQ](https://img.shields.io/badge/slack-@switcher/hq-blue.svg?logo=slack)](https://switcher-hq.slack.com/) diff --git a/package.json b/package.json index 494ce96..c438045 100644 --- a/package.json +++ b/package.json @@ -6,11 +6,12 @@ "types": "./src/index.d.ts", "author": { "name": "Roger Floriano", - "email": "switcher.project@gmail.com" + "email": "switcher.project@gmail.com", + "url": "https://github.com/petruki" }, "keywords": [ "feature", - "toogle", + "toggle", "flag", "switcher-api" ], @@ -26,16 +27,15 @@ "src/" ], "dependencies": { - "ip-cidr": "^3.0.10", "node-fetch": "^2.6.7" }, "devDependencies": { - "@typescript-eslint/eslint-plugin": "^5.28.0", - "@typescript-eslint/parser": "^5.28.0", + "@typescript-eslint/eslint-plugin": "^5.31.0", + "@typescript-eslint/parser": "^5.31.0", "babel-eslint": "^10.1.0", "chai": "^4.3.6", "chai-as-promised": "^7.1.1", - "eslint": "^8.18.0", + "eslint": "^8.20.0", "mocha": "^10.0.0", "mocha-sonarqube-reporter": "^1.0.2", "nyc": "^15.1.0", diff --git a/src/lib/snapshot.js b/src/lib/snapshot.js index 2cde446..de55dff 100644 --- a/src/lib/snapshot.js +++ b/src/lib/snapshot.js @@ -1,5 +1,5 @@ const fs = require('fs'); -const IPCIDR = require('ip-cidr'); +const IPCIDR = require('./utils/ipcidr'); const DateMoment = require('./utils/datemoment'); const { resolveSnapshot, checkSnapshotVersion } = require('./remote'); const { CheckSwitcherError } = require('./exceptions'); @@ -103,7 +103,7 @@ const processOperation = (strategy, operation, input, values) => { }; function processNETWORK(operation, input, values) { - const cidrRegex = /^([\d]{1,3}\.){3}[\d]{1,3}(\/([\d]|[1-2][\d]|3[0-2]))$/; + const cidrRegex = /^(\d{1,3}\.){3}\d{1,3}(\/(\d|[1-2]\d|3[0-2]))$/; switch(operation) { case OperationsType.EXIST: return processNETWORK_Exist(input, values, cidrRegex); @@ -117,7 +117,7 @@ function processNETWORK_Exist(input, values, cidrRegex) { for (const value of values) { if (value.match(cidrRegex)) { const cidr = new IPCIDR(value); - if (cidr.contains(input)) { + if (cidr.isIp4InCidr(input)) { return true; } } else { @@ -128,10 +128,10 @@ function processNETWORK_Exist(input, values, cidrRegex) { } function processNETWORK_NotExist(input, values, cidrRegex) { - const result = values.filter(element => { + const result = values.filter((element) => { if (element.match(cidrRegex)) { const cidr = new IPCIDR(element); - if (cidr.contains(input)) { + if (cidr.isIp4InCidr(input)) { return true; } } else { diff --git a/src/lib/utils/ipcidr.js b/src/lib/utils/ipcidr.js new file mode 100644 index 0000000..7d7e2f4 --- /dev/null +++ b/src/lib/utils/ipcidr.js @@ -0,0 +1,19 @@ +'use strict'; + +class IPCIDR { + constructor(cidr) { + this.cidr = cidr; + } + + ip4ToInt(ip) { + return ip.split('.').reduce((int, oct) => (int << 8) + parseInt(oct, 10), 0) >>> 0; + } + + isIp4InCidr(ip) { + const [range, bits = 32] = this.cidr.split('/'); + const mask = ~(2 ** (32 - Number(bits)) - 1); + return (this.ip4ToInt(ip) & mask) === (this.ip4ToInt(range) & mask); + } +} + +module.exports = IPCIDR; \ No newline at end of file