Skip to content

Commit

Permalink
Merge pull request #9 from hhxsv5/master
Browse files Browse the repository at this point in the history
Support skip verify tls
  • Loading branch information
hhxsv5 authored Jan 30, 2019
2 parents 95d8a4c + a85a4bb commit 2342157
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 9 deletions.
1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@
<env name="API_SECRET" value="secret" force="false"/>
<env name="API_PASSPHRASE" value="passphrase" force="false"/>
<env name="API_BASE_URI" value="https://openapi-v2.kucoin.com" force="false"/>
<env name="API_SKIP_VERIFY_TLS" value="0" force="false"/>
</php>
</phpunit>
23 changes: 22 additions & 1 deletion src/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ abstract class Api
*/
protected static $baseUri = 'https://openapi-v2.kucoin.com';

/**
* @var bool
*/
protected static $skipVerifyTls = false;

/**
* @var IAuth $auth
*/
Expand All @@ -27,7 +32,7 @@ abstract class Api
public function __construct(IAuth $auth = null, IHttp $http = null)
{
if (is_null($http)) {
$http = new GuzzleHttp();
$http = new GuzzleHttp($this);
}
$this->auth = $auth;
$this->http = $http;
Expand All @@ -49,6 +54,22 @@ public static function setBaseUri($baseUri)
static::$baseUri = $baseUri;
}

/**
* @return bool
*/
public static function isSkipVerifyTls()
{
return static::$skipVerifyTls;
}

/**
* @param bool $skipVerifyTls
*/
public static function setSkipVerifyTls($skipVerifyTls)
{
static::$skipVerifyTls = $skipVerifyTls;
}

/**
* @param string $method
* @param string $uri
Expand Down
15 changes: 15 additions & 0 deletions src/Http/BaseHttp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace KuCoin\SDK\Http;

use KuCoin\SDK\Api;

abstract class BaseHttp implements IHttp
{
protected $api;

public function __construct(Api $api)
{
$this->api = $api;
}
}
3 changes: 2 additions & 1 deletion src/Http/GuzzleHttp.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use KuCoin\SDK\Exceptions\HttpException;
use KuCoin\SDK\Exceptions\InvalidApiUriException;

class GuzzleHttp implements IHttp
class GuzzleHttp extends BaseHttp
{
protected static $clients = [];

Expand Down Expand Up @@ -46,6 +46,7 @@ public function request(Request $request, $timeout = 30)
'timeout' => $timeout,
'http_errors' => false,
'connect_timeout' => 30,
'verify' => !$this->api->isSkipVerifyTls(),
];
$client = static::getClient($config);
$options = [
Expand Down
4 changes: 3 additions & 1 deletion src/PrivateApi/WebSocketFeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use KuCoin\SDK\KuCoinApi;
use Ratchet\Client\WebSocket;
use React\EventLoop\Factory;
use React\EventLoop\TimerInterface;
use React\Socket\Connector as SocketConnector;
use Ratchet\Client\Connector as RatchetConnector;
use Ratchet\RFC6455\Messaging\MessageInterface;
Expand Down Expand Up @@ -101,6 +100,9 @@ public function getPrivateServer(array $params = [])
public function subscribeChannel(array $server, array $channel, callable $onMessage, callable $onClose = null, array $options = [])
{
$channel['type'] = 'subscribe';
if (!isset($options['tls']['verify_peer'])) {
$options['tls']['verify_peer'] = !static::isSkipVerifyTls();
}

$loop = Factory::create();
$reactConnector = new SocketConnector($loop, $options);
Expand Down
3 changes: 3 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class TestCase extends \PHPUnit\Framework\TestCase
protected $apiSecret;
protected $apiPassPhrase;
protected $apiBaseUri;
protected $apiSkipVerifyTls;

protected function setUp()
{
Expand All @@ -19,6 +20,8 @@ protected function setUp()
$this->apiSecret = getenv('API_SECRET');
$this->apiPassPhrase = getenv('API_PASSPHRASE');
$this->apiBaseUri = getenv('API_BASE_URI');
$this->apiSkipVerifyTls = (bool)getenv('API_SKIP_VERIFY_TLS');
KuCoinApi::setSkipVerifyTls($this->apiSkipVerifyTls);
if ($this->apiBaseUri) {
KuCoinApi::setBaseUri($this->apiBaseUri);
}
Expand Down
12 changes: 6 additions & 6 deletions tests/WebSocketFeedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ public function testSubscribePublicChannel(WebSocketFeed $api)
];

$options = [
'tls' => [
'verify_peer' => false,
],
// 'tls' => [
// 'verify_peer' => false,
// ],
];
$api->subscribePublicChannel($query, $channel, function (array $message, WebSocket $ws, LoopInterface $loop) use ($api) {
$this->assertInternalType('array', $message);
Expand Down Expand Up @@ -130,9 +130,9 @@ public function testSubscribePrivateChannel(WebSocketFeed $api)
];

$options = [
'tls' => [
'verify_peer' => false,
],
// 'tls' => [
// 'verify_peer' => false,
// ],
];
$api->subscribePrivateChannel($query, $channel, function (array $message, WebSocket $ws, LoopInterface $loop) use ($api) {
$this->assertInternalType('array', $message);
Expand Down

0 comments on commit 2342157

Please sign in to comment.