9

I have an array like this

var ALLOW_SUBNET = ['192.168.1.', '192.168.2.', '192.168.3.' , '192.168.4.'];

And I can get IP address of PC Client by using my own function:

getIPClient()
    var ipclient = input.getIPClient();

My question is how can I check if client IP is within my allowed subnet, I tried to use indexOf() function, but result was wrong. For example:

if IP Client is 192.168.1.115 => allow

if IP Client is 192.168.5.115 => deny.
acostela
  • 2,597
  • 3
  • 33
  • 50
  • 1
    `indexOf()` will not work as it matches the entire string! – Pugazh Sep 01 '16 at 07:10
  • Do the opposite. For each element of this array, check if the element is in your IP. – Ed de Almeida Sep 01 '16 at 07:10
  • in indexOf() u passing subnet saperated from IP or the ip address it self ?..if you doing so then saperate the subnet part from ip and try to get index . because ALLOW_SUBNET array contain only subnet '192.168.1.' – Ashish Patel Sep 01 '16 at 07:12
  • @Pugazh `indexOf()` works if he remove the last part of the client ip. He is only checking if the subnet is the same and so it could work. – Lelio Faieta Sep 01 '16 at 07:54

4 Answers4

10

You could use Array#some for it and check if a part of ALLOW_SUBNET is inside of ip at position 0.

function check(ip) {
    return ALLOW_SUBNET.some(function (a) { return !ip.indexOf(a); });
}

var ALLOW_SUBNET = ['192.168.1.', '192.168.2.', '192.168.3.', '192.168.4.'];

console.log(check('192.168.1.115'));
console.log(check('192.168.5.115'));

ES6 with String#startsWith

function check(ip) {
    return ALLOW_SUBNET.some(a => ip.startsWith(a));
}

var ALLOW_SUBNET = ['192.168.1.', '192.168.2.', '192.168.3.', '192.168.4.'];

console.log(check('192.168.1.115'));
console.log(check('192.168.5.115'));
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • You should be using [`startsWith`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith) not some ugly index check (see also [here](http://stackoverflow.com/q/646628/1048572)). – Bergi Sep 01 '16 at 12:14
  • looks like a new feature to me. – Nina Scholz Sep 01 '16 at 12:15
  • 1
    Newer than `some` for certain, but it's been around for years as well :-) – Bergi Sep 01 '16 at 12:17
5

Here is a solution.

var ALLOW_SUBNET = ['192.168.1.', '192.168.2.', '192.168.3.', '192.168.4.'];

function checkIP(ip) {
  var allow = false;
  for (var i = 0; i <= ALLOW_SUBNET.length; i++) {
    if (ip.indexOf(ALLOW_SUBNET[i]) > -1) {
      allow = true;
      break;
    }
  }
  return allow;
}

console.log(checkIP('192.168.9.3'));

console.log(checkIP('192.168.1.3'));
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Pugazh
  • 9,453
  • 5
  • 33
  • 54
3

You can try something like this:

Logic:

  • You have a common part 192.168. You can use a regex for it.
  • Your 3rd block can have 1-4. You can have a list of allowed values. This will allow you to handle cases when you wish to add 6 while 5 still is not allowed.
  • You can have a range value for last block.

var ipRegex = /^192.168/
var ALLOW_SUBNET = [1, 2, 3, 4];
var ALLOW_ADDRESS = [95, 120]

var validIp = ["192.168.1.115", "192.168.2.96"];
var invalidIPs = ["192.167.1.115", "192.168.5.115", "192.168.1.90", "192.168.1.215"];

function validateIP(ip) {
  var parts = ip.split(".");
  return !(
    !ipRegex.test(ip) ||
    ALLOW_SUBNET.indexOf(+parts[2]) < 0 ||
    !(ALLOW_ADDRESS[0] <= +parts[3] && ALLOW_ADDRESS[1] >= +parts[3])
  )
}

validIp.forEach(function(ip) {
  console.log(ip, validateIP(ip));
})
invalidIPs.forEach(function(ip) {
  console.log(ip, validateIP(ip));
})
Community
  • 1
  • 1
Rajesh
  • 24,354
  • 5
  • 48
  • 79
2

This would get the job done:

allow_deny = function(ipclient) {
  var ALLOW_SUBNET = ['192.168.1.', '192.168.2.', '192.168.3.','192.168.4.'];
  var arr = ipclient.split('.');
  arr.pop();
  var testedip = arr.join('.') + '.';
  return ((ALLOW_SUBNET.indexOf(testedip) > -1) ? 'allow' : 'deny');
}

console.log(allow_deny('192.168.1.115'));
console.log(allow_deny('192.168.5.115'));

This would result, checking your console, in:

> allow
> deny
Ed de Almeida
  • 3,675
  • 4
  • 25
  • 57