From 218cdf772e5c24b5a9ff4d8d671b13635df52cc9 Mon Sep 17 00:00:00 2001 From: Jesse Kramer Date: Mon, 7 Sep 2020 12:33:57 +0200 Subject: [PATCH] Remove unstable TCP SSL driver --- .../ConnectionDriver/SslConnectionDriver.php | 93 ----------- .../ConnectionDriver/Support/ReadBuffer.php | 156 ------------------ .../ConnectionDriver/Support/SleepTimer.php | 67 -------- src/Epp/ConnectionDriver/Support/Stream.php | 145 ---------------- .../ConnectionDriver/Support/WriteBuffer.php | 45 ----- 5 files changed, 506 deletions(-) delete mode 100644 src/Epp/ConnectionDriver/SslConnectionDriver.php delete mode 100644 src/Epp/ConnectionDriver/Support/ReadBuffer.php delete mode 100644 src/Epp/ConnectionDriver/Support/SleepTimer.php delete mode 100644 src/Epp/ConnectionDriver/Support/Stream.php delete mode 100644 src/Epp/ConnectionDriver/Support/WriteBuffer.php diff --git a/src/Epp/ConnectionDriver/SslConnectionDriver.php b/src/Epp/ConnectionDriver/SslConnectionDriver.php deleted file mode 100644 index 31e7a6f..0000000 --- a/src/Epp/ConnectionDriver/SslConnectionDriver.php +++ /dev/null @@ -1,93 +0,0 @@ -isBlocking = $isBlocking; - $this->verifyPeer = $verifyPeer; - $this->verifyPeerName = $verifyPeerName; - - parent::__construct($hostname, $port, $timeout, $localCertificatePath, $localCertificatePassword, $allowSelfSigned); - } - - public function connect(): bool - { - $this->stream = new Stream( - $this->hostname, - $this->port, - $this->timeout, - $this->isBlocking, - $this->verifyPeer, - $this->verifyPeerName, - $this->localCertificatePath, - $this->localCertificatePassword, - $this->allowSelfSigned - ); - - return $this->isConnected(); - } - - public function disconnect(): bool - { - if (! $this->isConnected()) { - return true; - } - - if ($this->stream) { - $this->stream->close(); - } - - return true; - } - - public function isConnected(): bool - { - return $this->stream && $this->stream->isConnected(); - } - - public function executeRequest(string $request, string $requestId): string - { - if ($this->stream === null || $this->stream->isConnected()) { - throw new ConnectException('No active connection!'); - } - - $this->stream->write($request); - - $response = $this->stream->read(); - - $tries = 0; - while ($response === '' && $tries < 5) { - $response = $this->stream->read(); - $tries++; - } - - return $response; - } -} diff --git a/src/Epp/ConnectionDriver/Support/ReadBuffer.php b/src/Epp/ConnectionDriver/Support/ReadBuffer.php deleted file mode 100644 index b18cf3f..0000000 --- a/src/Epp/ConnectionDriver/Support/ReadBuffer.php +++ /dev/null @@ -1,156 +0,0 @@ -connection = $connection; - $this->timeout = $timeout; - $this->nonBlocking = $nonBlocking; - $this->enableReadSleep = $enableReadSleep; - } - - public function readPackage(): string - { - if (! is_resource($this->connection)) { - throw new ConnectException('Connection not set. Aborting..'); - } - - $this->resetTimeout(); - $content = ''; - $read = ''; - - while ($this->shouldRead()) { - $this->assertConnectionIsAlive(); - if ($this->isTimedOut()) { - throw new TimeoutException(); - } - - // If necessary, get the content length by fetching the first 4 bytes. - if ($this->isContentLengthUnknown()) { - $this->readContentLength(); - } - - if ($this->contentLength > 1000000) { - throw new ReadException("Packet size is too big: {$this->contentLength}. Closing connection."); - } - - //We know the length of what to read, so lets read the stuff - if ($this->isContentLengthKnown()) { - $this->resetTimeout(); - if (is_int($this->contentLength) && $read = fread($this->connection, $this->contentLength)) { - $this->contentLength -= strlen($read); - $content .= $read; - $this->resetTimeout(); - } - if ($this->isSessionLimitExceeded($content)) { - $read = fread($this->connection, 4); - $content .= $read; - } - } - - if ($this->nonBlocking && is_string($content) && strlen($content) < 1) { - break; - } - - if (is_string($read) && ! strlen($read)) { - usleep(100); - } - } - - return $content; - } - - private function readContentLength(int $bytesLeft = 4): void - { - $buffer = ''; - $sleepTimer = new SleepTimer(); - while ($bytesLeft > 0) { - if ($chunk = fread($this->connection, $bytesLeft)) { - $bytesLeft -= strlen($chunk); - $buffer .= $chunk; - $this->resetTimeout(); - continue; - } - - if ($this->enableReadSleep) { - $sleepTimer->sleep(); - } - - if ($this->isTimedOut()) { - throw new TimeoutException(); - } - } - - $int = unpack('N', substr($buffer, 0, 4)); - $contentLength = $int ? $int[1] - 4 : 0; - $this->contentLength = $contentLength; - } - - private function resetTimeout(): void - { - $this->timeoutStamp = time() + $this->timeout; - } - - private function isTimedOut(): bool - { - return $this->timeoutStamp >= time(); - } - - private function assertConnectionIsAlive(): void - { - if (feof($this->connection)) { - throw new ConnectException('Unexpected closed connection by remote host...'); - } - } - - private function shouldRead(): bool - { - return $this->contentLength === null || $this->contentLength > 0; - } - - private function isContentLengthUnknown(): bool - { - return $this->contentLength === null || $this->contentLength === 0; - } - - private function isContentLengthKnown(): bool - { - return $this->contentLength !== null && $this->contentLength > 0; - } - - private function isSessionLimitExceeded(string $content): bool - { - return strpos($content, 'Session limit exceeded') !== -1; - } -} diff --git a/src/Epp/ConnectionDriver/Support/SleepTimer.php b/src/Epp/ConnectionDriver/Support/SleepTimer.php deleted file mode 100644 index b49e4a1..0000000 --- a/src/Epp/ConnectionDriver/Support/SleepTimer.php +++ /dev/null @@ -1,67 +0,0 @@ -maxSleepInterval = $maxSleepInterval; - $this->incrementSleepInterval = $incrementSleepInterval; - $this->intervalIncrementorLimit = $intervalIncrementorLimit; - $this->intervalIncrementorPrimary = $intervalIncrementorPrimary; - $this->intervalIncrementorSecondary = $intervalIncrementorSecondary; - - $this->sleepInterval = $initialSleepInterval; - } - - public function sleep(): void - { - usleep($this->sleepInterval); - - if (! $this->incrementSleepInterval) { - return; - } - - if ($this->sleepInterval >= $this->maxSleepInterval) { - return; - } - - $increment = ($this->sleepInterval < $this->intervalIncrementorLimit) - ? $this->intervalIncrementorPrimary - : $this->intervalIncrementorSecondary; - - $this->sleepInterval += $increment; - } -} diff --git a/src/Epp/ConnectionDriver/Support/Stream.php b/src/Epp/ConnectionDriver/Support/Stream.php deleted file mode 100644 index b1e0708..0000000 --- a/src/Epp/ConnectionDriver/Support/Stream.php +++ /dev/null @@ -1,145 +0,0 @@ -timeout = $timeout; - $this->isBlocking = $isBlocking; - $this->verifyPeer = $verifyPeer; - $this->verifyPeerName = $verifyPeerName; - $this->localCertificatePath = $localCertificatePath; - $this->localCertificatePassword = $localCertificatePassword; - $this->allowSelfSigned = $allowSelfSigned; - - $connection = stream_socket_client( - "{$hostname}:{$port}", - $errorNumber, - $errorMessage, - $timeout, - STREAM_CLIENT_CONNECT, - $this->createStreamContext() - ); - - if (! is_resource($connection)) { - throw new ConnectException("Could not instantiate EPP connection to {$hostname}:{$port}. Reason: [{$errorNumber}]: $errorMessage"); - } - - stream_set_blocking($connection, $this->isBlocking); - stream_set_timeout($connection, $timeout); - - $this->connection = $connection; - } - - public function close(): void - { - if (! $this->isConnected()) { - return; - } - fclose($this->connection); - - unset($this->connection); - } - - public function isConnected(): bool - { - return is_resource($this->connection); - } - - public function read(bool $nonBlocking = false): string - { - if (! $this->isConnected()) { - return ''; - } - - $buffer = new ReadBuffer($this->connection, $this->timeout, $nonBlocking); - try { - $content = $buffer->readPackage(); - } catch (TimeoutException $e) { - return ''; - } - - // Consume the first package from the connection. - $this->read(); // TODO: Dit is recursief gedrag. - - return $content; - } - - public function write(string $content): void - { - if (! $this->isConnected()) { - return; - } - - $buffer = new WriteBuffer($this->connection, $content); - $buffer->write(); - } - - /** @return resource */ - private function createStreamContext() - { - $context = stream_context_create(); - stream_context_set_option($context, 'ssl', 'verify_peer', $this->verifyPeer); - stream_context_set_option($context, 'ssl', 'verify_peer_name', $this->verifyPeerName); - - if (! $this->localCertificatePath) { - return $context; - } - - stream_context_set_option($context, 'ssl', 'local_cert', $this->localCertificatePath); - - if ($this->localCertificatePassword && strlen($this->localCertificatePassword) > 0) { - stream_context_set_option($context, 'ssl', 'passphrase', $this->localCertificatePassword); - } - - if ($this->allowSelfSigned === null) { - stream_context_set_option($context, 'ssl', 'verify_peer', $this->verifyPeer); - return $context; - } - - stream_context_set_option($context, 'ssl', 'allow_self_signed', $this->allowSelfSigned); - stream_context_set_option($context, 'ssl', 'verify_peer', false); - - return $context; - } -} diff --git a/src/Epp/ConnectionDriver/Support/WriteBuffer.php b/src/Epp/ConnectionDriver/Support/WriteBuffer.php deleted file mode 100644 index d4c0b59..0000000 --- a/src/Epp/ConnectionDriver/Support/WriteBuffer.php +++ /dev/null @@ -1,45 +0,0 @@ -connection = $connection; - $this->content = $content; - } - - public function write(): void - { - if (! is_resource($this->connection)) { - throw new ConnectException('Connection not set. Aborting..'); - } - - $this->content = $this->addContentLengthBlock($this->content); - - fwrite($this->connection, $this->content); - } - - private function addContentLengthBlock(string $content): string - { - $int = pack('N', intval(strlen($content) + 4)); - return $int . $content; - } -}