IpBlackList
Fixed bug where onClose was propagated if onOpen wasn't Filter out ports from IP4 addresses
This commit is contained in:
parent
8f653294c5
commit
550b32e1e7
@ -34,11 +34,19 @@ class IpBlackListComponent implements MessageComponentInterface {
|
|||||||
* @return IpBlackList
|
* @return IpBlackList
|
||||||
*/
|
*/
|
||||||
public function unblockAddress($ip) {
|
public function unblockAddress($ip) {
|
||||||
unset($this->_blacklist[$ip]);
|
unset($this->_blacklist[$this->filterAddress($ip)]);
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function isBlocked($address) {
|
||||||
|
return (isset($this->_blacklist[$this->filterAddress($address)]));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get an array of all the addresses blocked
|
* Get an array of all the addresses blocked
|
||||||
* @return array
|
* @return array
|
||||||
@ -47,11 +55,23 @@ class IpBlackListComponent implements MessageComponentInterface {
|
|||||||
return array_keys($this->_blacklist);
|
return array_keys($this->_blacklist);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function filterAddress($address) {
|
||||||
|
if (strstr($address, ':') && substr_count($address, '.') == 3) {
|
||||||
|
list($address, $port) = explode(':', $address);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $address;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
function onOpen(ConnectionInterface $conn) {
|
function onOpen(ConnectionInterface $conn) {
|
||||||
if (isset($this->_blacklist[$conn->remoteAddress])) {
|
if ($this->isBlocked($conn->remoteAddress)) {
|
||||||
return new CloseConnection($conn);
|
return new CloseConnection($conn);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -69,6 +89,10 @@ class IpBlackListComponent implements MessageComponentInterface {
|
|||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
function onClose(ConnectionInterface $conn) {
|
function onClose(ConnectionInterface $conn) {
|
||||||
|
if ($this->isBlocked($conn->remoteAddress)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
return $this->_decorating->onClose($conn);
|
return $this->_decorating->onClose($conn);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -68,4 +68,20 @@ class IpBlackListComponentTest extends \PHPUnit_Framework_TestCase {
|
|||||||
$this->assertEquals($conn2, $this->_mock->last['onError'][0]);
|
$this->assertEquals($conn2, $this->_mock->last['onError'][0]);
|
||||||
$this->assertEquals($e, $this->_mock->last['onError'][1]);
|
$this->assertEquals($e, $this->_mock->last['onError'][1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function addressProvider() {
|
||||||
|
return array(
|
||||||
|
array('127.0.0.1', '127.0.0.1')
|
||||||
|
, array('localhost', 'localhost')
|
||||||
|
, array('fe80::1%lo0', 'fe80::1%lo0')
|
||||||
|
, array('127.0.0.1', '127.0.0.1:6392')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider addressProvider
|
||||||
|
*/
|
||||||
|
public function testFilterAddress($expected, $input) {
|
||||||
|
$this->assertEquals($expected, $this->_comp->filterAddress($input));
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user