forked from alexjustesen/speedtest-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrometheusAllowedIpMiddleware.php
More file actions
43 lines (36 loc) · 1.06 KB
/
PrometheusAllowedIpMiddleware.php
File metadata and controls
43 lines (36 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
namespace App\Http\Middleware;
use App\Helpers\Network;
use App\Settings\DataIntegrationSettings;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
class PrometheusAllowedIpMiddleware
{
public function __construct(
protected DataIntegrationSettings $settings
) {}
/**
* Handle an incoming request.
*
* @param Closure(Request):Response $next
*/
public function handle(Request $request, Closure $next): Response
{
if (blank($this->settings->prometheus_allowed_ips)) {
return $next($request);
}
$clientIp = $request->ip();
$allowedIps = $this->settings->prometheus_allowed_ips;
foreach ($allowedIps as $allowedIp) {
if (str_contains($allowedIp, '/')) {
if (Network::ipInRange($clientIp, $allowedIp)) {
return $next($request);
}
} elseif ($clientIp === $allowedIp) {
return $next($request);
}
}
abort(403);
}
}