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

Fix trusted proxies settings & Move ips to config & Add ipv6 #692

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
21 changes: 3 additions & 18 deletions 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 @@ -146,7 +147,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')
Expand All @@ -159,23 +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', [
'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(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();
RMartinOscar marked this conversation as resolved.
Show resolved Hide resolved
}
}
11 changes: 11 additions & 0 deletions config/trustedproxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,15 @@
*/
'proxies' => in_array(env('TRUSTED_PROXIES', []), ['*', '**']) ?
env('TRUSTED_PROXIES') : explode(',', env('TRUSTED_PROXIES') ?? ''),

/*
* Automatically pull ips from url
*/
'auto' => [
'url' => 'https://api.cloudflare.com/client/v4/ips',
'keys' => [
'result.ipv4_cidrs',
'result.ipv6_cidrs',
],
],
RMartinOscar marked this conversation as resolved.
Show resolved Hide resolved
];
Loading