Skip to content

Commit

Permalink
Merge pull request #2627 from RaiderIO/development
Browse files Browse the repository at this point in the history
Release v11.7.2 - Map menu layout improvements on mobile
  • Loading branch information
Wotuu authored Nov 22, 2024
2 parents 392da14 + dd96003 commit 3051632
Show file tree
Hide file tree
Showing 36 changed files with 748 additions and 171 deletions.
10 changes: 6 additions & 4 deletions app/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,13 @@ class Handler extends ExceptionHandler
*/
public function report(Throwable $e): void
{
$handlerLogging = app()->make(HandlerLoggingInterface::class);
if (app()->has(HandlerLoggingInterface::class)) {
$handlerLogging = app()->make(HandlerLoggingInterface::class);

if ($e instanceof TooManyRequestsHttpException) {
$user = Auth::user();
$handlerLogging->tooManyRequests(request()?->ip() ?? 'unknown IP', $user?->id, $user?->name, $e);
if ($e instanceof TooManyRequestsHttpException) {
$user = Auth::user();
$handlerLogging->tooManyRequests(request()?->ip() ?? 'unknown IP', request()?->path(), $user?->id, $user?->name, $e);
}
}

parent::report($e);
Expand Down
2 changes: 1 addition & 1 deletion app/Exceptions/Logging/HandlerLogging.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class HandlerLogging extends RollbarStructuredLogging implements HandlerLoggingInterface
{
public function tooManyRequests(string $ip, ?int $userId, ?string $username, Throwable $throwable): void
public function tooManyRequests(string $ip, string $uri, ?int $userId, ?string $username, Throwable $throwable): void
{
$this->error(__METHOD__, get_defined_vars());
}
Expand Down
2 changes: 1 addition & 1 deletion app/Exceptions/Logging/HandlerLoggingInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

interface HandlerLoggingInterface
{
public function tooManyRequests(string $ip, ?int $userId, ?string $username, \Throwable $throwable): void;
public function tooManyRequests(string $ip, string $uri, ?int $userId, ?string $username, \Throwable $throwable): void;
}
5 changes: 5 additions & 0 deletions app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
use App\Http\Middleware\OnlyAjax;
use App\Http\Middleware\ReadOnlyMode;
use App\Http\Middleware\RedirectIfAuthenticated;
use App\Http\Middleware\TracksUserIpAddress;
use App\Http\Middleware\TrimStrings;
use App\Http\Middleware\TrustProxies;
use App\Http\Middleware\VerifyCsrfToken;
use App\Http\Middleware\ViewCacheBuster;
use BeyondCode\ServerTiming\Middleware\ServerTimingMiddleware;
Expand Down Expand Up @@ -58,6 +60,7 @@ class Kernel extends HttpKernel
ShareErrorsFromSession::class,
VerifyCsrfToken::class,
SubstituteBindings::class,
TrustProxies::class,
],

'api' => [
Expand All @@ -66,6 +69,7 @@ class Kernel extends HttpKernel
'debug_info_context_logger' => DebugInfoContextLogger::class,
'read_only_mode' => ReadOnlyMode::class,
'authentication' => ApiAuthentication::class,
TrustProxies::class,
],
];

Expand All @@ -89,5 +93,6 @@ class Kernel extends HttpKernel
'debugbarmessagelogger' => DebugBarMessageLogger::class,
'debug_info_context_logger' => DebugInfoContextLogger::class,
'read_only_mode' => ReadOnlyMode::class,
'track_ip' => TracksUserIpAddress::class,
];
}
38 changes: 38 additions & 0 deletions app/Http/Middleware/TracksUserIpAddress.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace App\Http\Middleware;

use App\Models\User;
use App\Models\UserIpAddress;
use Auth;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Symfony\Component\HttpFoundation\Response;

class TracksUserIpAddress
{
/**
* Handle an incoming request.
*/
public function handle(Request $request, Closure $next): Response
{
// Maybe this should be handled differently? Idk how heavy these queries will be
if (!$request->ajax() && Auth::check()) {
/** @var User $user */
$user = Auth::user();
UserIpAddress::upsert(
[
'user_id' => $user->id,
'ip_address' => $request->ip(),
'count' => 1, // Default value for new rows
'updated_at' => now(), // Example of tracking when a row is updated
],
['user_id', 'ip_address'],
['count' => DB::raw('count + 1'), 'updated_at'] // Update these columns if a conflict occurs
);
}

return $next($request);
}
}
22 changes: 22 additions & 0 deletions app/Http/Middleware/TrustProxies.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace App\Http\Middleware;

use App\Service\Cloudflare\CloudflareServiceInterface;
use Closure;
use Illuminate\Http\Middleware\TrustProxies as Middleware;
use Illuminate\Http\Request;

Expand All @@ -25,4 +27,24 @@ class TrustProxies extends Middleware
Request::HEADER_X_FORWARDED_PORT |
Request::HEADER_X_FORWARDED_PROTO |
Request::HEADER_X_FORWARDED_AWS_ELB;

public function __construct(
private readonly CloudflareServiceInterface $cloudflareService
) {
}

public function handle(Request $request, Closure $next)
{
// https://developers.cloudflare.com/fundamentals/reference/http-request-headers/
if (app()->isProduction()) {
// Ensure that we know the original IP address that made the request
// https://khalilst.medium.com/get-real-client-ip-behind-cloudflare-in-laravel-189cb89059ff
Request::setTrustedProxies(
$this->cloudflareService->getIpRanges(),
$this->headers
);
}

return parent::handle($request, $next);
}
}
7 changes: 5 additions & 2 deletions app/Models/Team.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;

Expand All @@ -23,6 +24,9 @@
* @property string $invite_code
* @property string $default_role
*
* @property Carbon $updated_at
* @property Carbon $created_at
*
* @property Collection<TeamUser> $teamUsers
* @property Collection<User> $members
* @property Collection<DungeonRoute> $dungeonroutes
Expand All @@ -32,13 +36,12 @@
class Team extends Model
{
use HasIconFile;
use GeneratesPublicKey;

protected $visible = ['name', 'description', 'public_key'];

protected $fillable = ['default_role'];

use GeneratesPublicKey;

/**
* https://stackoverflow.com/a/34485411/771270
*/
Expand Down
37 changes: 37 additions & 0 deletions app/Models/UserIpAddress.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace App\Models;

use Eloquent;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Carbon;

/**
* @property int $id
* @property int $user_id
* @property string $ip_address
* @property int $count
*
* @property Carbon $updated_at
* @property Carbon $created_at
*
* @property User $user
*
* @mixin Eloquent
*/
class UserIpAddress extends Model
{
protected $fillable = [
'user_id',
'ip_address',
'count',
'updated_at',
'created_at',
];

public function user(): BelongsTo
{
return $this->belongsTo(User::class, 'user_id');
}
}
5 changes: 3 additions & 2 deletions app/Models/UserReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
* @property string $message
* @property bool $contact_ok
* @property string $status
* @property User $author
*
* @property User $user
*
* @mixin Eloquent
*/
Expand All @@ -27,6 +28,6 @@ class UserReport extends Model

public function user(): BelongsTo
{
return $this->belongsTo(User::class, 'user_id');
return $this->belongsTo(User::class);
}
}
21 changes: 21 additions & 0 deletions app/Overrides/CustomRateLimiter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Overrides;

use Illuminate\Cache\RateLimiter as BaseRateLimiter;

class CustomRateLimiter extends BaseRateLimiter
{
public function __construct($cache)
{
parent::__construct($cache);
}

public function cleanRateLimiterKey($key): string
{
// Add a custom prefix specifically for rate limiter keys
$prefix = 'rate-limiter:';

return $prefix . parent::cleanRateLimiterKey($key);
}
}
24 changes: 13 additions & 11 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

use App\Models\Release;
use App\Models\User;
use App\Overrides\CustomRateLimiter;
use App\Service\Cloudflare\CloudflareServiceInterface;
use Auth;
use Illuminate\Cache\RateLimiter;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\ServiceProvider;
use Rollbar\Payload\Level;
use Rollbar\Rollbar;
Expand All @@ -16,8 +18,10 @@ class AppServiceProvider extends ServiceProvider
/**
* Bootstrap any application services.
*/
public function boot(): void
{
public function boot(
CloudflareServiceInterface $cloudflareService
): void {

Model::preventLazyLoading(!app()->isProduction());

/** @var User|null $user */
Expand All @@ -41,20 +45,18 @@ public function boot(): void
'correlationId' => correlationId(),
],
]);

// Ensure that we know the original IP address that made the request
// https://khalilst.medium.com/get-real-client-ip-behind-cloudflare-in-laravel-189cb89059ff
Request::setTrustedProxies(
['REMOTE_ADDR'],
Request::HEADER_X_FORWARDED_FOR
);
}

/**
* Register any application services.
*/
public function register(): void
{
//
// Bind our custom rate limiter
$this->app->extend(RateLimiter::class, function ($command, $app) {
return new CustomRateLimiter($app->make('cache')->driver(
$app['config']->get('cache.limiter')
));
});
}
}
3 changes: 3 additions & 0 deletions app/Providers/KeystoneGuruServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
use App\Service\Cache\DevCacheService;
use App\Service\ChallengeModeRunData\ChallengeModeRunDataService;
use App\Service\ChallengeModeRunData\ChallengeModeRunDataServiceInterface;
use App\Service\Cloudflare\CloudflareService;
use App\Service\Cloudflare\CloudflareServiceInterface;
use App\Service\CombatLog\CombatLogDataExtractionService;
use App\Service\CombatLog\CombatLogDataExtractionServiceInterface;
use App\Service\CombatLog\CombatLogMappingVersionService;
Expand Down Expand Up @@ -207,6 +209,7 @@ public function register(): void
$this->app->bind(WowheadServiceInterface::class, WowheadService::class);
// $this->app->bind(RaiderIOApiServiceInterface::class, RaiderIOApiService::class);
$this->app->bind(RaiderIOApiServiceInterface::class, RaiderIOKeystoneGuruApiService::class);
$this->app->bind(CloudflareServiceInterface::class, CloudflareService::class);

// Depends on CombatLogService, SeasonService, WowheadService
$this->app->bind(CombatLogDataExtractionServiceInterface::class, CombatLogDataExtractionService::class);
Expand Down
5 changes: 5 additions & 0 deletions app/Providers/LoggingServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use App\Service\Cache\Logging\CacheServiceLoggingInterface;
use App\Service\ChallengeModeRunData\Logging\ChallengeModeRunDataServiceLogging;
use App\Service\ChallengeModeRunData\Logging\ChallengeModeRunDataServiceLoggingInterface;
use App\Service\Cloudflare\Logging\CloudflareServiceLogging;
use App\Service\Cloudflare\Logging\CloudflareServiceLoggingInterface;
use App\Service\CombatLog\Builders\Logging\CreateRouteBodyCombatLogEventsBuilderLogging;
use App\Service\CombatLog\Builders\Logging\CreateRouteBodyCombatLogEventsBuilderLoggingInterface;
use App\Service\CombatLog\Builders\Logging\CreateRouteBodyCorrectionBuilderLogging;
Expand Down Expand Up @@ -99,6 +101,9 @@ public function register(): void
// Challenge Mode Run Data
$this->app->bind(ChallengeModeRunDataServiceLoggingInterface::class, ChallengeModeRunDataServiceLogging::class);

// Cloudflare
$this->app->bind(CloudflareServiceLoggingInterface::class, CloudflareServiceLogging::class);

// Combat log
/// Builders
$this->app->bind(DungeonRouteBuilderLoggingInterface::class, DungeonRouteBuilderLogging::class);
Expand Down
9 changes: 5 additions & 4 deletions app/Providers/RouteServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class RouteServiceProvider extends ServiceProvider
*/
public function boot(): void
{
//
$this->configureRateLimiting();

parent::boot();
}
Expand All @@ -32,9 +32,6 @@ public function map(): void
$this->mapApiRoutes();

$this->mapWebRoutes();

//
$this->configureRateLimiting();
}


Expand Down Expand Up @@ -101,6 +98,9 @@ protected function configureRateLimiting(): void

private function noLimitForExemptions(Request $request): ?Limit
{
// Temporarily disable this!
return Limit::none();

/** @var User|null $user */
$user = $request->user();

Expand All @@ -115,6 +115,7 @@ private function userKey(Request $request): string
{
/** @var User|null $user */
$user = $request->user();

return $user?->id ?: $request->ip();
}
}
Loading

0 comments on commit 3051632

Please sign in to comment.