diff --git a/CHANGELOG.md b/CHANGELOG.md index 4bd405f..6c55425 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Require Ruby >= 3.2. Dependabot resolves Bundler updates against the gemspec floor, and Ruby 3.1 cannot select nokogiri 1.19 (the release line that installs on the integration job's current Ruby). - Development dependencies for the Typhoeus adapter are `typhoeus` `~> 1.7` and `ethon` `>= 0.19.1`, `< 0.20`. The gem still has no runtime dependency on Typhoeus. +- Gem metadata includes homepage, changelog, and bug tracker URIs. The published gem ships the guides under `docs/` and leaves the internal research notes out. +- Faraday, HTTParty, Excon, and Net::HTTP guides describe `FaradayIntegration`, `ProxyHeadersConnectionAdapter`, `ExconIntegration`, and `NetHTTP.patch!`. ### Fixed diff --git a/docs/excon.md b/docs/excon.md index ba51702..796c845 100644 --- a/docs/excon.md +++ b/docs/excon.md @@ -1,84 +1,29 @@ # Excon -[Excon](https://github.com/excon/excon) is a fast, simple HTTP(S) client. The `RubyProxyHeaders::Excon` module adds proxy header support. +[Excon](https://github.com/excon/excon) already sends extra CONNECT headers as `:ssl_proxy_headers`. `RubyProxyHeaders::ExconIntegration.get` passes them through. + +Excon does not expose the CONNECT response on the origin response, so `X-ProxyMesh-IP` is not readable from this client. See [DEFERRED.md](../DEFERRED.md). ## Installation ```ruby -gem 'excon' -gem 'ruby_proxy_headers' +gem 'excon', '~> 1.4' +gem 'ruby-proxy-headers' ``` -## Basic Usage +## Usage ```ruby -require 'ruby_proxy_headers' +require 'ruby_proxy_headers/excon' -response = RubyProxyHeaders::Excon.get( +response = RubyProxyHeaders::ExconIntegration.get( 'https://api.ipify.org?format=json', - proxy: 'http://user:pass@proxy.example.com:8080', - proxy_headers: { 'X-ProxyMesh-Country' => 'US' } -) - -puts "Status: #{response.code}" -puts "Body: #{response.body}" -puts "Proxy IP: #{response.proxy_response_headers['x-proxymesh-ip']}" -``` - -## API Reference - -### `RubyProxyHeaders::Excon.get(url, options)` - -Make a GET request. - -**Parameters:** - -- `url` (String) - Target URL -- `options` (Hash): - - `:proxy` (String) - Proxy URL (required) - - `:proxy_headers` (Hash) - Headers to send to proxy - - `:headers` (Hash) - Request headers - -**Returns:** `ProxyResponse` - -### `RubyProxyHeaders::Excon.post(url, options)` - -Make a POST request. - -**Parameters:** - -- Same as `get`, plus: - - `:body` (String) - Request body - -### `RubyProxyHeaders::Excon.request(method, url, options)` - -Make a request with any HTTP method. - -## Response Object - -```ruby -response.status # HTTP status code (alias: code) -response.body # Response body -response.headers # Response headers -response.proxy_response_headers # Proxy CONNECT response headers -``` - -## Example: AWS SDK Compatible - -Excon is commonly used with AWS SDKs. Here's how to use proxy headers with AWS: - -```ruby -require 'ruby_proxy_headers' - -response = RubyProxyHeaders::Excon.get( - 'https://sts.amazonaws.com/?Action=GetCallerIdentity&Version=2011-06-15', - proxy: ENV['PROXY_URL'], - proxy_headers: { 'X-ProxyMesh-Country' => 'US' }, - headers: { - 'Authorization' => aws_signature, - 'X-Amz-Date' => Time.now.utc.strftime('%Y%m%dT%H%M%SZ') - } + proxy_url: 'http://user:pass@proxy.example.com:31280', + proxy_connect_headers: { 'X-ProxyMesh-Country' => 'US' } ) +puts response.status puts response.body ``` + +Header names are documented at . diff --git a/docs/faraday.md b/docs/faraday.md index d688688..4a773e7 100644 --- a/docs/faraday.md +++ b/docs/faraday.md @@ -1,122 +1,29 @@ # Faraday -[Faraday](https://lostisland.github.io/faraday/) is a popular HTTP client library with a middleware architecture. The `RubyProxyHeaders::Faraday` module provides middleware for proxy header support. +[Faraday](https://lostisland.github.io/faraday/) 2.x with `faraday-net_http` 3.4 or 3.5. `RubyProxyHeaders::FaradayIntegration` builds a connection whose adapter merges CONNECT response headers into `response.headers`. -## Installation - -Add Faraday to your Gemfile: - -```ruby -gem 'faraday' -gem 'ruby_proxy_headers' -``` +Proxy header names are documented at . -## Basic Usage - -### Using Middleware +## Installation ```ruby -require 'faraday' -require 'ruby_proxy_headers' - -conn = Faraday.new(url: 'https://api.ipify.org') do |f| - f.use RubyProxyHeaders::Faraday::Middleware, - proxy: 'http://user:pass@proxy.example.com:8080', - proxy_headers: { 'X-ProxyMesh-Country' => 'US' } - f.adapter Faraday.default_adapter -end - -response = conn.get('/?format=json') - -puts "Status: #{response.status}" -puts "Body: #{response.body}" -puts "Proxy IP: #{response.env[:proxy_response_headers]['x-proxymesh-ip']}" +gem 'faraday', '~> 2.14' +gem 'faraday-net_http', '~> 3.4' +gem 'ruby-proxy-headers' ``` -### Using Helper Method +## Usage ```ruby -require 'ruby_proxy_headers' +require 'ruby_proxy_headers/faraday' -conn = RubyProxyHeaders::Faraday.create_connection( - 'https://api.ipify.org', - proxy: 'http://user:pass@proxy:8080', - proxy_headers: { 'X-ProxyMesh-Country' => 'US' } +conn = RubyProxyHeaders::FaradayIntegration.connection( + proxy: 'http://user:pass@proxy.example.com:31280', + proxy_connect_headers: { 'X-ProxyMesh-Country' => 'US' } ) -response = conn.get('/?format=json') -puts response.body -``` - -## Middleware Options - -### `RubyProxyHeaders::Faraday::Middleware` - -**Options:** - -- `:proxy` (String) - Proxy URL (required) -- `:proxy_headers` (Hash) - Headers to send to proxy during CONNECT -- `:on_proxy_connect` (Proc) - Callback when proxy connection is established - -### Callback Example - -```ruby -conn = Faraday.new do |f| - f.use RubyProxyHeaders::Faraday::Middleware, - proxy: 'http://proxy:8080', - proxy_headers: { 'X-Country' => 'US' }, - on_proxy_connect: ->(headers) { - puts "Connected via: #{headers['x-proxymesh-ip']}" - } - f.adapter Faraday.default_adapter -end -``` - -## Accessing Proxy Headers - -Proxy response headers are stored in the Faraday environment: - -```ruby -response = conn.get('/') -proxy_headers = response.env[:proxy_response_headers] - -puts proxy_headers['x-proxymesh-ip'] -puts proxy_headers['x-proxymesh-country'] +response = conn.get('https://api.ipify.org?format=json') +puts response.headers['X-ProxyMesh-IP'] ``` -## API Reference - -### `RubyProxyHeaders::Faraday.create_connection(url, options)` - -Create a Faraday connection with proxy header support. - -**Parameters:** - -- `url` (String) - Base URL -- `options` (Hash): - - `:proxy` (String) - Proxy URL (required) - - `:proxy_headers` (Hash) - Headers to send to proxy - - Additional Faraday options - -**Returns:** `Faraday::Connection` - -## Example: Session Persistence - -```ruby -require 'faraday' -require 'ruby_proxy_headers' - -session_id = "session-#{SecureRandom.hex(8)}" - -conn = Faraday.new do |f| - f.use RubyProxyHeaders::Faraday::Middleware, - proxy: 'http://user:pass@proxyhost:port', - proxy_headers: { 'X-ProxyMesh-Session' => session_id } - f.adapter Faraday.default_adapter -end - -5.times do |i| - response = conn.get('https://api.ipify.org?format=json') - puts "Request #{i + 1}: #{response.body}" -end -``` +`proxy_connect_headers` are sent only on the CONNECT request. Origin headers are the normal Faraday request headers. When both sides use the same header name, the origin value is kept. diff --git a/docs/getting-started.md b/docs/getting-started.md index 252153c..4be1727 100644 --- a/docs/getting-started.md +++ b/docs/getting-started.md @@ -1,136 +1,66 @@ # Getting Started -This guide will help you get started with ruby-proxy-headers. - ## Installation -### Bundler (Recommended) - -Add to your `Gemfile`: - ```ruby -gem 'ruby_proxy_headers' +gem 'ruby-proxy-headers' ``` -Then run: - ```bash -bundle install +gem install ruby-proxy-headers ``` -### Direct Installation +Install `faraday` and `faraday-net_http`, `httparty`, `http` (>= 6), `rest-client`, `typhoeus` (~> 1.7), or `excon` only for the client you use. -```bash -gem install ruby_proxy_headers -``` +Proxy header names are documented at . Examples below use `http://user:pass@proxy.example.com:31280`. -## Basic Usage - -### 1. Import the Library +## Net::HTTP ```ruby -require 'ruby_proxy_headers' +require 'uri' +require 'ruby_proxy_headers/net_http' + +RubyProxyHeaders::NetHTTP.patch! + +uri = URI('https://api.ipify.org?format=json') +proxy = URI('http://user:pass@proxy.example.com:31280') +http = Net::HTTP.new(uri.host, uri.port, proxy.host, proxy.port, proxy.user, proxy.password) +http.use_ssl = true +http.proxy_connect_request_headers = { 'X-ProxyMesh-Country' => 'US' } +response = http.request(Net::HTTP::Get.new(uri)) +puts http.last_proxy_connect_response_headers['X-ProxyMesh-IP'] ``` -### 2. Make a Request with Proxy Headers - -Choose the module that matches your HTTP library: - -=== "Net::HTTP" - - ```ruby - response = RubyProxyHeaders::NetHTTP.get( - 'https://example.com', - proxy: 'http://user:pass@proxy:8080', - proxy_headers: { 'X-Custom-Header' => 'value' } - ) - ``` - -=== "Faraday" - - ```ruby - require 'faraday' - - conn = Faraday.new do |f| - f.use RubyProxyHeaders::Faraday::Middleware, - proxy: 'http://user:pass@proxy:8080', - proxy_headers: { 'X-Custom-Header' => 'value' } - f.adapter Faraday.default_adapter - end - - response = conn.get('https://example.com') - ``` - -=== "HTTParty" - - ```ruby - response = RubyProxyHeaders::HTTParty.get( - 'https://example.com', - proxy: 'http://user:pass@proxy:8080', - proxy_headers: { 'X-Custom-Header' => 'value' } - ) - ``` - -### 3. Access Proxy Response Headers +## Faraday ```ruby -# All modules provide proxy_response_headers -puts response.proxy_response_headers['x-proxymesh-ip'] -``` +require 'ruby_proxy_headers/faraday' -## Configuration Options - -### Proxy URL Format - -```ruby -# Full format -proxy: 'http://username:password@proxy.example.com:8080' - -# Without authentication -proxy: 'http://proxy.example.com:8080' +conn = RubyProxyHeaders::FaradayIntegration.connection( + proxy: 'http://user:pass@proxy.example.com:31280', + proxy_connect_headers: { 'X-ProxyMesh-Country' => 'US' } +) +puts conn.get('https://api.ipify.org?format=json').headers['X-ProxyMesh-IP'] ``` -### Proxy Headers +## HTTParty ```ruby -proxy_headers: { - 'X-ProxyMesh-Country' => 'US', - 'X-ProxyMesh-Session' => 'session123', - 'X-Custom-Header' => 'custom-value' -} +require 'httparty' +require 'ruby_proxy_headers/httparty' + +RubyProxyHeaders::NetHTTP.patch! +proxy = URI('http://user:pass@proxy.example.com:31280') +HTTParty.get( + 'https://api.ipify.org?format=json', + http_proxyaddr: proxy.host, + http_proxyport: proxy.port, + http_proxyuser: proxy.user, + http_proxypass: proxy.password, + proxy_connect_request_headers: { 'X-ProxyMesh-Country' => 'US' }, + connection_adapter: RubyProxyHeaders::ProxyHeadersConnectionAdapter +) +puts RubyProxyHeaders.proxy_connect_response_headers['X-ProxyMesh-IP'] ``` -## Environment Variables - -You can configure the proxy via environment variables: - -```bash -export PROXY_URL='http://user:pass@proxy.example.com:8080' -export HTTPS_PROXY='http://proxy.example.com:8080' -``` - -The libraries will use `HTTPS_PROXY` or `HTTP_PROXY` as fallback if no proxy is specified. - -## Error Handling - -```ruby -begin - response = RubyProxyHeaders::NetHTTP.get( - 'https://example.com', - proxy: 'http://proxy:8080', - proxy_headers: { 'X-Custom' => 'value' } - ) -rescue RubyProxyHeaders::ProxyAuthenticationError => e - puts "Proxy auth failed: #{e.message}" -rescue RubyProxyHeaders::ConnectError => e - puts "Proxy connection failed: #{e.message}" -rescue StandardError => e - puts "Error: #{e.message}" -end -``` - -## Next Steps - -- Explore library-specific guides in the sidebar -- Check the [Core API](core-api.md) for advanced usage -- See [Testing](testing.md) for running integration tests +Per-library pages: [Net::HTTP](net-http.md), [Faraday](faraday.md), [HTTParty](httparty.md), [HTTP.rb](http-gem.md), [Typhoeus](typhoeus.md), [RestClient](rest-client.md), [Excon](excon.md). diff --git a/docs/http-gem.md b/docs/http-gem.md index e2285d4..64b37af 100644 --- a/docs/http-gem.md +++ b/docs/http-gem.md @@ -6,7 +6,7 @@ ```ruby gem 'http' -gem 'ruby_proxy_headers' +gem 'ruby-proxy-headers' ``` ## Basic Usage @@ -14,10 +14,10 @@ gem 'ruby_proxy_headers' ### Using Client ```ruby -require 'ruby_proxy_headers' +require 'ruby_proxy_headers/http_gem' client = RubyProxyHeaders::HTTPGem.create_client( - proxy: 'http://user:pass@proxy.example.com:8080', + proxy: 'http://user:pass@proxy.example.com:31280', proxy_headers: { 'X-ProxyMesh-Country' => 'US' } ) @@ -31,11 +31,11 @@ puts "Proxy IP: #{client.last_proxy_response_headers['x-proxymesh-ip']}" ### Standalone Methods ```ruby -require 'ruby_proxy_headers' +require 'ruby_proxy_headers/http_gem' response = RubyProxyHeaders::HTTPGem.get( 'https://api.ipify.org?format=json', - proxy: 'http://proxy:8080', + proxy: 'http://user:pass@proxy.example.com:31280', proxy_headers: { 'X-Country' => 'US' } ) @@ -93,7 +93,7 @@ client.delete(url) ```ruby client = RubyProxyHeaders::HTTPGem.create_client( - proxy: 'http://proxy:8080', + proxy: 'http://user:pass@proxy.example.com:31280', proxy_headers: { 'X-Country' => 'US' } ) @@ -106,7 +106,7 @@ puts client.last_proxy_response_headers['x-proxymesh-ip'] ## Example: Chained Requests ```ruby -require 'ruby_proxy_headers' +require 'ruby_proxy_headers/http_gem' client = RubyProxyHeaders::HTTPGem.create_client( proxy: ENV['PROXY_URL'], diff --git a/docs/httparty.md b/docs/httparty.md index bb3f1e8..6d91549 100644 --- a/docs/httparty.md +++ b/docs/httparty.md @@ -1,136 +1,35 @@ # HTTParty -[HTTParty](https://github.com/jnunemaker/httparty) makes HTTP fun! The `RubyProxyHeaders::HTTParty` module provides both standalone functions and a mixin for class-based usage. +[HTTParty](https://github.com/jnunemaker/httparty) uses Net::HTTP. Pass `RubyProxyHeaders::ProxyHeadersConnectionAdapter` and call `RubyProxyHeaders::NetHTTP.patch!` once. CONNECT response headers are on the thread afterward, not on the HTTParty response. + +Proxy header names are documented at . ## Installation ```ruby -gem 'httparty' -gem 'ruby_proxy_headers' +gem 'httparty', '~> 0.24' +gem 'ruby-proxy-headers' ``` -## Basic Usage - -### Standalone Functions +## Usage ```ruby -require 'ruby_proxy_headers' +require 'httparty' +require 'ruby_proxy_headers/httparty' -response = RubyProxyHeaders::HTTParty.get( +RubyProxyHeaders::NetHTTP.patch! + +proxy = URI('http://user:pass@proxy.example.com:31280') +response = HTTParty.get( 'https://api.ipify.org?format=json', - proxy: 'http://user:pass@proxy.example.com:8080', - proxy_headers: { 'X-ProxyMesh-Country' => 'US' } + http_proxyaddr: proxy.host, + http_proxyport: proxy.port, + http_proxyuser: proxy.user, + http_proxypass: proxy.password, + proxy_connect_request_headers: { 'X-ProxyMesh-Country' => 'US' }, + connection_adapter: RubyProxyHeaders::ProxyHeadersConnectionAdapter ) -puts "Status: #{response.code}" -puts "Body: #{response.body}" -puts "Proxy IP: #{response.proxy_response_headers['x-proxymesh-ip']}" -``` - -### Class-Based Usage - -```ruby -require 'httparty' -require 'ruby_proxy_headers' - -class ProxyClient - include HTTParty - include RubyProxyHeaders::HTTParty - - base_uri 'https://api.ipify.org' - http_proxy 'proxy.example.com', 8080, 'user', 'pass' - proxy_headers 'X-ProxyMesh-Country' => 'US' -end - -response = ProxyClient.get('/?format=json') puts response.body -puts response.proxy_response_headers['x-proxymesh-ip'] -``` - -## API Reference - -### Standalone Methods - -#### `RubyProxyHeaders::HTTParty.get(url, options)` - -Make a GET request. - -**Parameters:** - -- `url` (String) - Target URL -- `options` (Hash): - - `:proxy` (String) - Proxy URL (required) - - `:proxy_headers` (Hash) - Headers to send to proxy - - `:headers` (Hash) - Request headers - -**Returns:** Response with `proxy_response_headers` accessor - -#### `RubyProxyHeaders::HTTParty.post(url, options)` - -Make a POST request. - -**Parameters:** - -- Same as `get`, plus: - - `:body` (String/Hash) - Request body - -### Class Methods (when including module) - -#### `proxy_headers(headers)` - -Set default proxy headers for all requests. - -```ruby -class MyClient - include HTTParty - include RubyProxyHeaders::HTTParty - - proxy_headers 'X-Country' => 'US', 'X-Session' => 'abc' -end -``` - -## Example: Multiple Countries - -```ruby -require 'ruby_proxy_headers' - -COUNTRIES = %w[US UK DE FR JP] - -COUNTRIES.each do |country| - response = RubyProxyHeaders::HTTParty.get( - 'https://api.ipify.org?format=json', - proxy: ENV['PROXY_URL'], - proxy_headers: { 'X-ProxyMesh-Country' => country } - ) - - puts "#{country}: #{response.body}" -end -``` - -## Example: Persistent Client - -```ruby -require 'httparty' -require 'ruby_proxy_headers' - -class IPChecker - include HTTParty - include RubyProxyHeaders::HTTParty - - base_uri 'https://api.ipify.org' - format :json - - http_proxy 'proxyhost', 1234, ENV['PROXY_USER'], ENV['PROXY_PASS'] - proxy_headers 'X-ProxyMesh-Session' => 'persistent' - - def self.check_ip - response = get('/?format=json') - { - ip: response['ip'], - proxy_ip: response.proxy_response_headers['x-proxymesh-ip'] - } - end -end - -puts IPChecker.check_ip +puts RubyProxyHeaders.proxy_connect_response_headers['X-ProxyMesh-IP'] ``` diff --git a/docs/index.md b/docs/index.md index dd26ae8..a0e7cd8 100644 --- a/docs/index.md +++ b/docs/index.md @@ -32,34 +32,38 @@ Standard Ruby HTTP libraries support basic proxy authentication but **do not** a | Library | Module | Status | |---------|--------|--------| | Net::HTTP | `RubyProxyHeaders::NetHTTP` | ✅ | -| Faraday | `RubyProxyHeaders::Faraday` | ✅ | -| HTTParty | `RubyProxyHeaders::HTTParty` | ✅ | -| HTTP.rb | `RubyProxyHeaders::HTTPGem` | ✅ | -| Typhoeus | `RubyProxyHeaders::Typhoeus` | ✅ | -| Excon | `RubyProxyHeaders::Excon` | ✅ | +| Faraday | `RubyProxyHeaders::FaradayIntegration` | ✅ | +| HTTParty | `RubyProxyHeaders::ProxyHeadersConnectionAdapter` | ✅ | +| HTTP.rb | `RubyProxyHeaders::HTTPGem` | ✅, http gem >= 6 | +| Typhoeus | `RubyProxyHeaders::Typhoeus` | ✅, Typhoeus ~> 1.7 | +| Excon | `RubyProxyHeaders::ExconIntegration` | Send only | | RestClient | `RubyProxyHeaders::RestClient` | ✅ | ## Quick Start ```ruby -require 'ruby_proxy_headers' +require 'uri' +require 'ruby_proxy_headers/net_http' -response = RubyProxyHeaders::NetHTTP.get( - 'https://api.ipify.org?format=json', - proxy: 'http://user:pass@proxy.example.com:8080', - proxy_headers: { 'X-ProxyMesh-Country' => 'US' } -) +RubyProxyHeaders::NetHTTP.patch! -puts "Status: #{response.code}" -puts "Body: #{response.body}" -puts "Proxy IP: #{response.proxy_response_headers['x-proxymesh-ip']}" +uri = URI('https://api.ipify.org?format=json') +proxy = URI('http://user:pass@proxy.example.com:31280') +http = Net::HTTP.new(uri.host, uri.port, proxy.host, proxy.port, proxy.user, proxy.password) +http.use_ssl = true +http.proxy_connect_request_headers = { 'X-ProxyMesh-Country' => 'US' } +response = http.request(Net::HTTP::Get.new(uri)) + +puts response.code +puts response.body +puts http.last_proxy_connect_response_headers['X-ProxyMesh-IP'] ``` ## Use Cases ### ProxyMesh Integration -[ProxyMesh](https://proxymesh.com) uses custom headers for: +[ProxyMesh](https://docs.proxymesh.com/) uses custom headers for: - **Country selection**: `X-ProxyMesh-Country: US` - **Session persistence**: `X-ProxyMesh-Session: abc123` @@ -79,7 +83,7 @@ Many proxy services use custom headers for: Add to your Gemfile: ```ruby -gem 'ruby_proxy_headers' +gem 'ruby-proxy-headers' ``` Then run: @@ -96,7 +100,7 @@ gem install ruby_proxy_headers ## Requirements -- Ruby >= 2.7 +- Ruby >= 3.2 - OpenSSL (included in standard Ruby) ## Related Projects diff --git a/docs/net-http.md b/docs/net-http.md index de980f3..2084dc3 100644 --- a/docs/net-http.md +++ b/docs/net-http.md @@ -1,131 +1,29 @@ # Net::HTTP -Net::HTTP is Ruby's built-in HTTP client. The `RubyProxyHeaders::NetHTTP` module provides a wrapper that adds proxy header support. +`RubyProxyHeaders::NetHTTP.patch!` prepends a small wrapper around the installed `Net::HTTP#connect`. The gem does not replace that method with its own copy. Call `patch!` once before opening connections. -## Installation +Proxy header names are documented at . -Net::HTTP is part of Ruby's standard library, so no additional gems are required. +## Usage ```ruby -require 'ruby_proxy_headers' -``` - -## Basic Usage - -### GET Request - -```ruby -response = RubyProxyHeaders::NetHTTP.get( - 'https://api.ipify.org?format=json', - proxy: 'http://user:pass@proxy.example.com:8080', - proxy_headers: { 'X-ProxyMesh-Country' => 'US' } -) +require 'uri' +require 'ruby_proxy_headers/net_http' -puts "Status: #{response.code}" -puts "Body: #{response.body}" -puts "Proxy IP: #{response.proxy_response_headers['x-proxymesh-ip']}" -``` +RubyProxyHeaders::NetHTTP.patch! -### POST Request +uri = URI('https://api.ipify.org?format=json') +proxy = URI('http://user:pass@proxy.example.com:31280') -```ruby -response = RubyProxyHeaders::NetHTTP.post( - 'https://httpbin.org/post', - '{"key": "value"}', - proxy: 'http://proxy:8080', - proxy_headers: { 'X-Session' => 'abc123' }, - headers: { 'Content-Type' => 'application/json' } -) +http = Net::HTTP.new(uri.host, uri.port, proxy.host, proxy.port, proxy.user, proxy.password) +http.use_ssl = true +http.proxy_connect_request_headers = { 'X-ProxyMesh-Country' => 'US' } +response = http.request(Net::HTTP::Get.new(uri)) puts response.body +puts http.last_proxy_connect_response_headers['X-ProxyMesh-IP'] ``` -### Generic Request - -```ruby -response = RubyProxyHeaders::NetHTTP.request( - :put, - 'https://httpbin.org/put', - proxy: 'http://proxy:8080', - proxy_headers: { 'X-Custom' => 'value' }, - body: 'request body', - headers: { 'Content-Type' => 'text/plain' } -) -``` - -## API Reference - -### `RubyProxyHeaders::NetHTTP.get(url, options)` - -Make a GET request. - -**Parameters:** - -- `url` (String) - Target URL -- `options` (Hash): - - `:proxy` (String) - Proxy URL (required) - - `:proxy_headers` (Hash) - Headers to send to proxy - - `:headers` (Hash) - Request headers - -**Returns:** `ProxyResponse` - -### `RubyProxyHeaders::NetHTTP.post(url, body, options)` - -Make a POST request. - -**Parameters:** +`last_proxy_connect_response_headers` is case-insensitive. The same headers are on `RubyProxyHeaders.proxy_connect_response_headers` for the current thread. -- `url` (String) - Target URL -- `body` (String) - Request body -- `options` (Hash) - Same as `get` - -**Returns:** `ProxyResponse` - -### `RubyProxyHeaders::NetHTTP.request(method, url, options)` - -Make a request with any HTTP method. - -**Parameters:** - -- `method` (Symbol) - HTTP method (`:get`, `:post`, `:put`, `:delete`, etc.) -- `url` (String) - Target URL -- `options` (Hash): - - `:proxy` (String) - Proxy URL (required) - - `:proxy_headers` (Hash) - Headers to send to proxy - - `:headers` (Hash) - Request headers - - `:body` (String) - Request body - -**Returns:** `ProxyResponse` - -## Response Object - -The `ProxyResponse` wraps the standard response and adds: - -```ruby -response.code # HTTP status code -response.body # Response body -response.headers # Response headers -response.proxy_response_headers # Headers from proxy CONNECT response -``` - -## Example: ProxyMesh Integration - -```ruby -require 'ruby_proxy_headers' -require 'json' - -proxy_url = 'http://user:pass@proxyhost:port' - -response = RubyProxyHeaders::NetHTTP.get( - 'https://api.ipify.org?format=json', - proxy: proxy_url, - proxy_headers: { - 'X-ProxyMesh-Country' => 'US', - 'X-ProxyMesh-Session' => 'my-session' - } -) - -ip_info = JSON.parse(response.body) -puts "Request IP: #{ip_info['ip']}" -puts "Proxy IP: #{response.proxy_response_headers['x-proxymesh-ip']}" -``` +An `https://` proxy needs net-http 0.5 or newer, which is what `proxy_use_ssl` requires. Set `http.proxy_use_ssl = true` for that proxy. diff --git a/docs/rest-client.md b/docs/rest-client.md index 4d183a4..da35ef3 100644 --- a/docs/rest-client.md +++ b/docs/rest-client.md @@ -6,17 +6,17 @@ ```ruby gem 'rest-client' -gem 'ruby_proxy_headers' +gem 'ruby-proxy-headers' ``` ## Basic Usage ```ruby -require 'ruby_proxy_headers' +require 'ruby_proxy_headers/rest_client' response = RubyProxyHeaders::RestClient.get( 'https://api.ipify.org?format=json', - proxy: 'http://user:pass@proxy.example.com:8080', + proxy: 'http://user:pass@proxy.example.com:31280', proxy_headers: { 'X-ProxyMesh-Country' => 'US' } ) @@ -70,7 +70,7 @@ response.proxy_response_headers # Proxy CONNECT response headers ## Example: RESTful API ```ruby -require 'ruby_proxy_headers' +require 'ruby_proxy_headers/rest_client' require 'json' proxy_opts = { diff --git a/docs/testing.md b/docs/testing.md index 51a3c3e..089321c 100644 --- a/docs/testing.md +++ b/docs/testing.md @@ -26,7 +26,7 @@ Integration tests require an actual proxy server. ```bash # Required -export PROXY_URL='http://user:pass@proxy.example.com:8080' +export PROXY_URL='http://user:pass@proxy.example.com:31280' # Optional export TEST_URL='https://api.ipify.org?format=json' @@ -51,31 +51,7 @@ ruby test/test_proxy_headers.rb -l ruby test/test_proxy_headers.rb -v ``` -## Test Output - -``` -============================================================ -ruby-proxy-headers Integration Tests -============================================================ -Proxy URL: http://****:****@proxy.example.com:8080 -Test URL: https://api.ipify.org?format=json -Proxy Header: X-ProxyMesh-IP -Send Headers: {"X-ProxyMesh-Country"=>"US"} -Tests: 7 -============================================================ - -Testing Net::HTTP... OK (X-ProxyMesh-IP=1.2.3.4) -Testing Faraday... OK (X-ProxyMesh-IP=1.2.3.4) -Testing HTTParty... OK (X-ProxyMesh-IP=1.2.3.4) -Testing HTTP.rb... OK (X-ProxyMesh-IP=1.2.3.4) -Testing Typhoeus... OK (X-ProxyMesh-IP=1.2.3.4) -Testing Excon... OK (X-ProxyMesh-IP=1.2.3.4) -Testing RestClient... OK (X-ProxyMesh-IP=1.2.3.4) - -============================================================ -Results: 7 passed, 0 failed -============================================================ -``` +The harness prints `Testing ... OK` or `FAIL`. Module names are `net_http`, `faraday`, `httparty`, `excon`, `typhoeus`, `http_gem`, and `rest_client`. Excon checks that the proxied request succeeds. It does not read CONNECT response headers. ## Writing Tests @@ -99,69 +75,31 @@ end ```ruby # test/my_integration_test.rb +require 'uri' require_relative '../lib/ruby_proxy_headers' proxy_url = ENV['PROXY_URL'] raise 'Set PROXY_URL' unless proxy_url -response = RubyProxyHeaders::NetHTTP.get( - 'https://api.ipify.org?format=json', - proxy: proxy_url, - proxy_headers: { 'X-Test' => 'value' } -) - -puts "Status: #{response.code}" -puts "Proxy Headers: #{response.proxy_response_headers}" +RubyProxyHeaders::NetHTTP.patch! +uri = URI('https://api.ipify.org?format=json') +proxy = URI(proxy_url) +http = Net::HTTP.new(uri.host, uri.port, proxy.host, proxy.port, proxy.user, proxy.password) +http.use_ssl = true +http.proxy_connect_request_headers = { 'X-Test' => 'value' } +response = http.request(Net::HTTP::Get.new(uri)) +puts response.code +puts http.last_proxy_connect_response_headers['X-ProxyMesh-IP'] ``` -## CI Configuration - -### GitHub Actions - -```yaml -# .github/workflows/test.yml -name: Tests - -on: [push, pull_request] - -jobs: - test: - runs-on: ubuntu-latest - strategy: - matrix: - ruby: ['2.7', '3.0', '3.1', '3.2', '3.3'] - - steps: - - uses: actions/checkout@v4 - - uses: ruby/setup-ruby@v1 - with: - ruby-version: ${{ matrix.ruby }} - bundler-cache: true - - run: bundle exec rake spec -``` - -## Troubleshooting +## CI -### Connection Timeouts +`.github/workflows/unit_tests.yml` runs `bundle exec rspec` on Ruby 3.2 and 3.3. `.github/workflows/proxy_integration_tests.yml` runs `test/test_proxy_headers.rb` when the `PROXY_URL` Actions secret is set. -```bash -# Increase timeout -export PROXY_TIMEOUT=60 -``` - -### SSL Certificate Errors - -```ruby -# Disable SSL verification (not recommended for production) -RubyProxyHeaders::Connection.new( - proxy_url, - verify_ssl: false -) -``` +## Troubleshooting -### Missing Dependencies +Set `open_timeout` and `read_timeout` on the client you are using. For a mock origin with a self-signed certificate, disable verification on that client (`verify_mode`, Faraday `ssl.verify`, HTTP.rb `verify_ssl: false`, or RestClient `verify_ssl: false`). Do not do that against a public site. ```bash -# Install all optional dependencies for testing -bundle install --with development +bundle install ``` diff --git a/mkdocs.yml b/mkdocs.yml index 35d8733..d2d336e 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -3,8 +3,8 @@ site_description: Send and receive custom proxy headers during HTTPS CONNECT tun site_author: ProxyMesh site_url: https://ruby-proxy-headers.readthedocs.io/ -repo_name: proxymeshai/ruby-proxy-headers -repo_url: https://github.com/proxymeshai/ruby-proxy-headers +repo_name: proxymesh/ruby-proxy-headers +repo_url: https://github.com/proxymesh/ruby-proxy-headers theme: name: material @@ -54,4 +54,4 @@ nav: extra: social: - icon: fontawesome/brands/github - link: https://github.com/proxymeshai/ruby-proxy-headers + link: https://github.com/proxymesh/ruby-proxy-headers diff --git a/ruby-proxy-headers.gemspec b/ruby-proxy-headers.gemspec index 5997817..23f50ba 100644 --- a/ruby-proxy-headers.gemspec +++ b/ruby-proxy-headers.gemspec @@ -18,8 +18,11 @@ Gem::Specification.new do |s| s.metadata['source_code_uri'] = 'https://github.com/proxymesh/ruby-proxy-headers' s.metadata['documentation_uri'] = 'https://rubydoc.info/gems/ruby-proxy-headers' + s.metadata['homepage_uri'] = 'https://github.com/proxymesh/ruby-proxy-headers' + s.metadata['changelog_uri'] = 'https://github.com/proxymesh/ruby-proxy-headers/blob/main/CHANGELOG.md' + s.metadata['bug_tracker_uri'] = 'https://github.com/proxymesh/ruby-proxy-headers/issues' - s.files = Dir['lib/**/*', 'LICENSE', 'README.md', 'IMPLEMENTATION_PRIORITY.md', 'LIBRARY_RESEARCH.md', 'DEFERRED.md', '.yardopts'] + s.files = Dir['lib/**/*', 'LICENSE', 'README.md', 'CHANGELOG.md', 'docs/**/*.md', '.yardopts'] s.require_paths = ['lib'] s.add_development_dependency 'bundler', '>= 2.4'