server('HTTP_X_FORWARDED_FOR') ?? request()->getClientIp(); $restrictedIps = Cache::get('ldcweb_ip_ranges'); if (\is_null($restrictedIps)) { $networks = ModelsLDCNetwork::all(); $networks = $networks->map(function (ModelsLDCNetwork $network) { $infos = collect(explode('.', strval($network->first_ip))); $min = intval($infos->get(3)); $max = collect(explode('.', strval($network->last_ip)))->get(3); for ($i = $min; $i <= $max; $i++) { $restrictedIps[] = $infos->get(0) . '.' . $infos->get(1) . '.' . $infos->get(2) . ".$i"; } return $restrictedIps; }); $restrictedIps = []; foreach ($networks->toArray() as $network) { $restrictedIps = array_merge($restrictedIps, $network); } Cache::put('ldcweb_ip_ranges', $restrictedIps, now()->addHours(4)); } if (!is_null($restrictedIps) && in_array($ip, $restrictedIps)) { return $next($request); } abort(403); } }