emotality / laravel-block-cf-origin

Laravel 包,用于阻止直接请求您的 Cloudflare 保护的源服务器。

1.0.1 2024-05-07 01:34 UTC

This package is auto-updated.

Last update: 2024-09-07 02:16:57 UTC


README

License Latest Version Total Downloads

Laravel 包,用于阻止直接请求您的 Cloudflare 保护的源服务器。

概述

以下情况适用时,应仅使用此包:

  1. 您不能添加防火墙规则(仅接受来自 CF Edge IP 地址的请求),因为您的服务器与其他不使用 Cloudflare 的项目共享。如果您在服务器上运行单个应用程序,则最好添加防火墙规则。
  2. 您不能添加拒绝/允许规则到您的 Nginx/Apache 配置中,因为您正在使用 set_real_ip_from / mod_remoteip 模块来转发用户的真实 IP(X-Forwarded-For)。如果您不需要转发用户的真实 IP,则最好在 Nginx/Apache 配置中添加拒绝/允许规则。
  3. 您不能安装 cloudflared 并创建隧道。 了解更多

有关更多信息,请参阅下面的 有用链接 部分。

要求

  • PHP 8.0+
  • PHP Redis 扩展
  • Laravel 9.0+

*注意:此包仅支持 Redis 缓存驱动!

安装

  1. composer require emotality/laravel-block-cf-origin
  2. php artisan vendor:publish --provider="Emotality\Cloudflare\CloudflareBlockOriginServiceProvider"
  3. app/Http/Kernel.php 中添加中间件
protected $middleware = [
    \Emotality\Cloudflare\BlockNonCloudflareRequests::class, // Top is preferred
    ...
];
  1. 添加 cronjob 以更新 Cloudflare 的网段
protected function schedule(Schedule $schedule): void
{
    ...
    $schedule->call(new \Emotality\Cloudflare\GetNetmasks)->weekly();
}
  1. 根据需要更新您的 config/cloudflare-block.php 配置和 .env 文件。
  2. 将 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