Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace abandoned m6web/firewall with mlocati/ip-lib #5

Merged
merged 2 commits into from
Sep 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
![Testing][ico-ga]
[![Total Downloads][ico-downloads]][link-downloads]

Middleware to provide IP filtering using [M6Web/Firewall](https://github.com/M6Web/Firewall).
Middleware to provide IP filtering.

## Requirements

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
},
"require": {
"php": "^7.2 || ^8.0",
"m6web/firewall": "^1.0",
"middlewares/utils": "^3.0",
"psr/http-server-middleware": "^1.0"
"psr/http-server-middleware": "^1.0",
"mlocati/ip-lib": "^1.18"
},
"require-dev": {
"phpunit/phpunit": "^8|^9",
Expand Down
108 changes: 92 additions & 16 deletions src/Firewall.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

namespace Middlewares;

use M6Web\Component\Firewall\Firewall as IpFirewall;
use IPLib\Address\AddressInterface;
use IPLib\Factory as IPFactory;
use IPLib\Range\RangeInterface;
use Middlewares\Utils\Factory;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
Expand Down Expand Up @@ -73,21 +75,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
return $this->responseFactory->createResponse(403);
}

$firewall = new IpFirewall();

if (!empty($this->whitelist)) {
$firewall->addList($this->whitelist, 'whitelist', true);
} else {
$firewall->setDefaultState(true);
}

if (!empty($this->blacklist)) {
$firewall->addList($this->blacklist, 'blacklist', false);
}

$firewall->setIpAddress($ip);

if (!$firewall->handle()) {
if (!$this->isIpAccessible($ip)) {
return $this->responseFactory->createResponse(403);
}

Expand All @@ -107,4 +95,92 @@ private function getIp(ServerRequestInterface $request): string

return isset($server['REMOTE_ADDR']) ? $server['REMOTE_ADDR'] : '';
}

/**
* Create range class instance from string
*
* @param string $range
*
* @return RangeInterface
*/
protected function createRangeInstance(string $range): RangeInterface
{
if (strpos($range, '-') !== false) {
$parts = explode('-', $range, 2);
return IPFactory::getRangesFromBoundaries($parts[0], $parts[1]);
}

return IPFactory::parseRangeString($range);
}

/**
* Convert IP list to range array
*
* @param array|null $list Data that needs to be converted
*
* @return array<RangeInterface>
*/
private function convertListToRangeArray(?array $list): array
{
if ($list === null) {
return [];
}

return array_map(
[$this, 'createRangeInstance'],
$list
);
}

/**
* Checks if IP address is in list
*
* @param AddressInterface $address IP address to check
* @param array $list List of addresses to check
*
* @return bool
*/
private function isAddressInList(AddressInterface $address, array $list): bool
{
/**
* @var RangeInterface $ipRange
*/
foreach ($this->convertListToRangeArray($list) as $ipRange) {
if ($ipRange->contains($address)) {
return true;
}
}

return false;
}

/**
* Checks if IP by current addresses is accessible
*
* @param string $ip Current IP
*
* @return bool
*/
private function isIpAccessible(string $ip): bool
{
if (empty($this->blacklist) && empty($this->whitelist)) {
return true;
}

$address = IPFactory::parseAddressString($ip);
if ($address === null) {
return false;
}

if (empty($this->blacklist)) {
return $this->isAddressInList($address, $this->whitelist);
}

if (empty($this->whitelist)) {
return !$this->isAddressInList($address, $this->blacklist);
}

return $this->isAddressInList($address, $this->whitelist) &&
!$this->isAddressInList($address, $this->blacklist);
}
}