mirror of
https://github.com/slawkens/myaac.git
synced 2025-04-26 17:29:21 +02:00
Change spaces to tabs
This commit is contained in:
parent
327dcb5f87
commit
35e28350bd
@ -1041,7 +1041,7 @@ function load_config_lua($filename)
|
|||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
function str_replace_first($search, $replace, $subject) {
|
function str_replace_first($search,$replace, $subject) {
|
||||||
$pos = strpos($subject, $search);
|
$pos = strpos($subject, $search);
|
||||||
if ($pos !== false) {
|
if ($pos !== false) {
|
||||||
return substr_replace($subject, $replace, $pos, strlen($search));
|
return substr_replace($subject, $replace, $pos, strlen($search));
|
||||||
|
@ -6,116 +6,115 @@ namespace MyAAC;
|
|||||||
|
|
||||||
class RateLimit
|
class RateLimit
|
||||||
{
|
{
|
||||||
|
public string $key;
|
||||||
|
public int $max_attempts;
|
||||||
|
public int $ttl;
|
||||||
|
public $enabled = false;
|
||||||
|
protected array $data;
|
||||||
|
|
||||||
public string $key;
|
public function __construct(string $key, int $max_attempts, int $ttl)
|
||||||
public int $max_attempts;
|
{
|
||||||
public int $ttl;
|
$this->key = $key;
|
||||||
public $enabled = false;
|
$this->max_attempts = $max_attempts;
|
||||||
protected array $data;
|
$this->ttl = $ttl;
|
||||||
|
}
|
||||||
|
|
||||||
public function __construct(string $key, int $max_attempts, int $ttl)
|
public function attempts(string $ip): int
|
||||||
{
|
{
|
||||||
$this->key = $key;
|
if (!$this->enabled) {
|
||||||
$this->max_attempts = $max_attempts;
|
return 0;
|
||||||
$this->ttl = $ttl;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public function attempts(string $ip): int
|
if (isset($this->data[$ip]['attempts'])) {
|
||||||
{
|
return $this->data[$ip]['attempts'];
|
||||||
if (!$this->enabled) {
|
}
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isset($this->data[$ip]['attempts'])) {
|
return 0;
|
||||||
return $this->data[$ip]['attempts'];
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
public function exceeded(string $ip): bool {
|
||||||
}
|
if (!$this->enabled) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
public function exceeded(string $ip): bool {
|
return $this->attempts($ip) > $this->max_attempts;
|
||||||
if (!$this->enabled) {
|
}
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this->attempts($ip) > $this->max_attempts;
|
public function increment(string $ip): bool
|
||||||
}
|
{
|
||||||
|
global $cache;
|
||||||
|
if ($this->enabled && $cache->enabled()) {
|
||||||
|
if (isset($this->data[$ip]['attempts']) && isset($this->data[$ip]['last'])) {
|
||||||
|
$this->data[$ip]['attempts']++;
|
||||||
|
$this->data[$ip]['last'] = time();
|
||||||
|
} else {
|
||||||
|
$this->data[$ip] = [
|
||||||
|
'attempts' => 1,
|
||||||
|
'last' => time(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
public function increment(string $ip): bool
|
$this->save();
|
||||||
{
|
}
|
||||||
global $cache;
|
|
||||||
if ($this->enabled && $cache->enabled()) {
|
|
||||||
if (isset($this->data[$ip]['attempts']) && isset($this->data[$ip]['last'])) {
|
|
||||||
$this->data[$ip]['attempts']++;
|
|
||||||
$this->data[$ip]['last'] = time();
|
|
||||||
} else {
|
|
||||||
$this->data[$ip] = [
|
|
||||||
'attempts' => 1,
|
|
||||||
'last' => time(),
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->save();
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
public function reset(string $ip): void
|
||||||
}
|
{
|
||||||
|
if (!$this->enabled) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
public function reset(string $ip): void
|
if (isset($this->data[$ip])) {
|
||||||
{
|
unset($this->data[$ip]);
|
||||||
if (!$this->enabled) {
|
}
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isset($this->data[$ip])) {
|
$this->save();
|
||||||
unset($this->data[$ip]);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
$this->save();
|
public function save(): void
|
||||||
}
|
{
|
||||||
|
global $cache;
|
||||||
|
if (!$this->enabled) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
public function save(): void
|
$data = $this->data;
|
||||||
{
|
$cache->set($this->key, serialize($data), $this->ttl * 60);
|
||||||
global $cache;
|
}
|
||||||
if (!$this->enabled) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
$data = $this->data;
|
public function load(): void
|
||||||
$cache->set($this->key, serialize($data), $this->ttl * 60);
|
{
|
||||||
}
|
global $cache;
|
||||||
|
if (!$this->enabled) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
public function load(): void
|
$data = [];
|
||||||
{
|
if ($this->enabled && $cache->enabled()) {
|
||||||
global $cache;
|
$tmp = '';
|
||||||
if (!$this->enabled) {
|
if ($cache->fetch($this->key, $tmp)) {
|
||||||
return;
|
$data = unserialize($tmp);
|
||||||
}
|
$to_remove = [];
|
||||||
|
foreach ($data as $ip => $t) {
|
||||||
|
if (time() - $t['last'] >= ($this->ttl * 60)) {
|
||||||
|
$to_remove[] = $ip;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$data = [];
|
if (count($to_remove)) {
|
||||||
if ($this->enabled && $cache->enabled()) {
|
foreach ($to_remove as $ip) {
|
||||||
$tmp = '';
|
unset($data[$ip]);
|
||||||
if ($cache->fetch($this->key, $tmp)) {
|
}
|
||||||
$data = unserialize($tmp);
|
|
||||||
$to_remove = [];
|
|
||||||
foreach ($data as $ip => $t) {
|
|
||||||
if (time() - $t['last'] >= ($this->ttl * 60)) {
|
|
||||||
$to_remove[] = $ip;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (count($to_remove)) {
|
$this->save();
|
||||||
foreach ($to_remove as $ip) {
|
}
|
||||||
unset($data[$ip]);
|
} else {
|
||||||
}
|
$data = [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$this->save();
|
$this->data = $data;
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
$data = [];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->data = $data;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user