TrafficLimiter.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 Exception;
  14. use IPLib\Factory;
  15. use PrivateBin\Configuration;
  16. /**
  17. * TrafficLimiter
  18. *
  19. * Handles traffic limiting, so no user does more than one call per 10 seconds.
  20. */
  21. class TrafficLimiter extends AbstractPersistence
  22. {
  23. /**
  24. * time limit in seconds, defaults to 10s
  25. *
  26. * @access private
  27. * @static
  28. * @var int
  29. */
  30. private static $_limit = 10;
  31. /**
  32. * listed ips are exempted from limits, defaults to null
  33. *
  34. * @access private
  35. * @static
  36. * @var string|null
  37. */
  38. private static $_exemptedIp = null;
  39. /**
  40. * key to fetch IP address
  41. *
  42. * @access private
  43. * @static
  44. * @var string
  45. */
  46. private static $_ipKey = 'REMOTE_ADDR';
  47. /**
  48. * set the time limit in seconds
  49. *
  50. * @access public
  51. * @static
  52. * @param int $limit
  53. */
  54. public static function setLimit($limit)
  55. {
  56. self::$_limit = $limit;
  57. }
  58. /**
  59. * set a list of ip(ranges) as string
  60. *
  61. * @access public
  62. * @static
  63. * @param string $exemptedIps
  64. */
  65. public static function setExemptedIp($exemptedIp)
  66. {
  67. self::$_exemptedIp = $exemptedIp;
  68. }
  69. /**
  70. * set configuration options of the traffic limiter
  71. *
  72. * @access public
  73. * @static
  74. * @param Configuration $conf
  75. */
  76. public static function setConfiguration(Configuration $conf)
  77. {
  78. self::setLimit($conf->getKey('limit', 'traffic'));
  79. self::setExemptedIp($conf->getKey('exemptedIp', 'traffic'));
  80. if (($option = $conf->getKey('header', 'traffic')) !== null) {
  81. $httpHeader = 'HTTP_' . $option;
  82. if (array_key_exists($httpHeader, $_SERVER) && !empty($_SERVER[$httpHeader])) {
  83. self::$_ipKey = $httpHeader;
  84. }
  85. }
  86. }
  87. /**
  88. * get a HMAC of the current visitors IP address
  89. *
  90. * @access public
  91. * @static
  92. * @param string $algo
  93. * @return string
  94. */
  95. public static function getHash($algo = 'sha512')
  96. {
  97. return hash_hmac($algo, $_SERVER[self::$_ipKey], ServerSalt::get());
  98. }
  99. /**
  100. * Validate $_ipKey against configured ipranges. If matched we will ignore the ip
  101. *
  102. * @access private
  103. * @static
  104. * @param string $ipRange
  105. * @return bool
  106. */
  107. private static function matchIp($ipRange = null)
  108. {
  109. if (is_string($ipRange)) {
  110. $ipRange = trim($ipRange);
  111. }
  112. $address = Factory::addressFromString($_SERVER[self::$_ipKey]);
  113. $range = Factory::rangeFromString($ipRange);
  114. // address could not be parsed, we might not be in IP space and try a string comparison instead
  115. if (is_null($address)) {
  116. return $_SERVER[self::$_ipKey] === $ipRange;
  117. }
  118. // range could not be parsed, possibly an invalid ip range given in config
  119. if (is_null($range)) {
  120. return false;
  121. }
  122. // Ip-lib throws an exception when something goes wrong, if so we want to catch it and set contained to false
  123. try {
  124. return $address->matches($range);
  125. } catch (Exception $e) {
  126. // If something is wrong with matching the ip, we assume it doesn't match
  127. return false;
  128. }
  129. }
  130. /**
  131. * traffic limiter
  132. *
  133. * Make sure the IP address makes at most 1 request every 10 seconds.
  134. *
  135. * @access public
  136. * @static
  137. * @throws Exception
  138. * @return bool
  139. */
  140. public static function canPass()
  141. {
  142. // disable limits if set to less then 1
  143. if (self::$_limit < 1) {
  144. return true;
  145. }
  146. // Check if $_ipKey is exempted from ratelimiting
  147. if (!is_null(self::$_exemptedIp)) {
  148. $exIp_array = explode(',', self::$_exemptedIp);
  149. foreach ($exIp_array as $ipRange) {
  150. if (self::matchIp($ipRange) === true) {
  151. return true;
  152. }
  153. }
  154. }
  155. // this hash is used as an array key, hence a shorter algo is used
  156. $hash = self::getHash('sha256');
  157. $now = time();
  158. $tl = (int) self::$_store->getValue('traffic_limiter', $hash);
  159. self::$_store->purgeValues('traffic_limiter', $now - self::$_limit);
  160. if ($tl > 0 && ($tl + self::$_limit >= $now)) {
  161. $result = false;
  162. } else {
  163. $tl = time();
  164. $result = true;
  165. }
  166. self::$_store->setValue((string) $tl, 'traffic_limiter', $hash);
  167. return $result;
  168. }
  169. }