ssh proxy: added support for https requests too

This commit is contained in:
HoneyryderChuck 2018-12-23 02:24:54 +00:00
parent cf7b4bff94
commit 72f5fbdfc9

View File

@ -6,10 +6,10 @@ module HTTPX
module Plugins module Plugins
module Proxy module Proxy
module SSH module SSH
def self.load_dependencies(klass, *) def self.load_dependencies(_klass, *)
# klass.plugin(:proxy) # klass.plugin(:proxy)
require "net/ssh/gateway" require "net/ssh/gateway"
end end
module InstanceMethods module InstanceMethods
def with_proxy(*args) def with_proxy(*args)
@ -31,13 +31,33 @@ module HTTPX
@_gateway = Net::SSH::Gateway.new(ssh_uri.host, ssh_username, ssh_options) @_gateway = Net::SSH::Gateway.new(ssh_uri.host, ssh_username, ssh_options)
begin begin
@_gateway.open(request_uri.host, request_uri.port) do |local_port| @_gateway.open(request_uri.host, request_uri.port) do |local_port|
io = TCPSocket.open("localhost", local_port) io = build_gateway_socket(local_port, request_uri)
super(*requests, **options.merge(io: io)) super(*requests, **options.merge(io: io))
end end
ensure ensure
@_gateway.shutdown! @_gateway.shutdown!
end end
end end
def build_gateway_socket(port, request_uri)
case request_uri.scheme
when "https"
ctx = OpenSSL::SSL::SSLContext.new
ctx_options = SSL::TLS_OPTIONS.merge(@options.ssl)
ctx.set_params(ctx_options) unless ctx_options.empty?
sock = TCPSocket.open("localhost", port)
io = OpenSSL::SSL::SSLSocket.new(sock, ctx)
io.hostname = request_uri.host
io.sync_close = true
io.connect
io.post_connection_check(request_uri.host) if ctx.verify_mode != OpenSSL::SSL::VERIFY_NONE
io
when "http"
TCPSocket.open("localhost", port)
else
raise Error, "unexpected scheme: #{request_uri.scheme}"
end
end
end end
module OptionsMethods module OptionsMethods