TrafficLimiter.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /**
  3. * PrivateBin
  4. *
  5. * a zero-knowledge paste bin
  6. *
  7. * @link https://github.com/PrivateBin/PrivateBin
  8. * @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
  9. * @license https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
  10. * @version 1.3.5
  11. */
  12. namespace PrivateBin\Persistence;
  13. use IPLib\Factory;
  14. use PrivateBin\Configuration;
  15. /**
  16. * TrafficLimiter
  17. *
  18. * Handles traffic limiting, so no user does more than one call per 10 seconds.
  19. */
  20. class TrafficLimiter extends AbstractPersistence
  21. {
  22. /**
  23. * time limit in seconds, defaults to 10s
  24. *
  25. * @access private
  26. * @static
  27. * @var int
  28. */
  29. private static $_limit = 10;
  30. /**
  31. * listed IPs are exempted from limits, defaults to null
  32. *
  33. * @access private
  34. * @static
  35. * @var string|null
  36. */
  37. private static $_exempted = null;
  38. /**
  39. * key to fetch IP address
  40. *
  41. * @access private
  42. * @static
  43. * @var string
  44. */
  45. private static $_ipKey = 'REMOTE_ADDR';
  46. /**
  47. * set the time limit in seconds
  48. *
  49. * @access public
  50. * @static
  51. * @param int $limit
  52. */
  53. public static function setLimit($limit)
  54. {
  55. self::$_limit = $limit;
  56. }
  57. /**
  58. * set a list of IP(-ranges) as string
  59. *
  60. * @access public
  61. * @static
  62. * @param string $exempted
  63. */
  64. public static function setExempted($exempted)
  65. {
  66. self::$_exempted = $exempted;
  67. }
  68. /**
  69. * set configuration options of the traffic limiter
  70. *
  71. * @access public
  72. * @static
  73. * @param Configuration $conf
  74. */
  75. public static function setConfiguration(Configuration $conf)
  76. {
  77. self::setLimit($conf->getKey('limit', 'traffic'));
  78. self::setExempted($conf->getKey('exempted', 'traffic'));
  79. if (($option = $conf->getKey('header', 'traffic')) !== '') {
  80. $httpHeader = 'HTTP_' . $option;
  81. if (array_key_exists($httpHeader, $_SERVER) && !empty($_SERVER[$httpHeader])) {
  82. self::$_ipKey = $httpHeader;
  83. }
  84. }
  85. }
  86. /**
  87. * get a HMAC of the current visitors IP address
  88. *
  89. * @access public
  90. * @static
  91. * @param string $algo
  92. * @return string
  93. */
  94. public static function getHash($algo = 'sha512')
  95. {
  96. return hash_hmac($algo, $_SERVER[self::$_ipKey], ServerSalt::get());
  97. }
  98. /**
  99. * Validate $_ipKey against configured ipranges. If matched we will ignore the ip
  100. *
  101. * @access private
  102. * @static
  103. * @param string $ipRange
  104. * @return bool
  105. */
  106. private static function matchIp($ipRange = null)
  107. {
  108. if (is_string($ipRange)) {
  109. $ipRange = trim($ipRange);
  110. }
  111. $address = Factory::addressFromString($_SERVER[self::$_ipKey]);
  112. $range = Factory::rangeFromString($ipRange);
  113. // address could not be parsed, we might not be in IP space and try a string comparison instead
  114. if (is_null($address)) {
  115. return $_SERVER[self::$_ipKey] === $ipRange;
  116. }
  117. // range could not be parsed, possibly an invalid ip range given in config
  118. if (is_null($range)) {
  119. return false;
  120. }
  121. return $address->matches($range);
  122. }
  123. /**
  124. * traffic limiter
  125. *
  126. * Make sure the IP address makes at most 1 request every 10 seconds.
  127. *
  128. * @access public
  129. * @static
  130. * @return bool
  131. */
  132. public static function canPass()
  133. {
  134. // disable limits if set to less then 1
  135. if (self::$_limit < 1) {
  136. return true;
  137. }
  138. // Check if $_ipKey is exempted from ratelimiting
  139. if (!empty(self::$_exempted)) {
  140. $exIp_array = explode(',', self::$_exempted);
  141. foreach ($exIp_array as $ipRange) {
  142. if (self::matchIp($ipRange) === true) {
  143. return true;
  144. }
  145. }
  146. }
  147. // this hash is used as an array key, hence a shorter algo is used
  148. $hash = self::getHash('sha256');
  149. $now = time();
  150. $tl = (int) self::$_store->getValue('traffic_limiter', $hash);
  151. self::$_store->purgeValues('traffic_limiter', $now - self::$_limit);
  152. if ($tl > 0 && ($tl + self::$_limit >= $now)) {
  153. $result = false;
  154. } else {
  155. $tl = time();
  156. $result = true;
  157. }
  158. if (!self::$_store->setValue((string) $tl, 'traffic_limiter', $hash)) {
  159. error_log('failed to store the traffic limiter, it probably contains outdated information');
  160. }
  161. return $result;
  162. }
  163. }