emotality / laravel-block-cf-origin
Laravel 包,用于阻止直接请求您的 Cloudflare 保护的源服务器。
1.0.1
2024-05-07 01:34 UTC
Requires
- php: ^8.0
- ext-redis: *
- illuminate/cache: ^9.0|^10.0|^11.0
- illuminate/console: ^9.0|^10.0|^11.0
- illuminate/http: ^9.0|^10.0|^11.0
- illuminate/support: ^9.0|^10.0|^11.0
- symfony/http-kernel: ^6.0|^7.0
Requires (Dev)
- laravel/pint: ^1.15
This package is auto-updated.
Last update: 2024-09-07 02:16:57 UTC
README
Laravel 包,用于阻止直接请求您的 Cloudflare 保护的源服务器。
概述
以下情况适用时,应仅使用此包:
- 您不能添加防火墙规则(仅接受来自 CF Edge IP 地址的请求),因为您的服务器与其他不使用 Cloudflare 的项目共享。如果您在服务器上运行单个应用程序,则最好添加防火墙规则。
- 您不能添加拒绝/允许规则到您的 Nginx/Apache 配置中,因为您正在使用
set_real_ip_from
/mod_remoteip
模块来转发用户的真实 IP(X-Forwarded-For)。如果您不需要转发用户的真实 IP,则最好在 Nginx/Apache 配置中添加拒绝/允许规则。 - 您不能安装
cloudflared
并创建隧道。 了解更多
有关更多信息,请参阅下面的 有用链接 部分。
要求
- PHP 8.0+
- PHP Redis 扩展
- Laravel 9.0+
*注意:此包仅支持 Redis 缓存驱动!
安装
composer require emotality/laravel-block-cf-origin
php artisan vendor:publish --provider="Emotality\Cloudflare\CloudflareBlockOriginServiceProvider"
- 在
app/Http/Kernel.php
中添加中间件
protected $middleware = [ \Emotality\Cloudflare\BlockNonCloudflareRequests::class, // Top is preferred ... ];
- 添加 cronjob 以更新 Cloudflare 的网段
protected function schedule(Schedule $schedule): void { ... $schedule->call(new \Emotality\Cloudflare\GetNetmasks)->weekly(); }
- 根据需要更新您的
config/cloudflare-block.php
配置和.env
文件。 - 将 FastCGI (PHP-FPM) 参数添加到您的 Nginx 配置中
server { server_name example.com; ... location ~ \.php$ { fastcgi_pass unix:/var/run/php/php8.2-fpm.sock; fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name; fastcgi_param CF_EDGE_IP $realip_remote_addr; <<----- HERE include fastcgi_params; } }
$realip_remote_addr
如果请求通过了 Cloudflare Edge 代理,将是 Cloudflare 的 IP,如果是直接请求,则是用户的 IP。
$remote_addr
将是用户的 IP 地址。
有用链接
贡献
此包处于早期阶段,欢迎报告任何问题或提出改进建议。请使用 master
分支进行任何拉取请求。
许可
laravel-block-cf-origin 在 MIT 许可下发布。有关详细信息,请参阅 LICENSE。