Last active
February 18, 2025 10:33
-
-
Save tansautn/33dd9467627526727dde2485fe60163e to your computer and use it in GitHub Desktop.
Laravel cloudflare trust proxy middleware. Single class does everythings !
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * -------------------------------------------------------------------------- | |
| * | |
| * -------------------------------------------------------------------------- | |
| * @PROJECT : Lalita | |
| * @AUTHOR : Zuko <https://github.com/tansautn> | |
| * @LINK : https://www.zuko.pro/ | |
| * @FILE : TrustProxiesMiddleware.php | |
| * @CREATED : 18:33 , 13/Nov/2024 | |
| */ | |
| namespace App\Http\Middleware; | |
| use Closure; | |
| use Illuminate\Http\Middleware\TrustProxies; | |
| use Illuminate\Http\Request; | |
| use Illuminate\Support\Facades\Cache; | |
| /** | |
| * Class TrustProxiesMiddleware | |
| * | |
| * @package App\Http\Middleware | |
| */ | |
| class TrustProxiesMiddleware extends TrustProxies | |
| { | |
| /** | |
| * The trusted proxies for this application. | |
| * | |
| * @var array | |
| */ | |
| protected $proxies = [ | |
| // Proxies obtained from https://www.cloudflare.com/ips-v4 | |
| '103.21.244.0/22', | |
| '103.22.200.0/22', | |
| '103.31.4.0/22', | |
| '104.16.0.0/12', | |
| '108.162.192.0/18', | |
| '131.0.72.0/22', | |
| '141.101.64.0/18', | |
| '162.158.0.0/15', | |
| '172.64.0.0/13', | |
| '173.245.48.0/20', | |
| '188.114.96.0/20', | |
| '190.93.240.0/20', | |
| '197.234.240.0/22', | |
| '198.41.128.0/17', | |
| //End cloudflare proxies. | |
| ]; | |
| /** | |
| * Create a new trusted proxies middleware instance. | |
| */ | |
| public function __construct() | |
| { | |
| // parent::__construct($config); | |
| $this->autoUpdateCloudflareIps(); | |
| } | |
| protected function autoUpdateCloudflareIps() | |
| { | |
| /* cache ket qua 10 ngay */ | |
| $proxyIps = Cache::remember('cloudFlareProxyIps', 1440, function () { | |
| $url = 'https://www.cloudflare.com/ips-v4'; | |
| $ips = file_get_contents($url); | |
| return array_filter(explode("\n", $ips)); | |
| }); | |
| $this->proxies = $proxyIps; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment