forked from laysense/laysite
30 lines
763 B
PHP
30 lines
763 B
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http;
|
|
|
|
final class Request extends \support\Request
|
|
{
|
|
private const LOOPBACK = ['127.0.0.1', '::1'];
|
|
|
|
public function clientAddress(): string
|
|
{
|
|
$peer = $this->getRemoteIp();
|
|
$forwarded = $this->header('x-real-ip', '');
|
|
if (!$this->fromTrustedProxy() || !filter_var($forwarded, FILTER_VALIDATE_IP)) {
|
|
return $peer;
|
|
}
|
|
|
|
return inet_ntop(inet_pton($forwarded));
|
|
}
|
|
|
|
private function fromTrustedProxy(): bool
|
|
{
|
|
$configured = getenv('LAYSITE_TRUSTED_PROXIES');
|
|
$trusted = $configured === false ? self::LOOPBACK : array_map('trim', explode(',', $configured));
|
|
|
|
return in_array($this->getRemoteIp(), $trusted, true);
|
|
}
|
|
}
|