Skip to content

Commit

Permalink
Support for exit rather than using response codes
Browse files Browse the repository at this point in the history
  • Loading branch information
dsone committed Mar 20, 2021
1 parent 24b3fa7 commit f8e90a7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
10 changes: 7 additions & 3 deletions src/Classes/StrainexDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ private function filterEvent(Throwable $exception) {
$referer = str_replace(['https://', 'http://'], '', $referer);
$filterReferer = config('strainex.filters.referer', []);
$mapReferer = [];
if (isset($mapReferer[0])) { // sequential array
if (isset($filterReferer[0])) { // sequential array
foreach ($filterReferer as $ref) {
$mapReferer[$ref] = 1;
}
Expand All @@ -98,8 +98,8 @@ private function filterEvent(Throwable $exception) {
}

$requestUrl = Request::url();
$preg = '/' . implode('|', config('strainex.filters.url', [ '.*' ])) . '/ims';
$urlMatched = preg_match($preg, $requestUrl, $match, PREG_UNMATCHED_AS_NULL);
$preg = '/' . implode('|', config('strainex.filters.url', [ '_____' ])) . '/ims';
$urlMatched = preg_match($preg, $requestUrl, $match, PREG_UNMATCHED_AS_NULL);

if ($urlMatched || $referer) {
// Block IP
Expand Down Expand Up @@ -129,6 +129,8 @@ private function filterEvent(Throwable $exception) {
$callback($exception, $data, !$referer ? 1 : 2);
}

// Exit
if (config('strainex.always_exit', false)) { exit(0); }
// Trigger new error, going back to $this->report, returning early because !!$strainex_abort
abort(config('strainex.blocked_status'));
}
Expand All @@ -140,6 +142,8 @@ private function filterEvent(Throwable $exception) {
$callback($exception, !$referer ? 1 : 2);
}

// Exit
if (config('strainex.always_exit', false)) { exit(0); }
// Trigger new error, going back to $this->report, returning early because !!$strainex_abort
abort(config('strainex.filtered_status'));
}
Expand Down
3 changes: 2 additions & 1 deletion src/Providers/StrainexServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,9 @@ private function abortIfBlocked() {
if ($ip && Redis::get(config('strainex.redis_string', 'strainex:ip:ban:') . $ip)) {
// When in production mode -> block request entirely
if (config('app.env') !== 'local') {
StrainexDecorator::$strainex_abort = true;
if (config('strainex.always_exit', false)) { exit(0); }

StrainexDecorator::$strainex_abort = true;
abort(config('strainex.blocked_status'));
}

Expand Down
11 changes: 10 additions & 1 deletion src/config/strainex.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,20 @@
* Make sure to use an HTML status code that is supported by Laravel.
* Not supported status codes throw a "message not found" Exception, ie status code 418 is not supported.
*
* Default: 503 - Maintenance
* Default: 503 - Maintenance for SEO spam filtered
* Default: 500 - Maintenance for blocked (subsequent) requests
*/
'filtered_status' => env('STRAINEX_FILTERED_STATUS', 503),
'blocked_status' => env('STRAINEX_BLOCKED_STATUS', 500),

/**
* If you prefer to not return any error codes,
* you can also simply `exit`.
*
* Above defined callbacks are not affected by this setting.
*/
'always_exit' => env('STRAINEX_ALWAYS_EXIT', false),

/**
* Should requests be blocked.
* To enable this you need to have Redis available.
Expand Down

0 comments on commit f8e90a7

Please sign in to comment.