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.2.0'.freeze

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Gateway.

Parameters:

  • host (String)
  • user (String)
  • gateway (Net::SSH::Gateway) (defaults to: nil)

    allow passing in an existing gateway. host and user will be ignored.



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

Parameters:

  • host (String)
  • port (Integer)
  • connection_options (Hash) (defaults to: nil)

Returns:

  • (Socket)


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

def open(host, port, connection_options = nil)
  ensure_connected!

  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, self) do |io, local_port, gateway|
    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

#shutdown!Object

Shuts down the Net::SSH::Gateway instance



52
53
54
55
56
# File 'lib/net/ssh/proxy/gateway.rb', line 52

def shutdown!
  @gateway&.shutdown!
  sleep 0.5 until !@gateway&.active?
  @gateway = nil
end