Class: RubyProxyHeaders::Connection

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(proxy, options = {}) ⇒ Connection

Returns a new instance of Connection.

Parameters:

  • proxy (String, Hash)

    Proxy URL or hash with :host, :port, :user, :password

  • options (Hash) (defaults to: {})

    Connection options

Options Hash (options):

  • :proxy_headers (Hash)

    Custom headers to send during CONNECT

  • :connect_timeout (Integer)

    Connection timeout in seconds (default: 30)

  • :verify_ssl (Boolean)

    Verify SSL certificates (default: true)



19
20
21
22
23
24
25
26
27
# File 'lib/ruby_proxy_headers/connection.rb', line 19

def initialize(proxy, options = {})
  @proxy = proxy.is_a?(String) ? RubyProxyHeaders.parse_proxy_url(proxy) : proxy
  @proxy_headers = options[:proxy_headers] || {}
  @connect_timeout = options[:connect_timeout] || 30
  @verify_ssl = options.fetch(:verify_ssl, true)
  @proxy_response_headers = {}
  @proxy_response_status = nil
  @socket = nil
end

Instance Attribute Details

#proxy_response_headersObject (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_statusObject (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

#socketObject (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

#closeObject

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.

Parameters:

  • target_host (String)

    Target hostname

  • target_port (Integer) (defaults to: 443)

    Target port (default: 443)

Returns:

  • (OpenSSL::SSL::SSLSocket)

    TLS-wrapped socket to target



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