Class: RubyProxyHeaders::Connection
- Inherits:
-
Object
- Object
- RubyProxyHeaders::Connection
- Defined in:
- lib/ruby_proxy_headers/connection.rb
Overview
Handles HTTPS CONNECT tunneling with custom proxy headers. This is the core component that manages the proxy connection, sends custom headers, and captures proxy response headers.
Instance Attribute Summary collapse
-
#proxy_response_headers ⇒ Object
readonly
Returns the value of attribute proxy_response_headers.
-
#proxy_response_status ⇒ Object
readonly
Returns the value of attribute proxy_response_status.
-
#socket ⇒ Object
readonly
Returns the value of attribute socket.
Instance Method Summary collapse
-
#close ⇒ Object
Close the connection.
-
#connect(target_host, target_port = 443) ⇒ OpenSSL::SSL::SSLSocket
Establish a tunnel through the proxy to the target host.
-
#initialize(proxy, options = {}) ⇒ Connection
constructor
A new instance of Connection.
Constructor Details
#initialize(proxy, options = {}) ⇒ Connection
Returns a new instance of Connection.
19 20 21 22 23 24 25 26 27 |
# File 'lib/ruby_proxy_headers/connection.rb', line 19 def initialize(proxy, = {}) @proxy = proxy.is_a?(String) ? RubyProxyHeaders.parse_proxy_url(proxy) : proxy @proxy_headers = [:proxy_headers] || {} @connect_timeout = [:connect_timeout] || 30 @verify_ssl = .fetch(:verify_ssl, true) @proxy_response_headers = {} @proxy_response_status = nil @socket = nil end |
Instance Attribute Details
#proxy_response_headers ⇒ Object (readonly)
Returns the value of attribute proxy_response_headers.
12 13 14 |
# File 'lib/ruby_proxy_headers/connection.rb', line 12 def proxy_response_headers @proxy_response_headers end |
#proxy_response_status ⇒ Object (readonly)
Returns the value of attribute proxy_response_status.
12 13 14 |
# File 'lib/ruby_proxy_headers/connection.rb', line 12 def proxy_response_status @proxy_response_status end |
#socket ⇒ Object (readonly)
Returns the value of attribute socket.
12 13 14 |
# File 'lib/ruby_proxy_headers/connection.rb', line 12 def socket @socket end |
Instance Method Details
#close ⇒ Object
Close the connection
57 58 59 |
# File 'lib/ruby_proxy_headers/connection.rb', line 57 def close @socket&.close end |
#connect(target_host, target_port = 443) ⇒ OpenSSL::SSL::SSLSocket
Establish a tunnel through the proxy to the target host.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/ruby_proxy_headers/connection.rb', line 33 def connect(target_host, target_port = 443) # Connect to proxy @socket = TCPSocket.new(@proxy[:host], @proxy[:port]) @socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1) # Send CONNECT request with custom headers connect_request = build_connect_request(target_host, target_port) @socket.write(connect_request) # Read and parse CONNECT response response = read_connect_response parse_connect_response(response) # Check for successful connection unless (200..299).cover?(@proxy_response_status) @socket.close raise_connect_error end # Upgrade to TLS upgrade_to_tls(target_host) end |