2017-11-19 20:05:13 +00:00
|
|
|
<?php
|
|
|
|
|
2018-02-25 21:30:56 +00:00
|
|
|
namespace Pterodactyl\Http\Middleware\Api;
|
2017-11-19 20:05:13 +00:00
|
|
|
|
|
|
|
use Closure;
|
|
|
|
use IPTools\IP;
|
|
|
|
use IPTools\Range;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
|
|
|
|
|
|
|
class AuthenticateIPAccess
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Determine if a request IP has permission to access the API.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
2019-09-06 04:32:57 +00:00
|
|
|
* @param \Closure $next
|
2017-11-19 20:05:13 +00:00
|
|
|
* @return mixed
|
|
|
|
*
|
|
|
|
* @throws \Exception
|
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
|
|
|
|
*/
|
|
|
|
public function handle(Request $request, Closure $next)
|
|
|
|
{
|
|
|
|
$model = $request->attributes->get('api_key');
|
|
|
|
|
|
|
|
if (is_null($model->allowed_ips) || empty($model->allowed_ips)) {
|
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
|
2017-11-26 19:26:38 +00:00
|
|
|
$find = new IP($request->ip());
|
2018-03-01 05:39:59 +00:00
|
|
|
foreach (json_decode($model->allowed_ips) as $ip) {
|
2017-11-26 19:26:38 +00:00
|
|
|
if (Range::parse($ip)->contains($find)) {
|
2017-11-19 20:05:13 +00:00
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-01 05:39:59 +00:00
|
|
|
throw new AccessDeniedHttpException('This IP address (' . $request->ip() . ') does not have permission to access the API using these credentials.');
|
2017-11-19 20:05:13 +00:00
|
|
|
}
|
|
|
|
}
|