[+] В тестовом режиме добавлена защита от ddos атак
This commit is contained in:
1 parent
0479d29b08
commit
4b510c78e7
2 files changed
+120
No files matched your search
@@ -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
|
||||
|
||||
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Copyright (c) 2023 Roman Grinko <rsgrinko@gmail.com>
|
||||
* 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(),
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user