Skip to content

Commit

Permalink
feat: add a laravelcloudflare.enabled config (#254)
Browse files Browse the repository at this point in the history
  • Loading branch information
asbiin authored Aug 13, 2022
1 parent 94a7df8 commit 8031864
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 8 deletions.
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ use Monicahq\Cloudflare\Facades\CloudflareProxies;
*/
public function boot()
{
LaravelCloudflare::getProxiesUsing(function() {
return CloudflareProxies::load();
});
LaravelCloudflare::getProxiesUsing(fn() => CloudflareProxies::load());
}
```

Expand Down Expand Up @@ -117,6 +115,17 @@ php artisan vendor:publish --provider="Monicahq\Cloudflare\TrustedProxyServicePr

This file contains some configurations, but you may not need to change them normally.

## Running tests for your package

When running tests for your package, you generally don't need to get Cloudflare's proxy addresses.
You can deactivate the Laravel Cloudflare middleware by adding the following environment variable in
your `.env` or `phpunit.xml` file:

```
LARAVEL_CLOUDFLARE_ENABLED=false
```


# Compatibility

| Laravel | [monicahq/laravel-cloudflare](https://github.com/monicahq/laravel-cloudflare) |
Expand Down
12 changes: 12 additions & 0 deletions config/laravelcloudflare.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@
|
*/

'enabled' => (bool) env('LARAVEL_CLOUDFLARE_ENABLED', true),

/*
|--------------------------------------------------------------------------
| Name of the cache to store values of the proxies
|--------------------------------------------------------------------------
|
| This value is the key used in the cache (table, redis, etc.) to store the
| values.
|
*/

'cache' => 'cloudflare.proxies',

/*
Expand Down
4 changes: 4 additions & 0 deletions src/Commands/Reload.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ class Reload extends Command
*/
public function handle(Cache $cache, Config $config)
{
if (! (bool) $config->get('laravelcloudflare.enabled')) {
return;
}

$proxies = LaravelCloudflare::getProxies();

$cache->store()->forever($config->get('laravelcloudflare.cache'), $proxies);
Expand Down
20 changes: 17 additions & 3 deletions src/Http/Middleware/TrustProxies.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,33 @@
class TrustProxies extends Middleware
{
/**
* Sets the trusted proxies on the request to the value of Cloudflare ips.
* Sets the trusted proxies on the request.
*
* @param \Illuminate\Http\Request $request
* @return void
*/
protected function setTrustedProxyIpAddresses(Request $request)
{
if ((bool) Config::get('laravelcloudflare.enabled')) {
$this->setTrustedProxyCloudflare($request);
}

parent::setTrustedProxyIpAddresses($request);
}

/**
* Sets the trusted proxies on the request to the value of Cloudflare ips.
*
* @param \Illuminate\Http\Request $request
* @return void
*/
protected function setTrustedProxyCloudflare(Request $request): void
{
$cacheKey = Config::get('laravelcloudflare.cache');
$cachedProxies = Cache::rememberForever($cacheKey, fn () => LaravelCloudflare::getProxies());

if (is_array($cachedProxies) && count($cachedProxies) > 0) {
$this->proxies = array_merge((array) $this->proxies, $cachedProxies);
}

parent::setTrustedProxyIpAddresses($request);
}
}
20 changes: 18 additions & 2 deletions tests/Unit/Commands/ReloadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ public function it_loads_proxies()

$this->artisan('cloudflare:reload')
->expectsOutput('Cloudflare\'s IP blocks have been reloaded.')
->assertExitCode(0);
->assertExitCode(0)
->run();

$this->assertTrue($this->app['cache']->has('cloudflare.proxies'));
$this->assertEquals(['expect'], $this->app['cache']->get('cloudflare.proxies'));
}

Expand All @@ -32,8 +34,22 @@ public function it_saves_address_in_cache()
'https://fake/ips-v6' => Http::response('::1/32', 200),
]);

$this->artisan('cloudflare:reload');
$this->artisan('cloudflare:reload')
->run();

$this->assertEquals(['0.0.0.0/20', '::1/32'], $this->app['cache']->get('cloudflare.proxies'));
}

/** @test */
public function it_deactivate_command()
{
config(['laravelcloudflare.enabled' => false]);

$this->artisan('cloudflare:reload')
->doesntExpectOutput('Cloudflare\'s IP blocks have been reloaded.')
->assertExitCode(0)
->run();

$this->assertFalse($this->app['cache']->has('cloudflare.proxies'));
}
}
15 changes: 15 additions & 0 deletions tests/Unit/Http/Middleware/TrustProxiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,19 @@ public function it_does_not_sets_trusted_proxies()

$this->assertEquals([], $proxies);
}

/** @test */
public function it_deactivates_middleware()
{
config(['laravelcloudflare.enabled' => false]);

$request = new Request();

$this->app->make(TrustProxies::class)->handle($request, fn () => null);

$proxies = $request->getTrustedProxies();

$this->assertEquals([], $proxies);
$this->assertFalse(Cache::has('cloudflare.proxies'));
}
}

0 comments on commit 8031864

Please sign in to comment.