Skip to content

Instantly share code, notes, and snippets.

@SyuTingSong
Created October 26, 2023 08:06
Show Gist options
  • Select an option

  • Save SyuTingSong/1e1cb54bc89ffb33f97374cdd482dd4a to your computer and use it in GitHub Desktop.

Select an option

Save SyuTingSong/1e1cb54bc89ffb33f97374cdd482dd4a to your computer and use it in GitHub Desktop.

Revisions

  1. SyuTingSong created this gist Oct 26, 2023.
    272 changes: 272 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,272 @@
    diff --git a/vendor/phpseclib/phpseclib/phpseclib/Net/SFTP.php b/vendor/phpseclib/phpseclib/phpseclib/Net/SFTP.php
    index cdf0bec..32ecd33 100644
    --- a/vendor/phpseclib/phpseclib/phpseclib/Net/SFTP.php
    +++ b/vendor/phpseclib/phpseclib/phpseclib/Net/SFTP.php
    @@ -598,7 +598,7 @@ class SFTP extends SSH2
    $response = $this->get_sftp_packet();
    if ($this->packet_type != NET_SFTP_VERSION) {
    throw new \UnexpectedValueException('Expected NET_SFTP_VERSION. '
    - . 'Got packet type: ' . $this->packet_type);
    + . 'Got packet type: ' . $this->get_packet_type_name($response));
    }

    $this->use_request_id = true;
    @@ -667,7 +667,7 @@ class SFTP extends SSH2
    $response = $this->get_sftp_packet();
    if ($this->packet_type != NET_SFTP_STATUS) {
    throw new \UnexpectedValueException('Expected NET_SFTP_STATUS. '
    - . 'Got packet type: ' . $this->packet_type);
    + . 'Got packet type: ' . $this->get_packet_type_name($response));
    }
    list($status) = Strings::unpackSSH2('N', $response);
    if ($status != NET_SFTP_STATUS_OK) {
    @@ -880,7 +880,7 @@ class SFTP extends SSH2
    return false;
    default:
    throw new \UnexpectedValueException('Expected NET_SFTP_NAME or NET_SFTP_STATUS. '
    - . 'Got packet type: ' . $this->packet_type);
    + . 'Got packet type: ' . $this->get_packet_type_name($response));
    }
    }

    @@ -955,7 +955,7 @@ class SFTP extends SSH2
    return false;
    default:
    throw new \UnexpectedValueException('Expected NET_SFTP_HANDLE or NET_SFTP_STATUS' .
    - 'Got packet type: ' . $this->packet_type);
    + 'Got packet type: ' . $this->get_packet_type_name($response));
    }

    if (!$this->close_handle($handle)) {
    @@ -1107,7 +1107,7 @@ class SFTP extends SSH2
    return $status;
    default:
    throw new \UnexpectedValueException('Expected NET_SFTP_HANDLE or NET_SFTP_STATUS. '
    - . 'Got packet type: ' . $this->packet_type);
    + . 'Got packet type: ' . $this->get_packet_type_name($response));
    }

    $this->update_stat_cache($dir, []);
    @@ -1162,7 +1162,7 @@ class SFTP extends SSH2
    break 2;
    default:
    throw new \UnexpectedValueException('Expected NET_SFTP_NAME or NET_SFTP_STATUS. '
    - . 'Got packet type: ' . $this->packet_type);
    + . 'Got packet type: ' . $this->get_packet_type_name($response));
    }
    }

    @@ -1517,7 +1517,7 @@ class SFTP extends SSH2
    }

    throw new \UnexpectedValueException('Expected NET_SFTP_ATTRS or NET_SFTP_STATUS. '
    - . 'Got packet type: ' . $this->packet_type);
    + . 'Got packet type: ' . $this->get_packet_type_name($response));
    }

    /**
    @@ -1584,7 +1584,7 @@ class SFTP extends SSH2
    break;
    default:
    throw new \UnexpectedValueException('Expected NET_SFTP_HANDLE or NET_SFTP_STATUS. '
    - . 'Got packet type: ' . $this->packet_type);
    + . 'Got packet type: ' . $this->get_packet_type_name($response));
    }

    return $this->setstat($filename, $attr, false);
    @@ -1707,7 +1707,7 @@ class SFTP extends SSH2
    }

    throw new \UnexpectedValueException('Expected NET_SFTP_ATTRS or NET_SFTP_STATUS. '
    - . 'Got packet type: ' . $this->packet_type);
    + . 'Got packet type: ' . $this->get_packet_type_name($response));
    }

    /**
    @@ -1755,7 +1755,7 @@ class SFTP extends SSH2
    $response = $this->get_sftp_packet();
    if ($this->packet_type != NET_SFTP_STATUS) {
    throw new \UnexpectedValueException('Expected NET_SFTP_STATUS. '
    - . 'Got packet type: ' . $this->packet_type);
    + . 'Got packet type: ' . $this->get_packet_type_name($response));
    }

    list($status) = Strings::unpackSSH2('N', $response);
    @@ -1868,7 +1868,7 @@ class SFTP extends SSH2
    return false;
    default:
    throw new \UnexpectedValueException('Expected NET_SFTP_NAME or NET_SFTP_STATUS. '
    - . 'Got packet type: ' . $this->packet_type);
    + . 'Got packet type: ' . $this->get_packet_type_name($response));
    }

    list($count) = Strings::unpackSSH2('N', $response);
    @@ -1936,7 +1936,7 @@ class SFTP extends SSH2
    $response = $this->get_sftp_packet();
    if ($this->packet_type != NET_SFTP_STATUS) {
    throw new \UnexpectedValueException('Expected NET_SFTP_STATUS. '
    - . 'Got packet type: ' . $this->packet_type);
    + . 'Got packet type: ' . $this->get_packet_type_name($response));
    }

    list($status) = Strings::unpackSSH2('N', $response);
    @@ -1996,7 +1996,7 @@ class SFTP extends SSH2
    $response = $this->get_sftp_packet();
    if ($this->packet_type != NET_SFTP_STATUS) {
    throw new \UnexpectedValueException('Expected NET_SFTP_STATUS. '
    - . 'Got packet type: ' . $this->packet_type);
    + . 'Got packet type: ' . $this->get_packet_type_name($response));
    }

    list($status) = Strings::unpackSSH2('N', $response);
    @@ -2035,7 +2035,7 @@ class SFTP extends SSH2
    $response = $this->get_sftp_packet();
    if ($this->packet_type != NET_SFTP_STATUS) {
    throw new \UnexpectedValueException('Expected NET_SFTP_STATUS. '
    - . 'Got packet type: ' . $this->packet_type);
    + . 'Got packet type: ' . $this->get_packet_type_name($response));
    }

    list($status) = Strings::unpackSSH2('N', $response);
    @@ -2158,7 +2158,7 @@ class SFTP extends SSH2
    return false;
    default:
    throw new \UnexpectedValueException('Expected NET_SFTP_HANDLE or NET_SFTP_STATUS. '
    - . 'Got packet type: ' . $this->packet_type);
    + . 'Got packet type: ' . $this->get_packet_type_name($response));
    }

    // http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-8.2.3
    @@ -2300,7 +2300,7 @@ class SFTP extends SSH2
    $response = $this->get_sftp_packet();
    if ($this->packet_type != NET_SFTP_STATUS) {
    throw new \UnexpectedValueException('Expected NET_SFTP_STATUS. '
    - . 'Got packet type: ' . $this->packet_type);
    + . 'Got packet type: ' . $this->get_packet_type_name($response));
    }

    list($status) = Strings::unpackSSH2('N', $response);
    @@ -2329,7 +2329,7 @@ class SFTP extends SSH2
    $response = $this->get_sftp_packet();
    if ($this->packet_type != NET_SFTP_STATUS) {
    throw new \UnexpectedValueException('Expected NET_SFTP_STATUS. '
    - . 'Got packet type: ' . $this->packet_type);
    + . 'Got packet type: ' . $this->get_packet_type_name($response));
    }

    list($status) = Strings::unpackSSH2('N', $response);
    @@ -2385,7 +2385,7 @@ class SFTP extends SSH2
    return false;
    default:
    throw new \UnexpectedValueException('Expected NET_SFTP_HANDLE or NET_SFTP_STATUS. '
    - . 'Got packet type: ' . $this->packet_type);
    + . 'Got packet type: ' . $this->get_packet_type_name($response));
    }

    if (is_resource($local_file)) {
    @@ -2478,7 +2478,7 @@ class SFTP extends SSH2
    return false;
    } else {
    throw new \UnexpectedValueException('Expected NET_SFTP_DATA or NET_SFTP_STATUS. '
    - . 'Got packet type: ' . $this->packet_type);
    + . 'Got packet type: ' . $this->get_packet_type_name($response));
    }
    }
    $response = null;
    @@ -2540,7 +2540,7 @@ class SFTP extends SSH2
    $response = $this->get_sftp_packet();
    if ($this->packet_type != NET_SFTP_STATUS) {
    throw new \UnexpectedValueException('Expected NET_SFTP_STATUS. '
    - . 'Got packet type: ' . $this->packet_type);
    + . 'Got packet type: ' . $this->get_packet_type_name($response));
    }

    // if $status isn't SSH_FX_OK it's probably SSH_FX_NO_SUCH_FILE or SSH_FX_PERMISSION_DENIED
    @@ -2725,7 +2725,7 @@ class SFTP extends SSH2
    return false;
    default:
    throw new \UnexpectedValueException('Expected NET_SFTP_HANDLE or NET_SFTP_STATUS. '
    - . 'Got packet type: ' . $this->packet_type);
    + . 'Got packet type: ' . $this->get_packet_type_name($response));
    }
    }

    @@ -2752,7 +2752,7 @@ class SFTP extends SSH2
    return false;
    default:
    throw new \UnexpectedValueException('Expected SSH_FXP_HANDLE or SSH_FXP_STATUS. '
    - . 'Got packet type: ' . $this->packet_type);
    + . 'Got packet type: ' . $this->get_packet_type_name($response));
    }
    }

    @@ -2992,7 +2992,7 @@ class SFTP extends SSH2
    $response = $this->get_sftp_packet();
    if ($this->packet_type != NET_SFTP_STATUS) {
    throw new \UnexpectedValueException('Expected NET_SFTP_STATUS. '
    - . 'Got packet type: ' . $this->packet_type);
    + . 'Got packet type: ' . $this->get_packet_type_name($response));
    }

    // if $status isn't SSH_FX_OK it's probably SSH_FX_NO_SUCH_FILE or SSH_FX_PERMISSION_DENIED
    @@ -3278,7 +3278,9 @@ class SFTP extends SSH2
    $stop = microtime(true);

    if (defined('NET_SFTP_LOGGING')) {
    - $packet_type = '-> ' . self::$packet_types[$type] .
    + $packet_type = '-> ' .
    + ($this->use_request_id ? ('[' . str_pad(strval($request_id), 2, '0', STR_PAD_LEFT) . ']') : '') .
    + self::$packet_types[$type] .
    ' (' . round($stop - $start, 4) . 's)';
    $this->append_log($packet_type, $data);
    }
    @@ -3297,6 +3299,20 @@ class SFTP extends SSH2
    $this->requestBuffer = [];
    }

    + private function get_packet_type_name($packet)
    + {
    + if (is_bool($this->packet_type)) {
    + return $this->packet_type ? 'true' : 'false';
    + }
    +
    + if ($this->packet_type === NET_SFTP_STATUS) {
    + [, $status] = \Safe\unpack('N', $packet);
    + return self::$status_codes[$status] ?? 'NET_SFTP_STATUS_UNKNOWN';
    + }
    +
    + return self::$packet_types[$this->packet_type] ?? 'NET_SFTP_UNKNOWN';
    + }
    +
    /**
    * Receives SFTP Packets
    *
    @@ -3317,6 +3333,15 @@ class SFTP extends SSH2
    $this->packet_type = $this->requestBuffer[$request_id]['packet_type'];
    $temp = $this->requestBuffer[$request_id]['packet'];
    unset($this->requestBuffer[$request_id]);
    +
    + if (defined('NET_SFTP_LOGGING')) {
    + $packet_type = '-< ' .
    + '[' . str_pad(strval($request_id), 2, '0', STR_PAD_LEFT) . ']' .
    + $this->get_packet_type_name($temp) .
    + ' (' . round($stop - $start, 4) . 's)' . '[buffer]';
    + $this->append_log($packet_type, $packet);
    + }
    +
    return $temp;
    }

    @@ -3382,8 +3407,10 @@ class SFTP extends SSH2
    $packet = Strings::shift($this->packet_buffer, $length);

    if (defined('NET_SFTP_LOGGING')) {
    - $packet_type = '<- ' . self::$packet_types[$this->packet_type] .
    - ' (' . round($stop - $start, 4) . 's)';
    + $packet_type = '<- ' .
    + ($this->use_request_id && isset($packet_id) ? ('[' . str_pad(strval($packet_id), 2, '0', STR_PAD_LEFT) . ']') : '') .
    + $this->get_packet_type_name($packet) .
    + ' (' . round($stop - $start, 4) . 's)' . (isset($request_id) && $this->use_request_id && $packet_id != $request_id ? '[buffer]' : '');
    $this->append_log($packet_type, $packet);
    }