Skip to content

Commit

Permalink
minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian Ajir committed Mar 20, 2020
1 parent c747563 commit 76aaa83
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ Provides a way to retrieve cloudfront proxies ip ranges with caching mechanism
## Symfony context
The initial purpose of this library was to be used in a symfony project, but it's theorically operational in other contexts like a laravel project.

As you can read in the [official documentation](https://symfony.com/doc/current/deployment/proxies.html#but-what-if-the-ip-of-my-reverse-proxy-changes-constantly)
If you are using CloudFront on top of your load balancer symfony does not provide an easy way to trust proxies traffic, as it will only trust the node sitting directly above your application (in this case your load balancer).
You also need to append the IP addresses or ranges of any additional proxy (in this case CloudFront IP ranges) to the array of trusted proxies.
As refered to the [Symfony official documentation](https://symfony.com/doc/current/deployment/proxies.html#but-what-if-the-ip-of-my-reverse-proxy-changes-constantly) , if you are using CloudFront on top of your load balancer symfony does not provide an easy way to trust proxies traffic, as it will only trust the node sitting directly above your application (in this case your load balancer).

That's why you also need to append the IP addresses or ranges of any additional proxy (in this case CloudFront IP ranges) to the array of trusted proxies.

### Usage

You have to inject a CacheInterface to ProxiesHelper constructor.
You have to inject a CacheInterface instance to the ProxiesHelper constructor.

In this example the FilesystemAdapter is used to store the cloudfront ips for one hour (3600 seconds).
In this example a FilesystemAdapter instance (from symfony/cache) is used to store the cloudfront ips for one hour (3600 seconds).

_Note that filesystem is often the worst choice for caching performances in production (except tmpfs storage)._
_Note the filesystem cache adapter is often the worst choice for caching performances in production (except on tmpfs storage)._

```php
// public/index.php
Expand Down
12 changes: 5 additions & 7 deletions src/ProxiesHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,11 @@ public function list(): array
// fetch the file
$data = json_decode(file_get_contents($this->source), true);
// filter to keep only cloudfront addresses
$ips = array_values(
array_filter(
array_map(static function ($item) {
return $item['service'] === 'CLOUDFRONT' ? $item['ip_prefix'] : null;
}, $data['prefixes'])
)
);
$ips = array_values(array_filter(
array_map(static function (array $item) {
return $item['service'] === 'CLOUDFRONT' ? $item['ip_prefix'] : null;
}, $data['prefixes'])
));
// update cache
$cached->set($ips);
$this->cachePool->save($cached);
Expand Down

0 comments on commit 76aaa83

Please sign in to comment.