From 837ba64c6a1d235501c31c2391711e73f7c499d2 Mon Sep 17 00:00:00 2001 From: Rhilip Date: Wed, 24 Jul 2019 21:15:59 +0800 Subject: [PATCH] feat(torrent/download): Add multi tracker behaviour 1. Some discuss show the multitracker behaviour with: One tracker One tier may not as good as we want, So we add another default chosen with config_key 'base.site_multi_tracker_behaviour' which only set one tier with full tracker list in `announce-list` 2. fix typo about word 'multi' from word 'muti' --- apps/models/Torrent.php | 31 +++++++++++++++++++---------- apps/timer/TrackerAnnounceTimer.php | 6 +++--- migration/ridpt.sql | 5 +++-- 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/apps/models/Torrent.php b/apps/models/Torrent.php index 525bcdb..7ac7690 100644 --- a/apps/models/Torrent.php +++ b/apps/models/Torrent.php @@ -194,17 +194,28 @@ public function getDownloadDict($encode = true) $dict["announce"] = $scheme . config("base.site_tracker_url") . $announce_suffix; /** BEP 0012 Multitracker Metadata Extension - * See more on : http://www.bittorrent.org/beps/bep_0012.html + * @see http://www.bittorrent.org/beps/bep_0012.html + * @see https://web.archive.org/web/20190724110959/https://blog.rhilip.info/archives/1108/ + * which discuss about multitracker behaviour on common bittorrent client ( Chinese Version ) */ - if ($muti_tracker = config("base.site_muti_tracker_url")) { - $dict["announce-list"] = []; - - // Add our main tracker into muti_tracker_list to avoid lost error.... - $muti_tracker = config("base.site_tracker_url") . "," . $muti_tracker; - - $muti_tracker_list = explode(",", $muti_tracker); - foreach (array_unique($muti_tracker_list) as $tracker) { // use array_unique to remove dupe tracker - $dict["announce-list"][] = [$scheme . $tracker . $announce_suffix]; + if ($multi_trackers = config("base.site_multi_tracker_url")) { + // Add our main tracker into muti_tracker_list to avoid lost.... + $multi_trackers = config("base.site_tracker_url") . "," . $multi_trackers; + $muti_trackers_list = explode(",", $multi_trackers); + $muti_trackers_list = array_unique($muti_trackers_list); // use array_unique to remove dupe tracker + // fulfill each tracker with scheme and suffix about user identity + $muti_trackers_list = array_map(function ($uri) use ($scheme, $announce_suffix) { + return $scheme . $uri . $announce_suffix; + }, $muti_trackers_list); + + if (config('base.site_multi_tracker_behaviour') == 'separate') { + /** d['announce-list'] = [ [tracker1], [backup1], [backup2] ] */ + foreach ($muti_trackers_list as $tracker) { // separate each tracker to different tier + $dict["announce-list"][] = [$tracker]; // Make each tracker as tier + } + } else { // config('base.site_multi_tracker_behaviour') == 'union' + /** d['announce-list'] = [[ tracker1, tracker2, tracker3 ]] */ + $dict["announce-list"][] = $muti_trackers_list; } } diff --git a/apps/timer/TrackerAnnounceTimer.php b/apps/timer/TrackerAnnounceTimer.php index 52277b6..a32e0bd 100644 --- a/apps/timer/TrackerAnnounceTimer.php +++ b/apps/timer/TrackerAnnounceTimer.php @@ -60,7 +60,7 @@ private function processAnnounceRequest($queries, $seeder, $userInfo, $torrentIn // If session is not exist and &event!=stopped, a new session should start if ($queries['event'] != 'stopped') { // Then create new session in database - // Update `torrents`, if peer's role is a seeder ,so complete +1 , elseif he is a leecher , so incomplete +1 + // TODO move to redis: Update `torrents`, if peer's role is a seeder ,so complete +1 , elseif he is a leecher , so incomplete +1 app()->pdo->createCommand("UPDATE `torrents` SET `{$torrentUpdateKey}` = `{$torrentUpdateKey}` +1 WHERE id=:tid")->bindParams([ "tid" => $torrentInfo["id"] ])->execute(); @@ -109,7 +109,7 @@ private function processAnnounceRequest($queries, $seeder, $userInfo, $torrentIn $this->getTorrentBuff($userInfo['id'], $torrentInfo["id"], $trueUploaded, $trueDownloaded, $thisUploaded, $thisDownloaded); - // Update Table `peers`, `snatched` by it's event tag + // TODO move to redis: Update Table `peers`, `snatched` by it's event tag // Notice : there MUST have history record in Table `snatched` if session is exist !!!!!!!! if ($queries["event"] === "stopped") { // Update `torrents`, if peer's role is a seeder ,so complete -1 , elseif he is a leecher , so incomplete -1 @@ -153,7 +153,7 @@ private function processAnnounceRequest($queries, $seeder, $userInfo, $torrentIn "ip" => $queries['remote_ip'], "uid" => $userInfo["id"], "tid" => $torrentInfo["id"], ]); - // Update `torrents`, with complete +1 incomplete -1 downloaded +1 + // TODO move to redis: Update `torrents`, with complete +1 incomplete -1 downloaded +1 app()->pdo->createCommand("UPDATE `torrents` SET `complete` = `complete` + 1, `incomplete` = `incomplete` -1 , `downloaded` = `downloaded` + 1 WHERE `id`=:tid")->bindParams([ "tid" => $torrentInfo["id"] ])->execute(); diff --git a/migration/ridpt.sql b/migration/ridpt.sql index 31d9d69..55c1455 100644 --- a/migration/ridpt.sql +++ b/migration/ridpt.sql @@ -3,7 +3,7 @@ -- https://www.phpmyadmin.net/ -- -- Host: 127.0.0.1 --- Generation Time: Jul 22, 2019 at 06:28 PM +-- Generation Time: Jul 24, 2019 at 09:07 PM -- Server version: 8.0.16 -- PHP Version: 7.3.7 @@ -672,7 +672,8 @@ INSERT INTO `site_config` (`name`, `value`) VALUES ('base.site_email', 'admin@ridpt.top'), ('base.site_generator', 'RidPT'), ('base.site_keywords', 'RidPT,Private Tracker'), -('base.site_muti_tracker_url', ''), +('base.site_multi_tracker_behaviour', 'union'), +('base.site_multi_tracker_url', ''), ('base.site_name', 'RidPT'), ('base.site_tracker_url', 'ridpt.top/tracker'), ('base.site_url', 'ridpt.top'),