2012-04-30 01:15:06 +08:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-11 17:58:15 +08:00
|
|
|
* PrivateBin
|
2012-04-30 01:15:06 +08:00
|
|
|
*
|
|
|
|
* a zero-knowledge paste bin
|
|
|
|
*
|
2016-07-11 17:58:15 +08:00
|
|
|
* @link https://github.com/PrivateBin/PrivateBin
|
2012-04-30 01:15:06 +08:00
|
|
|
* @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
|
2016-07-19 19:56:52 +08:00
|
|
|
* @license https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
|
2015-11-10 04:39:42 +08:00
|
|
|
* @version 0.22
|
2012-04-30 01:15:06 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2013-11-01 08:15:14 +08:00
|
|
|
* trafficlimiter
|
2012-04-30 01:15:06 +08:00
|
|
|
*
|
|
|
|
* Handles traffic limiting, so no user does more than one call per 10 seconds.
|
|
|
|
*/
|
2013-11-01 08:15:14 +08:00
|
|
|
class trafficlimiter extends persistence
|
2012-04-30 01:15:06 +08:00
|
|
|
{
|
|
|
|
/**
|
2015-08-16 21:55:31 +08:00
|
|
|
* time limit in seconds, defaults to 10s
|
|
|
|
*
|
2012-04-30 01:15:06 +08:00
|
|
|
* @access private
|
|
|
|
* @static
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
private static $_limit = 10;
|
|
|
|
|
2015-09-26 23:57:46 +08:00
|
|
|
/**
|
|
|
|
* key to fetch IP address
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @static
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $_ipKey = 'REMOTE_ADDR';
|
|
|
|
|
2012-04-30 01:15:06 +08:00
|
|
|
/**
|
|
|
|
* set the time limit in seconds
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
* @param int $limit
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function setLimit($limit)
|
|
|
|
{
|
|
|
|
self::$_limit = $limit;
|
|
|
|
}
|
|
|
|
|
2015-09-26 23:57:46 +08:00
|
|
|
/**
|
|
|
|
* set configuration options of the traffic limiter
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
* @param configuration $conf
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function setConfiguration(configuration $conf)
|
|
|
|
{
|
|
|
|
self::setLimit($conf->getKey('limit', 'traffic'));
|
|
|
|
self::setPath($conf->getKey('dir', 'traffic'));
|
|
|
|
if (($option = $conf->getKey('header', 'traffic')) !== null)
|
|
|
|
{
|
|
|
|
$httpHeader = 'HTTP_' . $option;
|
|
|
|
if (array_key_exists($httpHeader, $_SERVER) && !empty($_SERVER[$httpHeader]))
|
|
|
|
{
|
|
|
|
self::$_ipKey = $httpHeader;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get the current visitors IP address
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function getIp()
|
|
|
|
{
|
2015-12-23 03:58:23 +08:00
|
|
|
return $_SERVER[self::$_ipKey];
|
2015-09-26 23:57:46 +08:00
|
|
|
}
|
|
|
|
|
2012-04-30 01:15:06 +08:00
|
|
|
/**
|
|
|
|
* traffic limiter
|
|
|
|
*
|
|
|
|
* Make sure the IP address makes at most 1 request every 10 seconds.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @static
|
2015-08-28 03:41:21 +08:00
|
|
|
* @throws Exception
|
2012-04-30 01:15:06 +08:00
|
|
|
* @return bool
|
|
|
|
*/
|
2015-09-26 23:57:46 +08:00
|
|
|
public static function canPass()
|
2012-04-30 01:15:06 +08:00
|
|
|
{
|
2015-09-26 23:57:46 +08:00
|
|
|
// disable limits if set to less then 1
|
|
|
|
if (self::$_limit < 1) return true;
|
2013-10-31 06:54:42 +08:00
|
|
|
|
2015-12-23 03:58:23 +08:00
|
|
|
$ip = hash_hmac('sha256', self::getIp(), serversalt::get());
|
|
|
|
|
2013-11-01 08:15:14 +08:00
|
|
|
$file = 'traffic_limiter.php';
|
|
|
|
if (!self::_exists($file))
|
2012-04-30 01:15:06 +08:00
|
|
|
{
|
2013-11-01 08:15:14 +08:00
|
|
|
self::_store(
|
2012-04-30 01:15:06 +08:00
|
|
|
$file,
|
|
|
|
'<?php' . PHP_EOL .
|
|
|
|
'$GLOBALS[\'traffic_limiter\'] = array();' . PHP_EOL
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-11-01 08:15:14 +08:00
|
|
|
$path = self::getPath($file);
|
|
|
|
require $path;
|
2012-08-26 06:49:11 +08:00
|
|
|
$now = time();
|
2012-04-30 01:15:06 +08:00
|
|
|
$tl = $GLOBALS['traffic_limiter'];
|
|
|
|
|
|
|
|
// purge file of expired IPs to keep it small
|
2016-07-18 21:13:56 +08:00
|
|
|
foreach ($tl as $key => $time)
|
2012-04-30 01:15:06 +08:00
|
|
|
{
|
2012-08-26 06:49:11 +08:00
|
|
|
if ($time + self::$_limit < $now)
|
2012-04-30 01:15:06 +08:00
|
|
|
{
|
|
|
|
unset($tl[$key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-26 06:49:11 +08:00
|
|
|
if (array_key_exists($ip, $tl) && ($tl[$ip] + self::$_limit >= $now))
|
2012-04-30 01:15:06 +08:00
|
|
|
{
|
|
|
|
$result = false;
|
|
|
|
} else {
|
|
|
|
$tl[$ip] = time();
|
|
|
|
$result = true;
|
|
|
|
}
|
2013-11-01 08:15:14 +08:00
|
|
|
self::_store(
|
2012-04-30 01:15:06 +08:00
|
|
|
$file,
|
|
|
|
'<?php' . PHP_EOL .
|
|
|
|
'$GLOBALS[\'traffic_limiter\'] = ' .
|
|
|
|
var_export($tl, true) . ';' . PHP_EOL
|
|
|
|
);
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
}
|