From 6be16bea4a9a840cdc8c8f68f6105596f2dac855 Mon Sep 17 00:00:00 2001 From: Chris Boden Date: Sun, 29 Apr 2012 12:35:46 -0400 Subject: [PATCH] Added IpBlackListComponent refs #4 --- .../Component/Server/IpBlackListComponent.php | 81 +++++++++++++++++++ .../Server/IpBlackListComponentTest.php | 36 +++++++++ tests/Ratchet/Tests/Mock/Connection.php | 7 ++ 3 files changed, 124 insertions(+) create mode 100644 src/Ratchet/Component/Server/IpBlackListComponent.php create mode 100644 tests/Ratchet/Tests/Component/Server/IpBlackListComponentTest.php create mode 100644 tests/Ratchet/Tests/Mock/Connection.php diff --git a/src/Ratchet/Component/Server/IpBlackListComponent.php b/src/Ratchet/Component/Server/IpBlackListComponent.php new file mode 100644 index 0000000..551d204 --- /dev/null +++ b/src/Ratchet/Component/Server/IpBlackListComponent.php @@ -0,0 +1,81 @@ +_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); + } +} \ No newline at end of file diff --git a/tests/Ratchet/Tests/Component/Server/IpBlackListComponentTest.php b/tests/Ratchet/Tests/Component/Server/IpBlackListComponentTest.php new file mode 100644 index 0000000..55755b7 --- /dev/null +++ b/tests/Ratchet/Tests/Component/Server/IpBlackListComponentTest.php @@ -0,0 +1,36 @@ +_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()); + } +} \ No newline at end of file diff --git a/tests/Ratchet/Tests/Mock/Connection.php b/tests/Ratchet/Tests/Mock/Connection.php new file mode 100644 index 0000000..7c321f3 --- /dev/null +++ b/tests/Ratchet/Tests/Mock/Connection.php @@ -0,0 +1,7 @@ +