mxmbsocket/src/Ratchet/Server/IpBlackList.php
Chris Boden 4735218aa0 [BCB] Namespace changes
Removed the `Component` namespace
Removed the `Resource` namespace
Renamed components:
`IOServerComponent` => `IoServer`
`WebSocketComponent` => `WsServer`
`SessionComponent` => `SessionProvider`
`WAMPServerComponent` => `WampServer`
`IpBlackListComponent` => `IpBlackList`
`FlashPolicyComponent` => `FlashPolicy`
2012-05-08 23:14:28 -04:00

106 lines
2.4 KiB
PHP

<?php
namespace Ratchet\Server;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
class IpBlackList implements MessageComponentInterface {
/**
* @var array
*/
protected $_blacklist = array();
/**
* @var Ratchet\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) {
if (isset($this->_blacklist[$this->filterAddress($ip)])) {
unset($this->_blacklist[$this->filterAddress($ip)]);
}
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
* @return array
*/
public function getBlockedAddresses() {
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}
*/
function onOpen(ConnectionInterface $conn) {
if ($this->isBlocked($conn->remoteAddress)) {
return $conn->close();
}
return $this->_decorating->onOpen($conn);
}
/**
* {@inheritdoc}
*/
function onMessage(ConnectionInterface $from, $msg) {
return $this->_decorating->onMessage($from, $msg);
}
/**
* {@inheritdoc}
*/
function onClose(ConnectionInterface $conn) {
if (!$this->isBlocked($conn->remoteAddress)) {
$this->_decorating->onClose($conn);
}
}
/**
* {@inheritdoc}
*/
function onError(ConnectionInterface $conn, \Exception $e) {
if (!$this->isBlocked($conn->remoteAddress)) {
$this->_decorating->onError($conn, $e);
}
}
}