Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle IP rollover when using an IP range #90

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
5 changes: 3 additions & 2 deletions src/libs/cidr.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,6 @@ function get(range) {
}

module.exports = {
get
};
get,
long2ip,
}
31 changes: 21 additions & 10 deletions src/libs/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,23 @@ function getTargets(target, callback) {
if (target.match(/\-/)) {
const splitTarget = target.split('-');
const minHost = splitTarget[0];
const maxHost = splitTarget[1];
const ips = [];
let splitMinHost, maxHost;
let splitMinHost, splitMaxHost;
if (net.isIPv4(minHost)) {
splitMinHost = minHost.split('.');
if (net.isIPv4(splitTarget[1])) {
maxHost = splitTarget[1].split('.')[3];
splitMinHost = minHost.split(".").map(Number)

if (net.isIPv4(maxHost)) {
splitMaxHost = maxHost.split('.').map(Number);
} else {
// Check if the string is a positive integer
if (splitTarget[1] >>> 0 === parseFloat(splitTarget[1])) {
maxHost = splitTarget[1];
if (splitTarget[1] >>> 0 === Number(maxHost)) {
splitMaxHost = [
splitMinHost[0],
splitMinHost[1],
splitMinHost[2],
Number(maxHost)
];
} else {
callback('Invalid IPv4 target range, ie: 192.168.0.1-5, 192.168.0.1-192.168.0.5');
return;
Expand All @@ -61,11 +68,15 @@ function getTargets(target, callback) {
return;
}

const numMinHost = splitMinHost.reduce((prev, val, i) => prev + val * 256 ** (3 - i), 0)
const numMaxHost = splitMaxHost.reduce((prev, val, i) => prev + val * 256 ** (3 - i), 0)

if (net.isIPv4(minHost)) {
for (let i = parseInt(splitMinHost[3]); i <= parseInt(maxHost); i++) {
ips.push(splitMinHost[0] + '.' + splitMinHost[1] + '.' + splitMinHost[2] + '.' + i);
for (let ip = numMinHost; ip <= numMaxHost; ip++) {
ips.push(cidr.long2ip(ip))
}
}
}

if (!ips) {
callback('Invalid IPv4 target. Please specify a target using --target [cidr|ip|range]');
return;
Expand Down Expand Up @@ -306,4 +317,4 @@ module.exports = {
getTargets,
getPorts,
parse
};
};