Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(Add) Client List in User Profile with Connectable Check #1855

Merged
merged 2 commits into from
Aug 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ public function show($username): \Illuminate\Contracts\View\Factory | \Illuminat
$requested = TorrentRequest::where('user_id', '=', $user->id)->count();
$filled = TorrentRequest::where('filled_by', '=', $user->id)->whereNotNull('approved_by')->count();

$peers = Peer::with(['user'])->where('user_id', '=', $user->id)->latest('seeder')->get();

return \view('user.profile', [
'route' => 'profile',
'user' => $user,
Expand All @@ -106,6 +108,7 @@ public function show($username): \Illuminate\Contracts\View\Factory | \Illuminat
'requested' => $requested,
'filled' => $filled,
'invitedBy' => $invitedBy,
'peers' => $peers,
]);
}

Expand Down Expand Up @@ -2058,4 +2061,41 @@ public function flushOwnGhostPeers(Request $request, $username): \Illuminate\Htt

return \redirect()->back()->withSuccess('Peers were flushed successfully!');
}

/**
* Get A Users Active Table by IP and Port.
*
* @param \App\Models\User $username
* @param \App\Models\Peer $ip
* @param \App\Models\Peer $port
*/
public function activeByClient(Request $request, $username, $ip, $port): \Illuminate\Contracts\View\Factory | \Illuminate\View\View
{
$user = User::where('username', '=', $username)->firstOrFail();

\abort_unless($request->user()->group->is_modo || $request->user()->id == $user->id, 403);

$hisUpl = History::where('user_id', '=', $user->id)->sum('actual_uploaded');
$hisUplCre = History::where('user_id', '=', $user->id)->sum('uploaded');
$hisDownl = History::where('user_id', '=', $user->id)->sum('actual_downloaded');
$hisDownlCre = History::where('user_id', '=', $user->id)->sum('downloaded');

$active = Peer::with(['torrent' => function ($query) {
$query->withAnyStatus();
}])->sortable(['created_at' => 'desc'])
->where('user_id', '=', $user->id)
->where('ip', '=', $ip)
->where('port', '=', $port)
->distinct('info_hash')
->paginate(50);

return \view('user.private.active', ['user' => $user,
'route' => 'active',
'active' => $active,
'his_upl' => $hisUpl,
'his_upl_cre' => $hisUplCre,
'his_downl' => $hisDownl,
'his_downl_cre' => $hisDownlCre,
]);
}
}
1 change: 1 addition & 0 deletions app/Jobs/ProcessBasicAnnounceRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ public function handle()
$peer->left = $this->queries['left'];
$peer->torrent_id = $this->torrent->id;
$peer->user_id = $this->user->id;
$peer->updateConnectableStateIfNeeded();
$peer->save();
// End Peer Update

Expand Down
1 change: 1 addition & 0 deletions app/Jobs/ProcessCompletedAnnounceRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ public function handle()
$peer->left = 0;
$peer->torrent_id = $this->torrent->id;
$peer->user_id = $this->user->id;
$peer->updateConnectableStateIfNeeded();
$peer->save();
// End Peer Update

Expand Down
1 change: 1 addition & 0 deletions app/Jobs/ProcessStartedAnnounceRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public function handle()
$peer->left = $this->queries['left'];
$peer->torrent_id = $this->torrent->id;
$peer->user_id = $this->user->id;
$peer->updateConnectableStateIfNeeded();
$peer->save();
// End Peer Update

Expand Down
1 change: 1 addition & 0 deletions app/Jobs/ProcessStoppedAnnounceRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ public function handle()
$peer->left = $this->queries['left'];
$peer->torrent_id = $this->torrent->id;
$peer->user_id = $this->user->id;
$peer->updateConnectableStateIfNeeded();
$peer->save();
// End Peer Update

Expand Down
22 changes: 22 additions & 0 deletions app/Models/Peer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Kyslik\ColumnSortable\Sortable;
use Predis\Client as RedisClient;

/**
* App\Models\Peer.
Expand All @@ -35,6 +36,7 @@
* @property \Illuminate\Support\Carbon|null $updated_at
* @property int|null $torrent_id
* @property int|null $user_id
* @property bool $connectable
* @property-read \App\Models\Torrent $seed
* @property-read \App\Models\Torrent|null $torrent
* @property-read \App\Models\User|null $user
Expand Down Expand Up @@ -112,4 +114,24 @@ public function seed()
{
return $this->belongsTo(Torrent::class, 'torrents.id', 'torrent_id');
}

/**
* Updates Connectable State If Needed.
*
* @var resource
*/
public function updateConnectableStateIfNeeded()
{
$redis = new RedisClient();
if (! $redis->get('peers:connectable:'.$this->id)) {
$con = @fsockopen($this->ip, $this->port, $_, $_, 1);

$this->connectable = is_resource($con);
$redis->setex('peers:connectable:'.$this->id, config('announce.connectable_check_interval'), $this->connectable ? 'yes' : 'no');

if (is_resource($con)) {
fclose($con);
}
}
}
}
11 changes: 11 additions & 0 deletions config/announce.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,15 @@

'rate_limit' => 3,

/*
|--------------------------------------------------------------------------
| Connectable check interval
|--------------------------------------------------------------------------
|
| Amount Of Time until the next connectable check
|
*/

'connectable_check_interval' => 60 * 20,

];
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddConnectableStateToPeersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('peers', function (Blueprint $table) {
$table->boolean('connectable')->default(0);
});
}
}
2 changes: 2 additions & 0 deletions resources/lang/de/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@
'change-email-help' => 'Du musst dein Konto erneut bestätigen, nachdem du deine E-Mail-Adresse geändert hast',
'change-password' => 'Ändere das Passwort',
'change-password-help' => 'Du musst dich erneut anmelden, nachdem du dein Passwort geändert hast',
'client-connectable-state' => '{0}Nein|{1}Ja',
'client-list' => 'Clients und IP-Adressen',
'client-ip-address' => 'Client-IP-Adresse',
'code' => 'Code',
'comments' => 'Kommentare',
Expand Down
2 changes: 2 additions & 0 deletions resources/lang/en/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@
'change-email-help' => 'You will have to re-confirm your account, after you change your email',
'change-password' => 'Change Password',
'change-password-help' => 'You will have to login again, after you change your password',
'client-connectable-state' => '{0}No|{1}Yes',
'client-list' => 'Clients and IP-Addresses',
'client-ip-address' => 'Client IP Address',
'code' => 'Code',
'comments' => 'Comments',
Expand Down
51 changes: 51 additions & 0 deletions resources/views/user/profile.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,57 @@ class="{{ config('other.font-awesome') }} fa-coins"></i> @lang('user.certified-b
</div>
</div>
@if (auth()->user()->id == $user->id || auth()->user()->group->is_modo)
<div class="block">
<h3><i class="{{ config('other.font-awesome') }} fa-broadcast-tower"></i> @lang('user.client-list')</h3>
<div style="word-wrap: break-word; display: table; width: 100%;">
<table class="table table-condensed table-striped table-bordered">
<thead>
<tr>
<th>@lang('torrent.client')</th>
<th>@lang('common.ip')</th>
<th>@lang('common.port')</th>
<th>@lang('torrent.started')</th>
<th>@lang('torrent.last-update')</th>
<th>@lang('torrent.torrents')</th>
<th>Connectable</th>
</tr>
</thead>
<tbody>
@php $peer_array = []; @endphp
@foreach ($peers as $p)
@if (!in_array([$p->ip, $p->port], $peer_array))
@php $count = App\Models\Peer::with(['user'])->where('user_id', '=', $user->id)->latest('seeder')->where('ip', '=', $p->ip)->where('port', '=', $p->port)->count(); @endphp
<tr>
<td>
<span class="badge-extra text-purple text-bold">{{ $p->agent }}</span>
</td>
@if (auth()->user()->group->is_modo || auth()->user()->id == $p->user_id)
<td><span class="badge-extra text-bold">{{ $p->ip }}</span></td>
<td><span class="badge-extra text-bold">{{ $p->port }}</span></td>
@else
<td> ---</td>
<td> ---</td>
@endif
<td>{{ $p->created_at ? $p->created_at->diffForHumans() : 'N/A' }}</td>
<td>{{ $p->updated_at ? $p->updated_at->diffForHumans() : 'N/A' }}</td>
@if (auth()->user()->group->is_modo || auth()->user()->id == $p->user_id)
<td>
<a href="{{ route('user_active_by_client', ['username' => $user->username, 'ip' => $p->ip, 'port' => $p->port]) }}" itemprop="url" class="l-breadcrumb-item-link">
<span itemprop="title" class="l-breadcrumb-item-link-title">{{ $count }}</span>
</a>
</td>
@else
<td> ---</td>
@endif
<td>@choice('user.client-connectable-state', $p->connectable)</td>
</tr>
@php array_push($peer_array, [$p->ip, $p->port]); @endphp
@endif
@endforeach
</tbody>
</table>
</div>
</div>
<div class="block">
<h3><i class="{{ config('other.font-awesome') }} fa-lock"></i> @lang('user.private-info')</h3>
<div style="word-wrap: break-word; display: table; width: 100%;">
Expand Down
1 change: 1 addition & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@
Route::get('/{username}/resurrections', [App\Http\Controllers\UserController::class, 'resurrections'])->name('user_resurrections');
Route::get('/{username}/requested', [App\Http\Controllers\UserController::class, 'requested'])->name('user_requested');
Route::get('/{username}/active', [App\Http\Controllers\UserController::class, 'active'])->name('user_active');
Route::get('/{username}/activeByClient/{ip}/{port}', [App\Http\Controllers\UserController::class, 'activeByClient'])->name('user_active_by_client');
Route::get('/{username}/torrents', [App\Http\Controllers\UserController::class, 'torrents'])->name('user_torrents');
Route::get('/{username}/uploads', [App\Http\Controllers\UserController::class, 'uploads'])->name('user_uploads');
Route::get('/{username}/downloads', [App\Http\Controllers\UserController::class, 'downloads'])->name('user_downloads');
Expand Down