mirror of
https://gitee.com/koogua/course-tencent-cloud.git
synced 2025-06-24 20:06:09 +08:00
移除Throttle
This commit is contained in:
parent
b164a6c1b1
commit
69a384ee04
@ -30,8 +30,6 @@ class Controller extends \Phalcon\Mvc\Controller
|
||||
$this->setCors();
|
||||
}
|
||||
|
||||
$this->checkRateLimit();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -77,8 +77,6 @@ class Controller extends \Phalcon\Mvc\Controller
|
||||
$this->checkCsrfToken();
|
||||
}
|
||||
|
||||
$this->checkRateLimit();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -37,8 +37,6 @@ class LayerController extends \Phalcon\Mvc\Controller
|
||||
$this->checkCsrfToken();
|
||||
}
|
||||
|
||||
$this->checkRateLimit();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -121,7 +121,6 @@ class OrderConfirm extends LogicService
|
||||
'lesson_count' => $course->lesson_count,
|
||||
'study_expiry' => $course->study_expiry,
|
||||
'refund_expiry' => $course->refund_expiry,
|
||||
'origin_price' => $course->origin_price,
|
||||
'market_price' => $course->market_price,
|
||||
'vip_price' => $course->vip_price,
|
||||
];
|
||||
|
@ -1,65 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright Copyright (c) 2021 深圳市酷瓜软件有限公司
|
||||
* @license https://opensource.org/licenses/GPL-2.0
|
||||
* @link https://www.koogua.com
|
||||
*/
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
class Throttle extends Service
|
||||
{
|
||||
|
||||
public function checkRateLimit()
|
||||
{
|
||||
$config = $this->getConfig();
|
||||
|
||||
if (!$config->path('throttle.enabled')) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$cache = $this->getCache();
|
||||
|
||||
$sign = $this->getRequestSignature();
|
||||
|
||||
$cacheKey = $this->getCacheKey($sign);
|
||||
|
||||
if ($cache->ttl($cacheKey) < 1) {
|
||||
$cache->save($cacheKey, 0, $config->path('throttle.lifetime'));
|
||||
}
|
||||
|
||||
$rateLimit = $cache->get($cacheKey);
|
||||
|
||||
if ($rateLimit >= $config->path('throttle.rate_limit')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$cache->increment($cacheKey, 1);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
protected function getRequestSignature()
|
||||
{
|
||||
$authUser = $this->getAuthUser();
|
||||
|
||||
if (!empty($authUser['id'])) {
|
||||
return md5($authUser['id']);
|
||||
}
|
||||
|
||||
$httpHost = $this->request->getHttpHost();
|
||||
$clientAddress = $this->request->getClientAddress();
|
||||
|
||||
if ($httpHost && $clientAddress) {
|
||||
return md5($httpHost . '|' . $clientAddress);
|
||||
}
|
||||
|
||||
throw new \RuntimeException('Unable to generate request signature');
|
||||
}
|
||||
|
||||
protected function getCacheKey($sign)
|
||||
{
|
||||
return "throttle:{$sign}";
|
||||
}
|
||||
|
||||
}
|
@ -28,13 +28,6 @@ trait Security
|
||||
$validator->checkHttpReferer();
|
||||
}
|
||||
|
||||
public function checkRateLimit()
|
||||
{
|
||||
$validator = new SecurityValidator();
|
||||
|
||||
$validator->checkRateLimit();
|
||||
}
|
||||
|
||||
public function isNotSafeRequest()
|
||||
{
|
||||
/**
|
||||
|
@ -8,9 +8,7 @@
|
||||
namespace App\Validators;
|
||||
|
||||
use App\Exceptions\BadRequest as BadRequestException;
|
||||
use App\Exceptions\ServiceUnavailable as ServiceUnavailableException;
|
||||
use App\Library\CsrfToken as CsrfTokenService;
|
||||
use App\Services\Throttle as ThrottleService;
|
||||
|
||||
class Security extends Validator
|
||||
{
|
||||
@ -53,17 +51,6 @@ class Security extends Validator
|
||||
}
|
||||
}
|
||||
|
||||
public function checkRateLimit()
|
||||
{
|
||||
$service = new ThrottleService();
|
||||
|
||||
$result = $service->checkRateLimit();
|
||||
|
||||
if (!$result) {
|
||||
throw new ServiceUnavailableException('security.too_many_requests');
|
||||
}
|
||||
}
|
||||
|
||||
protected function getCsrfWhitelist()
|
||||
{
|
||||
return [];
|
||||
|
@ -147,21 +147,6 @@ $config['cors']['allow_headers'] = '*';
|
||||
*/
|
||||
$config['cors']['allow_methods'] = ['GET', 'POST', 'OPTIONS'];
|
||||
|
||||
/**
|
||||
* 限流开启
|
||||
*/
|
||||
$config['throttle']['enabled'] = true;
|
||||
|
||||
/**
|
||||
* 有效期(秒)
|
||||
*/
|
||||
$config['throttle']['lifetime'] = 60;
|
||||
|
||||
/**
|
||||
* 限流频率
|
||||
*/
|
||||
$config['throttle']['rate_limit'] = 60;
|
||||
|
||||
/**
|
||||
* 客户端ping服务端间隔(秒)
|
||||
*/
|
||||
|
@ -22,7 +22,6 @@ $error['sys.unknown_error'] = '未知错误';
|
||||
/**
|
||||
* 安全相关
|
||||
*/
|
||||
$error['security.too_many_requests'] = '请求过于频繁';
|
||||
$error['security.invalid_csrf_token'] = '无效的CSRF令牌';
|
||||
$error['security.invalid_http_referer'] = '无效请求来源';
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user