TrafficLimiter.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 PrivateBin\Configuration;
  14. /**
  15. * TrafficLimiter
  16. *
  17. * Handles traffic limiting, so no user does more than one call per 10 seconds.
  18. */
  19. class TrafficLimiter extends AbstractPersistence
  20. {
  21. /**
  22. * time limit in seconds, defaults to 10s
  23. *
  24. * @access private
  25. * @static
  26. * @var int
  27. */
  28. private static $_limit = 10;
  29. /**
  30. * listed ips are exempted from limits, defaults to null
  31. *
  32. * @access private
  33. * @static
  34. * @var array
  35. */
  36. private static $_exemptedIp = null;
  37. /**
  38. * key to fetch IP address
  39. *
  40. * @access private
  41. * @static
  42. * @var string
  43. */
  44. private static $_ipKey = 'REMOTE_ADDR';
  45. /**
  46. * set the time limit in seconds
  47. *
  48. * @access public
  49. * @static
  50. * @param int $limit
  51. */
  52. public static function setLimit($limit)
  53. {
  54. self::$_limit = $limit;
  55. }
  56. /**
  57. * set a list of ip(ranges) as array
  58. *
  59. * @access public
  60. * @static
  61. * @param array $exemptedIps
  62. */
  63. public static function setExemptedIp($exemptedIp)
  64. {
  65. self::$_exemptedIp = $exemptedIp;
  66. }
  67. /**
  68. * set configuration options of the traffic limiter
  69. *
  70. * @access public
  71. * @static
  72. * @param Configuration $conf
  73. */
  74. public static function setConfiguration(Configuration $conf)
  75. {
  76. self::setLimit($conf->getKey('limit', 'traffic'));
  77. self::setPath($conf->getKey('dir', 'traffic'));
  78. self::setExemptedIp($conf->getKey('exemptedIp', 'traffic'));
  79. if (($option = $conf->getKey('header', 'traffic')) !== null) {
  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. * traffic limiter
  100. *
  101. * Make sure the IP address makes at most 1 request every 10 seconds.
  102. *
  103. * @access public
  104. * @static
  105. * @throws \Exception
  106. * @return bool
  107. */
  108. public static function canPass()
  109. {
  110. // disable limits if set to less then 1
  111. if (self::$_limit < 1) {
  112. return true;
  113. }
  114. // Check if $_ipKey is exempted from ratelimiting
  115. if (!is_null(self::$_exemptedIp)) {
  116. $exIp_array = explode(",", self::$_exemptedIp);
  117. foreach ($exIp_array as $ipRange) {
  118. // Match $_ipKey to $ipRange and if it matches it will return with a true
  119. $address = \IPLib\Factory::addressFromString($_SERVER[self::$_ipKey]);
  120. $range = \IPLib\Factory::rangeFromString(trim($ipRange));
  121. // If $range is null something went wrong (possible invalid ip given in config)
  122. if ($range == null) {
  123. $contained = false;
  124. } else {
  125. // Ip-lib does throws and exception when something goes wrong, if so we want to catch it and set contained to false
  126. try {
  127. $contained = $address->matches($range);
  128. } catch (Exception $e) {
  129. // If something is wrong with matching the ip, we set $contained to false
  130. $contained = false;
  131. }
  132. }
  133. // Matches return true!
  134. if ($contained == true) {
  135. return true;
  136. }
  137. }
  138. }
  139. $file = 'traffic_limiter.php';
  140. if (self::_exists($file)) {
  141. require self::getPath($file);
  142. $tl = $GLOBALS['traffic_limiter'];
  143. } else {
  144. $tl = array();
  145. }
  146. // purge file of expired hashes to keep it small
  147. $now = time();
  148. foreach ($tl as $key => $time) {
  149. if ($time + self::$_limit < $now) {
  150. unset($tl[$key]);
  151. }
  152. }
  153. // this hash is used as an array key, hence a shorter algo is used
  154. $hash = self::getHash('sha256');
  155. if (array_key_exists($hash, $tl) && ($tl[$hash] + self::$_limit >= $now)) {
  156. $result = false;
  157. } else {
  158. $tl[$hash] = time();
  159. $result = true;
  160. }
  161. self::_store(
  162. $file,
  163. '<?php' . PHP_EOL .
  164. '$GLOBALS[\'traffic_limiter\'] = ' . var_export($tl, true) . ';'
  165. );
  166. return $result;
  167. }
  168. }