Path: blob/1.0-develop/app/Http/Middleware/Api/AuthenticateIPAccess.php
10277 views
<?php12namespace Pterodactyl\Http\Middleware\Api;34use IPTools\IP;5use IPTools\Range;6use Illuminate\Http\Request;7use Pterodactyl\Facades\Activity;8use Laravel\Sanctum\TransientToken;9use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;1011class AuthenticateIPAccess12{13/**14* Determine if a request IP has permission to access the API.15*16* @throws \Exception17* @throws AccessDeniedHttpException18*/19public function handle(Request $request, \Closure $next): mixed20{21/** @var TransientToken|\Pterodactyl\Models\ApiKey $token */22$token = $request->user()->currentAccessToken();2324// If this is a stateful request just push the request through to the next25// middleware in the stack, there is nothing we need to explicitly check. If26// this is a valid API Key, but there is no allowed IP restriction, also pass27// the request through.28if ($token instanceof TransientToken || empty($token->allowed_ips)) {29return $next($request);30}3132$find = new IP($request->ip());33foreach ($token->allowed_ips as $ip) {34if (Range::parse($ip)->contains($find)) {35return $next($request);36}37}3839Activity::event('auth:ip-blocked')40->actor($request->user())41->subject($request->user(), $token)42->property('identifier', $token->identifier)43->log();4445throw new AccessDeniedHttpException('This IP address (' . $request->ip() . ') does not have permission to access the API using these credentials.');46}47}484950