Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allowing TLS options (for WSS) on App facade #848

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions src/Ratchet/App.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php

namespace Ratchet;

use Ratchet\Tls\TlsOptions;
use React\EventLoop\LoopInterface;
use React\EventLoop\Factory as LoopFactory;
use React\Socket\Server as Reactor;
Expand All @@ -24,6 +27,8 @@
* A few configuration assumptions are made and some best-practice security conventions are applied by default.
*/
class App {
const OPTION_TLS = 'tls';

/**
* @var \Symfony\Component\Routing\RouteCollection
*/
Expand Down Expand Up @@ -57,12 +62,19 @@ class App {
protected $_routeCounter = 0;

/**
* @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 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 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 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 TlsOptions|null $tlsOptions Set of options for TSL/WSS used on Reactor initialization
*/
public function __construct($httpHost = 'localhost', $port = 8080, $address = '127.0.0.1', LoopInterface $loop = null) {
public function __construct(
$httpHost = 'localhost',
$port = 8080,
$address = '127.0.0.1',
LoopInterface $loop = null,
TlsOptions $tlsOptions = null
) {
if (extension_loaded('xdebug') && getenv('RATCHET_DISABLE_XDEBUG_WARN') === false) {
trigger_error('XDebug extension detected. Remember to disable this if performance testing or going live!', E_USER_WARNING);
}
Expand All @@ -74,7 +86,13 @@ public function __construct($httpHost = 'localhost', $port = 8080, $address = '1
$this->httpHost = $httpHost;
$this->port = $port;

$socket = new Reactor($address . ':' . $port, $loop);
$options = [];

if (null !== $tlsOptions) {
$options[self::OPTION_TLS] = $tlsOptions->toArray();
}

$socket = new Reactor($address . ':' . $port, $loop, $options);

$this->routes = new RouteCollection;
$this->_server = new IoServer(new HttpServer(new Router(new UrlMatcher($this->routes, new RequestContext))), $socket, $loop);
Expand Down
64 changes: 64 additions & 0 deletions src/Ratchet/Tls/TlsOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace Ratchet\Tls;

class TlsOptions
{
const FIELD_CERTIFICATE_PATH = 'local_cert';
const FIELD_CERTIFICATE_KEY = 'local_pk';
const FIELD_ALLOW_SELF_SIGNED = 'allow_self_signed';
const FIELD_VERIFY_PEER = 'verify_peer';
const FIELD_VERIFY_PEER_NAME = 'verify_peer_name';

/** @var string */
private $certificatePath;

/** @var string */
private $certificateKey;

/** @var bool */
private $allowSelfSigned;

/** @var bool */
private $verifyPeer;

/** @var bool */
private $verifyPeerName;

/**
* TlsOptions constructor.
*
* @param string $certificatePath
* @param string $certificateKey
* @param bool $allowSelfSigned
* @param bool $verifyPeer
* @param bool $verifyPeerName
*/
public function __construct(
$certificatePath,
$certificateKey,
$allowSelfSigned = false,
$verifyPeer = true,
$verifyPeerName = true
) {
$this->certificatePath = $certificatePath;
$this->certificateKey = $certificateKey;
$this->allowSelfSigned = $allowSelfSigned;
$this->verifyPeer = $verifyPeer;
$this->verifyPeerName = $verifyPeerName;
}

/**
* @return array
*/
public function toArray()
{
return [
self::FIELD_CERTIFICATE_PATH => $this->certificatePath,
self::FIELD_CERTIFICATE_KEY => $this->certificateKey,
self::FIELD_ALLOW_SELF_SIGNED => $this->allowSelfSigned,
self::FIELD_VERIFY_PEER => $this->verifyPeer,
self::FIELD_VERIFY_PEER_NAME => $this->verifyPeerName,
];
}
}