fmaj/cloudfront-trusted-proxies

提供了一种带有缓存机制的检索CloudFront代理IP范围的方法

1.0.3 2020-03-20 19:45 UTC

This package is auto-updated.

Last update: 2024-09-06 10:20:15 UTC


README

Build codecov Latest Stable Version Total Downloads Latest Unstable Version License

提供了一种带有缓存机制的检索CloudFront代理IP范围的方法

安装

composer require fmaj/cloudfront-trusted-proxies

Symfony上下文

这个库最初的目的是在symfony项目中使用,但理论上在其他上下文中也能工作,例如laravel项目。

根据Symfony官方文档,如果你在负载均衡器之上使用CloudFront,symfony无法提供信任代理流量的简单方法,因为它只会信任直接位于应用程序之上的节点(在这种情况下是负载均衡器)。

因此,你还需要将任何其他代理(在这种情况下是CloudFront IP范围)的IP地址或范围追加到信任代理的数组中。

用法

你必须向ProxiesHelper构造函数注入一个CacheInterface实例。

在这个例子中,使用了FilesystemAdapter实例(来自symfony/cache)来存储云Front IP,时间为一小时(3600秒)。

请注意,文件系统缓存适配器通常在生产环境中的缓存性能最差(除了在tmpfs存储上)。

// public/index.php
use Fmaj\CloudfrontTrustedProxies\ProxiesHelper;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Symfony\Component\HttpFoundation\Request;

/** @var \Psr\Cache\CacheItemPoolInterface $cachePool */
$cachePool = new FilesystemAdapter('cloudfront_trusted_ips', 3600);
$proxyHelper = new ProxiesHelper($cachePool);
Request::setTrustedProxies(
    $proxyHelper->list(),
    Request::HEADER_X_FORWARDED_AWS_ELB
);