TrafficLimiter.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 string
  58. *
  59. * @access public
  60. * @static
  61. * @param string $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. * Validate $_ipKey against configured ipranges. If matched ratelimiter will ignore ip
  100. *
  101. * @access private
  102. * @static
  103. * @param string $ipRange
  104. * @return bool
  105. */
  106. private static function matchIp($ipRange = null)
  107. {
  108. // Match $_ipKey to $ipRange and if it matches it will return with a true
  109. $address = \IPLib\Factory::addressFromString($_SERVER[self::$_ipKey]);
  110. $range = \IPLib\Factory::rangeFromString(trim($ipRange));
  111. // If $range is null something went wrong (possible invalid ip given in config). It's here becaue matches($range) does not accepts null vallue
  112. if ($range == null) {
  113. return false;
  114. }
  115. // Ip-lib does throws and exception when something goes wrong, if so we want to catch it and set contained to false
  116. try {
  117. return $address->matches($range);
  118. } catch (\Exception $e) {
  119. // If something is wrong with matching the ip, we do nothing
  120. }
  121. return false;
  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. * @throws \Exception
  131. * @return bool
  132. */
  133. public static function canPass()
  134. {
  135. // disable limits if set to less then 1
  136. if (self::$_limit < 1) {
  137. return true;
  138. }
  139. // Check if $_ipKey is exempted from ratelimiting
  140. if (!is_null(self::$_exemptedIp)) {
  141. $exIp_array = explode(',', self::$_exemptedIp);
  142. foreach ($exIp_array as $ipRange) {
  143. if (self::matchIp($ipRange) === true) {
  144. return true;
  145. }
  146. }
  147. }
  148. $file = 'traffic_limiter.php';
  149. if (self::_exists($file)) {
  150. require self::getPath($file);
  151. $tl = $GLOBALS['traffic_limiter'];
  152. } else {
  153. $tl = array();
  154. }
  155. // purge file of expired hashes to keep it small
  156. $now = time();
  157. foreach ($tl as $key => $time) {
  158. if ($time + self::$_limit < $now) {
  159. unset($tl[$key]);
  160. }
  161. }
  162. // this hash is used as an array key, hence a shorter algo is used
  163. $hash = self::getHash('sha256');
  164. if (array_key_exists($hash, $tl) && ($tl[$hash] + self::$_limit >= $now)) {
  165. $result = false;
  166. } else {
  167. $tl[$hash] = time();
  168. $result = true;
  169. }
  170. self::_store(
  171. $file,
  172. '<?php' . PHP_EOL .
  173. '$GLOBALS[\'traffic_limiter\'] = ' . var_export($tl, true) . ';'
  174. );
  175. return $result;
  176. }
  177. }