Skip to content

Commit 24034ea

Browse files
authored
Support ssl_cert_file/ssl_key_file (#123)
* Support ssl_cert_file/ssl_key_file close #122 close #84 * Update README
1 parent a6a7282 commit 24034ea

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,8 @@ go(function(){
478478
| cafile | string | ca文件 | `__DIR__ . '/cacert.pem'` | 默认自带 |
479479
| ssl_verify_peer | bool | 验证服务器端证书 | `false` \| `true` | 默认关闭 |
480480
| ssl_allow_self_signed | bool | 允许自签名证书 | `true` \| `false` | 默认允许 |
481+
| ssl_cert_file | string | cert 证书 | `__DIR__ . '/ssl.cert'` | 默认不设置 |
482+
| ssl_key_file | string | key 私钥 | `__DIR__ . '/ssl.key'` | 默认不设置 |
481483
| iconv | array | 指定编码转换 | `['gbk', 'utf-8']` | 共三个参数为`from,to,use_mb`, 默认自动识别 |
482484
| exception_report | int | 异常报告级别 | HttpExceptionMask::E_ALL | 默认汇报所有异常 |
483485
| exception_handle | callable\|array | 异常自定义处理函数 | `function(Exception $e){}` | 函数返回true时可忽略错误 |

src/Request.php

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ class Request extends \Swlib\Http\Request
4343
public $ssl = self::SSL_AUTO;
4444
/** @var string CA证书目录 */
4545
public $ca_file = '';
46+
public $ssl_cert_file = '';
47+
public $ssl_key_file = '';
4648
public $ssl_verify_peer = false;
4749
public $ssl_host_name = '';
4850
public $ssl_allow_self_signed = true;
@@ -281,6 +283,30 @@ public function withCAFile(string $ca_file = __DIR__ . '/cacert.pem'): self
281283
return $this;
282284
}
283285

286+
public function getSSLCertFile(): string
287+
{
288+
return $this->ssl_cert_file;
289+
}
290+
291+
public function withSSLCertFile(string $cert_file): self
292+
{
293+
$this->ssl_cert_file = $cert_file;
294+
295+
return $this;
296+
}
297+
298+
public function getSSLKeyFile(): string
299+
{
300+
return $this->ssl_key_file;
301+
}
302+
303+
public function withSSLKeyFile(string $key_file): self
304+
{
305+
$this->ssl_key_file = $key_file;
306+
307+
return $this;
308+
}
309+
284310
public function withSSLVerifyPeer(bool $verify_peer = false, ?string $ssl_host_name = ''): self
285311
{
286312
$this->ssl_verify_peer = $verify_peer;
@@ -712,9 +738,16 @@ public function exec()
712738
$settings['ssl_host_name'] = $this->uri->getHost();
713739
}
714740

741+
if ($this->getSSLCertFile()) {
742+
$settings['ssl_cert_file'] = $this->getSSLCertFile();
743+
}
744+
if ($this->getSSLKeyFile()) {
745+
$settings['ssl_key_file'] = $this->getSSLkeyFile();
746+
}
747+
715748
$settings += $this->getProxy();
716749

717-
if (!empty($ca_file = $this->getCAFile())) {
750+
if (!empty($this->getCAFile())) {
718751
$settings += $this->getSSLConf();
719752
}
720753
$this->client->set($settings);

src/Saber.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ class Saber
3131
'proxy' => null,
3232
'ssl' => Request::SSL_AUTO,
3333
'cafile' => __DIR__ . '/cacert.pem',
34+
'ssl_cert_file' => '',
35+
'ssl_key_file' => '',
3436
'ssl_verify_peer' => false,
3537
'ssl_host_name' => null,
3638
'ssl_allow_self_signed' => true,
@@ -493,6 +495,12 @@ private static function transOptionsToRequest(array $options, Request $request)
493495
$options['ssl_host_name'] ?? ''
494496
);
495497
}
498+
if (isset($options['ssl_cert_file'])) {
499+
$request->withSSLCertFile($options['ssl_cert_file']);
500+
}
501+
if (isset($options['ssl_key_file'])) {
502+
$request->withSSLKeyFile($options['ssl_key_file']);
503+
}
496504

497505
/** 绑定地址 */
498506
if (isset($options['bind_address'])) {

0 commit comments

Comments
 (0)