TrafficLimiter.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. // address could not be parsed, we might not be in IP space and try a string comparison instead
  112. if ($address == null) {
  113. return $_SERVER[self::$_ipKey] === $ipRange;
  114. }
  115. // range could not be parsed, possibly an invalid ip range given in config
  116. if ($range == null) {
  117. return false;
  118. }
  119. // Ip-lib throws an exception when something goes wrong, if so we want to catch it and set contained to false
  120. try {
  121. return $address->matches($range);
  122. } catch (\Exception $e) {
  123. // If something is wrong with matching the ip, we assume it doesn't match
  124. return false;
  125. }
  126. }
  127. /**
  128. * traffic limiter
  129. *
  130. * Make sure the IP address makes at most 1 request every 10 seconds.
  131. *
  132. * @access public
  133. * @static
  134. * @throws \Exception
  135. * @return bool
  136. */
  137. public static function canPass()
  138. {
  139. // disable limits if set to less then 1
  140. if (self::$_limit < 1) {
  141. return true;
  142. }
  143. // Check if $_ipKey is exempted from ratelimiting
  144. if (!is_null(self::$_exemptedIp)) {
  145. $exIp_array = explode(',', self::$_exemptedIp);
  146. foreach ($exIp_array as $ipRange) {
  147. if (self::matchIp($ipRange) === true) {
  148. return true;
  149. }
  150. }
  151. }
  152. $file = 'traffic_limiter.php';
  153. if (self::_exists($file)) {
  154. require self::getPath($file);
  155. $tl = $GLOBALS['traffic_limiter'];
  156. } else {
  157. $tl = array();
  158. }
  159. // purge file of expired hashes to keep it small
  160. $now = time();
  161. foreach ($tl as $key => $time) {
  162. if ($time + self::$_limit < $now) {
  163. unset($tl[$key]);
  164. }
  165. }
  166. // this hash is used as an array key, hence a shorter algo is used
  167. $hash = self::getHash('sha256');
  168. if (array_key_exists($hash, $tl) && ($tl[$hash] + self::$_limit >= $now)) {
  169. $result = false;
  170. } else {
  171. $tl[$hash] = time();
  172. $result = true;
  173. }
  174. self::_store(
  175. $file,
  176. '<?php' . PHP_EOL .
  177. '$GLOBALS[\'traffic_limiter\'] = ' . var_export($tl, true) . ';'
  178. );
  179. return $result;
  180. }
  181. }