Sha256: 5fb36281fff329f9d1e1e97c5bea1070f9d0169e5fe8e835ebffbd79161486d9

Contents?: true

Size: 1.81 KB

Versions: 28

Compression:

Stored size: 1.81 KB

Contents

require 'uri'
require 'net/ssh/proxy/command'

module Net
  module SSH
    module Proxy
      # An implementation of a jump proxy. To use it, instantiate it,
      # then pass the instantiated object via the :proxy key to
      # Net::SSH.start:
      #
      #   require 'net/ssh/proxy/jump'
      #
      #   proxy = Net::SSH::Proxy::Jump.new('user@proxy')
      #   Net::SSH.start('host', 'user', :proxy => proxy) do |ssh|
      #     ...
      #   end
      class Jump < Command
        # The jump proxies
        attr_reader :jump_proxies

        # Create a new socket factory that tunnels via multiple jump proxes as
        # [user@]host[:port].
        def initialize(jump_proxies)
          @jump_proxies = jump_proxies
        end

        # Return a new socket connected to the given host and port via the jump
        # proxy that was requested when the socket factory was instantiated.
        def open(host, port, connection_options = nil)
          build_proxy_command_equivalent(connection_options)
          super
        end

        # We cannot build the ProxyCommand template until we know if the :config
        # option was specified during `Net::SSH.start`.
        def build_proxy_command_equivalent(connection_options = nil)
          first_jump, extra_jumps = jump_proxies.split(",", 2)
          config = connection_options && connection_options[:config]
          uri = URI.parse("ssh://#{first_jump}")

          template = "ssh".dup
          template << " -l #{uri.user}"    if uri.user
          template << " -p #{uri.port}"    if uri.port
          template << " -J #{extra_jumps}" if extra_jumps
          template << " -F #{config}" if config != true && config
          template << " -W %h:%p "
          template << uri.host

          @command_line_template = template
        end
      end
    end
  end
end

Version data entries

28 entries across 28 versions & 6 rubygems

Version Path
net-ssh-7.3.0 lib/net/ssh/proxy/jump.rb
net-ssh-7.3.0.rc1 lib/net/ssh/proxy/jump.rb
harbr-2.8.1 vendor/bundle/ruby/3.2.0/gems/net-ssh-7.2.1/lib/net/ssh/proxy/jump.rb
net-ssh-7.2.3 lib/net/ssh/proxy/jump.rb
net-ssh-backports-6.3.6.backports lib/net/ssh/proxy/jump.rb
net-ssh-backports-6.3.5.backports lib/net/ssh/proxy/jump.rb
net-ssh-backports-6.3.4.backports lib/net/ssh/proxy/jump.rb
net-ssh-backports-6.3.3.backports lib/net/ssh/proxy/jump.rb
net-ssh-backports-6.3.2.backports lib/net/ssh/proxy/jump.rb
net-ssh-backports-6.3.1.backports lib/net/ssh/proxy/jump.rb
net-ssh-backports-6.3.0.backports lib/net/ssh/proxy/jump.rb
net-ssh-7.2.2.rc1 lib/net/ssh/proxy/jump.rb
net-ssh-7.2.1 lib/net/ssh/proxy/jump.rb
net-ssh-7.2.1.rc1 lib/net/ssh/proxy/jump.rb
honeybadger-5.4.0 vendor/bundle/ruby/3.2.0/gems/net-ssh-7.2.0/lib/net/ssh/proxy/jump.rb
honeybadger-5.3.0 vendor/bundle/ruby/3.2.0/gems/net-ssh-7.2.0/lib/net/ssh/proxy/jump.rb
net-ssh-7.2.0 lib/net/ssh/proxy/jump.rb
net-ssh-7.2.0.rc1 lib/net/ssh/proxy/jump.rb
net-ssh-7.2.0.beta1 lib/net/ssh/proxy/jump.rb
net-ssh-7.1.0 lib/net/ssh/proxy/jump.rb