-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
269 additions
and
99 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace Mchev\Banhammer\Exceptions; | ||
|
||
use Exception; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Http\Response; | ||
use Illuminate\Support\Facades\Log; | ||
use Symfony\Component\HttpKernel\Exception\HttpException; | ||
|
||
class BanhammerException extends HttpException | ||
{ | ||
public function __construct($message = null, Exception $previous = null, $code = 0) | ||
{ | ||
parent::__construct(403, $message, $previous, [], $code); | ||
} | ||
|
||
/** | ||
* Report or log an exception. | ||
*/ | ||
public function report(): void | ||
{ | ||
Log::error("Banhammer Exception: {$this->getMessage()}"); | ||
} | ||
|
||
/** | ||
* Render the exception into an HTTP response. | ||
*/ | ||
public function render(Request $request): Response | ||
{ | ||
return (config('ban.fallback_url')) | ||
? redirect(config('ban.fallback_url')) | ||
: abort(403, $this->getMessage()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
namespace Mchev\Banhammer\Middleware; | ||
|
||
use Closure; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Facades\Log; | ||
use Mchev\Banhammer\Exceptions\BanhammerException; | ||
use Mchev\Banhammer\Services\IpApiService; | ||
|
||
class BlockByCountry | ||
{ | ||
protected IpApiService $ipApiService; | ||
|
||
public function __construct(IpApiService $ipApiService) | ||
{ | ||
$this->ipApiService = $ipApiService; | ||
} | ||
|
||
public function handle(Request $request, Closure $next) | ||
{ | ||
$blockedCountries = config('ban.blocked_countries'); | ||
|
||
if ($blockedCountries && ! empty($blockedCountries)) { | ||
$ip = $request->ip(); | ||
|
||
if (! is_null($ip)) { | ||
try { | ||
// Get geolocation data using the IpApiService | ||
$geolocationData = $this->ipApiService->getGeolocationData($ip); | ||
|
||
if ($geolocationData['status'] === 'fail') { | ||
Log::notice('Banhammer country check failure: '.$message, [ | ||
'ip' => $ip, | ||
]); | ||
} | ||
|
||
if ($this->isCountryBlocked($geolocationData, $blockedCountries)) { | ||
throw new BanhammerException(config('ban.messages.country')); | ||
} | ||
} catch (\Exception $e) { | ||
Log::debug('Banhammer Exception: '.$e->getMessage(), [ | ||
'ip' => $ip, | ||
'country' => $geolocationData['countryCode'] ?? null, | ||
]); | ||
|
||
// Rethrow the exception to ensure the ban is enforced | ||
throw new BanhammerException(config('ban.messages.country')); | ||
} | ||
} | ||
} | ||
|
||
return $next($request); | ||
} | ||
|
||
protected function isCountryBlocked(array $geolocationData, array $blockedCountries): bool | ||
{ | ||
return isset($geolocationData['countryCode']) && | ||
$this->ipApiService->isCountryBlocked($geolocationData['countryCode'], $blockedCountries); | ||
} | ||
} |
Oops, something went wrong.