Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ipfilter.js 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*!
  2. * Express - IP Filter
  3. * Copyright(c) 2014 Bradley and Montgomery Inc.
  4. * MIT Licensed
  5. */
  6. 'use strict'
  7. /**
  8. * Module dependencies.
  9. */
  10. const _ = require('lodash')
  11. const iputil = require('ip')
  12. const rangeCheck = require('range_check')
  13. const IpDeniedError = require('./deniedError')
  14. const proxyaddr = require('proxy-addr')
  15. /**
  16. * express-ipfilter:
  17. *
  18. * IP Filtering middleware;
  19. *
  20. * Examples:
  21. *
  22. * const ipfilter = require('ipfilter'),
  23. * ips = ['127.0.0.1'];
  24. * getIps = function() { return ['127.0.0.1']; };
  25. *
  26. * app.use(ipfilter(ips));
  27. * app.use(ipfilter(getIps));
  28. *
  29. * Options:
  30. *
  31. * - `mode` whether to deny or grant access to the IPs provided. Defaults to 'deny'.
  32. * - `logF` Function to use for logging.
  33. * - `log` console log actions. Defaults to true.
  34. * - 'excluding' routes that should be excluded from ip filtering
  35. * - 'trustProxy' trust proxy settings just like in express. The trust proxy setting is implemented using the proxy-addr package. (http://expressjs.com/en/guide/behind-proxies.html)
  36. *
  37. * @param [ips] {Array} IP addresses or {Function} that returns the array of IP addresses
  38. * @param [opts] {Object} options
  39. * @api public
  40. */
  41. module.exports = function ipfilter(ips, opts) {
  42. ips = ips || false
  43. const getIps = _.isFunction(ips) ? ips : () => ips
  44. const logger = message => console.log(message)
  45. /**
  46. * Compile "proxy trust" value to function. (from express)
  47. *
  48. * @param {Boolean|String|Number|Array|Function} val
  49. * @return {Function}
  50. * @api private
  51. */
  52. const compileTrust = val => {
  53. if (typeof val === 'function') return val
  54. if (val === true) {
  55. // Support plain true/falses
  56. return () => true
  57. }
  58. if (typeof val === 'number') {
  59. // Support trusting hop count
  60. return (a, i) => i < val
  61. }
  62. if (typeof val === 'string') {
  63. // Support comma-separated values
  64. val = val.split(',')
  65. }
  66. return proxyaddr.compile(val || [])
  67. }
  68. const settings = _.defaults(opts || {}, {
  69. mode: 'deny',
  70. log: true,
  71. logF: logger,
  72. excluding: [],
  73. trustProxy: false // This is the default used by express.
  74. })
  75. if (!_.isFunction(settings.detectIp)) {
  76. settings.detectIp = req => proxyaddr(req, compileTrust(settings.trustProxy))
  77. }
  78. const testExplicitIp = (ip, constraint, mode) => {
  79. if (ip === constraint) {
  80. return mode === 'allow'
  81. } else {
  82. return mode === 'deny'
  83. }
  84. }
  85. const testCidrBlock = (ip, constraint, mode) => {
  86. if (rangeCheck.inRange(ip, constraint)) {
  87. return mode === 'allow'
  88. } else {
  89. return mode === 'deny'
  90. }
  91. }
  92. const testRange = (ip, constraint, mode) => {
  93. const filteredSet = _.filter(getIps(), constraint => {
  94. if (constraint.length > 1) {
  95. const startIp = iputil.toLong(constraint[0])
  96. const endIp = iputil.toLong(constraint[1])
  97. const longIp = iputil.toLong(ip)
  98. return longIp >= startIp && longIp <= endIp
  99. } else {
  100. return ip === constraint[0]
  101. }
  102. })
  103. if (filteredSet.length > 0) {
  104. return mode === 'allow'
  105. } else {
  106. return mode === 'deny'
  107. }
  108. }
  109. const testIp = function(ip, mode) {
  110. const constraint = this
  111. // Check if it is an array or a string
  112. if (typeof constraint === 'string') {
  113. if (rangeCheck.validRange(constraint)) {
  114. return testCidrBlock(ip, constraint, mode)
  115. } else {
  116. return testExplicitIp(ip, constraint, mode)
  117. }
  118. }
  119. if (typeof constraint === 'object') {
  120. return testRange(ip, constraint, mode)
  121. }
  122. }
  123. const matchClientIp = ip => {
  124. const mode = settings.mode.toLowerCase()
  125. const result = _.invokeMap(getIps(), testIp, ip, mode)
  126. if (mode === 'allow') {
  127. return _.some(result)
  128. } else {
  129. return _.every(result)
  130. }
  131. }
  132. const error = (ip, next) => {
  133. const err = new IpDeniedError('Access denied to IP address: ' + ip)
  134. return next(err)
  135. }
  136. return (req, res, next) => {
  137. if (settings.excluding.length > 0) {
  138. const results = _.filter(settings.excluding, exclude => {
  139. const regex = new RegExp(exclude)
  140. return regex.test(req.url)
  141. })
  142. if (results.length > 0) {
  143. if (settings.log && settings.logLevel !== 'deny') {
  144. settings.logF('Access granted for excluded path: ' + results[0])
  145. }
  146. return next()
  147. }
  148. }
  149. const _ips = getIps()
  150. if (!_ips || !_ips.length) {
  151. if (settings.mode == 'allow') {
  152. // ip list is empty, thus no one allowed
  153. return error('0.0.0.0/0', next)
  154. } else {
  155. // there are no blocked ips, skip
  156. return next()
  157. }
  158. }
  159. const ip = settings.detectIp(req)
  160. if (matchClientIp(ip, req)) {
  161. // Grant access
  162. if (settings.log && settings.logLevel !== 'deny') {
  163. settings.logF('Access granted to IP address: ' + ip)
  164. }
  165. return next()
  166. }
  167. // Deny access
  168. if (settings.log && settings.logLevel !== 'allow') {
  169. settings.logF('Access denied to IP address: ' + ip)
  170. }
  171. return error(ip, next)
  172. }
  173. }