Nic*_*las 9 php ssl session resume reconnect
我正在编写一个多线程的PHP客户端,它向一个apache反向代理发出https请求并测量一些统计信息.我正在写一篇关于通过TLS会话恢复提高绩效的学士论文.现在我需要做一个概念证明来证明/反驳这一点.目前我有这个代码:
$this->synchronized(function($this){
$this->before = microtime(true);
}, $this);
$url = 'https://192.168.0.171/';
# Some dummy data
$data = array('name' => 'Nicolas', 'bank account' => '123462343');
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data)
),
"ssl" => array(
"verify_peer" => false,
"verify_peer_name" => false,
"ciphers" => "HIGH:!SSLv2:!SSLv3"
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
$this->synchronized(function($this){
$this->after = microtime(true);
}, $this);
$this->counter_group->write($this->before, $this->after, $result);
Run Code Online (Sandbox Code Playgroud)
这段代码可以完成一次完整的握手,但我似乎无法弄清楚如何在php中进行恢复握手?
任何帮助将不胜感激!
您可以尝试PHP curl并使用CURL_LOCK_DATA_SSL_SESSION
来自PHP文档http://php.net/manual/en/function.curl-share-setopt.php
CURL_LOCK_DATA_SSL_SESSION 共享SSL会话ID,减少重新连接到同一服务器时在SSL握手上花费的时间.请注意,默认情况下,SSL会话ID在同一个句柄中重用
您可以从上面的描述中读到,会话ID由同一个句柄重用.但是如果你想之间共享处理可以使用curl_share_init例如
$sh = curl_share_init();
curl_share_setopt($sh, CURLSHOPT_SHARE, CURL_LOCK_DATA_SSL_SESSION);
curl_share_setopt($sh, CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS);
Run Code Online (Sandbox Code Playgroud)
那么你可以$sh在不同的请求之间重用
$ch1 = curl_init('https://192.168.0.171');
curl_setopt($ch1, CURLOPT_SHARE, $sh);
curl_setopt($ch1, CURLOPT_SSLVERSION, 6); // TLSV1.2
curl_setopt($ch1, CURLOPT_SSL_CIPHER_LIST, 'TLSv1');
curl_setopt($ch1, CURLOPT_POST, 1);
curl_setopt($ch1, CURLOPT_POSTFIELDS,
http_build_query( array('name' => 'Nicolas', 'bank account' => '123462343') ));
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch1);
Run Code Online (Sandbox Code Playgroud)
然后重用(恢复握手)
$ch2 = curl_init('https://192.168.0.171');
curl_setopt($ch2, CURLOPT_SHARE, $sh);
curl_setopt($ch2, CURLOPT_SSLVERSION, 6); // TLSV1.2
curl_setopt($ch2, CURLOPT_SSL_CIPHER_LIST, 'TLSv1');
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
// ( ... )
curl_exec($ch2);
Run Code Online (Sandbox Code Playgroud)
和紧密的联系
curl_close($ch1);
curl_close($ch2);
Run Code Online (Sandbox Code Playgroud)
但是你还需要使用CURLOPT_SSLVERSION和CURLOPT_SSL_CIPHER_LIST.此外,我认为你应该切换到另一种语言,因为PHP有自己的怪癖,如果你证明或反驳论文,最好使用更接近裸机的东西,这样你就可以确定额外的层(PHP)不会破坏你的基准.我确实测量了两个请求的性能,但它有点反直觉,但第二个几乎慢了两倍.