Skip to content

Commit

Permalink
Merge pull request #6 from vc-bharat/main
Browse files Browse the repository at this point in the history
Code refactoring for removed the unnecessary if conditions
  • Loading branch information
ruchit288 authored Jun 19, 2023
2 parents e8db8b9 + f093e9f commit dd49252
Showing 1 changed file with 18 additions and 22 deletions.
40 changes: 18 additions & 22 deletions src/Middleware/IpGatewayMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,38 +27,34 @@ class IpGatewayMiddleware
*/
public function handle($request, Closure $next)
{
$prohibitRequest = false;
if (config('ip-gateway')
&& config('ip-gateway.enable_package') === true
) {
if (config('ip-gateway.enable_blacklist') === true) { // Its check blacklisted ip-addresses from ip-config file.
foreach ($request->getClientIps() as $ip) {
if ($this->grantIpAddress($ip)) {
try {
$prohibitRequest = false;
$getClientIps = $request->getClientIps();
$enableBlacklist = config('ip-gateway.enable_blacklist') === true;
$redirectRoute = config('ip-gateway.redirect_route_to');

if (config('ip-gateway') && config('ip-gateway.enable_package') === true) {
foreach ($getClientIps as $ip) {
if ($enableBlacklist && $this->grantIpAddress($ip)) { // Its check blacklisted ip-addresses from ip-config file.
$prohibitRequest = true;
Log::warning($ip . ' IP address has tried to access.');
}
}
}

if (config('ip-gateway.enable_blacklist') === false) { // Its check whitelisted ip-addresses from ip-config file.
foreach ($request->getClientIps() as $ip) {
if (!$this->grantIpAddress($ip)) {
} elseif (!$enableBlacklist && !$this->grantIpAddress($ip)) { // Its check whitelisted ip-addresses from ip-config file.
$prohibitRequest = true;
Log::warning($ip . ' IP address has tried to access.');
}
}
}
}

if ($prohibitRequest === false) {
return $next($request);
} else {
if (config('ip-gateway.redirect_route_to') != '') {
return redirect(config('ip-gateway.redirect_route_to'));
} else {
return redirect('/404');
if ($prohibitRequest) {
return redirect($redirectRoute != '' ? $redirectRoute : '/404');
}

return $next($request);

} catch (\Exception $ex) {
Log::error('Problem occurred while handle an incoming request '.$ex->getMessage());
}

}

/**
Expand Down

0 comments on commit dd49252

Please sign in to comment.