Skip to content

Commit

Permalink
fix https detect
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaomlove committed Mar 16, 2024
1 parent 98088d8 commit eeb8e44
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions nexus/Nexus.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@ public function incrementLogSequence()
$this->logSequence++;
}

private function getFirst(string $result): string
{
if (str_contains($result, ",")) {
return strstr($result, ",", true);
}
return $result;
}

public function getRequestSchema()
{
$schema = $this->retrieveFromServer(['HTTP_X_FORWARDED_PROTO', 'REQUEST_SCHEME', 'HTTP_SCHEME']);
Expand All @@ -109,18 +117,19 @@ public function getRequestSchema()
$schema = 'https';
}
}
return $schema;
return $this->getFirst($schema);
}

public function getRequestHost(): string
{
$host = $this->retrieveFromServer(['HTTP_HOST', 'host', ], true);
return (string)$host;
$host = $this->retrieveFromServer(['HTTP_HOST', 'host', 'HTTP_X_FORWARDED_HOST'], true);
return $this->getFirst(strval($host));
}

public function getRequestIp()
public function getRequestIp(): string
{
return $this->retrieveFromServer(['HTTP_CF_CONNECTING_IP', 'HTTP_X_FORWARDED_FOR', 'x-forwarded-for', 'HTTP_REMOTE_ADDR', 'REMOTE_ADDR'], true);
$ip = $this->retrieveFromServer(['HTTP_CF_CONNECTING_IP', 'HTTP_X_FORWARDED_FOR', 'x-forwarded-for', 'HTTP_REMOTE_ADDR', 'REMOTE_ADDR'], true);
return $this->getFirst($ip);
}

private function retrieveFromServer(array $fields, bool $includeHeader = false)
Expand All @@ -134,12 +143,6 @@ private function retrieveFromServer(array $fields, bool $includeHeader = false)
}
foreach ($fields as $field) {
$result = $servers[$field] ?? null;
if ($result && in_array($field, ['HTTP_X_FORWARDED_FOR', 'x-forwarded-for'])) {
$result = preg_split('/[,\s]+/', $result);
}
if (is_array($result)) {
$result = Arr::first($result);
}
if ($result !== null && $result !== '') {
return $result;
}
Expand Down

0 comments on commit eeb8e44

Please sign in to comment.