2021-01-01 00:18:53 +03:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
namespace openvk\Web\Models\Entities;
|
|
|
|
use openvk\Web\Models\RowModel;
|
|
|
|
use openvk\Web\Util\DateTime;
|
|
|
|
|
|
|
|
class IP extends RowModel
|
|
|
|
{
|
|
|
|
protected $tableName = "ip";
|
|
|
|
|
|
|
|
const RL_RESET = 0;
|
|
|
|
const RL_CANEXEC = 1;
|
|
|
|
const RL_VIOLATION = 2;
|
|
|
|
const RL_BANNED = 3;
|
|
|
|
|
|
|
|
function getIp(): string
|
|
|
|
{
|
|
|
|
return inet_ntop($this->getRecord()->ip);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getDiscoveryDate(): DateTime
|
|
|
|
{
|
|
|
|
return new DateTime($this->getRecord()->first_seen);
|
|
|
|
}
|
|
|
|
|
|
|
|
function isBanned(): bool
|
|
|
|
{
|
|
|
|
return (bool) $this->getRecord()->banned;
|
|
|
|
}
|
|
|
|
|
|
|
|
function ban(): void
|
|
|
|
{
|
|
|
|
$this->stateChanges("banned", true);
|
|
|
|
$this->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
function pardon(): void
|
|
|
|
{
|
|
|
|
$this->stateChanges("banned", false);
|
|
|
|
$this->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
function clear(): void
|
|
|
|
{
|
|
|
|
$this->stateChanges("rate_limit_counter_start", 0);
|
|
|
|
$this->stateChanges("rate_limit_counter", 0);
|
|
|
|
$this->stateChanges("rate_limit_violation_counter_start", 0);
|
|
|
|
$this->stateChanges("rate_limit_violation_counter", 0);
|
|
|
|
$this->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
function rateLimit(int $actionComplexity = 1): int
|
|
|
|
{
|
|
|
|
$counterSessionStart = $this->getRecord()->rate_limit_counter_start;
|
|
|
|
$vCounterSessionStart = $this->getRecord()->rate_limit_violation_counter_start;
|
|
|
|
|
|
|
|
$aCounter = $this->getRecord()->rate_limit_counter;
|
|
|
|
$vCounter = $this->getRecord()->rate_limit_violation_counter;
|
|
|
|
|
|
|
|
$config = (object) OPENVK_ROOT_CONF["openvk"]["preferences"]["security"]["rateLimits"];
|
|
|
|
|
|
|
|
try {
|
|
|
|
if((time() - $config->time) > $counterSessionStart) {
|
|
|
|
$counterSessionStart = time();
|
|
|
|
$aCounter = $actionComplexity;
|
|
|
|
|
|
|
|
return static::RL_RESET;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(($aCounter + $actionComplexity) <= $config->actions) {
|
|
|
|
$aCounter += $actionComplexity;
|
|
|
|
|
|
|
|
return static::RL_CANEXEC;
|
|
|
|
}
|
|
|
|
|
|
|
|
if((time() - $config->maxViolationsAge) > $vCounterSessionStart) {
|
|
|
|
$vCounterSessionStart = time();
|
|
|
|
$vCounter = 1;
|
|
|
|
|
|
|
|
return static::RL_VIOLATION;
|
|
|
|
}
|
|
|
|
|
|
|
|
$vCounter += 1;
|
|
|
|
if($vCounter >= $config->maxViolations) {
|
|
|
|
$this->stateChanges("banned", true);
|
|
|
|
|
|
|
|
return static::RL_BANNED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return static::RL_VIOLATION;
|
|
|
|
} finally {
|
|
|
|
$this->stateChanges("rate_limit_counter_start", $counterSessionStart);
|
|
|
|
$this->stateChanges("rate_limit_counter", $aCounter);
|
|
|
|
$this->stateChanges("rate_limit_violation_counter_start", $vCounterSessionStart);
|
|
|
|
$this->stateChanges("rate_limit_violation_counter", $vCounter);
|
2023-08-11 16:43:39 +03:00
|
|
|
$this->save(false);
|
2021-01-01 00:18:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function setIp(string $ip): void
|
|
|
|
{
|
|
|
|
$ip = inet_pton($ip);
|
|
|
|
if(!$ip)
|
|
|
|
throw new \UnexpectedValueException("Malformed IP address");
|
|
|
|
|
|
|
|
$this->stateChanges("ip", $ip);
|
|
|
|
}
|
|
|
|
|
2023-10-03 19:40:13 +03:00
|
|
|
function save(?bool $log = false): void
|
2021-01-01 00:18:53 +03:00
|
|
|
{
|
|
|
|
if(is_null($this->getRecord()))
|
|
|
|
$this->stateChanges("first_seen", time());
|
|
|
|
|
2023-08-11 16:43:39 +03:00
|
|
|
parent::save($log);
|
2021-01-01 00:18:53 +03:00
|
|
|
}
|
|
|
|
}
|