Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/lib/rex/proto/ms_nrtp/client.rb
Views: 18569
class Rex::Proto::MsNrtp::Client12require 'rex/stopwatch'3require 'rex/proto/ms_nrtp/ms_nrtp_message'45include Rex::Proto::MsNrtp67# @return [String] The MS-NRTP server host.8attr_reader :host910# @return [Integer] The S-NRTP server port.11attr_reader :port1213# @return [String] The server resource component of the URI string.14attr_reader :resource1516# @return [Boolean] Whether or not SSL is used for the connection.17attr_reader :ssl1819# @return [Rex::Socket::Comm] An optional, explicit object to use for creating the connection.20attr_reader :comm2122# @!attribute timeout23# @return [Integer] The communication timeout in seconds.24attr_accessor :timeout2526# @param [String] host The MS-NRTP server host.27# @param [Integer,NilClass] port The MS-NRTP server port or nil for automatic based on ssl.28# @param [Boolean] ssl Whether or not SSL is used for the connection.29# @param [String] ssl_version The SSL version to use.30# @param [Rex::Socket::Comm] comm An optional, explicit object to use for creating the connection.31# @param [Integer] timeout The communication timeout in seconds.32def initialize(host, port, resource, context: {}, ssl: false, ssl_version: nil, comm: nil, timeout: 10)33@host = host34@port = port35@resource = resource36@context = context37@ssl = ssl38@ssl_version = ssl_version39@comm = comm40@timeout = timeout41end4243# Establish the connection to the remote server.44#45# @param [Integer] t An explicit timeout to use for the connection otherwise the default will be used.46# @return [NilClass]47def connect(t = -1)48timeout = (t.nil? or t == -1) ? @timeout : t4950@conn = Rex::Socket::Tcp.create(51'PeerHost' => @host,52'PeerPort' => @port.to_i,53'Context' => @context,54'SSL' => @ssl,55'SSLVersion' => @ssl_version,56'Timeout' => timeout,57'Comm' => @comm58)5960nil61end6263# Close the connection to the remote server.64#65# @return [NilClass]66def close67if @conn && !@conn.closed?68@conn.shutdown69@conn.close70end7172@conn = nil73end7475def recv76remaining = @timeout77message, elapsed_time = Rex::Stopwatch.elapsed_time do78::Timeout.timeout(remaining) do79MsNrtpMessage.read(@conn)80end81end82return nil unless message.operation_type == Enums::OperationTypeEnum[:Reply] && message.content_length?8384remaining -= elapsed_time85body = ''86while body.length < message.content_length87chunk, elapsed_time = Rex::Stopwatch.elapsed_time do88@conn.read(message.content_length - body.length, remaining)89end90remaining -= elapsed_time91body << chunk92end9394body95end9697def send(data, content_type)98message = MsNrtpMessage.new(99content_length: data.length,100headers: [101{ token: MsNrtpHeader::MsNrtpHeaderUri::TOKEN, header: { uri_value: "tcp://#{Rex::Socket.to_authority(@host, @port)}/#{@resource}" } },102{ token: MsNrtpHeader::MsNrtpHeaderContentType::TOKEN, header: { content_type_value: content_type } },103{ token: MsNrtpHeader::MsNrtpHeaderEnd::TOKEN }104]105)106@conn.put(message.to_binary_s + data)107end108109def send_recv(data, content_type)110send(data, content_type)111recv112end113114def send_binary(serialized_stream)115send(serialized_stream.to_binary_s, 'application/octet-stream'.encode('UTF-8'))116end117end118119120