1
0
mirror of https://github.com/danog/phpseclib.git synced 2024-12-04 18:48:24 +01:00

SFTP: reopen channel on channel closure

This commit is contained in:
terrafrost 2021-05-11 20:20:46 -05:00
parent b07738814e
commit 9c47b0a696

View File

@ -310,6 +310,16 @@ class Net_SFTP extends Net_SSH2
*/ */
var $preserveTime = false; var $preserveTime = false;
/**
* Was the last packet due to the channels being closed or not?
*
* @see self::get()
* @see self::get_sftp_packet()
* @var bool
* @access private
*/
var $channel_close = false;
/** /**
* Default Constructor. * Default Constructor.
* *
@ -484,6 +494,17 @@ class Net_SFTP extends Net_SSH2
return false; return false;
} }
return $this->_init_sftp_connection();
}
/**
* (Re)initializes the SFTP channel
*
* @return bool
* @access private
*/
function _init_sftp_connection()
{
$this->window_size_server_to_client[NET_SFTP_CHANNEL] = $this->window_size; $this->window_size_server_to_client[NET_SFTP_CHANNEL] = $this->window_size;
$packet = pack( $packet = pack(
@ -2354,8 +2375,14 @@ class Net_SFTP extends Net_SSH2
if ($fclose_check) { if ($fclose_check) {
fclose($fp); fclose($fp);
} }
// maybe the file was successfully transferred, maybe it wasn't
if ($this->channel_close) {
$this->_init_sftp_connection();
return false;
} else {
user_error('Expected SSH_FX_DATA or SSH_FXP_STATUS'); user_error('Expected SSH_FX_DATA or SSH_FXP_STATUS');
} }
}
$response = null; $response = null;
} }
@ -3116,6 +3143,8 @@ class Net_SFTP extends Net_SSH2
*/ */
function _get_sftp_packet($request_id = null) function _get_sftp_packet($request_id = null)
{ {
$this->channel_close = false;
if (isset($request_id) && isset($this->requestBuffer[$request_id])) { if (isset($request_id) && isset($this->requestBuffer[$request_id])) {
$this->packet_type = $this->requestBuffer[$request_id]['packet_type']; $this->packet_type = $this->requestBuffer[$request_id]['packet_type'];
$temp = $this->requestBuffer[$request_id]['packet']; $temp = $this->requestBuffer[$request_id]['packet'];
@ -3132,7 +3161,10 @@ class Net_SFTP extends Net_SSH2
// SFTP packet length // SFTP packet length
while (strlen($this->packet_buffer) < 4) { while (strlen($this->packet_buffer) < 4) {
$temp = $this->_get_channel_packet(NET_SFTP_CHANNEL, true); $temp = $this->_get_channel_packet(NET_SFTP_CHANNEL, true);
if (is_bool($temp)) { if ($temp === true) {
if ($this->channel_status[NET_SFTP_CHANNEL] === NET_SSH2_MSG_CHANNEL_CLOSE) {
$this->channel_close = true;
}
$this->packet_type = false; $this->packet_type = false;
$this->packet_buffer = ''; $this->packet_buffer = '';
return false; return false;