Skip to content

Commit

Permalink
Move the constants from Kernel class to a dependent enum class.
Browse files Browse the repository at this point in the history
  • Loading branch information
terrylinooo committed Jun 9, 2023
1 parent fd9f55a commit b00f55f
Show file tree
Hide file tree
Showing 24 changed files with 472 additions and 521 deletions.
14 changes: 7 additions & 7 deletions src/Firewall/Component/Ip.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ class Ip extends ComponentProvider
*/
const STATUS_CODE = 81;

const REASON_INVALID_IP = 40;
const REASON_DENY_IP = 41;
const REASON_ALLOW_IP = 42;
const REASON_INVALID_IP_DENIED = 40;
const REASON_DENY_IP_DENIED = 41;
const REASON_ALLOW_IP_DENIED = 42;

/**
* Only allow IPs in allowedList, then deny all.
Expand All @@ -123,31 +123,31 @@ public function check(string $ip): array
if (!filter_var($this->ip, FILTER_VALIDATE_IP)) {
return [
'status' => 'deny',
'code' => self::REASON_INVALID_IP,
'code' => self::REASON_INVALID_IP_DENIED,
'comment' => 'Invalid IP.',
];
}

if ($this->isAllowed()) {
return [
'status' => 'allow',
'code' => self::REASON_ALLOW_IP,
'code' => self::REASON_ALLOW_IP_DENIED,
'comment' => 'IP is in allowed list.',
];
}

if ($this->isDenied()) {
return [
'status' => 'deny',
'code' => self::REASON_DENY_IP,
'code' => self::REASON_DENY_IP_DENIED,
'comment' => 'IP is in denied list.',
];
}

if ($this->isDenyAll) {
return [
'status' => 'deny',
'code' => self::REASON_DENY_IP,
'code' => self::REASON_DENY_IP_DENIED,
'comment' => 'Deny all in strict mode.',
];
}
Expand Down
8 changes: 4 additions & 4 deletions src/Firewall/Firewall.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Server\MiddlewareInterface;
use Shieldon\Firewall\Kernel;
use Shieldon\Firewall\Kernel\Enum;
use Shieldon\Firewall\HttpFactory;
use Shieldon\Firewall\Container;
use Shieldon\Firewall\FirewallTrait;
Expand Down Expand Up @@ -237,18 +237,18 @@ public function run(): ResponseInterface
$response = $requestHandler->handle($response);

// Something is detected by Middlewares, return.
if ($response->getStatusCode() !== $this->kernel::HTTP_STATUS_OK) {
if ($response->getStatusCode() !== Enum::HTTP_STATUS_OK) {
return $response;
}

$result = $this->kernel->run();

if ($result !== $this->kernel::RESPONSE_ALLOW) {
if ($result !== Enum::RESPONSE_ALLOW) {
if ($this->kernel->captchaResponse()) {
$this->kernel->unban();

$response = $response->withHeader('Location', $this->kernel->getCurrentUrl());
$response = $response->withStatus($this->kernel::HTTP_STATUS_SEE_OTHER);
$response = $response->withStatus(Enum::HTTP_STATUS_SEE_OTHER);

return $response;
}
Expand Down
96 changes: 0 additions & 96 deletions src/Firewall/KernalEnum.php

This file was deleted.

100 changes: 17 additions & 83 deletions src/Firewall/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
use Shieldon\Firewall\Kernel\RuleTrait;
use Shieldon\Firewall\Kernel\SessionTrait;
use Shieldon\Firewall\Kernel\TemplateTrait;
use Shieldon\Firewall\Kernel\Enum;
use Shieldon\Firewall\Log\ActionLogger;
use Shieldon\Firewall\Container;
use Shieldon\Event\Event;
Expand Down Expand Up @@ -162,73 +163,6 @@ class Kernel
*/
use TemplateTrait;

/**
* HTTP Status Codes
*/
const HTTP_STATUS_OK = 200;
const HTTP_STATUS_SEE_OTHER = 303;
const HTTP_STATUS_BAD_REQUEST = 400;
const HTTP_STATUS_FORBIDDEN = 403;
const HTTP_STATUS_TOO_MANY_REQUESTS = 429;

/**
* Reason Codes (ALLOW)
*/
const REASON_IS_SEARCH_ENGINE = 100;
const REASON_IS_GOOGLE = 101;
const REASON_IS_BING = 102;
const REASON_IS_YAHOO = 103;
const REASON_IS_SOCIAL_NETWORK = 110;
const REASON_IS_FACEBOOK = 111;
const REASON_IS_TWITTER = 112;

/**
* Reason Codes (DENY)
*/
const REASON_TOO_MANY_SESSIONS = 1;
const REASON_TOO_MANY_ACCESSES = 2; // (not used)
const REASON_EMPTY_JS_COOKIE = 3;
const REASON_EMPTY_REFERER = 4;
const REASON_REACHED_LIMIT_DAY = 11;
const REASON_REACHED_LIMIT_HOUR = 12;
const REASON_REACHED_LIMIT_MINUTE = 13;
const REASON_REACHED_LIMIT_SECOND = 14;
const REASON_INVALID_IP = 40;
const REASON_DENY_IP = 41;
const REASON_ALLOW_IP = 42;
const REASON_COMPONENT_IP = 81;
const REASON_COMPONENT_RDNS = 82;
const REASON_COMPONENT_HEADER = 83;
const REASON_COMPONENT_USERAGENT = 84;
const REASON_COMPONENT_TRUSTED_ROBOT = 85;
const REASON_MANUAL_BAN = 99;

/**
* Action Codes
*/
const ACTION_DENY = 0;
const ACTION_ALLOW = 1;
const ACTION_TEMPORARILY_DENY = 2;
const ACTION_UNBAN = 9;

/**
* Result Codes
*/
const RESPONSE_DENY = 0;
const RESPONSE_ALLOW = 1;
const RESPONSE_TEMPORARILY_DENY = 2;
const RESPONSE_LIMIT_SESSION = 3;

/**
* Logger Codes
*/
const LOG_LIMIT = 3;
const LOG_PAGEVIEW = 11;
const LOG_BLACKLIST = 98;
const LOG_CAPTCHA = 99;

const KERNEL_DIR = __DIR__;

/**
* The result passed from filters, compoents, etc.
*
Expand Down Expand Up @@ -368,7 +302,7 @@ public function run(): int
// Ignore the excluded urls.
foreach ($this->excludedUrls as $url) {
if (strpos($this->getCurrentUrl(), $url) === 0) {
return $this->result = self::RESPONSE_ALLOW;
return $this->result = Enum::RESPONSE_ALLOW;
}
}

Expand All @@ -379,25 +313,25 @@ public function run(): int

$result = $this->process();

if ($result !== self::RESPONSE_ALLOW) {
if ($result !== Enum::RESPONSE_ALLOW) {
// Current session did not pass the CAPTCHA, it is still stuck in
// CAPTCHA page.
$actionCode = self::LOG_CAPTCHA;
$actionCode = Enum::LOG_CAPTCHA;

// If current session's respone code is RESPONSE_DENY, record it as
// `blacklist_count` in our logs.
// It is stuck in warning page, not CAPTCHA.
if ($result === self::RESPONSE_DENY) {
$actionCode = self::LOG_BLACKLIST;
if ($result === Enum::RESPONSE_DENY) {
$actionCode = Enum::LOG_BLACKLIST;
}

if ($result === self::RESPONSE_LIMIT_SESSION) {
$actionCode = self::LOG_LIMIT;
if ($result === Enum::RESPONSE_LIMIT_SESSION) {
$actionCode = Enum::LOG_LIMIT;
}

$this->log($actionCode);
} else {
$this->log(self::LOG_PAGEVIEW);
$this->log(Enum::LOG_PAGEVIEW);
}

// @ MessengerTrait
Expand Down Expand Up @@ -425,8 +359,8 @@ public function ban(string $ip = ''): void
}

$this->action(
self::ACTION_DENY,
self::REASON_MANUAL_BAN,
Enum::ACTION_DENY,
Enum::REASON_MANUAL_BAN_DENIED,
$ip
);
}
Expand All @@ -445,13 +379,13 @@ public function unban(string $ip = ''): void
}

$this->action(
self::ACTION_UNBAN,
self::REASON_MANUAL_BAN,
Enum::ACTION_UNBAN,
Enum::REASON_MANUAL_BAN_DENIED,
$ip
);
$this->log(self::ACTION_UNBAN);
$this->log(Enum::ACTION_UNBAN);

$this->result = self::RESPONSE_ALLOW;
$this->result = Enum::RESPONSE_ALLOW;
}

/**
Expand Down Expand Up @@ -613,7 +547,7 @@ protected function process(): int
}

// Stage 7 - Go into session limit check.
return $this->result = $this->sessionHandler(self::RESPONSE_ALLOW);
return $this->result = $this->sessionHandler(Enum::RESPONSE_ALLOW);
}

/**
Expand All @@ -637,7 +571,7 @@ protected function action(int $actionCode, int $reasonCode, string $assignIp = '
$rdns = gethostbyaddr($ip);
}

if ($actionCode === self::ACTION_UNBAN) {
if ($actionCode === Enum::ACTION_UNBAN) {
$this->driver->delete($ip, 'rule');
} else {
$logData['log_ip'] = $ip;
Expand Down
7 changes: 3 additions & 4 deletions src/Firewall/Kernel/CaptchaTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

namespace Shieldon\Firewall\Kernel;

use Shieldon\Firewall\Kernel;
use Shieldon\Firewall\Kernel\Enum;
use Shieldon\Firewall\Captcha\CaptchaInterface;

/*
Expand Down Expand Up @@ -51,7 +51,7 @@ trait CaptchaTrait
* Get a class name without namespace string.
*
* @param object $instance Class
*
*
* @return string
*/
abstract protected function getClassName($instance): string;
Expand Down Expand Up @@ -96,7 +96,6 @@ public function setCaptcha(CaptchaInterface $instance): void
public function captchaResponse(): bool
{
foreach ($this->captcha as $captcha) {

if (!$captcha->response()) {
return false;
}
Expand All @@ -108,7 +107,7 @@ public function captchaResponse(): bool
*/
if (!empty($this->sessionLimit['count'])) {
return (bool) $this->setResultCode(
$this->sessionHandler(Kernel::RESPONSE_ALLOW)
$this->sessionHandler(Enum::RESPONSE_ALLOW)
);
}

Expand Down
Loading

0 comments on commit b00f55f

Please sign in to comment.