Record changed port while re-announcing (#1102)

This commit is contained in:
Cønstantine Kovalensky 2023-11-15 14:16:03 +04:00 committed by GitHub
commit b3b659a5c3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 12 deletions

View file

@ -72,12 +72,8 @@ $info_hash_hex = mb_check_encoding($info_hash, 'UTF8') ? $info_hash : bin2hex($i
// Store peer id // Store peer id
$peer_id_sql = rtrim(DB()->escape(htmlCHR($peer_id)), ' '); $peer_id_sql = rtrim(DB()->escape(htmlCHR($peer_id)), ' ');
// Check info_hash version // Check info_hash length
if (strlen($info_hash) === 32) { if (strlen($info_hash) !== 20) {
$is_bt_v2 = true;
} elseif (strlen($info_hash) === 20) {
$is_bt_v2 = false;
} else {
msg_die('Invalid info_hash: ' . $info_hash_hex); msg_die('Invalid info_hash: ' . $info_hash_hex);
} }
@ -132,7 +128,7 @@ $peer_hash = hash('xxh128', $passkey . $info_hash_hex . $port);
// Events // Events
$stopped = ($event === 'stopped'); $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']; $port = $_SERVER['REMOTE_PORT'];
// Set seeder & complete // Set seeder & complete
@ -167,12 +163,12 @@ if ($lp_info) {
$releaser = $lp_info['releaser']; $releaser = $lp_info['releaser'];
$tor_type = $lp_info['tor_type']; $tor_type = $lp_info['tor_type'];
} else { } else {
$info_hash_sql = rtrim(DB()->escape($info_hash), ' ');
/** /**
* Currently torrent clients send truncated v2 hashes (the design raises questions). * Currently torrent clients send truncated v2 hashes (the design raises questions).
* https://github.com/bittorrent/bittorrent.org/issues/145#issuecomment-1720040343 * https://github.com/bittorrent/bittorrent.org/issues/145#issuecomment-1720040343
*/ */
$info_hash_sql = rtrim(DB()->escape($info_hash), ' '); $info_hash_where = "WHERE tor.info_hash = '$info_hash_sql' OR SUBSTRING(tor.info_hash_v2, 1, 20) = '$info_hash_sql'";
$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'";
$passkey_sql = DB()->escape($passkey); $passkey_sql = DB()->escape($passkey);
$sql = " $sql = "
@ -201,7 +197,7 @@ if ($lp_info) {
// Check hybrid status // Check hybrid status
if (!empty($row['info_hash']) && !empty($row['info_hash_v2'])) { if (!empty($row['info_hash']) && !empty($row['info_hash_v2'])) {
$is_hybrid = true; $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; $update_hybrid = true;
} }
} }
@ -303,11 +299,12 @@ if ($bb_cfg['tracker']['freeleech'] && $down_add) {
// Insert / update peer info // Insert / update peer info
$peer_info_updated = false; $peer_info_updated = false;
$update_time = ($stopped) ? 0 : TIMENOW; $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) { if ($lp_info) {
$sql = "UPDATE " . BB_BT_TRACKER . " SET update_time = $update_time"; $sql = "UPDATE " . BB_BT_TRACKER . " SET update_time = $update_time";
$sql .= ", $ip_version = '$ip_sql'"; $sql .= ", $ip_version = '$ip_sql'";
$sql .= ", port = '$port'";
$sql .= ", seeder = $seeder"; $sql .= ", seeder = $seeder";
$sql .= ($releaser != $lp_info['releaser']) ? ", releaser = $releaser" : ''; $sql .= ($releaser != $lp_info['releaser']) ? ", releaser = $releaser" : '';

View file

@ -42,6 +42,9 @@ foreach ($info_hash_array[1] as $hash) {
$decoded_hash = urldecode($hash); $decoded_hash = urldecode($hash);
if (strlen($decoded_hash) !== 20) {
continue;
}
if ($scrape_cache = CACHE('tr_cache')->get(SCRAPE_LIST_PREFIX . bin2hex($decoded_hash))) { 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]; $torrents['files'][$info_key = array_key_first($scrape_cache)] = $scrape_cache[$info_key];
} else { } else {
@ -79,7 +82,7 @@ if (!empty($info_hash_count)) {
foreach ($scrapes as $scrape) { foreach ($scrapes as $scrape) {
$hash_v1 = !empty($scrape['info_hash']) ? $scrape['info_hash'] : ''; $hash_v1 = !empty($scrape['info_hash']) ? $scrape['info_hash'] : '';
$hash_v2 = !empty($scrape['info_hash_v2']) ? substr($scrape['info_hash_v2'], 0, 20) : ''; $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] = [ $torrents['files'][$info_hash_scrape] = [
'complete' => (int)$scrape['seeders'], 'complete' => (int)$scrape['seeders'],