Skip to content

Commit

Permalink
Merge branch '3.0-isconnected-tweak' into 3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
terrafrost committed Feb 26, 2024
2 parents b837466 + d27429a commit c2fb513
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions phpseclib/Net/SSH2.php
Expand Up @@ -3344,11 +3344,38 @@ public function __destruct()
/**
* Is the connection still active?
*
* $level has 3x possible values:
* 0 (default): phpseclib takes a passive approach to see if the connection is still active by calling feof()
* on the socket
* 1: phpseclib takes an active approach to see if the connection is still active by sending an SSH_MSG_IGNORE
* packet that doesn't require a response
* 2: phpseclib takes an active approach to see if the connection is still active by sending an SSH_MSG_CHANNEL_OPEN
* packet and imediately trying to close that channel. some routers, in particular, however, will only let you
* open one channel, so this approach could yield false positives
*
* @param int $level
* @return bool
*/
public function isConnected()
public function isConnected($level = 0)
{
return ($this->bitmap & self::MASK_CONNECTED) && is_resource($this->fsock) && !feof($this->fsock);
if (!is_int($level) || $level < 0 || $level > 2) {
throw new \InvalidArgumentException('$level must be 0, 1 or 2');
}

if ($level == 0) {
return ($this->bitmap & self::MASK_CONNECTED) && is_resource($this->fsock) && !feof($this->fsock);
}
try {
if ($level == 1) {
$this->send_binary_packet(pack('CN', NET_SSH2_MSG_IGNORE, 0));
} else {
$this->openChannel(self::CHANNEL_KEEP_ALIVE);
$this->close_channel(self::CHANNEL_KEEP_ALIVE);
}
return true;
} catch (\Exception $e) {
return false;
}
}

/**
Expand Down

0 comments on commit c2fb513

Please sign in to comment.