Skip to content
This repository has been archived by the owner on Nov 2, 2020. It is now read-only.

Commit

Permalink
feat(ban_ips): Store banned ip in components/Site
Browse files Browse the repository at this point in the history
  • Loading branch information
Rhilip committed Aug 10, 2019
1 parent 720f37e commit 01084c9
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<a name="unreleased"></a>
## [Unreleased]

### Docs
- **template:** Add git commit hash in `CHANGELOG.md` (76bc527)


<a name="v0.1.5-alpha"></a>
## [v0.1.5-alpha] - 2019-08-09
Expand Down
9 changes: 8 additions & 1 deletion apps/components/Site.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ protected static function getStaticCacheNameSpace(): string
return 'Cache:site';
}

public function getBanIpsList(): array
{
return $this->getCacheValue('ip_ban_list', function () {
return app()->pdo->createCommand('SELECT `ip` FROM `ban_ips`')->queryColumn();
});
}

public function getTorrent($tid)
{
if (array_key_exists($tid, $this->torrents)) {
Expand Down Expand Up @@ -250,7 +257,7 @@ public static function ruleTeam(): array
public static function ruleCanUsedTeam(): array
{
return array_filter(static::ruleTeam(), function ($team) {
return app()->site->getCurUser()->getClass(true) >= $team['class_require'];
return app()->site->getCurUser()->getClass() >= $team['class_require'];
});
}

Expand Down
4 changes: 2 additions & 2 deletions apps/config/http_base.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@
],

'site' => [
'class' => \apps\components\Site::class
'class' => apps\components\Site::class
],
],

Expand All @@ -198,7 +198,7 @@
// 类库配置
'libraries' => [
'mailer' => [
'class' => \apps\libraries\Mailer::class,
'class' => apps\libraries\Mailer::class,
'debug' => env('MAILER_DEBUG'),
'host' => env('MAILER_HOST'),
'port' => env('MAILER_PORT'),
Expand Down
8 changes: 1 addition & 7 deletions apps/middleware/IpBanMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,14 @@

namespace apps\middleware;


use Rid\Utils\IpUtils;

class IpBanMiddleware
{
public function handle($callable, \Closure $next)
{
$ip = app()->request->getClientIp();

$ip_ban_list = app()->redis->get("SILE:ip_ban_list");
if ($ip_ban_list === false) {
$ip_ban_list = app()->pdo->createCommand("SELECT `ip` FROM `ban_ips`")->queryColumn();
app()->redis->setex("SILE:ip_ban_list", 86400, $ip_ban_list);
}
$ip_ban_list = app()->site->getBanIpsList();

if (count($ip_ban_list) > 0 && IpUtils::checkIp($ip, $ip_ban_list)) {
return app()->response->setStatusCode(403);
Expand Down

0 comments on commit 01084c9

Please sign in to comment.