From e12c94e651efc51e9efde6e343a08d7e4c236b30 Mon Sep 17 00:00:00 2001 From: xiaomlove Date: Thu, 7 Apr 2022 19:08:02 +0800 Subject: [PATCH] fix get host from octane --- app/Repositories/TrackerRepository.php | 2 +- include/globalfunctions.php | 18 ++++++++---------- nexus/Nexus.php | 8 +++++++- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/app/Repositories/TrackerRepository.php b/app/Repositories/TrackerRepository.php index 93b8ab5f..25ad9873 100644 --- a/app/Repositories/TrackerRepository.php +++ b/app/Repositories/TrackerRepository.php @@ -722,7 +722,7 @@ class TrackerRepository extends BaseRepository $peer->prev_action = $peer->last_action; } - if ($queries['event'] == 'started') { + if ($queries['event'] == 'started' || !$peer->exists) { $peer->started = $nowStr; $peer->uploadoffset = $queries['uploaded']; $peer->downloadoffset = $queries['downloaded']; diff --git a/include/globalfunctions.php b/include/globalfunctions.php index c03704cb..e85b58a7 100644 --- a/include/globalfunctions.php +++ b/include/globalfunctions.php @@ -458,14 +458,8 @@ function getSchemeAndHttpHost() } $isHttps = isHttps(); $protocol = $isHttps ? 'https' : 'http'; - if (RUNNING_IN_OCTANE) { - $host = request()->server('HTTP_HOST', ''); - } else { - $host = $_SERVER['HTTP_HOST'] ?? ''; - } - $result = "$protocol://" . $host; - return $result; - + $host = nexus()->getRequestHost(); + return "$protocol://" . $host; } function getBaseUrl() @@ -634,8 +628,12 @@ function get_tracker_schema_and_host($combine = false): array|string global $https_announce_urls, $announce_urls; $httpsAnnounceUrls = array_filter($https_announce_urls); $log = "cookie: " . json_encode($_COOKIE) . ", https_announce_urls: " . json_encode($httpsAnnounceUrls); - if ((isset($_COOKIE["c_secure_tracker_ssl"]) && $_COOKIE["c_secure_tracker_ssl"] == base64("yeah")) || !empty($httpsAnnounceUrls)) { - $log .= ", c_secure_tracker_ssl = base64('yeah'): " . base64("yeah") . ", or not empty https_announce_urls"; + if ( + (isset($_COOKIE["c_secure_tracker_ssl"]) && $_COOKIE["c_secure_tracker_ssl"] == base64("yeah")) + || !empty($httpsAnnounceUrls) + || isHttps() + ) { + $log .= ", c_secure_tracker_ssl = base64('yeah'): " . base64("yeah") . ", or not empty https_announce_urls, or isHttps()"; $tracker_ssl = true; } else { $tracker_ssl = false; diff --git a/nexus/Nexus.php b/nexus/Nexus.php index a20474ac..73e02118 100644 --- a/nexus/Nexus.php +++ b/nexus/Nexus.php @@ -107,9 +107,15 @@ final class Nexus return $schema; } + public function getRequestHost(): string + { + $host = $this->retrieveFromServer(['HTTP_HOST', 'host', ], true); + return (string)$host; + } + public function getRequestIp() { - return $this->retrieveFromServer(['HTTP_CF_CONNECTING_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_REMOTE_ADDR', 'REMOTE_ADDR']); + return $this->retrieveFromServer(['HTTP_CF_CONNECTING_IP', 'HTTP_X_FORWARDED_FOR', 'x-forwarded-for', 'HTTP_REMOTE_ADDR', 'REMOTE_ADDR'], true); } private function retrieveFromServer(array $fields, bool $includeHeader = false)