TrafficLimiter.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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.1
  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. * key to fetch IP address
  31. *
  32. * @access private
  33. * @static
  34. * @var string
  35. */
  36. private static $_ipKey = 'REMOTE_ADDR';
  37. /**
  38. * set the time limit in seconds
  39. *
  40. * @access public
  41. * @static
  42. * @param int $limit
  43. * @return void
  44. */
  45. public static function setLimit($limit)
  46. {
  47. self::$_limit = $limit;
  48. }
  49. /**
  50. * set configuration options of the traffic limiter
  51. *
  52. * @access public
  53. * @static
  54. * @param Configuration $conf
  55. * @return void
  56. */
  57. public static function setConfiguration(Configuration $conf)
  58. {
  59. self::setLimit($conf->getKey('limit', 'traffic'));
  60. self::setPath($conf->getKey('dir', 'traffic'));
  61. if (($option = $conf->getKey('header', 'traffic')) !== null) {
  62. $httpHeader = 'HTTP_' . $option;
  63. if (array_key_exists($httpHeader, $_SERVER) && !empty($_SERVER[$httpHeader])) {
  64. self::$_ipKey = $httpHeader;
  65. }
  66. }
  67. }
  68. /**
  69. * get a HMAC of the current visitors IP address
  70. *
  71. * @access public
  72. * @static
  73. * @param string $algo
  74. * @return string
  75. */
  76. public static function getHash($algo = 'sha512')
  77. {
  78. return hash_hmac($algo, $_SERVER[self::$_ipKey], ServerSalt::get());
  79. }
  80. /**
  81. * traffic limiter
  82. *
  83. * Make sure the IP address makes at most 1 request every 10 seconds.
  84. *
  85. * @access public
  86. * @static
  87. * @throws Exception
  88. * @return bool
  89. */
  90. public static function canPass()
  91. {
  92. // disable limits if set to less then 1
  93. if (self::$_limit < 1) {
  94. return true;
  95. }
  96. $file = 'traffic_limiter.php';
  97. if (!self::_exists($file)) {
  98. self::_store(
  99. $file,
  100. '<?php' . PHP_EOL .
  101. '$GLOBALS[\'traffic_limiter\'] = array();' . PHP_EOL
  102. );
  103. }
  104. $path = self::getPath($file);
  105. require $path;
  106. $now = time();
  107. $tl = $GLOBALS['traffic_limiter'];
  108. // purge file of expired hashes to keep it small
  109. foreach ($tl as $key => $time) {
  110. if ($time + self::$_limit < $now) {
  111. unset($tl[$key]);
  112. }
  113. }
  114. // this hash is used as an array key, hence a shorter hash is used
  115. $hash = self::getHash('sha256');
  116. if (array_key_exists($hash, $tl) && ($tl[$hash] + self::$_limit >= $now)) {
  117. $result = false;
  118. } else {
  119. $tl[$hash] = time();
  120. $result = true;
  121. }
  122. self::_store(
  123. $file,
  124. '<?php' . PHP_EOL .
  125. '$GLOBALS[\'traffic_limiter\'] = ' .
  126. var_export($tl, true) . ';' . PHP_EOL
  127. );
  128. return $result;
  129. }
  130. }