-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
93 lines (70 loc) · 2.25 KB
/
index.js
File metadata and controls
93 lines (70 loc) · 2.25 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
"use strict";
var fs = require("fs");
var requestIp = require("request-ip");
var MMDBReader = require("mmdb-reader");
module.exports = function (options, accessDenied) {
accessDenied = accessDenied || function (req, res) {
res.statusCode = 403;
res.end("Forbidden");
};
options = options || {};
verifyOptions(options);
var mmdb = new MMDBReader(options.geolite2);
function verifyOptions() {
if (!options.geolite2) {
throw new Error("options.geolite2 is not set");
}
// Check that geolite2 exists (fs.exists is deprecated)
var geo2 = fs.openSync(options.geolite2, "r");
fs.close(geo2);
options.blocked = options.blocked || [];
options.blockedCountries = options.blockedCountries || [];
options.allowedCountries = options.allowedCountries || [];
if (options.blockedCountries.length > 0 && options.allowedCountries.length > 0) {
throw new Error("You have to choose only allowed contries or only blocked countries");
}
}
function getIP(req) {
var ip = requestIp.getClientIp(req);
if (ip !== null) {
ip = ip.split(":");
ip = ip[ip.length - 1];
}
return ip;
}
function isBlocked(ip, req, res) {
req.location = req.location || {};
req.location.country = {
data: null,
isoCode: ""
};
// 1. Check that IP address is blocked
if (options.blocked.indexOf(ip) > -1) {
return true;
}
var blocked = false;
var query = mmdb.lookup(ip);
if (options.blockedCountries.length > 0) {
// 2. If user added country to Blocked Countries collection then only those countries
// are blocked
blocked = query !== null && options.blockedCountries.indexOf(query.country.iso_code) > -1;
} else if (options.allowedCountries.length > 0) {
// 3. If user added country to Allowed Countries collecction then all countries except allowed
// are blocked
blocked = query === null || options.allowedCountries.indexOf(query.country.iso_code) === -1;
}
if (!blocked && query !== null) {
req.location.country.data = query;
req.location.country.isoCode = query.country.iso_code;
}
return blocked;
}
return function (req, res, next) {
var ip = getIP(req);
if (isBlocked(ip, req, res)) {
accessDenied(req, res);
return;
}
next();
};
};