A HTTP Proxy server and client written in Crystal
Add this to your application's shard.yml
:
dependencies:
http_proxy:
github: mamantoha/http_proxy
require "http_proxy"
host = "127.0.0.1"
port = 8080
server = HTTP::Proxy::Server.new
address = server.bind_tcp(host, port)
puts "Listening on http://#{address}"
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(handlers: [
HTTP::LogHandler.new,
]) do |context|
context.perform
end
address = server.bind_tcp(host, port)
puts "Listening on http://#{address}"
server.listen
server = HTTP::Proxy::Server.new(handlers: [
HTTP::LogHandler.new,
HTTP::Proxy::Server::BasicAuthHandler.new("user", "passwd"),
]) do |context|
context.request.headers.add("X-Forwarded-For", "127.0.0.1")
context.perform
end
require "http_proxy"
proxy_client = HTTP::Proxy::Client.new("127.0.0.1", 8080)
uri = URI.parse("http://httpbingo.org")
client = HTTP::Client.new(uri)
client.proxy = proxy_client
response = client.get("/get")
uri = URI.parse("https://httpbingo.org")
proxy_client = HTTP::Proxy::Client.new("127.0.0.1", 8080, username: "user", password: "passwd")
response = HTTP::Client.new(uri) do |client|
client.proxy = proxy_client
client.get("/get")
end
puts response.status_code
puts response.body