27 if ($this->stream !==
null) {
31 $host = strval($this->config[
"host"]);
32 $port = strval($this->config[
"port"]);
33 $timeout = intval($this->config[
"timeout"]);
34 $blocking = intval($this->config[
"blocking"]);
36 if (empty($this->config[
"ssh"])) {
37 $address =
"tcp://" . (str_contains($host,
":") ?
"[" . $host .
"]" : $host) .
":" . $port;
38 $options = empty($this->config[
"tls"]) ? [] : [
"ssl" => [
"allow_self_signed" =>
true,
"verify_peer" =>
false,
"verify_peer_name" =>
false]];
40 $this->stream = @stream_socket_client($address, $errno, $errstr, $this->config[
"timeout"], STREAM_CLIENT_CONNECT, stream_context_create($options));
42 if ($this->stream ===
false) {
43 throw new TransportException(StringHelper::factory($errstr)->toUtf8()->toString(), $errno);
46 if (!empty($this->config[
"tls"])) {
47 stream_socket_enable_crypto($this->stream,
true, STREAM_CRYPTO_METHOD_SSLv23_CLIENT);
50 $this->session = @ssh2_connect($host, $port);
52 if ($this->session ===
false) {
53 throw new TransportException(
"failed to establish secure shell connection to server '" . $this->config[
"host"] .
":" . $this->config[
"port"] .
"'");
56 if (!@ssh2_auth_password($this->session, $this->config[
"username"], $this->config[
"password"])) {
60 $this->stream = @ssh2_shell($this->session,
"raw");
62 if ($this->stream ===
false) {
63 throw new TransportException(
"failed to open a secure shell on server '" . $this->config[
"host"] .
":" . $this->config[
"port"] .
"'");
67 @stream_set_timeout($this->stream, $timeout);
68 @stream_set_blocking($this->stream, $blocking ? 1 : 0);