Class: Net::SSH::Proxy::Gateway

Inherits:
Object
  • Object
show all
Defined in:
lib/net/ssh/proxy/gateway.rb,
lib/net/ssh/proxy/gateway/version.rb

Overview

Net::SSH::Proxy::Gateway is like any Net::SSH::Proxy (Command, Jump, SOCKS, ..) and can be passed to Net::SSH.start through the proxy: keyword.

Internally it uses Net::SSH::Gateway from the net-ssh-gateway gem.

Constant Summary collapse

VERSION =
'0.1.0'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(host, user, gateway = nil, **options) ⇒ Gateway


16
17
18
19
20
21
22
# File 'lib/net/ssh/proxy/gateway.rb', line 16

def initialize(host, user, gateway = nil, **options)
  if gateway
    @gateway = gateway
  else
    @gateway_options = [host, user, options]
  end
end

Instance Method Details

#gatewayObject


24
25
26
# File 'lib/net/ssh/proxy/gateway.rb', line 24

def gateway
  @gateway ||= Net::SSH::Gateway.new(*@gateway_options)
end

#open(host, port, connection_options = nil) ⇒ Socket


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/net/ssh/proxy/gateway.rb', line 32

def open(host, port, connection_options = nil)
  local_port = gateway.open(host, connection_options&.dig(:port) || 22)
  io = Socket.tcp('localhost'.freeze, local_port, nil, nil, connect_timeout: connection_options&.dig(:timeout))

  Thread.new(io, local_port) do |io, local_port|
    Thread.current.report_on_exception = false
    Thread.pass until io.closed?
    gateway.close(local_port)
  end

  io
rescue StandardError => ex
  io&.close unless io&.closed?
  @gateway&.close(local_port) if local_port
  raise
end