8000 Merge branch '3.0-isconnected-tweak' into 3.0 · phpseclib/phpseclib@c2fb513 · GitHub
[go: up one dir, main page]

Skip to content

Commit c2fb513

Browse files
committed
Merge branch '3.0-isconnected-tweak' into 3.0
2 parents b837466 + d27429a commit c2fb513

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

phpseclib/Net/SSH2.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3344,11 +3344,38 @@ public function __destruct()
33443344
/**
33453345
* Is the connection still active?
33463346
*
3347+
* $level has 3x possible values:
3348+
* 0 (default): phpseclib takes a passive approach to see if the connection is still active by calling feof()
3349+
* on the socket
3350+
* 1: phpseclib takes an active approach to see if the connection is still active by sending an SSH_MSG_IGNORE
3351+
* packet that doesn't require a response
3352+
* 2: phpseclib takes an active approach to see if the connection is still active by sending an SSH_MSG_CHANNEL_OPEN
3353+
* packet and imediately trying to close that channel. some routers, in particular, however, will only let you
3354+
* open one channel, so this approach could yield false positives
3355+
*
3356+
* @param int $level
33473357
* @return bool
33483358
*/
3349-
public function isConnected()
3359+
public function isConnected($level = 0)
33503360
{
3351-
return ($this->bitmap & self::MASK_CONNECTED) && is_resource($this->fsock) && !feof($this->fsock);
3361+
if (!is_int($level) || $level < 0 || $level > 2) {
3362+
throw new \InvalidArgumentException('$level must be 0, 1 or 2');
3363+
}
3364+
3365+
if ($level == 0) {
3366+
return ($this->bitmap & self::MASK_CONNECTED) && is_resource($this->fsock) && !feof($this->fsock);
3367+
}
3368+
try {
3369+
if ($level == 1) {
3370+
$this->send_binary_packet(pack('CN', NET_SSH2_MSG_IGNORE, 0));
3371+
} else {
3372+
$this->openChannel(self::CHANNEL_KEEP_ALIVE);
3373+
$this->close_channel(self::CHANNEL_KEEP_ALIVE);
3374+
}
3375+
return true;
3376+
} catch (\Exception $e) {
3377+
return false;
3378+
}
33523379
}
33533380

33543381
/**

0 commit comments

Comments
 (0)
0