diff --git a/src/Ratchet/App.php b/src/Ratchet/App.php index 29c44ab..47262b0 100644 --- a/src/Ratchet/App.php +++ b/src/Ratchet/App.php @@ -5,12 +5,10 @@ use React\EventLoop\LoopInterface; use React\EventLoop\Factory as LoopFactory; use React\Socket\Server as Reactor; -use React\Socket\SecureServer as SecureReactor; use Ratchet\Http\HttpServerInterface; use Ratchet\Http\OriginCheck; use Ratchet\Wamp\WampServerInterface; use Ratchet\Server\IoServer; -use Ratchet\Server\FlashPolicy; use Ratchet\Http\HttpServer; use Ratchet\Http\Router; use Ratchet\WebSocket\MessageComponentInterface as WsMessageComponentInterface; @@ -32,11 +30,6 @@ class App */ public $routes; - /** - * @var \Ratchet\Server\IoServer - */ - public $flashServer; - /** * @var \Ratchet\Server\IoServer */ @@ -61,7 +54,7 @@ class App /** * @param string $httpHost HTTP hostname clients intend to connect to. MUST match JS `new WebSocket('ws://$httpHost');` - * @param int $port Port to listen on. If 80, assuming production, Flash on 843 otherwise expecting Flash to be proxied through 8843 + * @param int $port Port to listen on. * @param string $address IP address to bind to. Default is localhost/proxy only. '0.0.0.0' for any machine. * @param LoopInterface $loop Specific React\EventLoop to bind the application to. null will create one for you. * @param array $context @@ -83,18 +76,6 @@ public function __construct($httpHost = 'localhost', $port = 8080, $address = '1 $this->routes = new RouteCollection(); $this->_server = new IoServer(new HttpServer(new Router(new UrlMatcher($this->routes, new RequestContext()))), $socket, $loop); - - $policy = new FlashPolicy(); - $policy->addAllowedAccess($httpHost, 80); - $policy->addAllowedAccess($httpHost, $port); - - if (80 == $port) { - $flashUri = '0.0.0.0:843'; - } else { - $flashUri = 8843; - } - $flashSock = new Reactor($flashUri, $loop); - $this->flashServer = new IoServer($policy, $flashSock); } /** @@ -131,13 +112,6 @@ public function route($path, ComponentInterface $controller, array $allowedOrigi $decorated = new OriginCheck($decorated, $allowedOrigins); } - //allow origins in flash policy server - if (empty($this->flashServer) === false) { - foreach ($allowedOrigins as $allowedOrgin) { - $this->flashServer->app->addAllowedAccess($allowedOrgin, $this->port); - } - } - $this->routes->add('rr-' . ++$this->_routeCounter, new Route($path, array('_controller' => $decorated), array('Origin' => $this->httpHost), array(), $httpHost, array(), array('GET'))); return $decorated; diff --git a/src/Ratchet/Server/FlashPolicy.php b/src/Ratchet/Server/FlashPolicy.php deleted file mode 100644 index 54a0015..0000000 --- a/src/Ratchet/Server/FlashPolicy.php +++ /dev/null @@ -1,213 +0,0 @@ -'; - - /** - * 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, (bool)$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/tests/unit/Server/FlashPolicyComponentTest.php b/tests/unit/Server/FlashPolicyComponentTest.php deleted file mode 100644 index ab86253..0000000 --- a/tests/unit/Server/FlashPolicyComponentTest.php +++ /dev/null @@ -1,170 +0,0 @@ -policy = new FlashPolicy(); - } - - public function testPolicyRender() - { - $this->policy->setSiteControl('all'); - $this->policy->addAllowedAccess('example.com', '*'); - $this->policy->addAllowedAccess('dev.example.com', '*'); - - $this->assertInstanceOf('SimpleXMLElement', $this->policy->renderPolicy()); - } - - public function testInvalidPolicyReader() - { - $this->expectException('UnexpectedValueException'); - $this->policy->renderPolicy(); - } - - public function testInvalidDomainPolicyReader() - { - $this->expectException('UnexpectedValueException'); - $this->policy->setSiteControl('all'); - $this->policy->addAllowedAccess('dev.example.*', '*'); - $this->policy->renderPolicy(); - } - - /** - * @dataProvider siteControl - */ - public function testSiteControlValidation($accept, $permittedCrossDomainPolicies) - { - $this->assertEquals($accept, $this->policy->validateSiteControl($permittedCrossDomainPolicies)); - } - - public static function siteControl() - { - return array( - array(true, 'all') - , array(true, 'none') - , array(true, 'master-only') - , array(false, 'by-content-type') - , array(false, 'by-ftp-filename') - , array(false, '') - , array(false, 'all ') - , array(false, 'asdf') - , array(false, '@893830') - , array(false, '*') - ); - } - - /** - * @dataProvider URI - */ - public function testDomainValidation($accept, $domain) - { - $this->assertEquals($accept, $this->policy->validateDomain($domain)); - } - - public static function URI() - { - return array( - array(true, '*') - , array(true, 'example.com') - , array(true, 'exam-ple.com') - , array(true, '*.example.com') - , array(true, 'www.example.com') - , array(true, 'dev.dev.example.com') - , array(true, 'http://example.com') - , array(true, 'https://example.com') - , array(true, 'http://*.example.com') - , array(false, 'exam*ple.com') - , array(true, '127.0.255.1') - , array(true, 'localhost') - , array(false, 'www.example.*') - , array(false, 'www.exa*le.com') - , array(false, 'www.example.*com') - , array(false, '*.example.*') - , array(false, 'gasldf*$#a0sdf0a8sdf') - ); - } - - /** - * @dataProvider ports - */ - public function testPortValidation($accept, $ports) - { - $this->assertEquals($accept, $this->policy->validatePorts($ports)); - } - - public static function ports() - { - return array( - array(true, '*') - , array(true, '80') - , array(true, '80,443') - , array(true, '507,516-523') - , array(true, '507,516-523,333') - , array(true, '507,516-523,507,516-523') - , array(false, '516-') - , array(true, '516-523,11') - , array(false, '516,-523,11') - , array(false, 'example') - , array(false, 'asdf,123') - , array(false, '--') - , array(false, ',,,') - , array(false, '838*') - ); - } - - public function testAddAllowedAccessOnlyAcceptsValidPorts() - { - $this->expectException('UnexpectedValueException'); - - $this->policy->addAllowedAccess('*', 'nope'); - } - - public function testSetSiteControlThrowsException() - { - $this->expectException('UnexpectedValueException'); - - $this->policy->setSiteControl('nope'); - } - - public function testErrorClosesConnection() - { - $conn = $this->createMock('\\Ratchet\\ConnectionInterface'); - $conn->expects($this->once())->method('close'); - - $this->policy->onError($conn, new \Exception()); - } - - public function testOnMessageSendsString() - { - $this->policy->addAllowedAccess('*', '*'); - - $conn = $this->createMock('\\Ratchet\\ConnectionInterface'); - $conn->expects($this->once())->method('send')->with($this->isType('string')); - - $this->policy->onMessage($conn, ' '); - } - - public function testOnOpenExists() - { - $this->assertTrue(method_exists($this->policy, 'onOpen')); - $conn = $this->createMock('\Ratchet\ConnectionInterface'); - $this->policy->onOpen($conn); - } - - public function testOnCloseExists() - { - $this->assertTrue(method_exists($this->policy, 'onClose')); - $conn = $this->createMock('\Ratchet\ConnectionInterface'); - $this->policy->onClose($conn); - } -}