Class: ESSH::Transport::Session
- Inherits:
-
Object
- Object
- ESSH::Transport::Session
- Includes:
- Net::SSH::Loggable, Net::SSH::Transport::Constants
- Defined in:
- lib/evented-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.
Defined Under Namespace
Classes: ServerVersion
Constant Summary collapse
- DEFAULT_PORT =
The standard port for the SSH protocol.
22
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.
-
#reactor ⇒ Object
readonly
The event loop that this SSH session is running on.
-
#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.
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 ⇒ 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.
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.
63 64 65 66 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 |
# File 'lib/evented-ssh/transport/session.rb', line 63 def initialize(host, **) self.logger = [:logger] @reactor = ::Libuv.reactor @host = host @port = [:port] || DEFAULT_PORT @bind_address = [:bind_address] || '0.0.0.0' @options = debug { "establishing connection to #{@host}:#{@port}" } actual_host = if IPAddress.valid?(@host) @host else @reactor.lookup(@host)[0][0] end @socket = PacketStream.new(self, **) @socket.connect(actual_host, @port) debug { "connection established" } @host_key_verifier = select_host_key_verifier([:paranoid]) @algorithms = Algorithms.new(self, ) @server_version = ServerVersion.new @socket.algorithms = @algorithms socket.direct_write "#{::Net::SSH::Transport::ServerVersion::PROTO_VERSION}\r\n" socket.start_read @algorithms.ready # Wait for this to complete end |
Instance Attribute Details
#algorithms ⇒ Object (readonly)
The Algorithms instance used to perform key exchanges.
48 49 50 |
# File 'lib/evented-ssh/transport/session.rb', line 48 def algorithms @algorithms end |
#host ⇒ Object (readonly)
The host to connect to, as given to the constructor.
33 34 35 |
# File 'lib/evented-ssh/transport/session.rb', line 33 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.
52 53 54 |
# File 'lib/evented-ssh/transport/session.rb', line 52 def host_key_verifier @host_key_verifier end |
#options ⇒ Object (readonly)
The hash of options that were given to the object at initialization.
55 56 57 |
# File 'lib/evented-ssh/transport/session.rb', line 55 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.
37 38 39 |
# File 'lib/evented-ssh/transport/session.rb', line 37 def port @port end |
#reactor ⇒ Object (readonly)
The event loop that this SSH session is running on
58 59 60 |
# File 'lib/evented-ssh/transport/session.rb', line 58 def reactor @reactor end |
#server_version ⇒ Object (readonly)
The ServerVersion instance that encapsulates the negotiated protocol version.
45 46 47 |
# File 'lib/evented-ssh/transport/session.rb', line 45 def server_version @server_version end |
#socket ⇒ Object (readonly)
The underlying socket object being used to communicate with the remote host.
41 42 43 |
# File 'lib/evented-ssh/transport/session.rb', line 41 def socket @socket end |
Instance Method Details
#close ⇒ Object
Cleans up (see PacketStream#cleanup) and closes the underlying socket.
129 130 131 132 |
# File 'lib/evented-ssh/transport/session.rb', line 129 def close info { "closing connection" } socket.shutdown end |
#closed? ⇒ Boolean
Returns true if the underlying socket has been closed.
124 125 126 |
# File 'lib/evented-ssh/transport/session.rb', line 124 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.
206 207 208 |
# File 'lib/evented-ssh/transport/session.rb', line 206 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.
213 214 215 |
# File 'lib/evented-ssh/transport/session.rb', line 213 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.
199 200 201 |
# File 'lib/evented-ssh/transport/session.rb', line 199 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).
219 220 221 |
# File 'lib/evented-ssh/transport/session.rb', line 219 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.
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/evented-ssh/transport/session.rb', line 106 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 != host string2 = peer_ip string2 = "[#{string2}]:#{port}" if port != DEFAULT_PORT string << "," << string2 end string end end |
#host_keys ⇒ Object
97 98 99 100 101 102 |
# File 'lib/evented-ssh/transport/session.rb', line 97 def host_keys @host_keys ||= begin known_hosts = .fetch(:known_hosts, ::Net::SSH::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.
175 176 177 |
# File 'lib/evented-ssh/transport/session.rb', line 175 def socket.get_packet 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).
169 170 171 |
# File 'lib/evented-ssh/transport/session.rb', line 169 def peer @peer ||= { ip: socket.peer_ip, port: @port.to_i, host: @host, canonized: host_as_string } end |
#poll_message ⇒ Object
179 180 181 |
# File 'lib/evented-ssh/transport/session.rb', line 179 def socket.get_packet 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.
186 187 188 189 |
# File 'lib/evented-ssh/transport/session.rb', line 186 def push(packet) socket.queue_packet(packet) process_waiting 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.
152 153 154 155 156 157 |
# File 'lib/evented-ssh/transport/session.rb', line 152 def rekey! if !algorithms.pending? algorithms.rekey! @algorithms.pending?&.promise&.value # Wait for this to complete 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.
162 163 164 165 |
# File 'lib/evented-ssh/transport/session.rb', line 162 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.
193 194 195 |
# File 'lib/evented-ssh/transport/session.rb', line 193 def () socket.enqueue_packet() end |
#service_request(service) ⇒ Object
Returns a new service_request packet for the given service name, ready for sending to the server.
145 146 147 |
# File 'lib/evented-ssh/transport/session.rb', line 145 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).
138 139 140 141 |
# File 'lib/evented-ssh/transport/session.rb', line 138 def shutdown! error { "forcing connection closed" } socket.close end |