diff options
author | marvin-borner@live.com | 2018-04-10 21:50:16 +0200 |
---|---|---|
committer | marvin-borner@live.com | 2018-04-10 21:54:48 +0200 |
commit | fc9401f04a3aca5abb22f87ebc210de8afe11d32 (patch) | |
tree | b0b310f3581764ec3955f4e496a05137a32951c3 /assets/php/vendor/cboden/ratchet/src/Ratchet/Server | |
parent | 286d643180672f20526f3dc3bd19d7b751e2fa97 (diff) |
Initial Commit
Diffstat (limited to 'assets/php/vendor/cboden/ratchet/src/Ratchet/Server')
5 files changed, 512 insertions, 0 deletions
diff --git a/assets/php/vendor/cboden/ratchet/src/Ratchet/Server/EchoServer.php b/assets/php/vendor/cboden/ratchet/src/Ratchet/Server/EchoServer.php new file mode 100644 index 0000000..2918e73 --- /dev/null +++ b/assets/php/vendor/cboden/ratchet/src/Ratchet/Server/EchoServer.php @@ -0,0 +1,23 @@ +<?php +namespace Ratchet\Server; +use Ratchet\MessageComponentInterface; +use Ratchet\ConnectionInterface; + +/** + * A simple Ratchet application that will reply to all messages with the message it received + */ +class EchoServer implements MessageComponentInterface { + public function onOpen(ConnectionInterface $conn) { + } + + public function onMessage(ConnectionInterface $from, $msg) { + $from->send($msg); + } + + public function onClose(ConnectionInterface $conn) { + } + + public function onError(ConnectionInterface $conn, \Exception $e) { + $conn->close(); + } +} diff --git a/assets/php/vendor/cboden/ratchet/src/Ratchet/Server/FlashPolicy.php b/assets/php/vendor/cboden/ratchet/src/Ratchet/Server/FlashPolicy.php new file mode 100644 index 0000000..4a1b8bd --- /dev/null +++ b/assets/php/vendor/cboden/ratchet/src/Ratchet/Server/FlashPolicy.php @@ -0,0 +1,200 @@ +<?php +namespace Ratchet\Server; +use Ratchet\MessageComponentInterface; +use Ratchet\ConnectionInterface; + +/** + * An app to go on a server stack to pass a policy file to a Flash socket + * Useful if you're using Flash as a WebSocket polyfill on IE + * Be sure to run your server instance on port 843 + * By default this lets accepts everything, make sure you tighten the rules up for production + * @final + * @link http://www.adobe.com/devnet/articles/crossdomain_policy_file_spec.html + * @link http://learn.adobe.com/wiki/download/attachments/64389123/CrossDomain_PolicyFile_Specification.pdf?version=1 + * @link view-source:http://www.adobe.com/xml/schemas/PolicyFileSocket.xsd + */ +class FlashPolicy implements MessageComponentInterface { + + /** + * Contains the root policy node + * @var string + */ + protected $_policy = '<?xml version="1.0"?><!DOCTYPE cross-domain-policy SYSTEM "http://www.adobe.com/xml/dtds/cross-domain-policy.dtd"><cross-domain-policy></cross-domain-policy>'; + + /** + * Stores an array of allowed domains and their ports + * @var array + */ + protected $_access = array(); + + /** + * @var string + */ + protected $_siteControl = ''; + + /** + * @var string + */ + protected $_cache = ''; + + /** + * @var string + */ + protected $_cacheValid = false; + + /** + * Add a domain to an allowed access list. + * + * @param string $domain Specifies a requesting domain to be granted access. Both named domains and IP + * addresses are acceptable values. Subdomains are considered different domains. A wildcard (*) can + * be used to match all domains when used alone, or multiple domains (subdomains) when used as a + * prefix for an explicit, second-level domain name separated with a dot (.) + * @param string $ports A comma-separated list of ports or range of ports that a socket connection + * is allowed to connect to. A range of ports is specified through a dash (-) between two port numbers. + * Ranges can be used with individual ports when separated with a comma. A single wildcard (*) can + * be used to allow all ports. + * @param bool $secure + * @throws \UnexpectedValueException + * @return FlashPolicy + */ + public function addAllowedAccess($domain, $ports = '*', $secure = false) { + if (!$this->validateDomain($domain)) { + throw new \UnexpectedValueException('Invalid domain'); + } + + if (!$this->validatePorts($ports)) { + throw new \UnexpectedValueException('Invalid Port'); + } + + $this->_access[] = array($domain, $ports, (boolean)$secure); + $this->_cacheValid = false; + + return $this; + } + + /** + * Removes all domains from the allowed access list. + * + * @return \Ratchet\Server\FlashPolicy + */ + public function clearAllowedAccess() { + $this->_access = array(); + $this->_cacheValid = false; + + return $this; + } + + /** + * site-control defines the meta-policy for the current domain. A meta-policy specifies acceptable + * domain policy files other than the master policy file located in the target domain's root and named + * crossdomain.xml. + * + * @param string $permittedCrossDomainPolicies + * @throws \UnexpectedValueException + * @return FlashPolicy + */ + public function setSiteControl($permittedCrossDomainPolicies = 'all') { + if (!$this->validateSiteControl($permittedCrossDomainPolicies)) { + throw new \UnexpectedValueException('Invalid site control set'); + } + + $this->_siteControl = $permittedCrossDomainPolicies; + $this->_cacheValid = false; + + return $this; + } + + /** + * {@inheritdoc} + */ + public function onOpen(ConnectionInterface $conn) { + } + + /** + * {@inheritdoc} + */ + public function onMessage(ConnectionInterface $from, $msg) { + if (!$this->_cacheValid) { + $this->_cache = $this->renderPolicy()->asXML(); + $this->_cacheValid = true; + } + + $from->send($this->_cache . "\0"); + $from->close(); + } + + /** + * {@inheritdoc} + */ + public function onClose(ConnectionInterface $conn) { + } + + /** + * {@inheritdoc} + */ + public function onError(ConnectionInterface $conn, \Exception $e) { + $conn->close(); + } + + /** + * Builds the crossdomain file based on the template policy + * + * @throws \UnexpectedValueException + * @return \SimpleXMLElement + */ + public function renderPolicy() { + $policy = new \SimpleXMLElement($this->_policy); + + $siteControl = $policy->addChild('site-control'); + + if ($this->_siteControl == '') { + $this->setSiteControl(); + } + + $siteControl->addAttribute('permitted-cross-domain-policies', $this->_siteControl); + + if (empty($this->_access)) { + throw new \UnexpectedValueException('You must add a domain through addAllowedAccess()'); + } + + foreach ($this->_access as $access) { + $tmp = $policy->addChild('allow-access-from'); + $tmp->addAttribute('domain', $access[0]); + $tmp->addAttribute('to-ports', $access[1]); + $tmp->addAttribute('secure', ($access[2] === true) ? 'true' : 'false'); + } + + return $policy; + } + + /** + * Make sure the proper site control was passed + * + * @param string $permittedCrossDomainPolicies + * @return bool + */ + public function validateSiteControl($permittedCrossDomainPolicies) { + //'by-content-type' and 'by-ftp-filename' are not available for sockets + return (bool)in_array($permittedCrossDomainPolicies, array('none', 'master-only', 'all')); + } + + /** + * Validate for proper domains (wildcards allowed) + * + * @param string $domain + * @return bool + */ + public function validateDomain($domain) { + return (bool)preg_match("/^((http(s)?:\/\/)?([a-z0-9-_]+\.|\*\.)*([a-z0-9-_\.]+)|\*)$/i", $domain); + } + + /** + * Make sure valid ports were passed + * + * @param string $port + * @return bool + */ + public function validatePorts($port) { + return (bool)preg_match('/^(\*|(\d+[,-]?)*\d+)$/', $port); + } +} diff --git a/assets/php/vendor/cboden/ratchet/src/Ratchet/Server/IoConnection.php b/assets/php/vendor/cboden/ratchet/src/Ratchet/Server/IoConnection.php new file mode 100644 index 0000000..9f864bb --- /dev/null +++ b/assets/php/vendor/cboden/ratchet/src/Ratchet/Server/IoConnection.php @@ -0,0 +1,38 @@ +<?php +namespace Ratchet\Server; +use Ratchet\ConnectionInterface; +use React\Socket\ConnectionInterface as ReactConn; + +/** + * {@inheritdoc} + */ +class IoConnection implements ConnectionInterface { + /** + * @var \React\Socket\ConnectionInterface + */ + protected $conn; + + + /** + * @param \React\Socket\ConnectionInterface $conn + */ + public function __construct(ReactConn $conn) { + $this->conn = $conn; + } + + /** + * {@inheritdoc} + */ + public function send($data) { + $this->conn->write($data); + + return $this; + } + + /** + * {@inheritdoc} + */ + public function close() { + $this->conn->end(); + } +} diff --git a/assets/php/vendor/cboden/ratchet/src/Ratchet/Server/IoServer.php b/assets/php/vendor/cboden/ratchet/src/Ratchet/Server/IoServer.php new file mode 100644 index 0000000..b3fb7e0 --- /dev/null +++ b/assets/php/vendor/cboden/ratchet/src/Ratchet/Server/IoServer.php @@ -0,0 +1,140 @@ +<?php +namespace Ratchet\Server; +use Ratchet\MessageComponentInterface; +use React\EventLoop\LoopInterface; +use React\Socket\ServerInterface; +use React\EventLoop\Factory as LoopFactory; +use React\Socket\Server as Reactor; +use React\Socket\SecureServer as SecureReactor; + +/** + * Creates an open-ended socket to listen on a port for incoming connections. + * Events are delegated through this to attached applications + */ +class IoServer { + /** + * @var \React\EventLoop\LoopInterface + */ + public $loop; + + /** + * @var \Ratchet\MessageComponentInterface + */ + public $app; + + /** + * The socket server the Ratchet Application is run off of + * @var \React\Socket\ServerInterface + */ + public $socket; + + /** + * @param \Ratchet\MessageComponentInterface $app The Ratchet application stack to host + * @param \React\Socket\ServerInterface $socket The React socket server to run the Ratchet application off of + * @param \React\EventLoop\LoopInterface|null $loop The React looper to run the Ratchet application off of + */ + public function __construct(MessageComponentInterface $app, ServerInterface $socket, LoopInterface $loop = null) { + if (false === strpos(PHP_VERSION, "hiphop")) { + gc_enable(); + } + + set_time_limit(0); + ob_implicit_flush(); + + $this->loop = $loop; + $this->app = $app; + $this->socket = $socket; + + $socket->on('connection', array($this, 'handleConnect')); + } + + /** + * @param \Ratchet\MessageComponentInterface $component The application that I/O will call when events are received + * @param int $port The port to server sockets on + * @param string $address The address to receive sockets on (0.0.0.0 means receive connections from any) + * @return IoServer + */ + public static function factory(MessageComponentInterface $component, $port = 80, $address = '0.0.0.0') { + $loop = LoopFactory::create(); + $socket = new Reactor($address . ':' . $port, $loop); + + return new static($component, $socket, $loop); + } + + /** + * Run the application by entering the event loop + * @throws \RuntimeException If a loop was not previously specified + */ + public function run() { + if (null === $this->loop) { + throw new \RuntimeException("A React Loop was not provided during instantiation"); + } + + // @codeCoverageIgnoreStart + $this->loop->run(); + // @codeCoverageIgnoreEnd + } + + /** + * Triggered when a new connection is received from React + * @param \React\Socket\ConnectionInterface $conn + */ + public function handleConnect($conn) { + $conn->decor = new IoConnection($conn); + $conn->decor->resourceId = (int)$conn->stream; + + $uri = $conn->getRemoteAddress(); + $conn->decor->remoteAddress = trim( + parse_url((strpos($uri, '://') === false ? 'tcp://' : '') . $uri, PHP_URL_HOST), + '[]' + ); + + $this->app->onOpen($conn->decor); + + $conn->on('data', function ($data) use ($conn) { + $this->handleData($data, $conn); + }); + $conn->on('close', function () use ($conn) { + $this->handleEnd($conn); + }); + $conn->on('error', function (\Exception $e) use ($conn) { + $this->handleError($e, $conn); + }); + } + + /** + * Data has been received from React + * @param string $data + * @param \React\Socket\ConnectionInterface $conn + */ + public function handleData($data, $conn) { + try { + $this->app->onMessage($conn->decor, $data); + } catch (\Exception $e) { + $this->handleError($e, $conn); + } + } + + /** + * A connection has been closed by React + * @param \React\Socket\ConnectionInterface $conn + */ + public function handleEnd($conn) { + try { + $this->app->onClose($conn->decor); + } catch (\Exception $e) { + $this->handleError($e, $conn); + } + + unset($conn->decor); + } + + /** + * An error has occurred, let the listening application know + * @param \Exception $e + * @param \React\Socket\ConnectionInterface $conn + */ + public function handleError(\Exception $e, $conn) { + $this->app->onError($conn->decor, $e); + } +} diff --git a/assets/php/vendor/cboden/ratchet/src/Ratchet/Server/IpBlackList.php b/assets/php/vendor/cboden/ratchet/src/Ratchet/Server/IpBlackList.php new file mode 100644 index 0000000..9342254 --- /dev/null +++ b/assets/php/vendor/cboden/ratchet/src/Ratchet/Server/IpBlackList.php @@ -0,0 +1,111 @@ +<?php +namespace Ratchet\Server; +use Ratchet\MessageComponentInterface; +use Ratchet\ConnectionInterface; + +class IpBlackList implements MessageComponentInterface { + /** + * @var array + */ + protected $_blacklist = array(); + + /** + * @var \Ratchet\MessageComponentInterface + */ + protected $_decorating; + + /** + * @param \Ratchet\MessageComponentInterface $component + */ + public function __construct(MessageComponentInterface $component) { + $this->_decorating = $component; + } + + /** + * Add an address to the blacklist that will not be allowed to connect to your application + * @param string $ip IP address to block from connecting to your application + * @return IpBlackList + */ + public function blockAddress($ip) { + $this->_blacklist[$ip] = true; + + return $this; + } + + /** + * Unblock an address so they can access your application again + * @param string $ip IP address to unblock from connecting to your application + * @return IpBlackList + */ + public function unblockAddress($ip) { + if (isset($this->_blacklist[$this->filterAddress($ip)])) { + unset($this->_blacklist[$this->filterAddress($ip)]); + } + + return $this; + } + + /** + * @param string $address + * @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 $address + * @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); + } + } +} |