Module: HTTPX::Plugins::Proxy::SSH::InstanceMethods

Defined in:
lib/httpx/plugins/proxy/ssh.rb,
sig/plugins/proxy/ssh.rbs

Instance Method Summary collapse

Instance Method Details

#initialize ⇒ Object



24
25
26
27
# File 'lib/httpx/plugins/proxy/ssh.rb', line 24

def initialize(*)
  super
  @_gateway = nil
end

#request(*args, **options) ⇒ Object

Raises:

  • (ArgumentError)


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/httpx/plugins/proxy/ssh.rb', line 29

def request(*args, **options)
  raise ArgumentError, "must perform at least one request" if args.empty?

  requests = args.first.is_a?(Request) ? args : build_requests(*args, options)

  request = requests.first

  return super unless request

  request_options = request.options

  return super unless request_options.proxy

  ssh_options = request_options.proxy.dup
  ssh_uris = Array(ssh_options.delete(:uri))

  proxy_ns = 0

  begin
    ssh_uri = URI(ssh_uris[proxy_ns])

    return super unless ssh_uri.scheme == "ssh"

    ssh_username = ssh_options.delete(:username)
    ssh_options[:port] ||= ssh_uri.port || 22
    if request_options.debug
      ssh_options[:verbose] = request_options.debug_level == 2 ? :debug : :info
    end

    request_uri = URI(requests.first.uri)
    @_gateway = Net::SSH::Gateway.new(ssh_uri.host, ssh_username, ssh_options)
  rescue StandardError => e
    log { "failed connecting to ssh proxy, trying next..." }
    proxy_ns += 1
    raise e if proxy_ns >= ssh_uris.size

    retry
  end

  begin
    @_gateway.open(request_uri.host, request_uri.port) do |local_port|
      io = build_gateway_socket(local_port, request_uri, request_options)
      super(*args, **options.merge(io: io))
    end
  ensure
    @_gateway.shutdown!
  end
end