1
0

TrafficLimiter.php 3.4 KB

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