update: announce

This commit is contained in:
Roardom
2023-01-04 05:17:37 -06:00
parent 4291b83864
commit 985423e267
14 changed files with 77 additions and 35 deletions
@@ -87,6 +87,8 @@ class AutoBanDisposableUsers extends Command
// Send Email
Mail::to($user->email)->send(new BanUser($user->email, $logban));
}
\cache()->forget('user:'.$user->passkey);
}
});
$this->comment('Automated User Banning Command Complete');
@@ -76,6 +76,8 @@ class AutoDeactivateWarning extends Command
if ($warning->warneduser->can_download === 0) {
$warning->warneduser->can_download = 1;
$warning->warneduser->save();
\cache()->forget('user:'.$warning->warneduser->passkey);
}
}
@@ -68,6 +68,8 @@ class AutoDisableInactiveUsers extends Command
$user->disabled_at = Carbon::now();
$user->save();
\cache()->forget('user:'.$user->passkey);
// Send Email
\dispatch(new SendDisableUserMail($user));
}
+2
View File
@@ -112,6 +112,8 @@ class AutoGroup extends Command
$user->group_id = UserGroups::ARCHIVIST;
$user->save();
}
\cache()->forget('user:'.$user->passkey);
}
$this->comment('Automated User Group Command Complete');
@@ -81,6 +81,8 @@ class AutoSoftDeleteDisabledUsers extends Command
$user->deleted_by = 1;
$user->save();
\cache()->forget('user:'.$user->passkey);
// Removes UserID from Torrents if any and replaces with System UserID (1)
foreach (Torrent::withAnyStatus()->where('user_id', '=', $user->id)->get() as $tor) {
$tor->user_id = 1;
+2
View File
@@ -95,6 +95,8 @@ class AutoWarning extends Command
if ($warning->warneduser->can_download === 1) {
$warning->warneduser->can_download = 0;
$warning->warneduser->save();
\cache()->forget('user:'.$warning->warneduser->passkey);
}
}
}
+25 -21
View File
@@ -22,7 +22,6 @@ use App\Helpers\Bencode;
use App\Jobs\ProcessAnnounce;
use App\Models\BlacklistClient;
use App\Models\Group;
use App\Models\Peer;
use App\Models\Torrent;
use App\Models\User;
use Illuminate\Http\Request;
@@ -100,7 +99,7 @@ class AnnounceController extends Controller
$queries = $this->checkAnnounceFields($request);
// Check user via supplied passkey.
$user = $this->checkUser($passkey, $queries);
[$user, $group] = $this->checkUser($passkey, $queries);
// Get Torrent Info Array from queries and judge if user can reach it.
$torrent = $this->checkTorrent($queries['info_hash']);
@@ -118,14 +117,14 @@ class AnnounceController extends Controller
// Check Download Slots.
if (\config('announce.slots_system.enabled')) {
$this->checkDownloadSlots($queries, $user);
$this->checkDownloadSlots($queries, $user, $group);
}
// Generate A Response For The Torrent Client.
$repDict = $this->generateSuccessAnnounceResponse($queries, $torrent, $user);
// Process Annnounce Job.
$this->processAnnounceJob($queries, $user, $torrent);
$this->processAnnounceJob($queries, $user, $torrent, $group);
} catch (TrackerException $exception) {
$repDict = $this->generateFailedAnnounceResponse($exception);
} finally {
@@ -170,7 +169,7 @@ class AnnounceController extends Controller
(string) $userAgent
), new TrackerException(121));
$clientBlacklist = \cache()->remember('client_blacklist', 86_400, fn () => BlacklistClient::all()->pluck('name')->toArray());
$clientBlacklist = \cache()->rememberForever('client_blacklist', fn () => BlacklistClient::all()->pluck('name')->toArray());
// Block Blacklisted Clients
\throw_if(
@@ -185,7 +184,7 @@ class AnnounceController extends Controller
* @throws \App\Exceptions\TrackerException
* @throws \Throwable
*/
protected function checkPasskey($passkey): void
protected function checkPasskey(string $passkey): void
{
// If Passkey Is Not Provided Return Error to Client
\throw_if($passkey === null, new TrackerException(130, [':attribute' => 'passkey']));
@@ -297,9 +296,9 @@ class AnnounceController extends Controller
* @throws \App\Exceptions\TrackerException
* @throws \Throwable
*/
protected function checkUser($passkey, $queries): object
protected function checkUser(string $passkey, array $queries): array
{
// Caached System Required Groups
// Cached System Required Groups
$bannedGroup = \cache()->rememberForever(
'banned_group',
fn () => Group::where('slug', '=', 'banned')->pluck('id')
@@ -314,11 +313,16 @@ class AnnounceController extends Controller
);
// Check Passkey Against Users Table
$user = \cache()->remember('user:'.$passkey, 1_800, fn () => User::with('group')
->select(['id', 'group_id', 'can_download', 'uploaded', 'downloaded'])
$user = \cache()->rememberForever('user:'.$passkey, fn () => User::query()
->select(['id', 'group_id', 'can_download'])
->where('passkey', '=', $passkey)
->first());
$group = \cache()->rememberForever('group:'.$user->group_id, fn () => Group::query()
->select(['id', 'download_slots', 'is_immune', 'is_freeleech', 'is_double_upload'])
->where('id', '=', $user->group_id)
->first());
// If User Doesn't Exist Return Error to Client
\throw_if($user === null, new TrackerException(140));
@@ -346,7 +350,7 @@ class AnnounceController extends Controller
new TrackerException(141, [':status' => 'Disabled'])
);
return $user;
return [$user, $group];
}
/**
@@ -355,7 +359,7 @@ class AnnounceController extends Controller
* @throws \App\Exceptions\TrackerException
* @throws \Throwable
*/
protected function checkTorrent($infoHash): object
protected function checkTorrent(string $infoHash): Torrent
{
// Check Info Hash Against Torrents Table
$torrent = Torrent::withAnyStatus()
@@ -398,7 +402,7 @@ class AnnounceController extends Controller
* @throws \App\Exceptions\TrackerException
* @throws \Throwable
*/
private function checkPeer($torrent, $queries, $user): void
private function checkPeer(Torrent $torrent, array $queries, User $user): void
{
\throw_if(
\strtolower($queries['event']) === 'completed'
@@ -417,7 +421,7 @@ class AnnounceController extends Controller
* @throws \Exception
* @throws \Throwable
*/
private function checkMinInterval($torrent, $queries, $user): void
private function checkMinInterval(Torrent $torrent, array $queries, User $user): void
{
$prevAnnounce = $torrent->peers
->where('peer_id', '=', $queries['peer_id'])
@@ -438,7 +442,7 @@ class AnnounceController extends Controller
* @throws \App\Exceptions\TrackerException
* @throws \Throwable
*/
private function checkMaxConnections($torrent, $user): void
private function checkMaxConnections(Torrent $torrent, User $user): void
{
// Pull Count On Users Peers Per Torrent For Rate Limiting
$connections = $torrent->peers
@@ -458,9 +462,9 @@ class AnnounceController extends Controller
* @throws \App\Exceptions\TrackerException
* @throws \Throwable
*/
private function checkDownloadSlots($queries, $user): void
private function checkDownloadSlots(array $queries, User $user, Group $group): void
{
$max = $user->group->download_slots;
$max = $group->download_slots;
if ($max !== null && $max >= 0 && $queries['left'] != 0) {
$count = DB::table('peers')
@@ -481,7 +485,7 @@ class AnnounceController extends Controller
*
* @throws \Exception
*/
private function generateSuccessAnnounceResponse($queries, $torrent, $user): array
private function generateSuccessAnnounceResponse(array $queries, Torrent $torrent, User $user): array
{
// Build Response For Bittorrent Client
$repDict = [
@@ -523,9 +527,9 @@ class AnnounceController extends Controller
/**
* Process Announce Database Queries.
*/
private function processAnnounceJob($queries, $user, $torrent): void
private function processAnnounceJob(array $queries, User $user, Torrent $torrent, Group $group): void
{
ProcessAnnounce::dispatch($queries, $user, $torrent);
ProcessAnnounce::dispatch($queries, $user, $torrent, $group);
}
protected function generateFailedAnnounceResponse(TrackerException $trackerException): array
@@ -539,7 +543,7 @@ class AnnounceController extends Controller
/**
* Send Final Announce Response.
*/
protected function sendFinalAnnounceResponse($repDict): Response
protected function sendFinalAnnounceResponse(array $repDict): Response
{
return response(Bencode::bencode($repDict), headers: self::HEADERS);
}
@@ -100,6 +100,8 @@ class LoginController extends Controller
$user->disabled_at = null;
$user->save();
\cache()->forget('user:'.$user->passkey);
return \to_route('home.index')
->withSuccess(\trans('auth.welcome-restore'));
}
@@ -115,6 +117,8 @@ class LoginController extends Controller
$user->disabled_at = null;
$user->save();
\cache()->forget('user:'.$user->passkey);
return \to_route('home.index')
->withSuccess(\trans('auth.welcome-restore'));
}
@@ -75,6 +75,8 @@ class BanController extends Controller
$user->save();
$ban->save();
\cache()->forget('user:'.$user->passkey);
// Send Notifications
$user->notify(new UserBan($ban));
@@ -119,6 +121,8 @@ class BanController extends Controller
$user->save();
$ban->save();
\cache()->forget('user:'.$user->passkey);
// Send Notifications
$user->notify(new UserBanExpire());
@@ -69,6 +69,8 @@ class BlacklistClientController extends Controller
$client->save();
\cache()->forget('client_blacklist');
return \to_route('staff.blacklists.clients.index')
->withSuccess('Blacklisted Client Was Updated Successfully!');
}
@@ -104,6 +106,8 @@ class BlacklistClientController extends Controller
$client->save();
\cache()->forget('client_blacklist');
return \to_route('staff.blacklists.clients.index')
->withSuccess('Blacklisted Client Stored Successfully!');
}
@@ -118,6 +122,8 @@ class BlacklistClientController extends Controller
$client = BlacklistClient::findOrFail($id);
$client->delete();
\cache()->forget('client_blacklist');
return \to_route('staff.blacklists.clients.index')
->withSuccess('Blacklisted Client Destroyed Successfully!');
}
@@ -175,6 +175,8 @@ class GroupController extends Controller
$group->save();
\cache()->forget('group:'.$group->id);
return \to_route('staff.groups.index')
->withSuccess('Group Was Updated Successfully!');
}
@@ -121,6 +121,8 @@ class UserController extends Controller
$user->internal_id = (int) $request->input('internal_id');
$user->save();
\cache()->forget('user:'.$user->passkey);
return \to_route('users.show', ['username' => $user->username])
->withSuccess('Account Was Updated Successfully!');
}
@@ -139,6 +141,8 @@ class UserController extends Controller
$user->can_chat = $request->input('can_chat');
$user->save();
\cache()->forget('user:'.$user->passkey);
return \to_route('users.show', ['username' => $user->username])
->withSuccess('Account Permissions Successfully Edited');
}
@@ -263,6 +267,8 @@ class UserController extends Controller
->withSuccess('Account Has Been Removed');
}
\cache()->forget('user:'.$user->passkey);
return \to_route('staff.dashboard.index')
->withErrors('Something Went Wrong!');
}
+1 -1
View File
@@ -295,7 +295,7 @@ class UserController extends Controller
$user->passkey = \md5(\random_bytes(60).$user->password);
$user->save();
\cache()->forget(\sprintf('user:%s', $user->passkey));
\cache()->forget('user:'.$user->passkey);
return \to_route('user_security', ['username' => $user->username, 'hash' => '#pid'])
->withSuccess('Your PID Was Changed Successfully!');
+17 -13
View File
@@ -35,7 +35,7 @@ class ProcessAnnounce implements ShouldQueue
/**
* Create a new job instance.
*/
public function __construct(protected $queries, protected $user, protected $torrent)
public function __construct(protected $queries, protected $user, protected $torrent, protected $group)
{
}
@@ -112,7 +112,7 @@ class ProcessAnnounce implements ShouldQueue
->first();
if ($personalFreeleech ||
$this->user->group->is_freeleech == 1 ||
$this->group->is_freeleech == 1 ||
$freeleechToken ||
\config('other.freeleech') == 1) {
$modDownloaded = 0;
@@ -126,7 +126,7 @@ class ProcessAnnounce implements ShouldQueue
}
if ($this->torrent->doubleup == 1 ||
$this->user->group->is_double_upload == 1 ||
$this->group->is_double_upload == 1 ||
\config('other.doubleup') == 1) {
$modUploaded = $uploaded * 2;
} else {
@@ -159,7 +159,8 @@ class ProcessAnnounce implements ShouldQueue
case 'started':
$history->active = 1;
$history->immune = $this->user->group->is_immune == 1;
// Allow downgrading from `immune`, but never upgrade to it
$history->immune = (int) ($history->immune === null ? $this->group->is_immune : (bool) $history->immune && (bool) $this->group->is_immune);
$history->save();
break;
@@ -181,9 +182,10 @@ class ProcessAnnounce implements ShouldQueue
$history->save();
// User Update
$this->user->uploaded += $modUploaded;
$this->user->downloaded += $modDownloaded;
$this->user->save();
$this->user->update([
'uploaded' => DB::raw('uploaded + '. (int) $modUploaded),
'downloaded' => DB::raw('downloaded + '. (int) $modDownloaded),
]);
// End User Update
// Torrent Completed Update
@@ -209,9 +211,10 @@ class ProcessAnnounce implements ShouldQueue
$peer->delete();
// User Update
$this->user->uploaded += $modUploaded;
$this->user->downloaded += $modDownloaded;
$this->user->save();
$this->user->update([
'uploaded' => DB::raw('uploaded + '. (int) $modUploaded),
'downloaded' => DB::raw('downloaded + '. (int) $modDownloaded),
]);
// End User Update
break;
@@ -233,9 +236,10 @@ class ProcessAnnounce implements ShouldQueue
$history->save();
// User Update
$this->user->uploaded += $modUploaded;
$this->user->downloaded += $modDownloaded;
$this->user->save();
$this->user->update([
'uploaded' => DB::raw('uploaded + '. (int) $modUploaded),
'downloaded' => DB::raw('downloaded + '. (int) $modDownloaded),
]);
// End User Update
}