fix: 优化代理域名及端口读取

This commit is contained in:
邹景立 2024-08-14 09:35:02 +08:00
parent 21aad56260
commit d7a50742f2

View File

@ -65,10 +65,16 @@ class ThinkRequest extends Request
// 请求真实IP // 请求真实IP
$this->realIP = $this->header['x-real-ip'] ?? ($this->header['x-forwarded-for'] ?? $connection->getRemoteIp()); $this->realIP = $this->header['x-real-ip'] ?? ($this->header['x-forwarded-for'] ?? $connection->getRemoteIp());
// 请求地址域名及端口处理 // 请求服务器的域名处理
$this->host = $this->header['x-host'] ?? ($this->header['x-requested-host'] ?? ($this->header['remote-host'] ?? $request->host())); $this->host = $this->header['x-host'] ?? ($this->header['x-requested-host'] ?? ($this->header['host'] ?? $request->host()));
$port = intval($this->header['x-requested-port'] ?? (strpos($this->host, ':') !== false ? explode(':', $this->host)[1] : $connection->getRemoteIp())); // 请求服务器的端口处理
if (strpos($this->host, ':') !== false && in_array($port, [80, 443])) $this->host = strstr($this->host, ':', true); if (!is_numeric($port = $this->header['x-port'] ?? ($this->header['x-requested-port'] ?? ($this->header['port'] ?? null)))) {
$port = strpos($this->host, ':') !== false ? explode(':', $this->host)[1] : $connection->getRemotePort();
}
// 如果是正常端口,不需要在 host 表示出来
if (strpos($this->host, ':') !== false && in_array($port, [80, 443])) {
$this->host = strstr($this->host, ':', true);
}
// 替换全局变量 // 替换全局变量
$_GET = $request->get(); $_GET = $request->get();