浏览代码

Combine doSslHandshake()

Ares 7 年之前
父节点
当前提交
df46b1db32
共有 2 个文件被更改,包括 15 次插入49 次删除
  1. 1 39
      Connection/AsyncTcpConnection.php
  2. 14 10
      Connection/TcpConnection.php

+ 1 - 39
Connection/AsyncTcpConnection.php

@@ -292,7 +292,7 @@ class AsyncTcpConnection extends TcpConnection
 
             // SSL handshake.
             if ($this->transport === 'ssl') {
-                $this->_sslHandshakeCompleted = $this->doSslHandshake($socket);
+                $this->_sslHandshakeCompleted = $this->doSslHandshake($socket,true);
                 if(!$this->_sslHandshakeCompleted){
                     return;
                 }
@@ -350,42 +350,4 @@ class AsyncTcpConnection extends TcpConnection
             }
         }
     }
-
-    /**
-     * SSL handshake.
-     *
-     * @param $socket
-     * @return bool
-     */
-    public function doSslHandshake($socket){
-        if (feof($socket)) {
-            $this->destroy();
-            return false;
-        }
-        $ret = stream_socket_enable_crypto($socket, true, STREAM_CRYPTO_METHOD_SSLv2_CLIENT |
-            STREAM_CRYPTO_METHOD_SSLv23_CLIENT);
-        // Negotiation has failed.
-        if(false === $ret) {
-            if (!feof($socket)) {
-                echo "\nSSL Handshake as client fail. \nBuffer:".bin2hex(fread($socket, 8182))."\n";
-            }
-            $this->destroy();
-            return false;
-        } elseif(0 === $ret) {
-            // There isn't enough data and should try again.
-            return false;
-        }
-        if (isset($this->onSslHandshake)) {
-            try {
-                call_user_func($this->onSslHandshake, $this);
-            } catch (\Exception $e) {
-                Worker::log($e);
-                exit(250);
-            } catch (\Error $e) {
-                Worker::log($e);
-                exit(250);
-            }
-        }
-        return true;
-    }
 }

+ 14 - 10
Connection/TcpConnection.php

@@ -565,10 +565,14 @@ class TcpConnection extends ConnectionInterface
     {
         // SSL handshake.
         if ($this->transport === 'ssl' && $this->_sslHandshakeCompleted !== true) {
-            if ($this->doSslHandshake($socket)) {
+            if ($this->doSslHandshake($socket,false)) {
                 $this->_sslHandshakeCompleted = true;
+                if ($this->_sendBuffer) {
+                    Worker::$globalEvent->add($socket, EventInterface::EV_WRITE, array($this, 'baseWrite'));
+                }
+            }else{
+                return;
             }
-            return;
         }
 
         $buffer = @fread($socket, self::READ_BUFFER_SIZE);
@@ -711,17 +715,21 @@ class TcpConnection extends ConnectionInterface
      * @param $socket
      * @return bool
      */
-    public function doSslHandshake($socket){
+    public function doSslHandshake($socket,$async){
         if (feof($socket)) {
             $this->destroy();
             return false;
         }
-        $ret = stream_socket_enable_crypto($socket, true, STREAM_CRYPTO_METHOD_SSLv2_SERVER |
-            STREAM_CRYPTO_METHOD_SSLv23_SERVER);
+        if($async){
+            $type=STREAM_CRYPTO_METHOD_SSLv2_CLIENT | STREAM_CRYPTO_METHOD_SSLv23_CLIENT;
+        }else{
+            $type=STREAM_CRYPTO_METHOD_SSLv2_SERVER | STREAM_CRYPTO_METHOD_SSLv23_SERVER;
+        }
+        $ret = stream_socket_enable_crypto($socket, true, $type);
         // Negotiation has failed.
         if(false === $ret) {
             if (!feof($socket)) {
-                echo "\nSSL Handshake fail. \nBuffer:".bin2hex(fread($socket, 8182))."\n";
+                echo "\nSSL Handshake fail as ".($async?'client':'server').". \nBuffer:".bin2hex(fread($socket, 8182))."\n";
             }
             $this->destroy();
             return false;
@@ -740,10 +748,6 @@ class TcpConnection extends ConnectionInterface
                 exit(250);
             }
         }
-
-        if ($this->_sendBuffer) {
-            Worker::$globalEvent->add($socket, EventInterface::EV_WRITE, array($this, 'baseWrite'));
-        }
         return true;
     }