|
| 1 | +--- |
| 2 | +name: ruby-proxy-headers |
| 3 | +description: >- |
| 4 | + Send and receive custom headers during HTTPS CONNECT tunneling in Ruby. |
| 5 | + Use when integrating proxy headers with Net::HTTP, Faraday, HTTParty, or Excon. |
| 6 | +--- |
| 7 | + |
| 8 | +# ruby-proxy-headers |
| 9 | + |
| 10 | +Send custom headers to proxies and receive proxy response headers during HTTPS CONNECT tunneling. |
| 11 | + |
| 12 | +## Installation |
| 13 | + |
| 14 | +```bash |
| 15 | +gem install ruby-proxy-headers |
| 16 | +``` |
| 17 | + |
| 18 | +Or in Gemfile: |
| 19 | + |
| 20 | +```ruby |
| 21 | +gem 'ruby-proxy-headers' |
| 22 | +``` |
| 23 | + |
| 24 | +Install faraday, httparty, or excon as needed. |
| 25 | + |
| 26 | +## Quick Reference |
| 27 | + |
| 28 | +| Library | Module | Key Methods | |
| 29 | +|---------|--------|-------------| |
| 30 | +| Net::HTTP | `ruby_proxy_headers/net_http` | `patch!`, `proxy_connect_request_headers=` | |
| 31 | +| Faraday | `ruby_proxy_headers/faraday` | `FaradayIntegration.connection` | |
| 32 | +| HTTParty | `ruby_proxy_headers/httparty` | `ProxyHeadersConnectionAdapter` | |
| 33 | +| Excon | `ruby_proxy_headers/excon` | `ExconIntegration.get` (send-only) | |
| 34 | + |
| 35 | +## Usage Patterns |
| 36 | + |
| 37 | +### Net::HTTP |
| 38 | + |
| 39 | +```ruby |
| 40 | +require 'uri' |
| 41 | +require 'openssl' |
| 42 | +require 'ruby_proxy_headers/net_http' |
| 43 | + |
| 44 | +RubyProxyHeaders::NetHTTP.patch! |
| 45 | + |
| 46 | +uri = URI('https://api.ipify.org?format=json') |
| 47 | +proxy = URI(ENV.fetch('PROXY_URL')) |
| 48 | + |
| 49 | +http = Net::HTTP.new(uri.host, uri.port, proxy.host, proxy.port, proxy.user, proxy.password) |
| 50 | +http.use_ssl = true |
| 51 | + |
| 52 | +http.proxy_connect_request_headers = { 'X-ProxyMesh-IP' => '203.0.113.1' } |
| 53 | + |
| 54 | +res = http.request(Net::HTTP::Get.new(uri)) |
| 55 | +puts http.last_proxy_connect_response_headers['X-ProxyMesh-IP'] |
| 56 | +``` |
| 57 | + |
| 58 | +Call `RubyProxyHeaders::NetHTTP.patch!` once before creating connections. |
| 59 | + |
| 60 | +### Faraday |
| 61 | + |
| 62 | +```ruby |
| 63 | +require 'ruby_proxy_headers/faraday' |
| 64 | + |
| 65 | +conn = RubyProxyHeaders::FaradayIntegration.connection( |
| 66 | + proxy: ENV.fetch('PROXY_URL'), |
| 67 | + proxy_connect_headers: { 'X-ProxyMesh-Country' => 'US' } |
| 68 | +) |
| 69 | + |
| 70 | +res = conn.get('https://api.ipify.org?format=json') |
| 71 | +puts res.headers['X-ProxyMesh-IP'] |
| 72 | +``` |
| 73 | + |
| 74 | +### HTTParty |
| 75 | + |
| 76 | +```ruby |
| 77 | +require 'httparty' |
| 78 | +require 'ruby_proxy_headers/httparty' |
| 79 | + |
| 80 | +RubyProxyHeaders::NetHTTP.patch! |
| 81 | + |
| 82 | +proxy = URI(ENV.fetch('PROXY_URL')) |
| 83 | +HTTParty.get( |
| 84 | + 'https://api.ipify.org?format=json', |
| 85 | + http_proxyaddr: proxy.host, |
| 86 | + http_proxyport: proxy.port, |
| 87 | + http_proxyuser: proxy.user, |
| 88 | + http_proxypass: proxy.password, |
| 89 | + proxy_connect_request_headers: { 'X-ProxyMesh-IP' => '203.0.113.1' }, |
| 90 | + connection_adapter: RubyProxyHeaders::ProxyHeadersConnectionAdapter |
| 91 | +) |
| 92 | + |
| 93 | +puts RubyProxyHeaders.proxy_connect_response_headers['X-ProxyMesh-IP'] |
| 94 | +``` |
| 95 | + |
| 96 | +### Excon (send-only) |
| 97 | + |
| 98 | +Excon supports sending headers on CONNECT but does not expose CONNECT response headers. |
| 99 | + |
| 100 | +```ruby |
| 101 | +require 'ruby_proxy_headers/excon' |
| 102 | + |
| 103 | +RubyProxyHeaders::ExconIntegration.get( |
| 104 | + 'https://api.ipify.org?format=json', |
| 105 | + proxy_url: ENV.fetch('PROXY_URL'), |
| 106 | + proxy_connect_headers: { 'X-ProxyMesh-Country' => 'US' } |
| 107 | +) |
| 108 | +``` |
| 109 | + |
| 110 | +## Reading Response Headers |
| 111 | + |
| 112 | +- **Net::HTTP**: `http.last_proxy_connect_response_headers` |
| 113 | +- **Faraday**: Merged into `response.headers` |
| 114 | +- **HTTParty**: `RubyProxyHeaders.proxy_connect_response_headers` (thread-local) |
| 115 | + |
| 116 | +## Proxy Headers |
| 117 | + |
| 118 | +Custom headers sent during CONNECT are proxy-specific. Check your proxy provider's docs. |
| 119 | + |
| 120 | +Example with [ProxyMesh](https://proxymesh.com): |
| 121 | + |
| 122 | +| Header | Direction | Purpose | |
| 123 | +|--------|-----------|---------| |
| 124 | +| `X-ProxyMesh-Country` | Send | Route through specific country | |
| 125 | +| `X-ProxyMesh-IP` | Send/Receive | Request or receive sticky IP | |
| 126 | + |
| 127 | +## Testing |
| 128 | + |
| 129 | +```bash |
| 130 | +export PROXY_URL='http://user:pass@proxy.example.com:8080' |
| 131 | +bundle exec ruby test/test_proxy_headers.rb -v |
| 132 | +``` |
0 commit comments