Class: Net::SSH::Transport::Session
- Inherits:
-
Object
- Object
- Net::SSH::Transport::Session
- Defined in:
- lib/net/ssh/transport/session.rb
Overview
The transport layer represents the lowest level of the SSH protocol, and implements basic message exchanging and protocol initialization. It will never be instantiated directly (unless you really know what you're about), but will instead be created for you automatically when you create a new SSH session via Net::SSH.start.
Constant Summary collapse
- DEFAULT_PORT =
The standard port for the SSH protocol.
22- MAX_QUEUED_PACKETS =
Packets held back during a re-key are bounded so a server can't exhaust memory (GHSA-ggxh-cfwq-5xqc).
16_384- MAX_QUEUED_BYTES =
32 * 1024 * 1024
Constants included from Constants
Constants::DEBUG, Constants::DISCONNECT, Constants::EXT_INFO, Constants::IGNORE, Constants::KEXDH_GEX_GROUP, Constants::KEXDH_GEX_INIT, Constants::KEXDH_GEX_REPLY, Constants::KEXDH_GEX_REQUEST, Constants::KEXDH_INIT, Constants::KEXDH_REPLY, Constants::KEXECDH_INIT, Constants::KEXECDH_REPLY, Constants::KEXINIT, Constants::NEWKEYS, Constants::SERVICE_ACCEPT, Constants::SERVICE_REQUEST, Constants::UNIMPLEMENTED
Instance Attribute Summary collapse
-
#algorithms ⇒ Object
readonly
The Algorithms instance used to perform key exchanges.
-
#host ⇒ Object
readonly
The host to connect to, as given to the constructor.
-
#host_key_verifier ⇒ Object
readonly
The host-key verifier object used to verify host keys, to ensure that the connection is not being spoofed.
-
#options ⇒ Object
readonly
The hash of options that were given to the object at initialization.
-
#port ⇒ Object
readonly
The port number to connect to, as given in the options to the constructor.
-
#queue ⇒ Object
readonly
this method is primarily for use in tests.
-
#server_sig_algs ⇒ Object
readonly
The signature algorithms the server accepts for publickey authentication, from its RFC 8308 server-sig-algs extension, or nil if it did not send one.
-
#server_version ⇒ Object
readonly
The ServerVersion instance that encapsulates the negotiated protocol version.
-
#socket ⇒ Object
readonly
The underlying socket object being used to communicate with the remote host.
Attributes included from Loggable
Instance Method Summary collapse
-
#close ⇒ Object
Cleans up (see PacketStream#cleanup) and closes the underlying socket.
-
#closed? ⇒ Boolean
Returns true if the underlying socket has been closed.
-
#configure_client(options = {}) ⇒ Object
Configure's the packet stream's client state with the given set of options.
-
#configure_server(options = {}) ⇒ Object
Configure's the packet stream's server state with the given set of options.
-
#enqueue_message(message) ⇒ Object
Enqueues the given message, such that it will be sent at the earliest opportunity.
-
#hint(which, value = true) ⇒ Object
Sets a new hint for the packet stream, which the packet stream may use to change its behavior.
-
#host_as_string ⇒ Object
Returns the host (and possibly IP address) in a format compatible with SSH known-host files.
- #host_keys ⇒ Object
-
#initialize(host, options = {}) ⇒ Session
constructor
Instantiates a new transport layer abstraction.
-
#next_message ⇒ Object
Blocks until a new packet is available to be read, and returns that packet.
-
#peer ⇒ Object
Returns a hash of information about the peer (remote) side of the socket, including :ip, :port, :host, and :canonized (see #host_as_string).
-
#poll_message(mode = :nonblock, consume_queue = true) ⇒ Object
Tries to read the next packet from the socket.
- #pubkey_algorithms ⇒ Object
-
#push(packet) ⇒ Object
Adds the given packet to the packet queue.
-
#rekey! ⇒ Object
Requests a rekey operation, and blocks until the operation completes.
-
#rekey_as_needed ⇒ Object
Returns immediately if a rekey is already in process.
-
#send_message(message) ⇒ Object
Sends the given message via the packet stream, blocking until the entire message has been sent.
-
#service_request(service) ⇒ Object
Returns a new service_request packet for the given service name, ready for sending to the server.
-
#shutdown! ⇒ Object
Performs a "hard" shutdown of the connection.
-
#wait ⇒ Object
Waits (blocks) until the given block returns true.
Methods included from Loggable
#debug, #error, #fatal, #info, #lwarn
Constructor Details
#initialize(host, options = {}) ⇒ Session
Instantiates a new transport layer abstraction. This will block until the initial key exchange completes, leaving you with a ready-to-use transport session.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/net/ssh/transport/session.rb', line 67 def initialize(host, = {}) self.logger = [:logger] @host = host @port = [:port] || DEFAULT_PORT @bind_address = [:bind_address] || nil @options = @host_key_verifier = select_host_key_verifier([:verify_host_key]) @socket = if (factory = [:proxy]) debug { "establishing connection to #{@host}:#{@port} through proxy" } factory.open(@host, @port, ) else debug { "establishing connection to #{@host}:#{@port}" } Socket.tcp(@host, @port, @bind_address, nil, connect_timeout: [:timeout]) end @socket.extend(PacketStream) @socket.logger = @logger set_no_delay unless [:no_delay] == false debug { "connection established" } @queue = [] @queued_bytes = 0 @server_version = ServerVersion.new(socket, logger, [:timeout]) @algorithms = Algorithms.new(self, ) @algorithms.start wait { algorithms.initialized? } rescue Errno::ETIMEDOUT @socket&.close raise Net::SSH::ConnectionTimeout rescue StandardError @socket&.close raise end |
Instance Attribute Details
#algorithms ⇒ Object (readonly)
The Algorithms instance used to perform key exchanges.
50 51 52 |
# File 'lib/net/ssh/transport/session.rb', line 50 def algorithms @algorithms end |
#host ⇒ Object (readonly)
The host to connect to, as given to the constructor.
35 36 37 |
# File 'lib/net/ssh/transport/session.rb', line 35 def host @host end |
#host_key_verifier ⇒ Object (readonly)
The host-key verifier object used to verify host keys, to ensure that the connection is not being spoofed.
54 55 56 |
# File 'lib/net/ssh/transport/session.rb', line 54 def host_key_verifier @host_key_verifier end |
#options ⇒ Object (readonly)
The hash of options that were given to the object at initialization.
57 58 59 |
# File 'lib/net/ssh/transport/session.rb', line 57 def @options end |
#port ⇒ Object (readonly)
The port number to connect to, as given in the options to the constructor. If no port number was given, this will default to DEFAULT_PORT.
39 40 41 |
# File 'lib/net/ssh/transport/session.rb', line 39 def port @port end |
#queue ⇒ Object (readonly)
this method is primarily for use in tests
303 304 305 |
# File 'lib/net/ssh/transport/session.rb', line 303 def queue @queue end |
#server_sig_algs ⇒ Object (readonly)
The signature algorithms the server accepts for publickey authentication, from its RFC 8308 server-sig-algs extension, or nil if it did not send one.
62 63 64 |
# File 'lib/net/ssh/transport/session.rb', line 62 def server_sig_algs @server_sig_algs end |
#server_version ⇒ Object (readonly)
The ServerVersion instance that encapsulates the negotiated protocol version.
47 48 49 |
# File 'lib/net/ssh/transport/session.rb', line 47 def server_version @server_version end |
#socket ⇒ Object (readonly)
The underlying socket object being used to communicate with the remote host.
43 44 45 |
# File 'lib/net/ssh/transport/session.rb', line 43 def socket @socket end |
Instance Method Details
#close ⇒ Object
Cleans up (see PacketStream#cleanup) and closes the underlying socket.
145 146 147 148 |
# File 'lib/net/ssh/transport/session.rb', line 145 def close socket.cleanup socket.close end |
#closed? ⇒ Boolean
Returns true if the underlying socket has been closed.
140 141 142 |
# File 'lib/net/ssh/transport/session.rb', line 140 def closed? socket.closed? end |
#configure_client(options = {}) ⇒ Object
Configure's the packet stream's client state with the given set of options. This is typically used to define the cipher, compression, and hmac algorithms to use when sending packets to the server.
283 284 285 |
# File 'lib/net/ssh/transport/session.rb', line 283 def configure_client( = {}) socket.client.set() end |
#configure_server(options = {}) ⇒ Object
Configure's the packet stream's server state with the given set of options. This is typically used to define the cipher, compression, and hmac algorithms to use when reading packets from the server.
290 291 292 |
# File 'lib/net/ssh/transport/session.rb', line 290 def configure_server( = {}) socket.server.set() end |
#enqueue_message(message) ⇒ Object
Enqueues the given message, such that it will be sent at the earliest opportunity. This does not block, but returns immediately.
276 277 278 |
# File 'lib/net/ssh/transport/session.rb', line 276 def () socket.enqueue_packet() end |
#hint(which, value = true) ⇒ Object
Sets a new hint for the packet stream, which the packet stream may use to change its behavior. (See PacketStream#hints).
296 297 298 |
# File 'lib/net/ssh/transport/session.rb', line 296 def hint(which, value = true) socket.hints[which] = value end |
#host_as_string ⇒ Object
Returns the host (and possibly IP address) in a format compatible with SSH known-host files.
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/net/ssh/transport/session.rb', line 121 def host_as_string @host_as_string ||= begin string = +"#{host}" string = +"[#{string}]:#{port}" if port != DEFAULT_PORT peer_ip = socket.peer_ip if peer_ip != Net::SSH::Transport::PacketStream::PROXY_COMMAND_HOST_IP && peer_ip != host string2 = peer_ip string2 = "[#{string2}]:#{port}" if port != DEFAULT_PORT string << "," << string2 end string end end |
#host_keys ⇒ Object
108 109 110 111 112 113 |
# File 'lib/net/ssh/transport/session.rb', line 108 def host_keys @host_keys ||= begin known_hosts = .fetch(:known_hosts, KnownHosts) known_hosts.search_for([:host_key_alias] || host_as_string, ) end end |
#next_message ⇒ Object
Blocks until a new packet is available to be read, and returns that packet. See #poll_message.
192 193 194 |
# File 'lib/net/ssh/transport/session.rb', line 192 def (:block) end |
#peer ⇒ Object
Returns a hash of information about the peer (remote) side of the socket, including :ip, :port, :host, and :canonized (see #host_as_string).
186 187 188 |
# File 'lib/net/ssh/transport/session.rb', line 186 def peer @peer ||= { ip: socket.peer_ip, port: @port.to_i, host: @host, canonized: host_as_string } end |
#poll_message(mode = :nonblock, consume_queue = true) ⇒ Object
Tries to read the next packet from the socket. If mode is :nonblock (the default), this will not block and will return nil if there are no packets waiting to be read. Otherwise, this will block until a packet is available. Note that some packet types (DISCONNECT, IGNORE, UNIMPLEMENTED, DEBUG, and KEXINIT) are handled silently by this method, and will never be returned.
If a key-exchange is in process and a disallowed packet type is received, it will be enqueued and otherwise ignored. When a key-exchange is not in process, and consume_queue is true, packets will be first read from the queue before the socket is queried.
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/net/ssh/transport/session.rb', line 207 def (mode = :nonblock, consume_queue = true) loop do return shift_queue if consume_queue && @queue.any? && algorithms.allow?(@queue.first) packet = socket.next_packet(mode, [:timeout]) return nil if packet.nil? case packet.type when DISCONNECT raise Net::SSH::Disconnect, "disconnected: #{packet[:description]} (#{packet[:reason_code]})" when IGNORE debug { "IGNORE packet received: #{packet[:data].inspect}" } when UNIMPLEMENTED lwarn { "UNIMPLEMENTED: #{packet[:number]}" } when DEBUG send(packet[:always_display] ? :fatal : :debug) { packet[:message] } when EXT_INFO accept_ext_info(packet) when KEXINIT algorithms.accept_kexinit(packet) else return packet if algorithms.allow?(packet) # Algorithms#rekey! clears initialized?, so the session id is what marks the first key exchange as done. raise Net::SSH::Exception, "unexpected packet type #{packet.type} during initial key exchange" unless algorithms.session_id push(packet) end end end |
#pubkey_algorithms ⇒ Object
115 116 117 |
# File 'lib/net/ssh/transport/session.rb', line 115 def pubkey_algorithms algorithms[:pubkey_algorithms] end |
#push(packet) ⇒ Object
Adds the given packet to the packet queue. If the queue is non-empty, #poll_message will return packets from the queue in the order they were received.
259 260 261 262 263 264 265 266 |
# File 'lib/net/ssh/transport/session.rb', line 259 def push(packet) @queued_bytes += packet.length if @queue.size >= MAX_QUEUED_PACKETS || @queued_bytes > MAX_QUEUED_BYTES raise Net::SSH::Exception, "too many packets queued during key exchange" end @queue.push(packet) end |
#rekey! ⇒ Object
Requests a rekey operation, and blocks until the operation completes. If a rekey is already pending, this returns immediately, having no effect.
168 169 170 171 172 173 |
# File 'lib/net/ssh/transport/session.rb', line 168 def rekey! if !algorithms.pending? algorithms.rekey! wait { algorithms.initialized? } end end |
#rekey_as_needed ⇒ Object
Returns immediately if a rekey is already in process. Otherwise, if a rekey is needed (as indicated by the socket, see PacketStream#if_needs_rekey?) one is performed, causing this method to block until it completes.
178 179 180 181 182 |
# File 'lib/net/ssh/transport/session.rb', line 178 def rekey_as_needed return if algorithms.pending? socket.if_needs_rekey? { rekey! } end |
#send_message(message) ⇒ Object
Sends the given message via the packet stream, blocking until the entire message has been sent.
270 271 272 |
# File 'lib/net/ssh/transport/session.rb', line 270 def () socket.send_packet() end |
#service_request(service) ⇒ Object
Returns a new service_request packet for the given service name, ready for sending to the server.
161 162 163 |
# File 'lib/net/ssh/transport/session.rb', line 161 def service_request(service) Net::SSH::Buffer.from(:byte, SERVICE_REQUEST, :string, service) end |
#shutdown! ⇒ Object
Performs a "hard" shutdown of the connection. In general, this should never be done, but it might be necessary (in a rescue clause, for instance, when the connection needs to close but you don't know the status of the underlying protocol's state).
154 155 156 157 |
# File 'lib/net/ssh/transport/session.rb', line 154 def shutdown! error { "forcing connection closed" } socket.close end |
#wait ⇒ Object
Waits (blocks) until the given block returns true. If no block is given, this just waits long enough to see if there are any pending packets. Any packets read are enqueued (see #push).
246 247 248 249 250 251 252 253 254 |
# File 'lib/net/ssh/transport/session.rb', line 246 def wait loop do break if block_given? && yield = (:nonblock, false) push() if break if !block_given? end end |