当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript SmartBuffer.readUInt16BE方法代码示例

本文整理汇总了TypeScript中smart-buffer.SmartBuffer.readUInt16BE方法的典型用法代码示例。如果您正苦于以下问题:TypeScript SmartBuffer.readUInt16BE方法的具体用法?TypeScript SmartBuffer.readUInt16BE怎么用?TypeScript SmartBuffer.readUInt16BE使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在smart-buffer.SmartBuffer的用法示例。


在下文中一共展示了SmartBuffer.readUInt16BE方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。

示例1: handleSocks5IncomingConnectionResponse

  /**
   * Handles Socks v5 incoming connection request (BIND).
   */
  private handleSocks5IncomingConnectionResponse() {
    // Peek at available data (we need at least 5 bytes to get the hostname length)
    const header = this._receiveBuffer.peek(5);

    if (header[0] !== 0x05 || header[1] !== Socks5Response.Granted) {
      this._closeSocket(
        `${ERRORS.Socks5ProxyRejectedIncomingBoundConnection} - ${
          Socks5Response[header[1]]
        }`
      );
    } else {
      // Read address type
      const addressType = header[3];

      let remoteHost: SocksRemoteHost;
      let buff: SmartBuffer;

      // IPv4
      if (addressType === Socks5HostType.IPv4) {
        // Check if data is available.
        const dataNeeded = SOCKS_INCOMING_PACKET_SIZES.Socks5ResponseIPv4;
        if (this._receiveBuffer.length < dataNeeded) {
          this._nextRequiredPacketBufferSize = dataNeeded;
          return;
        }

        buff = SmartBuffer.fromBuffer(
          this._receiveBuffer.get(dataNeeded).slice(4)
        );

        remoteHost = {
          host: ip.fromLong(buff.readUInt32BE()),
          port: buff.readUInt16BE()
        };

        // If given host is 0.0.0.0, assume remote proxy ip instead.
        if (remoteHost.host === '0.0.0.0') {
          remoteHost.host = this._options.proxy.ipaddress;
        }

        // Hostname
      } else if (addressType === Socks5HostType.Hostname) {
        const hostLength = header[4];
        const dataNeeded = SOCKS_INCOMING_PACKET_SIZES.Socks5ResponseHostname(
          hostLength
        ); // header + host length + port

        // Check if data is available.
        if (this._receiveBuffer.length < dataNeeded) {
          this._nextRequiredPacketBufferSize = dataNeeded;
          return;
        }

        buff = SmartBuffer.fromBuffer(
          this._receiveBuffer.get(dataNeeded).slice(5) // Slice at 5 to skip host length
        );

        remoteHost = {
          host: buff.readString(hostLength),
          port: buff.readUInt16BE()
        };
        // IPv6
      } else if (addressType === Socks5HostType.IPv6) {
        // Check if data is available.
        const dataNeeded = SOCKS_INCOMING_PACKET_SIZES.Socks5ResponseIPv6;
        if (this._receiveBuffer.length < dataNeeded) {
          this._nextRequiredPacketBufferSize = dataNeeded;
          return;
        }

        buff = SmartBuffer.fromBuffer(
          this._receiveBuffer.get(dataNeeded).slice(4)
        );

        remoteHost = {
          host: ip.toString(buff.readBuffer(16)),
          port: buff.readUInt16BE()
        };
      }

      this.state = SocksClientState.Established;
      this.removeInternalSocketHandlers();
      this.emit('established', { socket: this._socket, remoteHost });
    }
  }
开发者ID:JoshGlazebrook,项目名称:socks,代码行数:88,代码来源:socksclient.ts

示例2: handleSocks5FinalHandshakeResponse

  /**
   * Handles Socks v5 final handshake response.
   * @param data
   */
  private handleSocks5FinalHandshakeResponse() {
    // Peek at available data (we need at least 5 bytes to get the hostname length)
    const header = this._receiveBuffer.peek(5);

    if (header[0] !== 0x05 || header[1] !== Socks5Response.Granted) {
      this._closeSocket(
        `${ERRORS.InvalidSocks5FinalHandshakeRejected} - ${
          Socks5Response[header[1]]
        }`
      );
    } else {
      // Read address type
      const addressType = header[3];

      let remoteHost: SocksRemoteHost;
      let buff: SmartBuffer;

      // IPv4
      if (addressType === Socks5HostType.IPv4) {
        // Check if data is available.
        const dataNeeded = SOCKS_INCOMING_PACKET_SIZES.Socks5ResponseIPv4;
        if (this._receiveBuffer.length < dataNeeded) {
          this._nextRequiredPacketBufferSize = dataNeeded;
          return;
        }

        buff = SmartBuffer.fromBuffer(
          this._receiveBuffer.get(dataNeeded).slice(4)
        );

        remoteHost = {
          host: ip.fromLong(buff.readUInt32BE()),
          port: buff.readUInt16BE()
        };

        // If given host is 0.0.0.0, assume remote proxy ip instead.
        if (remoteHost.host === '0.0.0.0') {
          remoteHost.host = this._options.proxy.ipaddress;
        }

        // Hostname
      } else if (addressType === Socks5HostType.Hostname) {
        const hostLength = header[4];
        const dataNeeded = SOCKS_INCOMING_PACKET_SIZES.Socks5ResponseHostname(
          hostLength
        ); // header + host length + host + port

        // Check if data is available.
        if (this._receiveBuffer.length < dataNeeded) {
          this._nextRequiredPacketBufferSize = dataNeeded;
          return;
        }

        buff = SmartBuffer.fromBuffer(
          this._receiveBuffer.get(dataNeeded).slice(5) // Slice at 5 to skip host length
        );

        remoteHost = {
          host: buff.readString(hostLength),
          port: buff.readUInt16BE()
        };
        // IPv6
      } else if (addressType === Socks5HostType.IPv6) {
        // Check if data is available.
        const dataNeeded = SOCKS_INCOMING_PACKET_SIZES.Socks5ResponseIPv6;
        if (this._receiveBuffer.length < dataNeeded) {
          this._nextRequiredPacketBufferSize = dataNeeded;
          return;
        }

        buff = SmartBuffer.fromBuffer(
          this._receiveBuffer.get(dataNeeded).slice(4)
        );

        remoteHost = {
          host: ip.toString(buff.readBuffer(16)),
          port: buff.readUInt16BE()
        };
      }

      // We have everything we need
      this.state = SocksClientState.ReceivedFinalResponse;

      // If using CONNECT, the client is now in the established state.
      if (SocksCommand[this._options.command] === SocksCommand.connect) {
        this.state = SocksClientState.Established;
        this.removeInternalSocketHandlers();
        this.emit('established', { socket: this._socket });
      } else if (SocksCommand[this._options.command] === SocksCommand.bind) {
        /* If using BIND, the Socks client is now in BoundWaitingForConnection state.
           This means that the remote proxy server is waiting for a remote connection to the bound port. */
        this.state = SocksClientState.BoundWaitingForConnection;
        this._nextRequiredPacketBufferSize =
          SOCKS_INCOMING_PACKET_SIZES.Socks5ResponseHeader;
        this.emit('bound', { socket: this._socket, remoteHost });
        /*
//.........这里部分代码省略.........
开发者ID:JoshGlazebrook,项目名称:socks,代码行数:101,代码来源:socksclient.ts


注:本文中的smart-buffer.SmartBuffer.readUInt16BE方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。