diff --git a/core/bootstrap.php b/core/bootstrap.php index 095625b..f37c0f3 100644 --- a/core/bootstrap.php +++ b/core/bootstrap.php @@ -18,6 +18,8 @@ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ + + use Core\Helpers\DDosProtection; use Core\Models\User; use Core\Helpers\Cache; use Core\Models\UTM; @@ -136,6 +138,7 @@ User::logout(); } + $ddosProtectObject = new DDosProtection('bootstrap'); try { $userId = User::getCurrentUserId(); } catch (CoreException $e) { @@ -143,6 +146,7 @@ } if (!empty($userId)) { try { + $ddosProtectObject->setUserId($userId); $USER = new User($userId); } catch (CoreException $e) { $USER = null; @@ -150,6 +154,7 @@ } else { $USER = null; } + $ddosProtectObject->checkDDos(); // debug function sendTelegram(?string $message, ?string $file = null): void diff --git a/core/lib/Core/Helpers/DDosProtection.class.php b/core/lib/Core/Helpers/DDosProtection.class.php new file mode 100644 index 0000000..1e3c914 --- /dev/null +++ b/core/lib/Core/Helpers/DDosProtection.class.php @@ -0,0 +1,115 @@ + + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + + + namespace Core\Helpers; + + use Core\DataBases\DB; + + /** + * Класс защиты от DDOS атак + */ + class DDosProtection + { + const TABLE = DB_TABLE_PREFIX . 'ddos_protection'; + + private ?string $place = null; + + private ?int $userId = null; + + private ?string $observedKey = null; + + private int $maxAttempts = 5; + + private int $timeInterval = 20; + + + public function __construct(?string $place = null) + { + $this->place = $place; + } + + public function setUserId(int $userId): self + { + $this->userId = $userId; + return $this; + } + + public function setObservedKey(string $key): self + { + $this->observedKey = $key; + return $this; + } + + private function checkAndSetDefaultParams(): void + { + if (empty($this->observedKey)) { + $this->observedKey = SystemFunctions::getIP(); + if ($this->observedKey === 'undefined') { + $this->observedKey = md5($_SERVER['HTTP_USER_AGENT']); + } + if ($this->userId !== null) { + $this->observedKey .= '-' . $this->userId; + } + } + + if (empty($this->timeInterval)) { + $this->timeInterval = 60; + } + } + + private function clearData(): void + { + /** @var DB $db */ + $DB = DB::getInstance(); + $DB->query('DELETE FROM ' . self::TABLE . ' WHERE last_active < ' . (time() - $this->timeInterval)); + } + + public function checkDDos(): void + { + $this->clearData(); + $this->checkAndSetDefaultParams(); + + /** @var DB $db */ + $DB = DB::getInstance(); + $item = $DB->getItem(self::TABLE, ['observed_key' => $this->observedKey]); + if ($item) { + if ($item['attempts'] >= $item['attempts_limit'] && (time() - $item['last_active']) <= $item['time_interval']) { + die('DDoS protected active'); + } + + $DB->update(self::TABLE, ['id' => $item['id']], ['attempts' => ++$item['attempts'], 'last_active' => time()]); + } else { + $DB->addItem(self::TABLE, + [ + 'user_id' => $this->userId, + 'place' => $this->place, + 'observed_key' => $this->observedKey, + 'attempts' => 1, + 'attempts_limit' => $this->maxAttempts, + 'time_interval' => $this->timeInterval, + 'last_active' => time(), + ] + ); + } + } + } \ No newline at end of file