Skip to content

Commit

Permalink
Pull from url innstead of hardcoded
Browse files Browse the repository at this point in the history
  • Loading branch information
RMartinOscar committed Oct 31, 2024
1 parent d87a13b commit f44eb21
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 25 deletions.
3 changes: 2 additions & 1 deletion app/Filament/Pages/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Models\Backup;
use App\Notifications\MailTested;
use App\Services\Helpers\TrustedProxyService;
use App\Traits\EnvironmentWriterTrait;
use Exception;
use Filament\Actions\Action;
Expand Down Expand Up @@ -159,7 +160,7 @@ 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', config('trustedproxy.cloudflare'))),
->action(fn (Set $set, TrustedProxyService $service) => $set('TRUSTED_PROXIES', $service->handle())),
]),
];
}
Expand Down
42 changes: 42 additions & 0 deletions app/Services/Helpers/TrustedProxyService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace App\Services\Helpers;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;

class TrustedProxyService
{
/**
* TrustedProxyService constructor.
*/
public function __construct(
protected Client $client
) {
}

/**
* Get provider ips list from given url
*/
public function handle(): array
{
$ips = collect();
try {
$response = $this->client->request(
'GET',
config('trustedproxy.auto.url'),
config('panel.guzzle')
);
if ($response->getStatusCode() === 200) {
$result = json_decode($response->getBody(), true);
foreach (config('trustedproxy.auto.keys') as $value) {
$ips->push(...data_get($result, $value));
}
$ips->unique();
}
} catch (GuzzleException $e) {
}

return $ips->values()->all();
}
}
33 changes: 9 additions & 24 deletions config/trustedproxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,14 @@
'proxies' => in_array(env('TRUSTED_PROXIES', []), ['*', '**']) ?
env('TRUSTED_PROXIES') : explode(',', env('TRUSTED_PROXIES') ?? ''),

'cloudflare' => [
'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',

'2400:cb00::/32',
'2606:4700::/32',
'2803:f800::/32',
'2405:b500::/32',
'2405:8100::/32',
'2a06:98c0::/29',
'2c0f:f248::/32',
/*
* Automatically pull ips from url
*/
'auto' => [
'url' => 'https://api.cloudflare.com/client/v4/ips',
'keys' => [
'result.ipv4_cidrs',
'result.ipv6_cidrs',
],
],
];

0 comments on commit f44eb21

Please sign in to comment.