Skip to content

Commit

Permalink
update: torrent annnounce
Browse files Browse the repository at this point in the history
  • Loading branch information
HDVinnie committed Nov 24, 2021
1 parent 1c9dfde commit 1c44bea
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 55 deletions.
66 changes: 35 additions & 31 deletions app/Http/Controllers/AnnounceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,15 +291,18 @@ protected function checkUser($passkey, $queries): object
$disabledGroup = \cache()->rememberForever('disabled_group', fn () => Group::where('slug', '=', 'disabled')->pluck('id'));

// Check Passkey Against Users Table
$user = User::where('passkey', '=', $passkey)->first();
$user = User::with('group')
->select(['id', 'group_id', 'active', 'can_download', 'uploaded', 'downloaded'])
->where('passkey', '=', $passkey)
->first();

// If User Doesn't Exist Return Error to Client
if ($user === null) {
throw new TrackerException(140);
}

// If User Account Is Unactivated/Validating Return Error to Client
if ($user->active === 0 || $user->group_id === $validatingGroup[0]) {
if ($user->active === 0 || $user->group->id === $validatingGroup[0]) {
throw new TrackerException(141, [':status' => 'Unactivated/Validating']);
}

Expand All @@ -309,12 +312,12 @@ protected function checkUser($passkey, $queries): object
}

// If User Is Banned Return Error to Client
if ($user->group_id === $bannedGroup[0]) {
if ($user->group->id === $bannedGroup[0]) {
throw new TrackerException(141, [':status' => 'Banned']);
}

// If User Is Disabled Return Error to Client
if ($user->group_id === $disabledGroup[0]) {
if ($user->group->id === $disabledGroup[0]) {
throw new TrackerException(141, [':status' => 'Disabled']);
}

Expand All @@ -329,9 +332,10 @@ protected function checkUser($passkey, $queries): object
protected function checkTorrent($infoHash): object
{
// Check Info Hash Against Torrents Table
$torrent = Torrent::withAnyStatus()
->where('info_hash', '=', $infoHash)
->first();
$torrent = Torrent::select(['id', 'free', 'doubleup', 'seeders', 'leechers', 'times_completed'])
->withAnyStatus()
->where('info_hash', '=', $infoHash)
->first();

// If Torrent Doesnt Exsist Return Error to Client
if ($torrent === null) {
Expand Down Expand Up @@ -361,12 +365,10 @@ protected function checkTorrent($infoHash): object
*/
private function checkPeer($torrent, $queries, $user): void
{
$peer = Peer::where('torrent_id', '=', $torrent->id)
if (! Peer::where('torrent_id', '=', $torrent->id)
->where('peer_id', $queries['peer_id'])
->where('user_id', '=', $user->id)
->first();

if ($peer === null && \strtolower($queries['event']) === 'completed') {
->exists() && \strtolower($queries['event']) === 'completed') {
throw new TrackerException(152);
}
}
Expand Down Expand Up @@ -399,7 +401,9 @@ private function checkMinInterval($queries, $user): void
private function checkMaxConnections($torrent, $user): void
{
// Pull Count On Users Peers Per Torrent For Rate Limiting
$connections = Peer::where('torrent_id', '=', $torrent->id)->where('user_id', '=', $user->id)->count();
$connections = Peer::where('torrent_id', '=', $torrent->id)
->where('user_id', '=', $user->id)
->count();

// If Users Peer Count On A Single Torrent Is Greater Than X Return Error to Client
if ($connections > \config('announce.rate_limit')) {
Expand All @@ -415,10 +419,12 @@ private function checkMaxConnections($torrent, $user): void
private function checkDownloadSlots($user): void
{
if (\config('announce.slots_system.enabled')) {
$max = $user->group()->download_slots;
$max = $user->group->download_slots;

if ($max > 0) {
$count = Peer::where('user_id', '=', $user->id)->where('seeder', '=', 0)->count();
$count = Peer::where('user_id', '=', $user->id)
->where('seeder', '=', 0)
->count();
if ($count >= $max) {
throw new TrackerException(164, [':max' => $max]);
}
Expand All @@ -439,23 +445,32 @@ private function generateSuccessAnnounceResponse($queries, $torrent, $user): arr
$repDict = [
'interval' => \rand(self::MIN, self::MAX),
'min interval' => self::MIN,
'complete' => $torrent->seeders,
'incomplete' => $torrent->leechers,
'complete' => (int) $torrent->seeders,
'incomplete' => (int) $torrent->leechers,
];

/**
* For non `stopped` event only
* We query peers from database and send peerlist, otherwise just quick return.
*/
if (\strtolower($queries['event']) !== 'stopped') {
$limit = ($queries['numwant'] <= 50 ? $queries['numwant'] : 50);
$limit = ($queries['numwant'] <= 25 ? $queries['numwant'] : 25);

// Get Torrents Peers
if ($queries['left'] == 0) {
// Only include leechers in a seeder's peerlist
$peers = Peer::where('torrent_id', '=', $torrent->id)->where('seeder', '=', 0)->where('user_id', '!=', $user->id)->take($limit)->get()->toArray();
$peers = Peer::where('torrent_id', '=', $torrent->id)
->where('seeder', '=', 0)
->where('user_id', '!=', $user->id)
->take($limit)
->get(['peer_id', 'ip', 'port'])
->toArray();
} else {
$peers = Peer::where('torrent_id', '=', $torrent->id)->where('user_id', '!=', $user->id)->take($limit)->get()->toArray();
$peers = Peer::where('torrent_id', '=', $torrent->id)
->where('user_id', '!=', $user->id)
->take($limit)
->get(['peer_id', 'ip', 'port'])
->toArray();
}

$repDict['peers'] = $this->givePeers($peers, $queries['compact'], $queries['no_peer_id']);
Expand Down Expand Up @@ -490,18 +505,7 @@ protected function generateFailedAnnounceResponse(TrackerException $trackerExcep
return [
'failure reason' => $trackerException->getMessage(),
'min interval' => self::MIN,
/**
* BEP 31: Failure Retry Extension.
*
* However most bittorrent client don't support it, so this feature is disabled default
* - libtorrent-rasterbar (e.g. qBittorrent, Deluge )
* This library will obey the `min interval` key if exist or it will retry in 60s (By default `min interval`)
* - libtransmission (e.g. Transmission )
* This library will ignore any other key if failed
*
* @see http://www.bittorrent.org/beps/bep_0031.html
*/
//'retry in' => self::MIN
//'retry in' => self::MIN
];
}

Expand Down
9 changes: 4 additions & 5 deletions app/Jobs/ProcessBasicAnnounceRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
namespace App\Jobs;

use App\Models\FreeleechToken;
use App\Models\Group;
use App\Models\History;
use App\Models\Peer;
use App\Models\PersonalFreeleech;
use App\Models\User;
use App\Models\Torrent;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
Expand All @@ -38,7 +38,7 @@ class ProcessBasicAnnounceRequest implements ShouldQueue
*
* @param $queries
*/
public function __construct(protected $queries, protected \App\Models\User $user, protected \App\Models\Torrent $torrent)
public function __construct(protected $queries, protected User $user, protected Torrent $torrent)
{
}

Expand Down Expand Up @@ -99,15 +99,14 @@ public function handle()
// Modification of Upload and Download
$personalFreeleech = PersonalFreeleech::where('user_id', '=', $this->user->id)->first();
$freeleechToken = FreeleechToken::where('user_id', '=', $this->user->id)->where('torrent_id', '=', $this->torrent->id)->first();
$group = Group::whereId($this->user->group_id)->first();

if (\config('other.freeleech') == 1 || $this->torrent->free == 1 || $personalFreeleech || $group->is_freeleech == 1 || $freeleechToken) {
if (\config('other.freeleech') == 1 || $this->torrent->free == 1 || $personalFreeleech || $this->user->group->is_freeleech == 1 || $freeleechToken) {
$modDownloaded = 0;
} else {
$modDownloaded = $downloaded;
}

if (\config('other.doubleup') == 1 || $this->torrent->doubleup == 1 || $group->is_double_upload == 1) {
if (\config('other.doubleup') == 1 || $this->torrent->doubleup == 1 || $this->user->group->is_double_upload == 1) {
$modUploaded = $uploaded * 2;
} else {
$modUploaded = $uploaded;
Expand Down
17 changes: 10 additions & 7 deletions app/Jobs/ProcessCompletedAnnounceRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
namespace App\Jobs;

use App\Models\FreeleechToken;
use App\Models\Group;
use App\Models\History;
use App\Models\Peer;
use App\Models\PersonalFreeleech;
Expand Down Expand Up @@ -72,7 +71,9 @@ public function handle()
}

// Get history information
$history = History::where('info_hash', '=', $this->queries['info_hash'])->where('user_id', '=', $this->user->id)->first();
$history = History::where('info_hash', '=', $this->queries['info_hash'])
->where('user_id', '=', $this->user->id)
->first();

// If no History record found then create one
if ($history === null) {
Expand All @@ -95,17 +96,19 @@ public function handle()
$oldUpdate = $peer->updated_at->timestamp ?? Carbon::now()->timestamp;

// Modification of Upload and Download
$personalFreeleech = PersonalFreeleech::where('user_id', '=', $this->user->id)->first();
$freeleechToken = FreeleechToken::where('user_id', '=', $this->user->id)->where('torrent_id', '=', $this->torrent->id)->first();
$group = Group::whereId($this->user->group_id)->first();
$personalFreeleech = PersonalFreeleech::where('user_id', '=', $this->user->id)
->first();
$freeleechToken = FreeleechToken::where('user_id', '=', $this->user->id)
->where('torrent_id', '=', $this->torrent->id)
->first();

if (\config('other.freeleech') == 1 || $this->torrent->free == 1 || $personalFreeleech || $group->is_freeleech == 1 || $freeleechToken) {
if (\config('other.freeleech') == 1 || $this->torrent->free == 1 || $personalFreeleech || $this->user->group->is_freeleech == 1 || $freeleechToken) {
$modDownloaded = 0;
} else {
$modDownloaded = $downloaded;
}

if (\config('other.doubleup') == 1 || $this->torrent->doubleup == 1 || $group->is_double_upload == 1) {
if (\config('other.doubleup') == 1 || $this->torrent->doubleup == 1 || $this->user->group->is_double_upload == 1) {
$modUploaded = $uploaded * 2;
} else {
$modUploaded = $uploaded;
Expand Down
12 changes: 9 additions & 3 deletions app/Jobs/ProcessStartedAnnounceRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ public function handle()
}

// Get history information
$history = History::where('info_hash', '=', $this->queries['info_hash'])->where('user_id', '=', $this->user->id)->first();
$history = History::where('info_hash', '=', $this->queries['info_hash'])
->where('user_id', '=', $this->user->id)
->first();

// If no History record found then create one
if ($history === null) {
Expand Down Expand Up @@ -117,8 +119,12 @@ public function handle()
// End History Update

// Sync Seeders / Leechers Count
$this->torrent->seeders = Peer::where('torrent_id', '=', $this->torrent->id)->where('left', '=', '0')->count();
$this->torrent->leechers = Peer::where('torrent_id', '=', $this->torrent->id)->where('left', '>', '0')->count();
$this->torrent->seeders = Peer::where('torrent_id', '=', $this->torrent->id)
->where('left', '=', '0')
->count();
$this->torrent->leechers = Peer::where('torrent_id', '=', $this->torrent->id)
->where('left', '>', '0')
->count();
$this->torrent->save();
}
}
25 changes: 16 additions & 9 deletions app/Jobs/ProcessStoppedAnnounceRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
namespace App\Jobs;

use App\Models\FreeleechToken;
use App\Models\Group;
use App\Models\History;
use App\Models\Peer;
use App\Models\PersonalFreeleech;
Expand Down Expand Up @@ -72,7 +71,9 @@ public function handle()
}

// Get history information
$history = History::where('info_hash', '=', $this->queries['info_hash'])->where('user_id', '=', $this->user->id)->first();
$history = History::where('info_hash', '=', $this->queries['info_hash'])
->where('user_id', '=', $this->user->id)
->first();

// If no History record found then create one
if ($history === null) {
Expand All @@ -95,17 +96,19 @@ public function handle()
$oldUpdate = $peer->updated_at->timestamp ?? Carbon::now()->timestamp;

// Modification of Upload and Download
$personalFreeleech = PersonalFreeleech::where('user_id', '=', $this->user->id)->first();
$freeleechToken = FreeleechToken::where('user_id', '=', $this->user->id)->where('torrent_id', '=', $this->torrent->id)->first();
$group = Group::whereId($this->user->group_id)->first();
$personalFreeleech = PersonalFreeleech::where('user_id', '=', $this->user->id)
->first();
$freeleechToken = FreeleechToken::where('user_id', '=', $this->user->id)
->where('torrent_id', '=', $this->torrent->id)
->first();

if (\config('other.freeleech') == 1 || $this->torrent->free == 1 || $personalFreeleech || $group->is_freeleech == 1 || $freeleechToken) {
if (\config('other.freeleech') == 1 || $this->torrent->free == 1 || $personalFreeleech || $this->user->group->is_freeleech == 1 || $freeleechToken) {
$modDownloaded = 0;
} else {
$modDownloaded = $downloaded;
}

if (\config('other.doubleup') == 1 || $this->torrent->doubleup == 1 || $group->is_double_upload == 1) {
if (\config('other.doubleup') == 1 || $this->torrent->doubleup == 1 || $this->user->group->is_double_upload == 1) {
$modUploaded = $uploaded * 2;
} else {
$modUploaded = $uploaded;
Expand Down Expand Up @@ -159,8 +162,12 @@ public function handle()
// End User Update

// Sync Seeders / Leechers Count
$this->torrent->seeders = Peer::where('torrent_id', '=', $this->torrent->id)->where('left', '=', '0')->count();
$this->torrent->leechers = Peer::where('torrent_id', '=', $this->torrent->id)->where('left', '>', '0')->count();
$this->torrent->seeders = Peer::where('torrent_id', '=', $this->torrent->id)
->where('left', '=', '0')
->count();
$this->torrent->leechers = Peer::where('torrent_id', '=', $this->torrent->id)
->where('left', '>', '0')
->count();
$this->torrent->save();
}
}

0 comments on commit 1c44bea

Please sign in to comment.