From 340ae8099baeb8b6d744b3853b3278dfb2306000 Mon Sep 17 00:00:00 2001 From: MartinOscar <40749467+RMartinOscar@users.noreply.github.com> Date: Fri, 1 Nov 2024 23:16:59 +0100 Subject: [PATCH] Fix trusted proxies settings & Move ips to config & Add ipv6 (#692) * Fix blank proxy & Move hardcoded cloudflare ips * Add cloudflare's ipv6 * Pull from url innstead of hardcoded * Remove Service --- app/Filament/Pages/Settings.php | 41 ++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/app/Filament/Pages/Settings.php b/app/Filament/Pages/Settings.php index a5c3572791..a5630bc7fa 100644 --- a/app/Filament/Pages/Settings.php +++ b/app/Filament/Pages/Settings.php @@ -25,6 +25,8 @@ use Filament\Pages\Concerns\HasUnsavedDataChangesAlert; use Filament\Pages\Concerns\InteractsWithHeaderActions; use Filament\Pages\Page; +use GuzzleHttp\Client; +use GuzzleHttp\Exception\GuzzleException; use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\Notification as MailNotification; use Illuminate\Support\HtmlString; @@ -149,7 +151,7 @@ private function generalSettings(): array ->separator() ->splitKeys(['Tab', ' ']) ->placeholder('New IP or IP Range') - ->default(env('TRUSTED_PROXIES', config('trustedproxy.proxies'))) + ->default(env('TRUSTED_PROXIES', implode(',', config('trustedproxy.proxies')))) ->hintActions([ FormAction::make('clear') ->label('Clear') @@ -162,23 +164,26 @@ private function generalSettings(): array ->label('Set to Cloudflare IPs') ->icon('tabler-brand-cloudflare') ->authorize(fn () => auth()->user()->can('update settings')) - ->action(fn (Set $set) => $set('TRUSTED_PROXIES', [ - '173.245.48.0/20', - '103.21.244.0/22', - '103.22.200.0/22', - '103.31.4.0/22', - '141.101.64.0/18', - '108.162.192.0/18', - '190.93.240.0/20', - '188.114.96.0/20', - '197.234.240.0/22', - '198.41.128.0/17', - '162.158.0.0/15', - '104.16.0.0/13', - '104.24.0.0/14', - '172.64.0.0/13', - '131.0.72.0/22', - ])), + ->action(function (Client $client, Set $set) { + $ips = collect(); + try { + $response = $client->request( + 'GET', + 'https://api.cloudflare.com/client/v4/ips', + config('panel.guzzle') + ); + if ($response->getStatusCode() === 200) { + $result = json_decode($response->getBody(), true)['result']; + foreach (['ipv4_cidrs', 'ipv6_cidrs'] as $value) { + $ips->push(...data_get($result, $value)); + } + $ips->unique(); + } + } catch (GuzzleException $e) { + } + + $set('TRUSTED_PROXIES', $ips->values()->all()); + }), ]), ]; }