A HTTP Proxy server and client written in Crystal
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
samples
spec
src
.gitignore
.travis.yml
CHANGELOG.md
LICENSE
README.md
shard.yml

README.md

HTTP::Proxy

Build Status GitHub release License

A HTTP Proxy server and client written in Crystal

Installation

Add this to your application's shard.yml:

dependencies:
  http_proxy:
    github: mamantoha/http_proxy

Usage

Server

require "http_proxy"

host = "127.0.0.1"
port = 8080

server = HTTP::Proxy::Server.new(host, port)

server.bind_tcp(port)
puts "Listening on http://#{server.host}:#{server.port}"
server.listen
require "http_proxy"
require "option_parser"

host = "192.168.0.1"
port = 3128

OptionParser.parse! do |opts|
  opts.on("-h HOST", "--host HOST", "define host to run server") do |opt|
    host = opt
  end

  opts.on("-p PORT", "--port PORT", "define port to run server") do |opt|
    port = opt.to_i
  end
end

server = HTTP::Proxy::Server.new(host, port, handlers: [
  HTTP::LogHandler.new,
]) do |context|
  context.perform
end

server.bind_tcp(port)
puts "Listening on http://#{server.host}:#{server.port}"
server.listen

Basic Authentication

server = HTTP::Proxy::Server.new("127.0.0.1", 8080, handlers: [
  HTTP::LogHandler.new,
  HTTP::Proxy::Server::BasicAuth.new("user", "passwd"),
]) do |context|
  context.request.headers.add("X-Forwarded-For", "127.0.0.1")
  context.perform
end

Client

Make HTTP request:

Note: you should send full URL instead of path when using HTTP proxy.

require "http_proxy"

proxy_client = HTTP::Proxy::Client.new("127.0.0.1", 8080)

uri = URI.parse("http://httpbin.org")
client = HTTP::Client.new(uri)
client.set_proxy(proxy_client)
response = client.get("http://httpbin.org/get")

Make HTTPS request:

require "http_proxy"

proxy_client = HTTP::Proxy::Client.new("127.0.0.1", 8080)

uri = URI.parse("https://httpbin.org")
response = HTTP::Client.new(uri) do |client|
  client.set_proxy(proxy_client)
  client.get("/get")
end

Basic Authentication

uri = URI.parse("http://httpbin.org")
proxy_client = HTTP::Proxy::Client.new("127.0.0.1", 8080, username: "user", password: "passwd")

response = HTTP::Client.new(uri) do |client|
  client.set_proxy(proxy_client)
  client.get("http://httpbin.org/get")
end

puts response.status_code
puts response.body

Development

Proxy server

  • Basic HTTP Proxy: GET, POST, PUT, DELETE support
  • Basic HTTP Proxy: OPTIONS support
  • HTTPS Proxy: CONNECT support
  • Make context.request & context.response writable
  • Basic Authentication
  • MITM HTTPS Proxy

Proxy client

  • Basic HTTP Proxy: GET, POST, PUT, DELETE support
  • Basic HTTP Proxy: OPTIONS support
  • HTTPS Proxy: CONNECT support
  • Basic Authentication

Contributing

  1. Fork it ( https://github.com/mamantoha/http_proxy/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

  • bbtfr Theo Li - creator, maintainer
  • mamantoha Anton Maminov - maintainer