Skip to content
This repository has been archived by the owner on Nov 2, 2020. It is now read-only.

Commit

Permalink
feat(torrent/download): Add multi tracker behaviour
Browse files Browse the repository at this point in the history
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'
  • Loading branch information
Rhilip committed Jul 24, 2019
1 parent 6dc1028 commit 837ba64
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
31 changes: 21 additions & 10 deletions apps/models/Torrent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
6 changes: 3 additions & 3 deletions apps/timer/TrackerAnnounceTimer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down
5 changes: 3 additions & 2 deletions migration/ridpt.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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'),
Expand Down

0 comments on commit 837ba64

Please sign in to comment.