-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
45 lines (36 loc) · 1.08 KB
/
index.js
File metadata and controls
45 lines (36 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
'use strict';
const CIDRMatcher = require('cidr-matcher');
function IPAccessControl ({
allow = [], deny = [], order = 'deny, allow',
}) {
if (!Array.isArray(allow)) {
allow = [ allow ];
}
if (!Array.isArray(deny)) {
deny = [ deny ];
}
const allowMatcher = new CIDRMatcher(allow);
const allowed = (ip) => allowMatcher.contains(ip);
const denyMatcher = new CIDRMatcher(deny);
const denied = (ip) => denyMatcher.contains(ip);
this.check = function(ip) {
if (order === 'allow, deny') {
// If the ip does not match the Allow rule or it does match
// the Deny rule, then the client will be denied access.
if (!allowed(ip) || denied(ip)) {
return false;
}
return true;
} else if (order === 'deny, allow') {
// If the ip does not match the deny rule or it does match
// the allow rule, then it will be granted access.
if (!denied(ip) || allowed(ip)) {
return true;
}
return false;
}
// Unrecognized ordering, deny access.
return false;
};
}
module.exports = IPAccessControl;