Skip to content

Commit

Permalink
Merge pull request #2828 from Roardom/client-blacklist
Browse files Browse the repository at this point in the history
(Update) use peer id for client blacklist instead of user agent
  • Loading branch information
HDVinnie committed Jan 3, 2024
2 parents 8038155 + a37e201 commit b544437
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 11 deletions.
10 changes: 7 additions & 3 deletions app/Http/Controllers/AnnounceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,14 @@ private function checkClient(Request $request): void
}

// Block Blacklisted Clients
$clientBlacklist = cache()->rememberForever('client_blacklist', fn () => BlacklistClient::pluck('name')->toArray());
$blacklistedPeerIdPrefixes = cache()->rememberForever('client_blacklist', fn () => BlacklistClient::pluck('peer_id_prefix')->toArray());

if (\in_array($userAgent, $clientBlacklist)) {
throw new TrackerException(128, [':ua' => $request->header('User-Agent')]);
$peerId = $request->query->get('peer_id');

foreach ($blacklistedPeerIdPrefixes as $blacklistedPeerIdPrefix) {
if (str_starts_with($peerId, $blacklistedPeerIdPrefix)) {
throw new TrackerException(128, [':ua' => $request->header('User-Agent')]);
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions app/Http/Requests/Staff/StoreBlacklistClientRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public function rules(): array
'string',
'unique:blacklist_clients',
],
'peer_id_prefix' => [
'required',
'string',
'unique:blacklist_clients',
],
'reason' => [
'sometimes',
'string',
Expand Down
5 changes: 5 additions & 0 deletions app/Http/Requests/Staff/UpdateBlacklistClientRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public function rules(): array
'string',
'max:255',
],
'peer_id_prefix' => [
'required',
'string',
'unique:blacklist_clients',
],
'reason' => [
'required',
'string',
Expand Down
5 changes: 3 additions & 2 deletions database/factories/BlacklistClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ class BlacklistClientFactory extends Factory
public function definition(): array
{
return [
'name' => $this->faker->unique()->name(),
'reason' => $this->faker->text(),
'name' => $this->faker->unique()->name(),
'reason' => $this->faker->text(),
'peer_id_prefix' => $this->faker->unique()->text(5),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

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

return new class () extends Migration {
public function up(): void
{
Schema::table('blacklist_clients', function (Blueprint $table): void {
$table->string('peer_id_prefix');
});
}
};
13 changes: 13 additions & 0 deletions resources/views/Staff/blacklist/clients/create.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,19 @@ class="form__text"
{{ __('common.name') }}
</label>
</p>
<p class="form__group">
<input
id="peer_id_prefix"
class="form__text"
name="peer_id_prefix"
required
type="text"
value="{{ old('peer_id_prefix') }}"
/>
<label class="form__label form__label--floating" for="peer_id_prefix">
Peer ID Prefix
</label>
</p>
<p class="form__group">
<input
id="reason"
Expand Down
13 changes: 13 additions & 0 deletions resources/views/Staff/blacklist/clients/edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,19 @@ class="form__text"
{{ __('common.name') }}
</label>
</p>
<p class="form__group">
<input
id="peer_id_prefix"
class="form__text"
name="peer_id_prefix"
required
type="text"
value="{{ $client->peer_id_prefix }}"
/>
<label class="form__label form__label--floating" for="peer_id_prefix">
Peer ID Prefix
</label>
</p>
<p class="form__group">
<input
id="reason"
Expand Down
4 changes: 2 additions & 2 deletions resources/views/Staff/blacklist/clients/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<header class="panel__header">
<h2 class="panel__heading">Blacklisted Clients</h2>
<div class="panel__actions">
<div class="panel_action">
<div class="panel__action">
<a
href="{{ route('staff.blacklisted_clients.create') }}"
class="form__button form__button--text"
Expand All @@ -28,7 +28,7 @@ class="form__button form__button--text"
<table class="data-table">
<thead>
<tr>
<th>{{ __('common.name') }}</th>
<th>Peer ID prefix</th>
<th>{{ __('common.reason') }}</th>
<th>{{ __('common.actions') }}</th>
</tr>
Expand Down
7 changes: 7 additions & 0 deletions resources/views/page/blacklist/client.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,16 @@
<h2 class="panel__heading">{{ __('page.blacklist-clients') }}</h2>
<div class="data-table-wrapper">
<table class="data-table">
<thead>
<tr>
<th>Client Name</th>
<th>Reason</th>
</tr>
</thead>
@foreach ($clients as $client)
<tr>
<td>{{ $client->name }}</td>
<td>{{ $client->reason }}</td>
</tr>
@endforeach
</table>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@

test('store returns an ok response', function (): void {
$response = $this->actingAs($this->staffUser)->post(route('staff.blacklisted_clients.store'), [
'name' => 'Test Name',
'reason' => 'Test Reason',
'name' => 'Test Name',
'reason' => 'Test Reason',
'peer_id_prefix' => 'Test Peer ID Prefix',
]);
$response->assertRedirect(route('staff.blacklisted_clients.index'));
$response->assertSessionHas('success', 'Blacklisted Client Stored Successfully!');
Expand All @@ -90,8 +91,9 @@
$blacklistClient = BlacklistClient::factory()->create();

$response = $this->actingAs($this->staffUser)->patch(route('staff.blacklisted_clients.update', [$blacklistClient]), [
'name' => 'Test Name Updated',
'reason' => 'Test Reason Updated',
'name' => 'Test Name Updated',
'reason' => 'Test Reason Updated',
'peer_id_prefix' => 'Test Peer ID Prefix Updated',
]);
$response->assertRedirect(route('staff.blacklisted_clients.index'));
$response->assertSessionHas('success', 'Blacklisted Client Was Updated Successfully!');
Expand Down

0 comments on commit b544437

Please sign in to comment.