module HTTPX::Plugins::Proxy::SSH::InstanceMethods

  1. lib/httpx/plugins/proxy/ssh.rb

Methods

Public Class

  1. new

Public Instance

  1. request

Public Class methods

new(*)
[show source]
   # File lib/httpx/plugins/proxy/ssh.rb
24 def initialize(*)
25   super
26   @_gateway = nil
27 end

Public Instance methods

request(*args, **options)
[show source]
   # File lib/httpx/plugins/proxy/ssh.rb
29 def request(*args, **options)
30   raise ArgumentError, "must perform at least one request" if args.empty?
31 
32   requests = args.first.is_a?(Request) ? args : build_requests(*args, options)
33 
34   request = requests.first
35 
36   return super unless request
37 
38   request_options = request.options
39 
40   return super unless request_options.proxy
41 
42   ssh_options = request_options.proxy.dup
43   ssh_uris = Array(ssh_options.delete(:uri))
44 
45   proxy_ns = 0
46 
47   begin
48     ssh_uri = URI(ssh_uris[proxy_ns])
49 
50     return super unless ssh_uri.scheme == "ssh"
51 
52     ssh_username = ssh_options.delete(:username)
53     ssh_options[:port] ||= ssh_uri.port || 22
54     if request_options.debug
55       ssh_options[:verbose] = request_options.debug_level == 2 ? :debug : :info
56     end
57 
58     request_uri = URI(requests.first.uri)
59     @_gateway = Net::SSH::Gateway.new(ssh_uri.host, ssh_username, ssh_options)
60   rescue StandardError => e
61     log { "failed connecting to ssh proxy, trying next..." }
62     proxy_ns += 1
63     raise e if proxy_ns >= ssh_uris.size
64 
65     retry
66   end
67 
68   begin
69     @_gateway.open(request_uri.host, request_uri.port) do |local_port|
70       io = build_gateway_socket(local_port, request_uri, request_options)
71       super(*args, **options.merge(io: io))
72     end
73   ensure
74     @_gateway.shutdown!
75   end
76 end