Skip to content

Commit

Permalink
Merge pull request #9 from aios/feature_ssl
Browse files Browse the repository at this point in the history
SSL fix
  • Loading branch information
stenin-nikita authored Jan 16, 2018
2 parents 4a71a23 + 1b8273f commit 441be4e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 19 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ Open your config/broadcasting.php and add the following to it:
'redis_connection' => env('CENTRIFUGE_REDIS_CONNECTION', 'default'), // name of redis connection
'redis_prefix' => env('CENTRIFUGE_REDIS_PREFIX', 'centrifugo'), // prefix name for queue in Redis
'redis_num_shards' => env('CENTRIFUGE_REDIS_NUM_SHARDS', 0), // number of shards for redis API queue
'verify' => env('CENTRIFUGE_VERIFY', false), // Verify host ssl if centrifuge uses this
'ssl_key' => env('CENTRIFUGE_SSL_KEY', null), // Self-Signed SSl Key for Host (require verify=true)
],
// ...
],
Expand Down Expand Up @@ -90,6 +92,8 @@ CENTRIFUGE_REDIS_API=false
CENTRIFUGE_REDIS_CONNECTION=default
CENTRIFUGE_REDIS_PREFIX=centrifugo
CENTRIFUGE_REDIS_NUM_SHARDS=0
CENTRIFUGE_SSL_KEY=/etc/ssl/some.pem
CENTRIFUGE_VERIFY=false
```

Do not forget to install the broadcast driver
Expand Down
53 changes: 34 additions & 19 deletions src/Centrifuge.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ class Centrifuge implements CentrifugeContract
/**
* Create a new Centrifuge instance.
*
* @param array $config
* @param \GuzzleHttp\Client $httpClient
* @param \Predis\Client|null $redisClient
* @param array $config
* @param \GuzzleHttp\Client $httpClient
* @param \Predis\Client|null $redisClient
*/
public function __construct(array $config, HttpClient $httpClient, RedisClient $redisClient = null)
{
Expand All @@ -53,17 +53,19 @@ public function __construct(array $config, HttpClient $httpClient, RedisClient $
/**
* Init centrifuge configuration.
*
* @param array $config
* @param array $config
* @return array
*/
protected function initConfiguration(array $config)
{
$defaults = [
'url' => 'http://localhost:8000',
'secret' => null,
'redis_api' => false,
'redis_prefix' => 'centrifugo',
'url' => 'http://localhost:8000',
'secret' => null,
'redis_api' => false,
'redis_prefix' => 'centrifugo',
'redis_num_shards' => 0,
'ssl_key' => null,
'verify' => true,
];

foreach ($config as $key => $value) {
Expand Down Expand Up @@ -230,13 +232,14 @@ protected function getSecret()
* Send message to centrifuge server.
*
* @param string $method
* @param array $params
* @param array $params
* @return mixed
*/
protected function send($method, array $params = [])
{
try {
if ($this->config['redis_api'] === true && ! is_null($this->redisClient) && in_array($method, $this->redisMethods)) {
if ($this->config['redis_api'] === true && ! is_null($this->redisClient) && in_array($method,
$this->redisMethods)) {
$result = $this->redisSend($method, $params);
} else {
$result = $this->httpSend($method, $params);
Expand All @@ -256,7 +259,7 @@ protected function send($method, array $params = [])
* Send message to centrifuge server from http client.
*
* @param string $method
* @param array $params
* @param array $params
* @return mixed
*/
protected function httpSend($method, array $params = [])
Expand All @@ -265,16 +268,28 @@ protected function httpSend($method, array $params = [])

$headers = [
'Content-type' => 'application/json',
'X-API-Sign' => $this->generateApiSign($json),
'X-API-Sign' => $this->generateApiSign($json),
];

try {
$response = $this->httpClient->post($this->prepareUrl(), [
'headers' => $headers,
'body' => $json,
$url = parse_url($this->prepareUrl());

$config = collect([
'headers' => $headers,
'body' => $json,
'http_errors' => false,
]);

if ($url['scheme'] == 'https') {
$config->put('verify', collect($this->config)->get('verify', false));

if (collect($this->config)->get('ssl_key')) {
$config->put('ssl_key', collect($this->config)->get('ssl_key'));
}
}

$response = $this->httpClient->post($this->prepareUrl(), $config->toArray());

$finally = json_decode((string) $response->getBody(), true)[0];
} catch (ClientException $e) {
throw $e;
Expand Down Expand Up @@ -302,10 +317,10 @@ protected function prepareUrl()

/**
* Send message to centrifuge server from redis client.
*
* @param string $method
* @param array $params
* @return mixed
* @param $method
* @param array $params
* @return array
* @throws PredisException
*/
protected function redisSend($method, array $params = [])
{
Expand Down

0 comments on commit 441be4e

Please sign in to comment.