@@ -18,29 +18,29 @@ module.exports = function() {
18
18
// Check for IP range allowances. Requests will be allowed through if the IP address is in range.
19
19
var ipRanges = process . env . ANONYMOUS_ACCESS_BLOCKER_ALLOWED_IP_RANGES ;
20
20
if ( ipRanges ) {
21
- // The set of allowed ranges has to be separated by space
22
- // characters or a comma.
23
- var allowedRanges = ipRanges . split ( / \s + | , / ) ;
21
+ // The set of allowed ranges has to be separated by space characters, a comma, or newline.
22
+ var allowedRanges = ipRanges . split ( / \s + | , | \n / ) ;
24
23
25
24
// Using req.ips requires that express 'trust proxy' setting is
26
25
// true. When it *is* set the value for ips is extracted from the
27
26
// X-Forwarded-For request header. The originating IP address is
28
27
// the last one in the array.
29
- var requestIP = ( req . ips . length > 0 ) ? req . ips . slice ( ) . pop ( ) : req . ip ;
30
- console . log ( 'Client IP: ' + requestIP ) ;
28
+ var requestIP = ( process . env . CLIENT_IP_ADDRESS_HEADER ) ? req . header ( process . env . CLIENT_IP_ADDRESS_HEADER ) : req . ip ;
29
+ requestIP = rangeCheck . searchIP ( requestIP ) ;
31
30
32
31
// Deny the request if request IP is not in one of the allowed
33
32
// IP address ranges.
34
33
var requestAllowed = range_check . in_range ( requestIP , allowedRanges ) ;
35
34
36
35
if ( requestAllowed ) {
37
-
38
36
// Allow the request to process
37
+ console . log ( 'Allowed IP: ' + requestIP ) ;
39
38
return next ( ) ;
40
39
}
41
40
}
42
41
43
42
// Request is not allowed. Send the contents of the unauthorized.html file.
43
+ console . log ( 'Blocked IP: ' + requestIP ) ;
44
44
res . sendfile ( __dirname + '/unauthorized.html' ) ;
45
45
return ;
46
46
}
0 commit comments