From f84b664d6ea462d5f91e2c20d698ba93f4022ba9 Mon Sep 17 00:00:00 2001 From: sy-records <52o@qq52o.cn> Date: Tue, 20 Apr 2021 14:10:45 +0800 Subject: [PATCH 1/2] Support ssl_cert_file/ssl_key_file close #122 close #84 --- README.md | 2 ++ src/Request.php | 35 ++++++++++++++++++++++++++++++++++- src/Saber.php | 8 ++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 6f2d848..86d8912 100644 --- a/README.md +++ b/README.md @@ -478,6 +478,8 @@ go(function(){ | cafile | string | ca文件 | `__DIR__ . '/cacert.pem'` | 默认自带 | | ssl_verify_peer | bool | 验证服务器端证书 | `false` \| `true` | 默认关闭 | | ssl_allow_self_signed | bool | 允许自签名证书 | `true` \| `false` | 默认允许 | +| ssl_cert_file | string | cert 证书 | `__DIR__ . '/ssl.cert'` | 默认不设置 | +| ssl_key_file | string | key 私钥 | `__DIR__ . '/ssl.key'` | 默认不设置 | | iconv | array | 指定编码转换 | `['gbk', 'utf-8']` | 共三个参数为`from,to,use_mb`, 默认自动识别 | | exception_report | int | 异常报告级别 | HttpExceptionMask::E_ALL | 默认汇报所有异常 | | exception_handle | callable\|array | 异常自定义处理函数 | `function(Exception $e){}` | 函数返回true时可忽略错误 | diff --git a/src/Request.php b/src/Request.php index 1c208a2..c280a50 100755 --- a/src/Request.php +++ b/src/Request.php @@ -43,6 +43,8 @@ class Request extends \Swlib\Http\Request public $ssl = self::SSL_AUTO; /** @var string CA证书目录 */ public $ca_file = ''; + public $ssl_cert_file = ''; + public $ssl_key_file = ''; public $ssl_verify_peer = false; public $ssl_host_name = ''; public $ssl_allow_self_signed = true; @@ -281,6 +283,30 @@ public function withCAFile(string $ca_file = __DIR__ . '/cacert.pem'): self return $this; } + public function getSSLCertFile(): string + { + return $this->ssl_cert_file; + } + + public function withSSLCertFile(string $cert_file): self + { + $this->ssl_cert_file = $cert_file; + + return $this; + } + + public function getSSLKeyFile(): string + { + return $this->ssl_key_file; + } + + public function withSSLKeyFile(string $key_file): self + { + $this->ssl_key_file = $key_file; + + return $this; + } + public function withSSLVerifyPeer(bool $verify_peer = false, ?string $ssl_host_name = ''): self { $this->ssl_verify_peer = $verify_peer; @@ -712,9 +738,16 @@ public function exec() $settings['ssl_host_name'] = $this->uri->getHost(); } + if ($this->getSSLCertFile()) { + $settings['ssl_cert_file'] = $this->getSSLCertFile(); + } + if ($this->getSSLKeyFile()) { + $settings['ssl_key_file'] = $this->getSSLkeyFile(); + } + $settings += $this->getProxy(); - if (!empty($ca_file = $this->getCAFile())) { + if (!empty($this->getCAFile())) { $settings += $this->getSSLConf(); } $this->client->set($settings); diff --git a/src/Saber.php b/src/Saber.php index ba3659b..17cfd42 100644 --- a/src/Saber.php +++ b/src/Saber.php @@ -31,6 +31,8 @@ class Saber 'proxy' => null, 'ssl' => Request::SSL_AUTO, 'cafile' => __DIR__ . '/cacert.pem', + 'ssl_cert_file' => '', + 'ssl_key_file' => '', 'ssl_verify_peer' => false, 'ssl_host_name' => null, 'ssl_allow_self_signed' => true, @@ -493,6 +495,12 @@ private static function transOptionsToRequest(array $options, Request $request) $options['ssl_host_name'] ?? '' ); } + if (isset($options['ssl_cert_file'])) { + $request->withSSLCertFile($options['ssl_cert_file']); + } + if (isset($options['ssl_key_file'])) { + $request->withSSLKeyFile($options['ssl_key_file']); + } /** 绑定地址 */ if (isset($options['bind_address'])) { From 414b308aafe2e4aa2ebe422255f8fa033c86a074 Mon Sep 17 00:00:00 2001 From: sy-records <52o@qq52o.cn> Date: Tue, 20 Apr 2021 14:24:23 +0800 Subject: [PATCH 2/2] Update README --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 86d8912..0245166 100644 --- a/README.md +++ b/README.md @@ -478,8 +478,8 @@ go(function(){ | cafile | string | ca文件 | `__DIR__ . '/cacert.pem'` | 默认自带 | | ssl_verify_peer | bool | 验证服务器端证书 | `false` \| `true` | 默认关闭 | | ssl_allow_self_signed | bool | 允许自签名证书 | `true` \| `false` | 默认允许 | -| ssl_cert_file | string | cert 证书 | `__DIR__ . '/ssl.cert'` | 默认不设置 | -| ssl_key_file | string | key 私钥 | `__DIR__ . '/ssl.key'` | 默认不设置 | +| ssl_cert_file | string | cert 证书 | `__DIR__ . '/ssl.cert'` | 默认不设置 | +| ssl_key_file | string | key 私钥 | `__DIR__ . '/ssl.key'` | 默认不设置 | | iconv | array | 指定编码转换 | `['gbk', 'utf-8']` | 共三个参数为`from,to,use_mb`, 默认自动识别 | | exception_report | int | 异常报告级别 | HttpExceptionMask::E_ALL | 默认汇报所有异常 | | exception_handle | callable\|array | 异常自定义处理函数 | `function(Exception $e){}` | 函数返回true时可忽略错误 |