refactor: announce

This commit is contained in:
HDVinnie
2022-07-19 01:48:33 -04:00
parent 3a3201ba51
commit d5940a4430
7 changed files with 464 additions and 661 deletions

View File

@@ -23,11 +23,15 @@ use App\Jobs\ProcessBasicAnnounceRequest;
use App\Jobs\ProcessCompletedAnnounceRequest;
use App\Jobs\ProcessStartedAnnounceRequest;
use App\Jobs\ProcessStoppedAnnounceRequest;
use App\Models\FreeleechToken;
use App\Models\Group;
use App\Models\History;
use App\Models\Peer;
use App\Models\PersonalFreeleech;
use App\Models\Torrent;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
class AnnounceController extends Controller
{
@@ -58,6 +62,7 @@ class AnnounceController extends Controller
* Announce Code.
*
* @throws \Exception
* @throws \Throwable
*/
public function index(Request $request, string $passkey): ?\Illuminate\Http\Response
{
@@ -120,7 +125,7 @@ class AnnounceController extends Controller
/**
* Dispatch The Specfic Annnounce Event Job.
*/
$this->sendAnnounceJob($queries, $user, $torrent);
$this->processAnnounceJob($queries, $user, $torrent);
} catch (TrackerException $exception) {
$repDict = $this->generateFailedAnnounceResponse($exception);
} finally {
@@ -130,6 +135,7 @@ class AnnounceController extends Controller
/**
* @throws \App\Exceptions\TrackerException
* @throws \Throwable
*/
protected function checkClient(Request $request): void
{
@@ -164,6 +170,7 @@ class AnnounceController extends Controller
* Check Passkey Exist and Valid.
*
* @throws \App\Exceptions\TrackerException
* @throws \Throwable
*/
protected function checkPasskey($passkey): void
{
@@ -180,6 +187,7 @@ class AnnounceController extends Controller
/**
* @throws \App\Exceptions\TrackerException
* @throws \Throwable
*/
private function checkAnnounceFields(Request $request): array
{
@@ -246,6 +254,7 @@ class AnnounceController extends Controller
* Get User Via Validated Passkey.
*
* @throws \App\Exceptions\TrackerException
* @throws \Throwable
*/
protected function checkUser($passkey, $queries): object
{
@@ -280,6 +289,7 @@ class AnnounceController extends Controller
/**
* @throws \App\Exceptions\TrackerException
* @throws \Throwable
*/
protected function checkTorrent($infoHash): object
{
@@ -306,6 +316,7 @@ class AnnounceController extends Controller
/**
* @throws \App\Exceptions\TrackerException
* @throws \Throwable
*/
private function checkPeer($torrent, $queries, $user): void
{
@@ -319,6 +330,7 @@ class AnnounceController extends Controller
/**
* @throws \App\Exceptions\TrackerException
* @throws \Exception
* @throws \Throwable
*/
private function checkMinInterval($torrent, $queries, $user): void
{
@@ -334,6 +346,7 @@ class AnnounceController extends Controller
/**
* @throws \App\Exceptions\TrackerException
* @throws \Throwable
*/
private function checkMaxConnections($torrent, $user): void
{
@@ -348,6 +361,7 @@ class AnnounceController extends Controller
/**
* @throws \App\Exceptions\TrackerException
* @throws \Throwable
*/
private function checkDownloadSlots($queries, $user): void
{
@@ -381,7 +395,7 @@ class AnnounceController extends Controller
* We query peers from database and send peerlist, otherwise just quick return.
*/
if (\strtolower($queries['event']) !== 'stopped') {
$limit = ($queries['numwant'] <= 25 ? $queries['numwant'] : 25);
$limit = (min($queries['numwant'], 25));
// Get Torrents Peers
if ($queries['left'] == 0) {
@@ -410,16 +424,452 @@ class AnnounceController extends Controller
/**
* TODO: Paused Event (http://www.bittorrent.org/beps/bep_0021.html).
*/
private function sendAnnounceJob($queries, $user, $torrent): void
private function processAnnounceJob($queries, $user, $torrent): void
{
if (\strtolower($queries['event']) === 'started') {
ProcessStartedAnnounceRequest::dispatch($queries, $user, $torrent);
// Get The Current Peer
$peer = Peer::where('torrent_id', '=', $torrent->id)
->where('peer_id', $queries['peer_id'])
->where('user_id', '=', $user->id)
->first();
// Creates a new peer if not existing
if ($peer === null) {
if ($queries['uploaded'] > 0 || $queries['downloaded'] > 0) {
$queries['event'] = 'started';
}
$peer = new Peer();
}
// Get history information
$history = History::where('torrent_id', '=', $torrent->id)
->where('user_id', '=', $user->id)
->first();
// If no History record found then create one
if ($history === null) {
$history = new History();
$history->user_id = $user->id;
$history->torrent_id = $torrent->id;
$history->info_hash = $queries['info_hash'];
}
$realUploaded = $queries['uploaded'];
$realDownloaded = $queries['downloaded'];
// Peer Update
$peer->peer_id = $queries['peer_id'];
$peer->md5_peer_id = \md5($queries['peer_id']);
$peer->info_hash = $queries['info_hash'];
$peer->ip = $queries['ip-address'];
$peer->port = $queries['port'];
$peer->agent = $queries['user-agent'];
$peer->uploaded = $realUploaded;
$peer->downloaded = $realDownloaded;
$peer->seeder = $queries['left'] == 0;
$peer->left = $queries['left'];
$peer->torrent_id = $torrent->id;
$peer->user_id = $user->id;
$peer->updateConnectableStateIfNeeded();
$peer->save();
// End Peer Update
// History Update
$history->agent = $queries['user-agent'];
$history->active = 1;
$history->seeder = $queries['left'] == 0;
$history->immune = $user->group->is_immune == 1;
$history->uploaded += 0;
$history->actual_uploaded += 0;
$history->client_uploaded = $realUploaded;
$history->downloaded += 0;
$history->actual_downloaded += 0;
$history->client_downloaded = $realDownloaded;
$history->save();
// End History Update
// Sync Seeders / Leechers Count
$torrent->seeders = Peer::where('torrent_id', '=', $torrent->id)
->where('left', '=', '0')
->count();
$torrent->leechers = Peer::where('torrent_id', '=', $torrent->id)
->where('left', '>', '0')
->count();
$torrent->save();
} elseif (\strtolower($queries['event']) === 'completed') {
ProcessCompletedAnnounceRequest::dispatch($queries, $user, $torrent);
// Get The Current Peer
$peer = Peer::where('torrent_id', '=', $torrent->id)
->where('peer_id', $queries['peer_id'])
->where('user_id', '=', $user->id)
->first();
// Flag is tripped if new session is created but client reports up/down > 0
$ghost = false;
// Creates a new peer if not existing
if ($peer === null) {
if ($queries['uploaded'] > 0 || $queries['downloaded'] > 0) {
$ghost = true;
$queries['event'] = 'started';
}
$peer = new Peer();
}
// Get history information
$history = History::where('torrent_id', '=', $torrent->id)
->where('user_id', '=', $user->id)
->first();
// If no History record found then create one
if ($history === null) {
$history = new History();
$history->user_id = $user->id;
$history->torrent_id = $torrent->id;
$history->info_hash = $queries['info_hash'];
}
$realUploaded = $queries['uploaded'];
$realDownloaded = $queries['downloaded'];
if ($ghost) {
$uploaded = ($realUploaded >= $history->client_uploaded) ? ($realUploaded - $history->client_uploaded) : 0;
$downloaded = ($realDownloaded >= $history->client_downloaded) ? ($realDownloaded - $history->client_downloaded) : 0;
} else {
$uploaded = ($realUploaded >= $peer->uploaded) ? ($realUploaded - $peer->uploaded) : 0;
$downloaded = ($realDownloaded >= $peer->downloaded) ? ($realDownloaded - $peer->downloaded) : 0;
}
$oldUpdate = $peer->updated_at->timestamp ?? Carbon::now()->timestamp;
// Modification of Upload and Download
$personalFreeleech = PersonalFreeleech::where('user_id', '=', $user->id)
->first();
$freeleechToken = FreeleechToken::where('user_id', '=', $user->id)
->where('torrent_id', '=', $torrent->id)
->first();
if (\config('other.freeleech') == 1 || $personalFreeleech || $user->group->is_freeleech == 1 || $freeleechToken) {
$modDownloaded = 0;
} elseif ($torrent->free >= 1) {
// FL value in DB are from 0% to 100%.
// Divide it by 100 and multiply it with "downloaded" to get discount download.
$fl_discount = $downloaded * $torrent->free / 100;
$modDownloaded = $downloaded - $fl_discount;
} else {
$modDownloaded = $downloaded;
}
if (\config('other.doubleup') == 1 || $torrent->doubleup == 1 || $user->group->is_double_upload == 1) {
$modUploaded = $uploaded * 2;
} else {
$modUploaded = $uploaded;
}
// Peer Update
$peer->peer_id = $queries['peer_id'];
$peer->md5_peer_id = \md5($queries['peer_id']);
$peer->info_hash = $queries['info_hash'];
$peer->ip = $queries['ip-address'];
$peer->port = $queries['port'];
$peer->agent = $queries['user-agent'];
$peer->uploaded = $realUploaded;
$peer->downloaded = $realDownloaded;
$peer->seeder = 1;
$peer->left = 0;
$peer->torrent_id = $torrent->id;
$peer->user_id = $user->id;
$peer->updateConnectableStateIfNeeded();
$peer->save();
// End Peer Update
// History Update
$history->agent = $queries['user-agent'];
$history->active = 1;
$history->seeder = $queries['left'] == 0;
$history->uploaded += $modUploaded;
$history->actual_uploaded += $uploaded;
$history->client_uploaded = $realUploaded;
$history->downloaded += $modDownloaded;
$history->actual_downloaded += $downloaded;
$history->client_downloaded = $realDownloaded;
$history->completed_at = Carbon::now();
// Seedtime allocation
if ($queries['left'] == 0) {
$newUpdate = $peer->updated_at->timestamp;
$diff = $newUpdate - $oldUpdate;
$history->seedtime += $diff;
}
$history->save();
// End History Update
// User Update
$user->uploaded += $modUploaded;
$user->downloaded += $modDownloaded;
$user->save();
// End User Update
// Torrent Completed Update
$torrent->increment('times_completed');
// Sync Seeders / Leechers Count
$torrent->seeders = Peer::where('torrent_id', '=', $torrent->id)
->where('left', '=', '0')
->count();
$torrent->leechers = Peer::where('torrent_id', '=', $torrent->id)
->where('left', '>', '0')
->count();
$torrent->save();
} elseif (\strtolower($queries['event']) === 'stopped') {
ProcessStoppedAnnounceRequest::dispatch($queries, $user, $torrent);
// Get The Current Peer
$peer = Peer::where('torrent_id', '=', $torrent->id)
->where('peer_id', $queries['peer_id'])
->where('user_id', '=', $user->id)
->first();
// Flag is tripped if new session is created but client reports up/down > 0
$ghost = false;
// Creates a new peer if not existing
if ($peer === null) {
if ($queries['uploaded'] > 0 || $queries['downloaded'] > 0) {
$ghost = true;
$queries['event'] = 'started';
}
$peer = new Peer();
}
// Get history information
$history = History::where('torrent_id', '=', $torrent->id)
->where('user_id', '=', $user->id)
->first();
// If no History record found then create one
if ($history === null) {
$history = new History();
$history->user_id = $user->id;
$history->torrent_id = $torrent->id;
$history->info_hash = $queries['info_hash'];
}
$realUploaded = $queries['uploaded'];
$realDownloaded = $queries['downloaded'];
if ($ghost) {
$uploaded = ($realUploaded >= $history->client_uploaded) ? ($realUploaded - $history->client_uploaded) : 0;
$downloaded = ($realDownloaded >= $history->client_downloaded) ? ($realDownloaded - $history->client_downloaded) : 0;
} else {
$uploaded = ($realUploaded >= $peer->uploaded) ? ($realUploaded - $peer->uploaded) : 0;
$downloaded = ($realDownloaded >= $peer->downloaded) ? ($realDownloaded - $peer->downloaded) : 0;
}
$oldUpdate = $peer->updated_at->timestamp ?? Carbon::now()->timestamp;
// Modification of Upload and Download
$personalFreeleech = PersonalFreeleech::where('user_id', '=', $user->id)
->first();
$freeleechToken = FreeleechToken::where('user_id', '=', $user->id)
->where('torrent_id', '=', $torrent->id)
->first();
if (\config('other.freeleech') == 1 || $personalFreeleech || $user->group->is_freeleech == 1 || $freeleechToken) {
$modDownloaded = 0;
} elseif ($torrent->free >= 1) {
// FL value in DB are from 0% to 100%.
// Divide it by 100 and multiply it with "downloaded" to get discount download.
$fl_discount = $downloaded * $torrent->free / 100;
$modDownloaded = $downloaded - $fl_discount;
} else {
$modDownloaded = $downloaded;
}
if (\config('other.doubleup') == 1 || $torrent->doubleup == 1 || $user->group->is_double_upload == 1) {
$modUploaded = $uploaded * 2;
} else {
$modUploaded = $uploaded;
}
// Peer Update
$peer->peer_id = $queries['peer_id'];
$peer->md5_peer_id = \md5($queries['peer_id']);
$peer->info_hash = $queries['info_hash'];
$peer->ip = $queries['ip-address'];
$peer->port = $queries['port'];
$peer->agent = $queries['user-agent'];
$peer->uploaded = $realUploaded;
$peer->downloaded = $realDownloaded;
$peer->seeder = $queries['left'] == 0;
$peer->left = $queries['left'];
$peer->torrent_id = $torrent->id;
$peer->user_id = $user->id;
$peer->updateConnectableStateIfNeeded();
$peer->save();
// End Peer Update
// History Update
$history->agent = $queries['user-agent'];
$history->active = 0;
$history->seeder = $queries['left'] == 0;
$history->uploaded += $modUploaded;
$history->actual_uploaded += $uploaded;
$history->client_uploaded = $realUploaded;
$history->downloaded += $modDownloaded;
$history->actual_downloaded += $downloaded;
$history->client_downloaded = $realDownloaded;
// Seedtime allocation
if ($queries['left'] == 0) {
$newUpdate = $peer->updated_at->timestamp;
$diff = $newUpdate - $oldUpdate;
$history->seedtime += $diff;
}
$history->save();
// End History Update
// Peer Delete (Now that history is updated)
$peer->delete();
// End Peer Delete
// User Update
$user->uploaded += $modUploaded;
$user->downloaded += $modDownloaded;
$user->save();
// End User Update
// Sync Seeders / Leechers Count
$torrent->seeders = Peer::where('torrent_id', '=', $torrent->id)
->where('left', '=', '0')
->count();
$torrent->leechers = Peer::where('torrent_id', '=', $torrent->id)
->where('left', '>', '0')
->count();
$torrent->save();
} else {
ProcessBasicAnnounceRequest::dispatch($queries, $user, $torrent);
// Get The Current Peer
$peer = Peer::where('torrent_id', '=', $torrent->id)
->where('peer_id', $queries['peer_id'])
->where('user_id', '=', $user->id)
->first();
// Flag is tripped if new session is created but client reports up/down > 0
$ghost = false;
if ($peer === null && \strtolower($queries['event']) === 'completed') {
$delete();
}
// Creates a new peer if not existing
if ($peer === null) {
if ($queries['uploaded'] > 0 || $queries['downloaded'] > 0) {
$ghost = true;
$queries['event'] = 'started';
}
$peer = new Peer();
}
// Get history information
$history = History::where('torrent_id', '=', $torrent->id)
->where('user_id', '=', $user->id)
->first();
// If no History record found then create one
if ($history === null) {
$history = new History();
$history->user_id = $user->id;
$history->torrent_id = $torrent->id;
$history->info_hash = $queries['info_hash'];
}
$realUploaded = $queries['uploaded'];
$realDownloaded = $queries['downloaded'];
if ($ghost) {
$uploaded = ($realUploaded >= $history->client_uploaded) ? ($realUploaded - $history->client_uploaded) : 0;
$downloaded = ($realDownloaded >= $history->client_downloaded) ? ($realDownloaded - $history->client_downloaded) : 0;
} else {
$uploaded = ($realUploaded >= $peer->uploaded) ? ($realUploaded - $peer->uploaded) : 0;
$downloaded = ($realDownloaded >= $peer->downloaded) ? ($realDownloaded - $peer->downloaded) : 0;
}
$oldUpdate = $peer->updated_at->timestamp ?? Carbon::now()->timestamp;
// Modification of Upload and Download
$personalFreeleech = PersonalFreeleech::where('user_id', '=', $user->id)->first();
$freeleechToken = FreeleechToken::where('user_id', '=', $user->id)
->where('torrent_id', '=', $torrent->id)
->first();
if (\config('other.freeleech') == 1 || $personalFreeleech || $user->group->is_freeleech == 1 || $freeleechToken) {
$modDownloaded = 0;
} elseif ($torrent->free >= 1) {
// FL value in DB are from 0% to 100%.
// Divide it by 100 and multiply it with "downloaded" to get discount download.
$fl_discount = $downloaded * $torrent->free / 100;
$modDownloaded = $downloaded - $fl_discount;
} else {
$modDownloaded = $downloaded;
}
if (\config('other.doubleup') == 1 || $torrent->doubleup == 1 || $user->group->is_double_upload == 1) {
$modUploaded = $uploaded * 2;
} else {
$modUploaded = $uploaded;
}
// Peer Update
$peer->peer_id = $queries['peer_id'];
$peer->md5_peer_id = \md5($queries['peer_id']);
$peer->info_hash = $queries['info_hash'];
$peer->ip = $queries['ip-address'];
$peer->port = $queries['port'];
$peer->agent = $queries['user-agent'];
$peer->uploaded = $realUploaded;
$peer->downloaded = $realDownloaded;
$peer->seeder = $queries['left'] == 0;
$peer->left = $queries['left'];
$peer->torrent_id = $torrent->id;
$peer->user_id = $user->id;
$peer->updateConnectableStateIfNeeded();
$peer->save();
// End Peer Update
// History Update
$history->agent = $queries['user-agent'];
$history->active = 1;
$history->seeder = $queries['left'] == 0;
$history->uploaded += $modUploaded;
$history->actual_uploaded += $uploaded;
$history->client_uploaded = $realUploaded;
$history->downloaded += $modDownloaded;
$history->actual_downloaded += $downloaded;
$history->client_downloaded = $realDownloaded;
// Seedtime allocation
if ($queries['left'] == 0) {
$newUpdate = $peer->updated_at->timestamp;
$diff = $newUpdate - $oldUpdate;
$history->seedtime += $diff;
}
$history->save();
// End History Update
// User Update
$user->uploaded += $modUploaded;
$user->downloaded += $modDownloaded;
$user->save();
// End User Update
// Sync Seeders / Leechers Count
$torrent->seeders = Peer::where('torrent_id', '=', $torrent->id)
->where('left', '=', '0')
->count();
$torrent->leechers = Peer::where('torrent_id', '=', $torrent->id)
->where('left', '>', '0')
->count();
$torrent->save();
}
}

View File

@@ -1,173 +0,0 @@
<?php
/**
* NOTICE OF LICENSE.
*
* UNIT3D Community Edition is open-sourced software licensed under the GNU Affero General Public License v3.0
* The details is bundled with this project in the file LICENSE.txt.
*
* @project UNIT3D Community Edition
*
* @author HDVinnie <hdinnovations@protonmail.com>
* @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0
*/
namespace App\Jobs;
use App\Models\FreeleechToken;
use App\Models\History;
use App\Models\Peer;
use App\Models\PersonalFreeleech;
use App\Models\Torrent;
use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Carbon;
class ProcessBasicAnnounceRequest implements ShouldQueue
{
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use SerializesModels;
/**
* ProcessBasicAnnounceRequest Constructor.
*/
public function __construct(protected $queries, protected User $user, protected Torrent $torrent)
{
}
/**
* Execute the job.
*
* @throws \App\Exceptions\TrackerException
*/
public function handle(): void
{
// Get The Current Peer
$peer = Peer::where('torrent_id', '=', $this->torrent->id)
->where('peer_id', $this->queries['peer_id'])
->where('user_id', '=', $this->user->id)
->first();
// Flag is tripped if new session is created but client reports up/down > 0
$ghost = false;
if ($peer === null && \strtolower($this->queries['event']) === 'completed') {
$this->delete();
}
// Creates a new peer if not existing
if ($peer === null) {
if ($this->queries['uploaded'] > 0 || $this->queries['downloaded'] > 0) {
$ghost = true;
$this->queries['event'] = 'started';
}
$peer = new Peer();
}
// Get history information
$history = History::where('torrent_id', '=', $this->torrent->id)
->where('user_id', '=', $this->user->id)
->first();
// If no History record found then create one
if ($history === null) {
$history = new History();
$history->user_id = $this->user->id;
$history->torrent_id = $this->torrent->id;
$history->info_hash = $this->queries['info_hash'];
}
$realUploaded = $this->queries['uploaded'];
$realDownloaded = $this->queries['downloaded'];
if ($ghost) {
$uploaded = ($realUploaded >= $history->client_uploaded) ? ($realUploaded - $history->client_uploaded) : 0;
$downloaded = ($realDownloaded >= $history->client_downloaded) ? ($realDownloaded - $history->client_downloaded) : 0;
} else {
$uploaded = ($realUploaded >= $peer->uploaded) ? ($realUploaded - $peer->uploaded) : 0;
$downloaded = ($realDownloaded >= $peer->downloaded) ? ($realDownloaded - $peer->downloaded) : 0;
}
$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();
if (\config('other.freeleech') == 1 || $personalFreeleech || $this->user->group->is_freeleech == 1 || $freeleechToken) {
$modDownloaded = 0;
} elseif ($this->torrent->free >= 1) {
// FL value in DB are from 0% to 100%.
// Divide it by 100 and multiply it with "downloaded" to get discount download.
$fl_discount = $downloaded * $this->torrent->free / 100;
$modDownloaded = $downloaded - $fl_discount;
} else {
$modDownloaded = $downloaded;
}
if (\config('other.doubleup') == 1 || $this->torrent->doubleup == 1 || $this->user->group->is_double_upload == 1) {
$modUploaded = $uploaded * 2;
} else {
$modUploaded = $uploaded;
}
// Peer Update
$peer->peer_id = $this->queries['peer_id'];
$peer->md5_peer_id = \md5($this->queries['peer_id']);
$peer->info_hash = $this->queries['info_hash'];
$peer->ip = $this->queries['ip-address'];
$peer->port = $this->queries['port'];
$peer->agent = $this->queries['user-agent'];
$peer->uploaded = $realUploaded;
$peer->downloaded = $realDownloaded;
$peer->seeder = $this->queries['left'] == 0;
$peer->left = $this->queries['left'];
$peer->torrent_id = $this->torrent->id;
$peer->user_id = $this->user->id;
$peer->updateConnectableStateIfNeeded();
$peer->save();
// End Peer Update
// History Update
$history->agent = $this->queries['user-agent'];
$history->active = 1;
$history->seeder = $this->queries['left'] == 0;
$history->uploaded += $modUploaded;
$history->actual_uploaded += $uploaded;
$history->client_uploaded = $realUploaded;
$history->downloaded += $modDownloaded;
$history->actual_downloaded += $downloaded;
$history->client_downloaded = $realDownloaded;
// Seedtime allocation
if ($this->queries['left'] == 0) {
$newUpdate = $peer->updated_at->timestamp;
$diff = $newUpdate - $oldUpdate;
$history->seedtime += $diff;
}
$history->save();
// End History Update
// User Update
$this->user->uploaded += $modUploaded;
$this->user->downloaded += $modDownloaded;
$this->user->save();
// 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->save();
}
}

View File

@@ -1,175 +0,0 @@
<?php
/**
* NOTICE OF LICENSE.
*
* UNIT3D Community Edition is open-sourced software licensed under the GNU Affero General Public License v3.0
* The details is bundled with this project in the file LICENSE.txt.
*
* @project UNIT3D Community Edition
*
* @author HDVinnie <hdinnovations@protonmail.com>
* @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0
*/
namespace App\Jobs;
use App\Models\FreeleechToken;
use App\Models\History;
use App\Models\Peer;
use App\Models\PersonalFreeleech;
use App\Models\Torrent;
use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Carbon;
class ProcessCompletedAnnounceRequest implements ShouldQueue
{
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use SerializesModels;
/**
* ProcessCompletedAnnounceRequest Constructor.
*/
public function __construct(protected $queries, protected User $user, protected Torrent $torrent)
{
}
/**
* Execute the job.
*
* @throws \Exception
*/
public function handle(): void
{
// Get The Current Peer
$peer = Peer::where('torrent_id', '=', $this->torrent->id)
->where('peer_id', $this->queries['peer_id'])
->where('user_id', '=', $this->user->id)
->first();
// Flag is tripped if new session is created but client reports up/down > 0
$ghost = false;
// Creates a new peer if not existing
if ($peer === null) {
if ($this->queries['uploaded'] > 0 || $this->queries['downloaded'] > 0) {
$ghost = true;
$this->queries['event'] = 'started';
}
$peer = new Peer();
}
// Get history information
$history = History::where('torrent_id', '=', $this->torrent->id)
->where('user_id', '=', $this->user->id)
->first();
// If no History record found then create one
if ($history === null) {
$history = new History();
$history->user_id = $this->user->id;
$history->torrent_id = $this->torrent->id;
$history->info_hash = $this->queries['info_hash'];
}
$realUploaded = $this->queries['uploaded'];
$realDownloaded = $this->queries['downloaded'];
if ($ghost) {
$uploaded = ($realUploaded >= $history->client_uploaded) ? ($realUploaded - $history->client_uploaded) : 0;
$downloaded = ($realDownloaded >= $history->client_downloaded) ? ($realDownloaded - $history->client_downloaded) : 0;
} else {
$uploaded = ($realUploaded >= $peer->uploaded) ? ($realUploaded - $peer->uploaded) : 0;
$downloaded = ($realDownloaded >= $peer->downloaded) ? ($realDownloaded - $peer->downloaded) : 0;
}
$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();
if (\config('other.freeleech') == 1 || $personalFreeleech || $this->user->group->is_freeleech == 1 || $freeleechToken) {
$modDownloaded = 0;
} elseif ($this->torrent->free >= 1) {
// FL value in DB are from 0% to 100%.
// Divide it by 100 and multiply it with "downloaded" to get discount download.
$fl_discount = $downloaded * $this->torrent->free / 100;
$modDownloaded = $downloaded - $fl_discount;
} else {
$modDownloaded = $downloaded;
}
if (\config('other.doubleup') == 1 || $this->torrent->doubleup == 1 || $this->user->group->is_double_upload == 1) {
$modUploaded = $uploaded * 2;
} else {
$modUploaded = $uploaded;
}
// Peer Update
$peer->peer_id = $this->queries['peer_id'];
$peer->md5_peer_id = \md5($this->queries['peer_id']);
$peer->info_hash = $this->queries['info_hash'];
$peer->ip = $this->queries['ip-address'];
$peer->port = $this->queries['port'];
$peer->agent = $this->queries['user-agent'];
$peer->uploaded = $realUploaded;
$peer->downloaded = $realDownloaded;
$peer->seeder = 1;
$peer->left = 0;
$peer->torrent_id = $this->torrent->id;
$peer->user_id = $this->user->id;
$peer->updateConnectableStateIfNeeded();
$peer->save();
// End Peer Update
// History Update
$history->agent = $this->queries['user-agent'];
$history->active = 1;
$history->seeder = $this->queries['left'] == 0;
$history->uploaded += $modUploaded;
$history->actual_uploaded += $uploaded;
$history->client_uploaded = $realUploaded;
$history->downloaded += $modDownloaded;
$history->actual_downloaded += $downloaded;
$history->client_downloaded = $realDownloaded;
$history->completed_at = Carbon::now();
// Seedtime allocation
if ($this->queries['left'] == 0) {
$newUpdate = $peer->updated_at->timestamp;
$diff = $newUpdate - $oldUpdate;
$history->seedtime += $diff;
}
$history->save();
// End History Update
// User Update
$this->user->uploaded += $modUploaded;
$this->user->downloaded += $modDownloaded;
$this->user->save();
// End User Update
// Torrent Completed Update
$this->torrent->increment('times_completed');
// 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->save();
}
}

View File

@@ -1,125 +0,0 @@
<?php
/**
* NOTICE OF LICENSE.
*
* UNIT3D Community Edition is open-sourced software licensed under the GNU Affero General Public License v3.0
* The details is bundled with this project in the file LICENSE.txt.
*
* @project UNIT3D Community Edition
*
* @author HDVinnie <hdinnovations@protonmail.com>
* @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0
*/
namespace App\Jobs;
use App\Models\History;
use App\Models\Peer;
use App\Models\Torrent;
use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\Middleware\WithoutOverlapping;
use Illuminate\Queue\SerializesModels;
class ProcessStartedAnnounceRequest implements ShouldQueue
{
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use SerializesModels;
/**
* ProcessStartedAnnounceRequest Constructor.
*/
public function __construct(protected $queries, protected User $user, protected Torrent $torrent)
{
}
/**
* Get the middleware the job should pass through.
*/
public function middleware(): array
{
return [new WithoutOverlapping($this->user->id.'.'.$this->queries['info_hash'])];
}
/**
* Execute the job.
*/
public function handle(): void
{
// Get The Current Peer
$peer = Peer::where('torrent_id', '=', $this->torrent->id)
->where('peer_id', $this->queries['peer_id'])
->where('user_id', '=', $this->user->id)
->first();
// Creates a new peer if not existing
if ($peer === null) {
if ($this->queries['uploaded'] > 0 || $this->queries['downloaded'] > 0) {
$this->queries['event'] = 'started';
}
$peer = new Peer();
}
// Get history information
$history = History::where('torrent_id', '=', $this->torrent->id)
->where('user_id', '=', $this->user->id)
->first();
// If no History record found then create one
if ($history === null) {
$history = new History();
$history->user_id = $this->user->id;
$history->torrent_id = $this->torrent->id;
$history->info_hash = $this->queries['info_hash'];
}
$realUploaded = $this->queries['uploaded'];
$realDownloaded = $this->queries['downloaded'];
// Peer Update
$peer->peer_id = $this->queries['peer_id'];
$peer->md5_peer_id = \md5($this->queries['peer_id']);
$peer->info_hash = $this->queries['info_hash'];
$peer->ip = $this->queries['ip-address'];
$peer->port = $this->queries['port'];
$peer->agent = $this->queries['user-agent'];
$peer->uploaded = $realUploaded;
$peer->downloaded = $realDownloaded;
$peer->seeder = $this->queries['left'] == 0;
$peer->left = $this->queries['left'];
$peer->torrent_id = $this->torrent->id;
$peer->user_id = $this->user->id;
$peer->updateConnectableStateIfNeeded();
$peer->save();
// End Peer Update
// History Update
$history->agent = $this->queries['user-agent'];
$history->active = 1;
$history->seeder = $this->queries['left'] == 0;
$history->immune = $this->user->group->is_immune == 1;
$history->uploaded += 0;
$history->actual_uploaded += 0;
$history->client_uploaded = $realUploaded;
$history->downloaded += 0;
$history->actual_downloaded += 0;
$history->client_downloaded = $realDownloaded;
$history->save();
// 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->save();
}
}

View File

@@ -1,175 +0,0 @@
<?php
/**
* NOTICE OF LICENSE.
*
* UNIT3D Community Edition is open-sourced software licensed under the GNU Affero General Public License v3.0
* The details is bundled with this project in the file LICENSE.txt.
*
* @project UNIT3D Community Edition
*
* @author HDVinnie <hdinnovations@protonmail.com>
* @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0
*/
namespace App\Jobs;
use App\Models\FreeleechToken;
use App\Models\History;
use App\Models\Peer;
use App\Models\PersonalFreeleech;
use App\Models\Torrent;
use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Carbon;
class ProcessStoppedAnnounceRequest implements ShouldQueue
{
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use SerializesModels;
/**
* ProcessStoppedAnnounceRequest Constructor.
*/
public function __construct(protected $queries, protected User $user, protected Torrent $torrent)
{
}
/**
* Execute the job.
*
* @throws \Exception
*/
public function handle(): void
{
// Get The Current Peer
$peer = Peer::where('torrent_id', '=', $this->torrent->id)
->where('peer_id', $this->queries['peer_id'])
->where('user_id', '=', $this->user->id)
->first();
// Flag is tripped if new session is created but client reports up/down > 0
$ghost = false;
// Creates a new peer if not existing
if ($peer === null) {
if ($this->queries['uploaded'] > 0 || $this->queries['downloaded'] > 0) {
$ghost = true;
$this->queries['event'] = 'started';
}
$peer = new Peer();
}
// Get history information
$history = History::where('torrent_id', '=', $this->torrent->id)
->where('user_id', '=', $this->user->id)
->first();
// If no History record found then create one
if ($history === null) {
$history = new History();
$history->user_id = $this->user->id;
$history->torrent_id = $this->torrent->id;
$history->info_hash = $this->queries['info_hash'];
}
$realUploaded = $this->queries['uploaded'];
$realDownloaded = $this->queries['downloaded'];
if ($ghost) {
$uploaded = ($realUploaded >= $history->client_uploaded) ? ($realUploaded - $history->client_uploaded) : 0;
$downloaded = ($realDownloaded >= $history->client_downloaded) ? ($realDownloaded - $history->client_downloaded) : 0;
} else {
$uploaded = ($realUploaded >= $peer->uploaded) ? ($realUploaded - $peer->uploaded) : 0;
$downloaded = ($realDownloaded >= $peer->downloaded) ? ($realDownloaded - $peer->downloaded) : 0;
}
$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();
if (\config('other.freeleech') == 1 || $personalFreeleech || $this->user->group->is_freeleech == 1 || $freeleechToken) {
$modDownloaded = 0;
} elseif ($this->torrent->free >= 1) {
// FL value in DB are from 0% to 100%.
// Divide it by 100 and multiply it with "downloaded" to get discount download.
$fl_discount = $downloaded * $this->torrent->free / 100;
$modDownloaded = $downloaded - $fl_discount;
} else {
$modDownloaded = $downloaded;
}
if (\config('other.doubleup') == 1 || $this->torrent->doubleup == 1 || $this->user->group->is_double_upload == 1) {
$modUploaded = $uploaded * 2;
} else {
$modUploaded = $uploaded;
}
// Peer Update
$peer->peer_id = $this->queries['peer_id'];
$peer->md5_peer_id = \md5($this->queries['peer_id']);
$peer->info_hash = $this->queries['info_hash'];
$peer->ip = $this->queries['ip-address'];
$peer->port = $this->queries['port'];
$peer->agent = $this->queries['user-agent'];
$peer->uploaded = $realUploaded;
$peer->downloaded = $realDownloaded;
$peer->seeder = $this->queries['left'] == 0;
$peer->left = $this->queries['left'];
$peer->torrent_id = $this->torrent->id;
$peer->user_id = $this->user->id;
$peer->updateConnectableStateIfNeeded();
$peer->save();
// End Peer Update
// History Update
$history->agent = $this->queries['user-agent'];
$history->active = 0;
$history->seeder = $this->queries['left'] == 0;
$history->uploaded += $modUploaded;
$history->actual_uploaded += $uploaded;
$history->client_uploaded = $realUploaded;
$history->downloaded += $modDownloaded;
$history->actual_downloaded += $downloaded;
$history->client_downloaded = $realDownloaded;
// Seedtime allocation
if ($this->queries['left'] == 0) {
$newUpdate = $peer->updated_at->timestamp;
$diff = $newUpdate - $oldUpdate;
$history->seedtime += $diff;
}
$history->save();
// End History Update
// Peer Delete (Now that history is updated)
$peer->delete();
// End Peer Delete
// User Update
$this->user->uploaded += $modUploaded;
$this->user->downloaded += $modDownloaded;
$this->user->save();
// 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->save();
}
}

View File

@@ -137,52 +137,53 @@ return [
'client' => env('REDIS_CLIENT', 'phpredis'),
'options' => [
'cluster' => env('REDIS_CLUSTER', 'redis'),
'serializer' => Redis::SERIALIZER_IGBINARY,
],
'default' => [
'url' => env('REDIS_URL'),
'host' => env('REDIS_HOST', '127.0.0.1'),
'username' => env('REDIS_USERNAME'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_DB', 0),
'read_write_timeout' => -1,
],
'cache' => [
'url' => env('REDIS_URL'),
'host' => env('REDIS_HOST', '127.0.0.1'),
'username' => env('REDIS_USERNAME'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_CACHE_DB', 1),
'read_write_timeout' => -1,
],
'job' => [
'url' => env('REDIS_URL'),
'host' => env('REDIS_HOST', '127.0.0.1'),
'username' => env('REDIS_USERNAME'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_JOB_DB', 2),
'read_write_timeout' => -1,
],
'broadcast' => [
'url' => env('REDIS_URL'),
'host' => env('REDIS_HOST', '127.0.0.1'),
'username' => env('REDIS_USERNAME'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_BROADCAST_DB', 3),
'read_write_timeout' => -1,
],
'session' => [
'url' => env('REDIS_URL'),
'host' => env('REDIS_HOST', '127.0.0.1'),
'username' => env('REDIS_USERNAME'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_BROADCAST_DB', 4),
'read_write_timeout' => -1,
],
],

View File

@@ -64,7 +64,7 @@ return [
'redis' => [
'driver' => 'redis',
'connection' => 'job',
'connection' => 'default',
'queue' => env('REDIS_QUEUE', 'default'),
'retry_after' => 120,
'block_for' => null,