environment('production')) { return $next($request); } if (!$allowed) { abort(403, 'admin ip not allowed'); } $ip = $request->ip(); foreach ($allowed as $rule) { if ($this->matchIp($ip, $rule)) { return $next($request); } } abort(403, 'admin ip not allowed'); } private function matchIp(string $ip, string $rule): bool { if (strpos($rule, '/') === false) { return $ip === $rule; } [$subnet, $mask] = explode('/', $rule, 2); $mask = (int) $mask; $ipLong = ip2long($ip); $subLong = ip2long($subnet); if ($ipLong === false || $subLong === false || $mask < 0 || $mask > 32) return false; $maskLong = -1 << (32 - $mask); return (($ipLong & $maskLong) === ($subLong & $maskLong)); } }