TrafficLimiter.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 0.22
  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 the current visitors IP address
  70. *
  71. * @access public
  72. * @static
  73. * @return string
  74. */
  75. public static function getIp()
  76. {
  77. return $_SERVER[self::$_ipKey];
  78. }
  79. /**
  80. * traffic limiter
  81. *
  82. * Make sure the IP address makes at most 1 request every 10 seconds.
  83. *
  84. * @access public
  85. * @static
  86. * @throws Exception
  87. * @return bool
  88. */
  89. public static function canPass()
  90. {
  91. // disable limits if set to less then 1
  92. if (self::$_limit < 1) {
  93. return true;
  94. }
  95. $ip = hash_hmac('sha256', self::getIp(), ServerSalt::get());
  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 IPs to keep it small
  109. foreach ($tl as $key => $time) {
  110. if ($time + self::$_limit < $now) {
  111. unset($tl[$key]);
  112. }
  113. }
  114. if (array_key_exists($ip, $tl) && ($tl[$ip] + self::$_limit >= $now)) {
  115. $result = false;
  116. } else {
  117. $tl[$ip] = 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. }