From d5d0a687360d864364305219b1fbcf64ef00183e Mon Sep 17 00:00:00 2001 From: Roman Kelesidis Date: Sun, 10 Sep 2023 00:12:00 +0700 Subject: [PATCH] Tracker announce & scrape improvements --- bt/announce.php | 42 +++++++++++++++++------------------------ bt/includes/init_tr.php | 20 ++++++++++++++------ bt/scrape.php | 10 +++++++++- common.php | 2 ++ library/config.php | 3 ++- 5 files changed, 44 insertions(+), 33 deletions(-) diff --git a/bt/announce.php b/bt/announce.php index 7c1c04858..d75b5d383 100644 --- a/bt/announce.php +++ b/bt/announce.php @@ -18,11 +18,6 @@ if (empty($_SERVER['HTTP_USER_AGENT'])) { die; } -// Ignore 'completed' event -if (isset($_GET['event']) && $_GET['event'] === 'completed') { - dummy_exit(random_int(600, 1200)); -} - $announce_interval = $bb_cfg['announce_interval']; $passkey_key = $bb_cfg['passkey_key']; @@ -119,32 +114,32 @@ if (!\TorrentPier\Helpers\IPHelper::isValid($ip)) { $ip_sql = \TorrentPier\Helpers\IPHelper::ip2long($ip); // Peer unique id -$peer_hash = md5( - rtrim($info_hash, ' ') . $passkey . $ip . $port -); - -// Get cached peer info from previous announce (last peer info) -$lp_info = CACHE('tr_cache')->get(PEER_HASH_PREFIX . $peer_hash); - -// Drop fast announce -if ($lp_info && (!isset($event) || $event !== 'stopped')) { - drop_fast_announce($lp_info); -} +$peer_hash = md5(rtrim($info_hash, ' ') . $passkey . $ip . $port); // Events $seeder = ($left == 0) ? 1 : 0; $stopped = ($event === 'stopped'); $completed = ($event === 'completed'); +// Completed event +if ($completed && $seeder) { +} + +// Get cached peer info from previous announce (last peer info) +$lp_info = CACHE('tr_cache')->get(PEER_HASH_PREFIX . $peer_hash); + +// Drop fast announce +if ($lp_info && (!isset($event) || !$stopped)) { + if ($lp_cached_peers = CACHE('tr_cache')->get(PEERS_LIST_PREFIX . $lp_info['topic_id'])) { + drop_fast_announce($lp_info, $lp_cached_peers); // Use cache but with new calculated interval and seed, peer count set + } +} + // Stopped event if ($stopped) { CACHE('tr_cache')->rm(PEER_HASH_PREFIX . $peer_hash); } -// Completed event -if ($completed) { -} - // Get last peer info from DB if (!CACHE('tr_cache')->used && !$lp_info) { $lp_info = DB()->fetch_row(" @@ -153,10 +148,6 @@ if (!CACHE('tr_cache')->used && !$lp_info) { } if ($lp_info) { - if (!$stopped) { - drop_fast_announce($lp_info); - } - $user_id = $lp_info['user_id']; $topic_id = $lp_info['topic_id']; $releaser = $lp_info['releaser']; @@ -185,7 +176,7 @@ if ($lp_info) { $releaser = (int)($user_id == $row['poster_id']); $tor_type = $row['tor_type']; - // Check user and topic id + // Verify if torrent registered on tracker and user authorized if (empty($topic_id)) { msg_die('Torrent not registered, info_hash = ' . bin2hex($info_hash)); } @@ -398,6 +389,7 @@ if (!$output) { 'min interval' => (int)$announce_interval, 'complete' => (int)$seeders, 'incomplete' => (int)$leechers, + 'warning message' => 'Statistics were updated', 'peers' => $peers, ]; diff --git a/bt/includes/init_tr.php b/bt/includes/init_tr.php index 631b60b1c..486fa5b7e 100644 --- a/bt/includes/init_tr.php +++ b/bt/includes/init_tr.php @@ -37,7 +37,7 @@ function error_exit($msg = '') exit; } -function drop_fast_announce($lp_info) +function drop_fast_announce($lp_info, $lp_cached_peers = []) { global $announce_interval; @@ -47,7 +47,7 @@ function drop_fast_announce($lp_info) $new_ann_intrv = $lp_info['update_time'] + $announce_interval - TIMENOW; - dummy_exit($new_ann_intrv); + dummy_exit($new_ann_intrv, $lp_cached_peers); } function msg_die($msg) @@ -55,19 +55,27 @@ function msg_die($msg) $output = \SandFox\Bencode\Bencode::encode([ 'min interval' => (int)1800, 'failure reason' => (string)$msg, - 'warning message' => (string)$msg, ]); die($output); } -function dummy_exit($interval = 1800) +function dummy_exit($interval = 1800, $cache_dict = []) { - $output = \SandFox\Bencode\Bencode::encode([ + $output = [ 'interval' => (int)$interval, 'min interval' => (int)$interval, 'peers' => (string)DUMMY_PEER, - ]); + ]; + + if (!empty($cache_dict)) { + $output['complete'] = $cache_dict['complete']; + $output['incomplete'] = $cache_dict['incomplete']; + $output['warning message'] = 'Next statistics update in: ' . (floor($interval / 60) % 60) . ' minutes'; + $output['peers'] = $cache_dict['peers']; + } + + $output = \SandFox\Bencode\Bencode::encode($output); die($output); } diff --git a/bt/scrape.php b/bt/scrape.php index 9e346c6dc..ba004f44b 100644 --- a/bt/scrape.php +++ b/bt/scrape.php @@ -39,6 +39,12 @@ if (strlen($info_hash) == 32) { msg_die('Invalid info_hash: ' . $info_hash); } +$info_hash_hex = bin2hex($info_hash); + +if ($lp_scrape_info = CACHE('tr_cache')->get(SCRAPE_LIST_PREFIX . $info_hash_hex)) { + die(\SandFox\Bencode\Bencode::encode($lp_scrape_info)); +} + $info_hash_sql = rtrim(DB()->escape($info_hash), ' '); /** * Поскольку торрент-клиенты в настоящее время обрезают инфо-хэш до 20 символов (независимо от его типа, как известно v1 = 20 символов, а v2 = 32 символа), @@ -55,7 +61,7 @@ $row = DB()->fetch_row(" "); if (!$row) { - msg_die('Torrent not registered, info_hash = ' . bin2hex($info_hash)); + msg_die('Torrent not registered, info_hash = ' . $info_hash_hex); } $output['files'][$info_hash] = [ @@ -64,6 +70,8 @@ $output['files'][$info_hash] = [ 'incomplete' => (int)$row['leechers'], ]; +$peers_list_cached = CACHE('tr_cache')->set(SCRAPE_LIST_PREFIX . $info_hash_hex, $output, SCRAPE_LIST_EXPIRE);; + echo \SandFox\Bencode\Bencode::encode($output); exit; diff --git a/common.php b/common.php index f9f873e95..615977e1f 100644 --- a/common.php +++ b/common.php @@ -346,9 +346,11 @@ if (!defined('IN_TRACKER')) { define('PEER_HASH_EXPIRE', round($bb_cfg['announce_interval'] * (0.85 * $bb_cfg['tracker']['expire_factor']))); define('PEERS_LIST_EXPIRE', round($bb_cfg['announce_interval'] * 0.7)); + define('SCRAPE_LIST_EXPIRE', round($bb_cfg['scrape_interval'] * 0.7)); define('PEER_HASH_PREFIX', 'peer_'); define('PEERS_LIST_PREFIX', 'peers_list_'); + define('SCRAPE_LIST_PREFIX', 'scrape_list_'); header('Content-Type: text/plain'); header('Pragma: no-cache'); diff --git a/library/config.php b/library/config.php index 58bd30aa4..3aed91afa 100644 --- a/library/config.php +++ b/library/config.php @@ -66,7 +66,7 @@ $bb_cfg['cache'] = [ 'redis' => [ 'host' => '127.0.0.1', 'port' => 6379, - 'pconnect' => true, + 'pconnect' => PHP_ZTS ? false : true, 'con_required' => true, ], // Available cache types: filecache, memcache, sqlite, redis, apcu (filecache by default) @@ -95,6 +95,7 @@ $bb_cfg['gzip_compress'] = false; // compress output // Tracker $bb_cfg['announce_interval'] = 2400; // Announce interval (default: 2400) +$bb_cfg['scrape_interval'] = 60; // Scrape interval (default: 60) $bb_cfg['passkey_key'] = 'uk'; // Passkey key name in GET request $bb_cfg['ignore_reported_ip'] = false; // Ignore IP reported by client $bb_cfg['verify_reported_ip'] = true; // Verify IP reported by client against $_SERVER['HTTP_X_FORWARDED_FOR']