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

Support ssl_cert_file/ssl_key_file #123

Merged
merged 2 commits into from
Apr 20, 2021
Merged
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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时可忽略错误 |
Expand Down
35 changes: 34 additions & 1 deletion src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions src/Saber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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'])) {
Expand Down