-
-
Save Epictetus/1990878 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'eventmachine' | |
# most of code is from [thin-glazed](https://github.com/freelancing-god/thin-glazed) | |
class HttpClient < EventMachine::Connection | |
attr_reader :proxy | |
def initialize(proxy) | |
@proxy = proxy | |
@connected = EventMachine::DefaultDeferrable.new | |
end | |
def connection_completed | |
@connected.succeed | |
end | |
def receive_data(data) | |
proxy.relay_from_client(data) | |
end | |
def send(data) | |
@connected.callback { send_data data } | |
end | |
def unbind | |
proxy.unbind_client | |
end | |
end | |
class HttpProxy < EventMachine::Connection | |
attr_reader :client_port | |
def initialize(client_port) | |
@client_port = client_port | |
end | |
def receive_data(data) | |
client.send_data data unless data.nil? | |
end | |
def relay_from_client(data) | |
send_data data unless data.nil? | |
end | |
def unbind | |
client.close_connection | |
@client = nil | |
end | |
def unbind_client | |
close_connection_after_writing | |
@client = nil | |
end | |
private | |
def client | |
@client ||= EventMachine.connect '127.0.0.1', client_port, HttpClient, self | |
end | |
end | |
class HttpsProxy < HttpProxy | |
def post_init | |
start_tls | |
end | |
def receive_data(data) | |
super data.gsub(/\r\n\r\n/, "\r\nX_FORWARDED_PROTO: https\r\n\r\n") | |
end | |
end | |
EventMachine.run do | |
host, port = "127.0.0.1", 3793 | |
EventMachine.start_server(host, port, HttpsProxy, 4567) | |
puts "Open https://#{host}:#{port}" | |
end | |
# dummy_app.rb | |
# ------------ | |
# | |
# require 'sinatra' | |
# | |
# get '/foo' do | |
# 'FOO' | |
# end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment