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.
4
  • 1
    indexOf() will not work as it matches the entire string! Commented Sep 1, 2016 at 7:10
  • Do the opposite. For each element of this array, check if the element is in your IP. Commented Sep 1, 2016 at 7: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.' Commented Sep 1, 2016 at 7: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. Commented Sep 1, 2016 at 7:54

4 Answers 4

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'));

Sign up to request clarification or add additional context in comments.

3 Comments

You should be using startsWith not some ugly index check (see also here).
looks like a new feature to me.
Newer than some for certain, but it's been around for years as well :-)
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'));

3 Comments

@Bergi : Thanks for sharing. I will avoid it here after.
@Bergi : Updated the answer!
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));
})

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.