From b3b659a5c3e14c5275756099df4cb1f22be09f46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B8nstantine=20Kovalensky?= <45331093+kovalensky@users.noreply.github.com> Date: Wed, 15 Nov 2023 14:16:03 +0400 Subject: [PATCH] Record changed port while re-announcing (#1102) --- bt/announce.php | 19 ++++++++----------- bt/scrape.php | 5 ++++- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/bt/announce.php b/bt/announce.php index 32510414f..0e76743c7 100644 --- a/bt/announce.php +++ b/bt/announce.php @@ -72,12 +72,8 @@ $info_hash_hex = mb_check_encoding($info_hash, 'UTF8') ? $info_hash : bin2hex($i // Store peer id $peer_id_sql = rtrim(DB()->escape(htmlCHR($peer_id)), ' '); -// Check info_hash version -if (strlen($info_hash) === 32) { - $is_bt_v2 = true; -} elseif (strlen($info_hash) === 20) { - $is_bt_v2 = false; -} else { +// Check info_hash length +if (strlen($info_hash) !== 20) { msg_die('Invalid info_hash: ' . $info_hash_hex); } @@ -132,7 +128,7 @@ $peer_hash = hash('xxh128', $passkey . $info_hash_hex . $port); // Events $stopped = ($event === 'stopped'); -// Get the real port to help some NAT users +// Get the real port to help port-restricted NAT users $port = $_SERVER['REMOTE_PORT']; // Set seeder & complete @@ -167,12 +163,12 @@ if ($lp_info) { $releaser = $lp_info['releaser']; $tor_type = $lp_info['tor_type']; } else { + $info_hash_sql = rtrim(DB()->escape($info_hash), ' '); /** * Currently torrent clients send truncated v2 hashes (the design raises questions). * https://github.com/bittorrent/bittorrent.org/issues/145#issuecomment-1720040343 */ - $info_hash_sql = rtrim(DB()->escape($info_hash), ' '); - $info_hash_where = $is_bt_v2 ? "WHERE tor.info_hash_v2 = '$info_hash_sql'" : "WHERE tor.info_hash = '$info_hash_sql' OR SUBSTRING(tor.info_hash_v2, 1, 20) = '$info_hash_sql'"; + $info_hash_where = "WHERE tor.info_hash = '$info_hash_sql' OR SUBSTRING(tor.info_hash_v2, 1, 20) = '$info_hash_sql'"; $passkey_sql = DB()->escape($passkey); $sql = " @@ -201,7 +197,7 @@ if ($lp_info) { // Check hybrid status if (!empty($row['info_hash']) && !empty($row['info_hash_v2'])) { $is_hybrid = true; - if ($info_hash === $row['info_hash']) { // Change this to substr($row['info_hash_v2'], 0, 20) in the future to update statistics, when v2 torrents will be default. + if ($info_hash === $row['info_hash']) { // Change this to substr($row['info_hash_v2'], 0, 20) in the future for updating statistics, in case of v2 torrents being prioritized. $update_hybrid = true; } } @@ -303,11 +299,12 @@ if ($bb_cfg['tracker']['freeleech'] && $down_add) { // Insert / update peer info $peer_info_updated = false; $update_time = ($stopped) ? 0 : TIMENOW; -if (!isset($is_hybrid) || isset($update_hybrid)) { // Update statistics only for one topic +if (!isset($is_hybrid) || isset($update_hybrid)) { // Record statistics only for one topic if ($lp_info) { $sql = "UPDATE " . BB_BT_TRACKER . " SET update_time = $update_time"; $sql .= ", $ip_version = '$ip_sql'"; + $sql .= ", port = '$port'"; $sql .= ", seeder = $seeder"; $sql .= ($releaser != $lp_info['releaser']) ? ", releaser = $releaser" : ''; diff --git a/bt/scrape.php b/bt/scrape.php index 30511cb88..095b63a19 100644 --- a/bt/scrape.php +++ b/bt/scrape.php @@ -42,6 +42,9 @@ foreach ($info_hash_array[1] as $hash) { $decoded_hash = urldecode($hash); + if (strlen($decoded_hash) !== 20) { + continue; + } if ($scrape_cache = CACHE('tr_cache')->get(SCRAPE_LIST_PREFIX . bin2hex($decoded_hash))) { $torrents['files'][$info_key = array_key_first($scrape_cache)] = $scrape_cache[$info_key]; } else { @@ -79,7 +82,7 @@ if (!empty($info_hash_count)) { foreach ($scrapes as $scrape) { $hash_v1 = !empty($scrape['info_hash']) ? $scrape['info_hash'] : ''; $hash_v2 = !empty($scrape['info_hash_v2']) ? substr($scrape['info_hash_v2'], 0, 20) : ''; - $info_hash_scrape = (in_array(urlencode($hash_v2), $info_hash_array[1])) ? $hash_v2 : $hash_v1; + $info_hash_scrape = (in_array(urlencode($hash_v1), $info_hash_array[1])) ? $hash_v1 : $hash_v2; // Replace logic to prioritize $hash_v2, in case of future prioritization of v2 $torrents['files'][$info_hash_scrape] = [ 'complete' => (int)$scrape['seeders'],