parent
b0d5274985
commit
6be16bea4a
81
src/Ratchet/Component/Server/IpBlackListComponent.php
Normal file
81
src/Ratchet/Component/Server/IpBlackListComponent.php
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
<?php
|
||||||
|
namespace Ratchet\Component\Server;
|
||||||
|
use Ratchet\Component\MessageComponentInterface;
|
||||||
|
use Ratchet\Resource\ConnectionInterface;
|
||||||
|
use Ratchet\Resource\Command\Action\CloseConnection;
|
||||||
|
|
||||||
|
class IpBlackListComponent implements MessageComponentInterface {
|
||||||
|
/**
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $_blacklist = array();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var Ratchet\Component\MessageComponentInterface
|
||||||
|
*/
|
||||||
|
protected $_decorating;
|
||||||
|
|
||||||
|
public function __construct(MessageComponentInterface $component) {
|
||||||
|
$this->_decorating = $component;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string IP address to block from connecting to yoru application
|
||||||
|
* @return IpBlackList
|
||||||
|
*/
|
||||||
|
public function blockAddress($ip) {
|
||||||
|
$this->_blacklist[$ip] = true;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string IP address to unblock from connecting to yoru application
|
||||||
|
* @return IpBlackList
|
||||||
|
*/
|
||||||
|
public function unblockAddress($ip) {
|
||||||
|
unset($this->_blacklist[$ip]);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get an array of all the addresses blocked
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getBlockedAddresses() {
|
||||||
|
return array_keys($this->_blacklist);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
function onOpen(ConnectionInterface $conn) {
|
||||||
|
if (isset($this->_blacklist[$conn->remoteAddress])) {
|
||||||
|
return new CloseConnection($conn);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->_decorating->onOpen($conn);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
function onMessage(ConnectionInterface $from, $msg) {
|
||||||
|
return $this->_decorating->onMessage($from, $msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
function onClose(ConnectionInterface $conn) {
|
||||||
|
return $this->_decorating->onClose($conn);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
function onError(ConnectionInterface $conn, \Exception $e) {
|
||||||
|
return $this->_decorating->onError($conn, $e);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,36 @@
|
|||||||
|
<?php
|
||||||
|
namespace Ratchet\Tests\Component\Server;
|
||||||
|
use Ratchet\Component\Server\IpBlackListComponent;
|
||||||
|
use Ratchet\Tests\Mock\Connection;
|
||||||
|
use Ratchet\Tests\Mock\Component as MockComponent;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers Ratchet\Component\Server\IpBlackList
|
||||||
|
*/
|
||||||
|
class IpBlackListComponentTest extends \PHPUnit_Framework_TestCase {
|
||||||
|
protected $_comp;
|
||||||
|
|
||||||
|
public function setUp() {
|
||||||
|
$this->_comp = new IpBlackListComponent(new MockComponent);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testBlockAndCloseOnOpen() {
|
||||||
|
$conn = new Connection;
|
||||||
|
|
||||||
|
$this->_comp->blockAddress($conn->remoteAddress);
|
||||||
|
|
||||||
|
$ret = $this->_comp->onOpen($conn);
|
||||||
|
|
||||||
|
$this->assertInstanceOf('\\Ratchet\\Resource\\Command\\Action\\CloseConnection', $ret);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAddAndRemoveWithFluentInterfaces() {
|
||||||
|
$blockOne = '127.0.0.1';
|
||||||
|
$blockTwo = '192.168.1.1';
|
||||||
|
$unblock = '75.119.207.140';
|
||||||
|
|
||||||
|
$this->_comp->blockAddress($unblock)->blockAddress($blockOne)->unblockAddress($unblock)->blockAddress($blockTwo);
|
||||||
|
|
||||||
|
$this->assertEquals(array($blockOne, $blockTwo), $this->_comp->getBlockedAddresses());
|
||||||
|
}
|
||||||
|
}
|
7
tests/Ratchet/Tests/Mock/Connection.php
Normal file
7
tests/Ratchet/Tests/Mock/Connection.php
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
<?php
|
||||||
|
namespace Ratchet\Tests\Mock;
|
||||||
|
use Ratchet\Resource\ConnectionInterface;
|
||||||
|
|
||||||
|
class Connection implements ConnectionInterface {
|
||||||
|
public $remoteAddress = '127.0.0.1';
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user