Last active
January 26, 2017 06:47
-
-
Save mfazekas/ac0556c54438af510df8b4cc1e91188d to your computer and use it in GitHub Desktop.
proxy server for proxying percy.io to dev.percy.local and percy.io
This file contains hidden or 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 'webrick' | |
require 'webrick/https' | |
require 'webrick/httpproxy' | |
require 'byebug' | |
require 'net/http' | |
# make sure /etc/host has percy.io as localhost | |
API_HOST = 'percy.io' | |
API_URL = "https://#{API_HOST}" | |
API_IP = '104.25.54.35' | |
WEBAPP_URL = 'http://dev.percy.local:4200' | |
COOKIE_DOMAIN_FROM = 'percy.io' | |
COOKIE_DOMAIN_TO = 'percy.io' | |
cert_name = [ | |
%w[CN percy.io], | |
] | |
module OverwriteOpenOnSocket | |
# when making actual connection percy.io, we want to use the read ip | |
def open(addr, port, local_host, local_port) | |
addr = API_IP if addr == API_HOST | |
super(addr, port, local_host, local_port) | |
end | |
end | |
class << TCPSocket | |
prepend OverwriteOpenOnSocket | |
end | |
class ForwardToPercy < WEBrick::HTTPServer | |
def forward_to_webapp(req, res) | |
uri = URI("#{WEBAPP_URL}#{req.path}") | |
uri.query = req.query_string | |
http = Net::HTTP.start(uri.host, uri.port, use_ssl: (uri.scheme == 'https'), verify_mode: OpenSSL::SSL::VERIFY_NONE) do |http| | |
request = Net::HTTP::Get.new uri | |
request['cookie'] = req.header['cookie'] if req.header['cookie'] | |
fw_response = http.request request | |
fw_response.read_header.each do |k,v| | |
case k | |
when 'transfer-encoding' | |
else | |
res[k] = v | |
end | |
end | |
res.body = fw_response.body | |
res['content-length'] = res.body.length | |
res.status = fw_response.code | |
end | |
end | |
def forward_to_api(req, res) | |
uri = URI("#{API_URL}#{req.path}") | |
uri.query = req.query_string | |
http = Net::HTTP.start(uri.host, uri.port, use_ssl: (uri.scheme == 'https'), verify_mode: OpenSSL::SSL::VERIFY_NONE) do |http| | |
request = Net::HTTP::Get.new uri | |
request['cookie'] = req.header['cookie'] if req.header['cookie'] | |
fw_response = http.request request | |
log " response from API: #{fw_response}" | |
fw_response.read_header.each do |k,v| | |
case k | |
when 'transfer-encoding' | |
else | |
res[k] = v | |
end | |
end | |
res.body = fw_response.body | |
res['content-length'] = res.body.length | |
res.status = fw_response.code | |
end | |
end | |
def service(req, res) | |
log "service req:#{req} res:#{res}" | |
if req.path.start_with?('/api/') | |
forward_to_api(req, res) | |
else | |
forward_to_webapp(req, res) | |
end | |
end | |
def log(msg) | |
puts " ++ #{msg}" | |
end | |
end | |
server = ForwardToPercy.new(Port: 443, | |
SSLEnable: true, | |
SSLCertName: cert_name | |
) | |
trap 'INT' do server.shutdown end | |
trap 'TERM' do server.shutdown end | |
server.start |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment