Laravel Cloudflare

monicahq/laravel-cloudflare image

Laravel Cloudflare stats

Downloads
1.1M
Stars
219
Open Issues
3
Forks
28

View on GitHub →

Add Cloudflare ip addresses to trusted proxies for Laravel.

Trust Cloudflare's Proxies for Laravel

Add Cloudflare ip addresses to trusted proxies for Laravel.

Installation

  1. Install package using composer:
composer require monicahq/laravel-cloudflare

You don't need to add this package to your service providers.

  1. Configure Middleware
  • Option 1: Add Cloudflare TrustProxies middleware and remove default one

Replace TrustProxies middleware in app/Http/Kernel.php, by modifying the middleware array:

protected $middleware = [
- \App\Http\Middleware\TrustProxies::class,
+ \Monicahq\Cloudflare\Http\Middleware\TrustProxies::class
...
  • Option 2: Extend current middleware to use Cloudflare TrustProxies middleware

Another option is to extend the App\Http\Middleware\TrustProxies class to Monicahq\Cloudflare\Http\Middleware\TrustProxies:

namespace App\Http\Middleware;
 
use Illuminate\Http\Request;
- use Illuminate\Http\Middleware\TrustProxies as Middleware;
+ use Monicahq\Cloudflare\Http\Middleware\TrustProxies as Middleware;
 
class TrustProxies extends Middleware
{
...

Custom proxies callback

You can define your own proxies callback by calling the LaravelCloudflare::getProxiesUsing() to change the behavior of the LaravelCloudflare::getProxies() method. This method should typically be called in the boot method of your AppServiceProvider class:

use Monicahq\Cloudflare\LaravelCloudflare;
use Monicahq\Cloudflare\Facades\CloudflareProxies;
 
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
LaravelCloudflare::getProxiesUsing(fn() => CloudflareProxies::load());
}

How it works

The middleware uses Illuminate\Http\Middleware\TrustProxies as a backend.

When the cloudflare ips are detected, they are used as trusted proxies.

Refreshing the Cache

This package retrieves Cloudflare's IP blocks, and stores them in cache. When request comes, the middleware will get Cloudflare's IP blocks from cache, and load them as trusted proxies.

You'll need to refresh the cloudflare cache regularely to always have up to date proxy.

Use the cloudflare:reload artisan command to refresh the IP blocks:

php artisan cloudflare:reload

Suggestion: add the command in the schedule

Add a new line in app/Console/Kernel.php, in the schedule function:

$schedule->command('cloudflare:reload')->daily();

View current Cloudflare's IP blocks

You can use the cloudflare:view artisan command to see the cached IP blocks:

php artisan cloudflare:view

Option: publish the package config file

If you want, you can publish the package config file to config/laravelcloudflare.php:

php artisan vendor:publish --provider="Monicahq\Cloudflare\TrustedProxyServiceProvider"

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
5.x-6.x <= 1.8.0
7.x-8.53 2.0.0
8.54-9.x >= 3.0.0

Citations

This package was inspired by lukasz-adamski/laravel-cloudflare and forked from ogunkarakus/laravel-cloudflare.

License

Author: Alexis Saettler

This project is part of MonicaHQ.

Copyright © 2019–2022.

Licensed under the MIT License. View license.

monicahq photo

A Personal Relationship Management tool to help you document your social life

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.


Monicahq Laravel Cloudflare Related Articles

Laravel Roundup - November image

Laravel Roundup - November

Read article
Monica is a Laravel Powered Personal Relationship Manager image

Monica is a Laravel Powered Personal Relationship Manager

Read article
The Certification of Competence for Laravel logo

The Certification of Competence for Laravel

A community-driven, proctored assessment across 4 levels designed to validate real-world Laravel knowledge, from Junior to mastery-level Artisan. Official Vue.js, Official Nuxt, Angular, React, JS certifications also available.

The Certification of Competence for Laravel
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
Securing Laravel logo

Securing Laravel

The essential security resource for Laravel devs, covering everything you need to keep your apps secure. Sign up to receive weekly security tips and monthly in depth articles, diving deep into security concepts you need to know!

Securing Laravel
Statamic logo

Statamic

The drop-in ready Laravel CMS you’re been waiting for. Go full-stack or headless, flat file or database – it’s up to you.

Statamic
Blastup logo

Blastup

Blastup provides social media enhancement services including buying Instagram likes, followers, and views, with features like instant delivery and a variety of packages to suit different needs.

Blastup