53 lines
1.7 KiB
PHP
53 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Middleware;
|
|
|
|
use App\Models\LDCNetwork as ModelsLDCNetwork;
|
|
use Closure;
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
class LDCNetwork
|
|
{
|
|
/**
|
|
* Handle an incoming request.
|
|
*
|
|
* @param mixed $request
|
|
* @param \Closure $next
|
|
* @return mixed
|
|
*/
|
|
public function handle($request, Closure $next)
|
|
{
|
|
if (\config('app.env') === 'local' || (\config('app.env') === 'production' && \config('app.debug') === true)) {
|
|
return $next($request);
|
|
}
|
|
|
|
/** HA PROXY HACK */
|
|
$ip = request()->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);
|
|
}
|
|
}
|