]> git.openstreetmap.org Git - nominatim.git/commitdiff
remove IP blocking and rate-limiting code
authorSarah Hoffmann <lonvia@denofr.de>
Mon, 18 Jul 2016 12:41:09 +0000 (14:41 +0200)
committerSarah Hoffmann <lonvia@denofr.de>
Mon, 18 Jul 2016 19:33:09 +0000 (21:33 +0200)
Closes #472.

lib/init-website.php
lib/init.php
lib/leakybucket.php [deleted file]
lib/lib.php
settings/settings.php
website/details.php
website/lookup.php
website/reverse.php

index 04bc518ca9d3ec044dee0aa16e23ac5e064b016d..61a417314ad1fa66ccc4a72e6e91fc43e5c0154a 100644 (file)
        }
        if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') exit;
 
-       $aBucketKeys = array();
-
-       if (isset($_SERVER["HTTP_REFERER"])) $aBucketKeys[] = str_replace('www.','',strtolower(parse_url($_SERVER["HTTP_REFERER"], PHP_URL_HOST)));
-       if (isset($_SERVER["REMOTE_ADDR"])) $aBucketKeys[] = $_SERVER["REMOTE_ADDR"];
-       if (isset($_GET["email"])) $aBucketKeys[] = $_GET["email"];
-
-       $fBucketVal = doBucket($aBucketKeys, 
-                       (defined('CONST_ConnectionBucket_PageType')?constant('CONST_ConnectionBucket_Cost_'.CONST_ConnectionBucket_PageType):1) + user_busy_cost(),
-                       CONST_ConnectionBucket_LeakRate, CONST_ConnectionBucket_BlockLimit);
-
-       if ($fBucketVal > CONST_ConnectionBucket_WaitLimit && $fBucketVal < CONST_ConnectionBucket_BlockLimit)
-       {
-               $m = getBucketMemcache();
-               $iCurrentSleeping = $m->increment('sleepCounter');
-               if (false === $iCurrentSleeping)
-               {
-                       $m->add('sleepCounter', 0);
-                       $iCurrentSleeping = $m->increment('sleepCounter');
-               }
-               if ($iCurrentSleeping >= CONST_ConnectionBucket_MaxSleeping || isBucketSleeping($aBucketKeys))
-               {
-                       // Too many threads sleeping already.  This becomes a hard block.
-                       $fBucketVal = doBucket($aBucketKeys, CONST_ConnectionBucket_BlockLimit, CONST_ConnectionBucket_LeakRate, CONST_ConnectionBucket_BlockLimit);
-               }
-               else
-               {
-                       setBucketSleeping($aBucketKeys, true);
-                       sleep(($fBucketVal - CONST_ConnectionBucket_WaitLimit)/CONST_ConnectionBucket_LeakRate);
-                       $fBucketVal = doBucket($aBucketKeys, CONST_ConnectionBucket_LeakRate, CONST_ConnectionBucket_LeakRate, CONST_ConnectionBucket_BlockLimit);
-                       setBucketSleeping($aBucketKeys, false);
-               }
-               $m->decrement('sleepCounter');
-       }
-
-       if (strpos(CONST_BlockedIPs, ','.$_SERVER["REMOTE_ADDR"].',') !== false || $fBucketVal >= CONST_ConnectionBucket_BlockLimit)
-       {
-               header("HTTP/1.0 429 Too Many Requests");
-               echo "Your IP has been blocked. \n";
-               echo CONST_BlockMessage;
-               exit;
-       }
-
        header('Content-type: text/html; charset=utf-8');
 
index 26952d05857f4ea9ecb83530ad138d10713e8fe1..1d47c98fb2e4a6f652a557af640352c315f1e97a 100644 (file)
@@ -1,7 +1,6 @@
 <?php
 
        require_once(CONST_BasePath.'/lib/lib.php');
-       require_once(CONST_BasePath.'/lib/leakybucket.php');
        require_once(CONST_BasePath.'/lib/db.php');
 
        if (get_magic_quotes_gpc())
diff --git a/lib/leakybucket.php b/lib/leakybucket.php
deleted file mode 100644 (file)
index 47e8447..0000000
+++ /dev/null
@@ -1,168 +0,0 @@
-<?php
-
-       function getBucketMemcache()
-       {
-               static $m;
-
-               if (!CONST_ConnectionBucket_MemcacheServerAddress) return null;
-               if (!isset($m))
-               {
-                       $m = new Memcached();
-                       $m->addServer(CONST_ConnectionBucket_MemcacheServerAddress, CONST_ConnectionBucket_MemcacheServerPort);
-               }
-               return $m;
-       }
-
-       function doBucket($asKey, $iRequestCost, $iLeakPerSecond, $iThreshold)
-       {
-               $m = getBucketMemcache();
-               if (!$m) return 0;
-
-               $iMaxVal = 0;
-               $t = time();
-
-               foreach($asKey as $sKey)
-               {
-                       $aCurrentBlock = $m->get($sKey);
-                       if (!$aCurrentBlock)
-                       {
-                               $aCurrentBlock = array($iRequestCost, $t, false);
-                       }
-                       else
-                       {
-                               // add RequestCost
-                               // remove leak * the time since the last request 
-                               $aCurrentBlock[0] += $iRequestCost - ($t - $aCurrentBlock[1])*$iLeakPerSecond;
-                               $aCurrentBlock[1] = $t;
-                       }
-
-                       if ($aCurrentBlock[0] <= 0)
-                       {
-                               $m->delete($sKey);
-                       }
-                       else
-                       {
-                               // If we have hit the threshold stop and record this to the block list
-                               if ($aCurrentBlock[0] >= $iThreshold)
-                               {
-                                       $aCurrentBlock[0] = $iThreshold;
-
-                                       // Make up to 10 attempts to record this to memcache (with locking to prevent conflicts)
-                                       $i = 10;
-                                       for($i = 0; $i < 10; $i++)
-                                       {
-                                               $aBlockedList = $m->get('blockedList', null, $hCasToken);
-                                               if (!$aBlockedList)
-                                               {
-                                                       $aBlockedList = array();
-                                                       $m->add('blockedList', $aBlockedList);
-                                                       $aBlockedList = $m->get('blockedList', null, $hCasToken);
-                                               }
-                                               if (!isset($aBlockedList[$sKey]))
-                                               {
-                                                       $aBlockedList[$sKey] = array(1, $t);
-                                               }
-                                               else
-                                               {
-                                                       $aBlockedList[$sKey][0]++;
-                                                       $aBlockedList[$sKey][1] = $t;
-                                               }
-                                               if (sizeof($aBlockedList) > CONST_ConnectionBucket_MaxBlockList)
-                                               {
-                                                       uasort($aBlockedList, 'byValue1');
-                                                       $aBlockedList = array_slice($aBlockedList, 0, CONST_ConnectionBucket_MaxBlockList);
-                                               }
-                                               $x = $m->cas($hCasToken, 'blockedList', $aBlockedList);
-                                               if ($x) break;
-                                       }
-                               }
-                               // Only keep in memcache until the time it would have expired (to avoid clutering memcache)
-                                               $m->set($sKey, $aCurrentBlock, $t + 1 + $aCurrentBlock[0]/$iLeakPerSecond);
-                       }
-
-                       // Bucket result in the largest bucket we find
-                       $iMaxVal = max($iMaxVal, $aCurrentBlock[0]);
-               }
-
-               return $iMaxVal;
-       }
-
-       function isBucketSleeping($asKey)
-       {
-               $m = getBucketMemcache();
-               if (!$m) return false;
-
-               foreach($asKey as $sKey)
-               {
-                       $aCurrentBlock = $m->get($sKey);
-                       if ($aCurrentBlock[2]) return true;
-               }
-               return false;
-       }
-
-       function setBucketSleeping($asKey, $bVal)
-       {
-               $m = getBucketMemcache();
-               if (!$m) return false;
-
-               $iMaxVal = 0;
-               $t = time();
-
-               foreach($asKey as $sKey)
-               {
-                       $aCurrentBlock = $m->get($sKey);
-                       $aCurrentBlock[2] = $bVal;
-                       $m->set($sKey, $aCurrentBlock, $t + 1 + $aCurrentBlock[0]/CONST_ConnectionBucket_LeakRate);
-               }
-               return true;
-       }
-
-       function byValue1($a, $b)
-       {
-               if ($a[1] == $b[1])
-               {
-                       return 0;
-               }
-               return ($a[1] > $b[1]) ? -1 : 1;
-       }
-
-       function byLastBlockTime($a, $b)
-       {
-               if ($a['lastBlockTimestamp'] == $b['lastBlockTimestamp'])
-               {
-                       return 0;
-               }
-               return ($a['lastBlockTimestamp'] > $b['lastBlockTimestamp']) ? -1 : 1;
-       }
-
-       function getBucketBlocks()
-       {
-               $m = getBucketMemcache();
-               if (!$m) return null;
-               $t = time();
-               $aBlockedList = $m->get('blockedList', null, $hCasToken);
-               if (!$aBlockedList) $aBlockedList = array();
-               foreach($aBlockedList as $sKey => $aDetails)
-               {
-                       $aCurrentBlock = $m->get($sKey);
-                       if (!$aCurrentBlock) $aCurrentBlock = array(0, $t);
-                       $iCurrentBucketSize = max(0, $aCurrentBlock[0] - ($t - $aCurrentBlock[1])*CONST_ConnectionBucket_LeakRate);
-                       $aBlockedList[$sKey] = array(
-                               'totalBlocks' => $aDetails[0],
-                               'lastBlockTimestamp' => $aDetails[1],
-                               'isSleeping' => (isset($aCurrentBlock[2])?$aCurrentBlock[2]:false),
-                               'currentBucketSize' => $iCurrentBucketSize,
-                               'currentlyBlocked' => $iCurrentBucketSize + (CONST_ConnectionBucket_Cost_Reverse) >= CONST_ConnectionBucket_BlockLimit,
-                               );
-               }
-               uasort($aBlockedList, 'byLastBlockTime');
-               return $aBlockedList;
-       }
-
-       function clearBucketBlocks()
-       {
-               $m = getBucketMemcache();
-               if (!$m) return false;
-               $m->delete('blockedList');
-               return true;
-       }
index ec5b9b1f4330f3a68cce22ef2959767045ddeaaa..1f772331ce95b75b24337bec7af8bd8aea3f874b 100644 (file)
@@ -9,14 +9,6 @@
        }
 
 
-       function getLoadAverage()
-       {
-               $sLoadAverage = file_get_contents('/proc/loadavg');
-               $aLoadAverage = explode(' ',$sLoadAverage);
-               return (float)$aLoadAverage[0];
-       }
-
-
        function getProcessorCount()
        {
                $sCPU = file_get_contents('/proc/cpuinfo');
index d44bb967d4ab19687bf0a92fb583faf0934bba4b..cdb02924a6c0dcfa9d76dce4f2e978dc3b36205d 100644 (file)
        @define('CONST_Replication_Update_Interval', '60');  // How often upstream publishes diffs
        @define('CONST_Replication_Recheck_Interval', '60'); // How long to sleep if no update found yet
 
-       // Connection buckets to rate limit people being nasty
-       @define('CONST_ConnectionBucket_MemcacheServerAddress', false);
-       @define('CONST_ConnectionBucket_MemcacheServerPort', 11211);
-       @define('CONST_ConnectionBucket_MaxBlockList', 100);
-       @define('CONST_ConnectionBucket_LeakRate', 1);
-       @define('CONST_ConnectionBucket_BlockLimit', 10);
-       @define('CONST_ConnectionBucket_WaitLimit', 6);
-       @define('CONST_ConnectionBucket_MaxSleeping', 10);
-       @define('CONST_ConnectionBucket_Cost_Reverse', 1);
-       @define('CONST_ConnectionBucket_Cost_Search', 2);
-       @define('CONST_ConnectionBucket_Cost_Details', 3);
-       @define('CONST_ConnectionBucket_Cost_Status', 1);
-
-       // Override this function to add an adjustment factor to the cost
-       // based on server load. e.g. getBlockingProcesses
-       if (!function_exists('user_busy_cost'))
-       {
-               function user_busy_cost()
-               {
-                       return 0;
-               }
-       }
-
        // Website settings
        @define('CONST_NoAccessControl', true);
-       @define('CONST_BlockedIPs', '');
-       @define('CONST_BulkUserIPs', '');
-       @define('CONST_BlockMessage', ''); // additional info to show for blocked IPs
 
        @define('CONST_Website_BaseURL', 'http://'.php_uname('n').'/');
        // Language to assume when none is supplied with the query.
index 495db2b40e668277fd18ceb92bfbc10cba5c923f..10cd07e148676465faf67f19ed74c6a9d97e209a 100755 (executable)
@@ -7,14 +7,7 @@
        require_once(CONST_BasePath.'/lib/output.php');
 
        $sOutputFormat = 'html';
-       /*
-          $fLoadAvg = getLoadAverage();
-          if ($fLoadAvg > 3)
-          {
-          echo "Page temporarily blocked due to high server load\n";
-          exit;
-          }
-        */
+
        ini_set('memory_limit', '200M');
 
        $oDB =& getDB();
index 9d0cea4cc3602d4dd22b840c53cb79b2f621821a..2e2a1c2f514f99a6a2152f91badeb03b298f35d4 100755 (executable)
@@ -7,18 +7,6 @@
        require_once(CONST_BasePath.'/lib/PlaceLookup.php');
        require_once(CONST_BasePath.'/lib/output.php');
 
-       if (strpos(CONST_BulkUserIPs, ','.$_SERVER["REMOTE_ADDR"].',') !== false)
-       {
-               $fLoadAvg = getLoadAverage();
-               if ($fLoadAvg > 2) sleep(60);
-               if ($fLoadAvg > 4) sleep(120);
-               if ($fLoadAvg > 6)
-               {
-                       userError("Bulk User: Temporary block due to high server load");
-                       exit;
-               }
-       }
-
        $oDB =& getDB();
        ini_set('memory_limit', '200M');
 
index 7f5eca78e5cea47237dbcbf5279761f0f38403ad..0e0f2ff84965962998a9804c20e05e8e74d0fbfd 100755 (executable)
@@ -8,19 +8,6 @@
        require_once(CONST_BasePath.'/lib/ReverseGeocode.php');
        require_once(CONST_BasePath.'/lib/output.php');
 
-       if (strpos(CONST_BulkUserIPs, ','.$_SERVER["REMOTE_ADDR"].',') !== false)
-       {
-               $fLoadAvg = getLoadAverage();
-               if ($fLoadAvg > 2) sleep(60);
-               if ($fLoadAvg > 4) sleep(120);
-               if ($fLoadAvg > 6)
-               {
-                       echo "Bulk User: Temporary block due to high server load\n";
-                       exit;
-               }
-       }
-
-
        $bAsGeoJSON = getParamBool('polygon_geojson');
        $bAsKML = getParamBool('polygon_kml');
        $bAsSVG = getParamBool('polygon_svg');