Tracker announce & scrape improvements

This commit is contained in:
Roman Kelesidis 2023-09-10 00:12:00 +07:00
commit d5d0a68736
5 changed files with 44 additions and 33 deletions

View file

@ -18,11 +18,6 @@ if (empty($_SERVER['HTTP_USER_AGENT'])) {
die; die;
} }
// Ignore 'completed' event
if (isset($_GET['event']) && $_GET['event'] === 'completed') {
dummy_exit(random_int(600, 1200));
}
$announce_interval = $bb_cfg['announce_interval']; $announce_interval = $bb_cfg['announce_interval'];
$passkey_key = $bb_cfg['passkey_key']; $passkey_key = $bb_cfg['passkey_key'];
@ -119,32 +114,32 @@ if (!\TorrentPier\Helpers\IPHelper::isValid($ip)) {
$ip_sql = \TorrentPier\Helpers\IPHelper::ip2long($ip); $ip_sql = \TorrentPier\Helpers\IPHelper::ip2long($ip);
// Peer unique id // Peer unique id
$peer_hash = md5( $peer_hash = md5(rtrim($info_hash, ' ') . $passkey . $ip . $port);
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);
}
// Events // Events
$seeder = ($left == 0) ? 1 : 0; $seeder = ($left == 0) ? 1 : 0;
$stopped = ($event === 'stopped'); $stopped = ($event === 'stopped');
$completed = ($event === 'completed'); $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 // Stopped event
if ($stopped) { if ($stopped) {
CACHE('tr_cache')->rm(PEER_HASH_PREFIX . $peer_hash); CACHE('tr_cache')->rm(PEER_HASH_PREFIX . $peer_hash);
} }
// Completed event
if ($completed) {
}
// Get last peer info from DB // Get last peer info from DB
if (!CACHE('tr_cache')->used && !$lp_info) { if (!CACHE('tr_cache')->used && !$lp_info) {
$lp_info = DB()->fetch_row(" $lp_info = DB()->fetch_row("
@ -153,10 +148,6 @@ if (!CACHE('tr_cache')->used && !$lp_info) {
} }
if ($lp_info) { if ($lp_info) {
if (!$stopped) {
drop_fast_announce($lp_info);
}
$user_id = $lp_info['user_id']; $user_id = $lp_info['user_id'];
$topic_id = $lp_info['topic_id']; $topic_id = $lp_info['topic_id'];
$releaser = $lp_info['releaser']; $releaser = $lp_info['releaser'];
@ -185,7 +176,7 @@ if ($lp_info) {
$releaser = (int)($user_id == $row['poster_id']); $releaser = (int)($user_id == $row['poster_id']);
$tor_type = $row['tor_type']; $tor_type = $row['tor_type'];
// Check user and topic id // Verify if torrent registered on tracker and user authorized
if (empty($topic_id)) { if (empty($topic_id)) {
msg_die('Torrent not registered, info_hash = ' . bin2hex($info_hash)); msg_die('Torrent not registered, info_hash = ' . bin2hex($info_hash));
} }
@ -398,6 +389,7 @@ if (!$output) {
'min interval' => (int)$announce_interval, 'min interval' => (int)$announce_interval,
'complete' => (int)$seeders, 'complete' => (int)$seeders,
'incomplete' => (int)$leechers, 'incomplete' => (int)$leechers,
'warning message' => 'Statistics were updated',
'peers' => $peers, 'peers' => $peers,
]; ];

View file

@ -37,7 +37,7 @@ function error_exit($msg = '')
exit; exit;
} }
function drop_fast_announce($lp_info) function drop_fast_announce($lp_info, $lp_cached_peers = [])
{ {
global $announce_interval; global $announce_interval;
@ -47,7 +47,7 @@ function drop_fast_announce($lp_info)
$new_ann_intrv = $lp_info['update_time'] + $announce_interval - TIMENOW; $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) function msg_die($msg)
@ -55,19 +55,27 @@ function msg_die($msg)
$output = \SandFox\Bencode\Bencode::encode([ $output = \SandFox\Bencode\Bencode::encode([
'min interval' => (int)1800, 'min interval' => (int)1800,
'failure reason' => (string)$msg, 'failure reason' => (string)$msg,
'warning message' => (string)$msg,
]); ]);
die($output); die($output);
} }
function dummy_exit($interval = 1800) function dummy_exit($interval = 1800, $cache_dict = [])
{ {
$output = \SandFox\Bencode\Bencode::encode([ $output = [
'interval' => (int)$interval, 'interval' => (int)$interval,
'min interval' => (int)$interval, 'min interval' => (int)$interval,
'peers' => (string)DUMMY_PEER, '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); die($output);
} }

View file

@ -39,6 +39,12 @@ if (strlen($info_hash) == 32) {
msg_die('Invalid info_hash: ' . $info_hash); 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), ' '); $info_hash_sql = rtrim(DB()->escape($info_hash), ' ');
/** /**
* Поскольку торрент-клиенты в настоящее время обрезают инфо-хэш до 20 символов (независимо от его типа, как известно v1 = 20 символов, а v2 = 32 символа), * Поскольку торрент-клиенты в настоящее время обрезают инфо-хэш до 20 символов (независимо от его типа, как известно v1 = 20 символов, а v2 = 32 символа),
@ -55,7 +61,7 @@ $row = DB()->fetch_row("
"); ");
if (!$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] = [ $output['files'][$info_hash] = [
@ -64,6 +70,8 @@ $output['files'][$info_hash] = [
'incomplete' => (int)$row['leechers'], '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); echo \SandFox\Bencode\Bencode::encode($output);
exit; exit;

View file

@ -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('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('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('PEER_HASH_PREFIX', 'peer_');
define('PEERS_LIST_PREFIX', 'peers_list_'); define('PEERS_LIST_PREFIX', 'peers_list_');
define('SCRAPE_LIST_PREFIX', 'scrape_list_');
header('Content-Type: text/plain'); header('Content-Type: text/plain');
header('Pragma: no-cache'); header('Pragma: no-cache');

View file

@ -66,7 +66,7 @@ $bb_cfg['cache'] = [
'redis' => [ 'redis' => [
'host' => '127.0.0.1', 'host' => '127.0.0.1',
'port' => 6379, 'port' => 6379,
'pconnect' => true, 'pconnect' => PHP_ZTS ? false : true,
'con_required' => true, 'con_required' => true,
], ],
// Available cache types: filecache, memcache, sqlite, redis, apcu (filecache by default) // Available cache types: filecache, memcache, sqlite, redis, apcu (filecache by default)
@ -95,6 +95,7 @@ $bb_cfg['gzip_compress'] = false; // compress output
// Tracker // Tracker
$bb_cfg['announce_interval'] = 2400; // Announce interval (default: 2400) $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['passkey_key'] = 'uk'; // Passkey key name in GET request
$bb_cfg['ignore_reported_ip'] = false; // Ignore IP reported by client $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'] $bb_cfg['verify_reported_ip'] = true; // Verify IP reported by client against $_SERVER['HTTP_X_FORWARDED_FOR']