2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2003-07-23 12:51:36 -04:00
|
|
|
#
|
|
|
|
# httpproxy.rb -- HTTPProxy Class
|
|
|
|
#
|
|
|
|
# Author: IPR -- Internet Programming with Ruby -- writers
|
|
|
|
# Copyright (c) 2002 GOTO Kentaro
|
|
|
|
# Copyright (c) 2002 Internet Programming with Ruby writers. All rights
|
|
|
|
# reserved.
|
|
|
|
#
|
|
|
|
# $IPR: httpproxy.rb,v 1.18 2003/03/08 18:58:10 gotoyuzo Exp $
|
|
|
|
# $kNotwork: straw.rb,v 1.3 2002/02/12 15:13:07 gotoken Exp $
|
|
|
|
|
|
|
|
require "webrick/httpserver"
|
|
|
|
require "net/http"
|
|
|
|
|
|
|
|
module WEBrick
|
2013-01-25 20:12:54 -05:00
|
|
|
|
|
|
|
NullReader = Object.new # :nodoc:
|
|
|
|
class << NullReader # :nodoc:
|
2005-01-07 06:05:22 -05:00
|
|
|
def read(*args)
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
alias gets read
|
|
|
|
end
|
|
|
|
|
2013-01-25 20:12:54 -05:00
|
|
|
FakeProxyURI = Object.new # :nodoc:
|
|
|
|
class << FakeProxyURI # :nodoc:
|
2007-12-31 09:17:41 -05:00
|
|
|
def method_missing(meth, *args)
|
|
|
|
if %w(scheme host port path query userinfo).member?(meth.to_s)
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-25 20:12:54 -05:00
|
|
|
# :startdoc:
|
|
|
|
|
2011-05-09 20:13:58 -04:00
|
|
|
##
|
|
|
|
# An HTTP Proxy server which proxies GET, HEAD and POST requests.
|
2012-02-02 18:17:24 -05:00
|
|
|
#
|
|
|
|
# To create a simple proxy server:
|
|
|
|
#
|
|
|
|
# require 'webrick'
|
|
|
|
# require 'webrick/httpproxy'
|
|
|
|
#
|
|
|
|
# proxy = WEBrick::HTTPProxyServer.new Port: 8000
|
|
|
|
#
|
2013-03-07 20:46:59 -05:00
|
|
|
# trap 'INT' do proxy.shutdown end
|
|
|
|
# trap 'TERM' do proxy.shutdown end
|
2012-02-02 18:17:24 -05:00
|
|
|
#
|
2013-03-07 20:46:59 -05:00
|
|
|
# proxy.start
|
2012-02-02 18:17:24 -05:00
|
|
|
#
|
|
|
|
# See ::new for proxy-specific configuration items.
|
|
|
|
#
|
|
|
|
# == Modifying proxied responses
|
|
|
|
#
|
|
|
|
# To modify content the proxy server returns use the +:ProxyContentHandler+
|
|
|
|
# option:
|
|
|
|
#
|
|
|
|
# handler = proc do |req, res|
|
|
|
|
# if res['content-type'] == 'text/plain' then
|
|
|
|
# res.body << "\nThis content was proxied!\n"
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# proxy =
|
|
|
|
# WEBrick::HTTPProxyServer.new Port: 8000, ProxyContentHandler: handler
|
2011-05-09 20:13:58 -04:00
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
class HTTPProxyServer < HTTPServer
|
2011-05-09 20:13:58 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# Proxy server configurations. The proxy server handles the following
|
|
|
|
# configuration items in addition to those supported by HTTPServer:
|
|
|
|
#
|
|
|
|
# :ProxyAuthProc:: Called with a request and response to authorize a
|
|
|
|
# request
|
|
|
|
# :ProxyVia:: Appended to the via header
|
|
|
|
# :ProxyURI:: The proxy server's URI
|
2012-02-02 18:17:24 -05:00
|
|
|
# :ProxyContentHandler:: Called with a request and response and allows
|
2011-05-09 20:13:58 -04:00
|
|
|
# modification of the response
|
|
|
|
# :ProxyTimeout:: Sets the proxy timeouts to 30 seconds for open and 60
|
|
|
|
# seconds for read operations
|
|
|
|
|
2005-08-07 16:16:15 -04:00
|
|
|
def initialize(config={}, default=Config::HTTP)
|
|
|
|
super(config, default)
|
2003-07-23 12:51:36 -04:00
|
|
|
c = @config
|
|
|
|
@via = "#{c[:HTTPVersion]} #{c[:ServerName]}:#{c[:Port]}"
|
|
|
|
end
|
|
|
|
|
2013-01-25 20:12:54 -05:00
|
|
|
# :stopdoc:
|
2003-07-23 12:51:36 -04:00
|
|
|
def service(req, res)
|
|
|
|
if req.request_method == "CONNECT"
|
2007-12-31 09:17:41 -05:00
|
|
|
do_CONNECT(req, res)
|
2003-07-23 12:51:36 -04:00
|
|
|
elsif req.unparsed_uri =~ %r!^http://!
|
|
|
|
proxy_service(req, res)
|
|
|
|
else
|
|
|
|
super(req, res)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def proxy_auth(req, res)
|
|
|
|
if proc = @config[:ProxyAuthProc]
|
|
|
|
proc.call(req, res)
|
|
|
|
end
|
|
|
|
req.header.delete("proxy-authorization")
|
|
|
|
end
|
|
|
|
|
|
|
|
def proxy_uri(req, res)
|
2007-12-31 09:17:41 -05:00
|
|
|
# should return upstream proxy server's URI
|
|
|
|
return @config[:ProxyURI]
|
2003-07-23 12:51:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def proxy_service(req, res)
|
|
|
|
# Proxy Authentication
|
2009-03-05 22:56:38 -05:00
|
|
|
proxy_auth(req, res)
|
2003-07-23 12:51:36 -04:00
|
|
|
|
|
|
|
begin
|
2007-12-31 09:17:41 -05:00
|
|
|
self.send("do_#{req.request_method}", req, res)
|
|
|
|
rescue NoMethodError
|
|
|
|
raise HTTPStatus::MethodNotAllowed,
|
|
|
|
"unsupported method `#{req.request_method}'."
|
2003-07-23 12:51:36 -04:00
|
|
|
rescue => err
|
|
|
|
logger.debug("#{err.class}: #{err.message}")
|
|
|
|
raise HTTPStatus::ServiceUnavailable, err.message
|
|
|
|
end
|
|
|
|
|
|
|
|
# Process contents
|
|
|
|
if handler = @config[:ProxyContentHandler]
|
|
|
|
handler.call(req, res)
|
|
|
|
end
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-31 09:17:41 -05:00
|
|
|
def do_CONNECT(req, res)
|
2003-07-23 12:51:36 -04:00
|
|
|
# Proxy Authentication
|
|
|
|
proxy_auth(req, res)
|
|
|
|
|
|
|
|
ua = Thread.current[:WEBrickSocket] # User-Agent
|
|
|
|
raise HTTPStatus::InternalServerError,
|
|
|
|
"[BUG] cannot get socket" unless ua
|
|
|
|
|
|
|
|
host, port = req.unparsed_uri.split(":", 2)
|
|
|
|
# Proxy authentication for upstream proxy server
|
|
|
|
if proxy = proxy_uri(req, res)
|
|
|
|
proxy_request_line = "CONNECT #{host}:#{port} HTTP/1.0"
|
|
|
|
if proxy.userinfo
|
2016-01-09 19:35:43 -05:00
|
|
|
credentials = "Basic " + [proxy.userinfo].pack("m0")
|
2003-07-23 12:51:36 -04:00
|
|
|
end
|
|
|
|
host, port = proxy.host, proxy.port
|
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
@logger.debug("CONNECT: upstream proxy is `#{host}:#{port}'.")
|
|
|
|
os = TCPSocket.new(host, port) # origin server
|
|
|
|
|
|
|
|
if proxy
|
|
|
|
@logger.debug("CONNECT: sending a Request-Line")
|
|
|
|
os << proxy_request_line << CRLF
|
|
|
|
@logger.debug("CONNECT: > #{proxy_request_line}")
|
|
|
|
if credentials
|
2015-07-13 10:58:12 -04:00
|
|
|
@logger.debug("CONNECT: sending credentials")
|
2003-07-23 12:51:36 -04:00
|
|
|
os << "Proxy-Authorization: " << credentials << CRLF
|
|
|
|
end
|
|
|
|
os << CRLF
|
|
|
|
proxy_status_line = os.gets(LF)
|
2015-07-13 10:58:12 -04:00
|
|
|
@logger.debug("CONNECT: read Status-Line from the upstream server")
|
2003-07-23 12:51:36 -04:00
|
|
|
@logger.debug("CONNECT: < #{proxy_status_line}")
|
|
|
|
if %r{^HTTP/\d+\.\d+\s+200\s*} =~ proxy_status_line
|
|
|
|
while line = os.gets(LF)
|
|
|
|
break if /\A(#{CRLF}|#{LF})\z/om =~ line
|
|
|
|
end
|
|
|
|
else
|
|
|
|
raise HTTPStatus::BadGateway
|
|
|
|
end
|
|
|
|
end
|
|
|
|
@logger.debug("CONNECT #{host}:#{port}: succeeded")
|
|
|
|
res.status = HTTPStatus::RC_OK
|
|
|
|
rescue => ex
|
|
|
|
@logger.debug("CONNECT #{host}:#{port}: failed `#{ex.message}'")
|
|
|
|
res.set_error(ex)
|
|
|
|
raise HTTPStatus::EOFError
|
|
|
|
ensure
|
2004-08-21 12:16:18 -04:00
|
|
|
if handler = @config[:ProxyContentHandler]
|
|
|
|
handler.call(req, res)
|
|
|
|
end
|
2003-07-23 12:51:36 -04:00
|
|
|
res.send_response(ua)
|
|
|
|
access_log(@config, req, res)
|
2005-01-07 06:05:22 -05:00
|
|
|
|
2013-03-07 20:46:59 -05:00
|
|
|
# Should clear request-line not to send the response twice.
|
2005-01-07 06:05:22 -05:00
|
|
|
# see: HTTPServer#run
|
|
|
|
req.parse(NullReader) rescue nil
|
2003-07-23 12:51:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
while fds = IO::select([ua, os])
|
|
|
|
if fds[0].member?(ua)
|
|
|
|
buf = ua.sysread(1024);
|
2008-11-08 04:41:24 -05:00
|
|
|
@logger.debug("CONNECT: #{buf.bytesize} byte from User-Agent")
|
2003-07-23 12:51:36 -04:00
|
|
|
os.syswrite(buf)
|
|
|
|
elsif fds[0].member?(os)
|
|
|
|
buf = os.sysread(1024);
|
2008-11-08 04:41:24 -05:00
|
|
|
@logger.debug("CONNECT: #{buf.bytesize} byte from #{host}:#{port}")
|
2003-07-23 12:51:36 -04:00
|
|
|
ua.syswrite(buf)
|
|
|
|
end
|
|
|
|
end
|
2014-06-26 05:05:58 -04:00
|
|
|
rescue
|
2003-07-23 12:51:36 -04:00
|
|
|
os.close
|
|
|
|
@logger.debug("CONNECT #{host}:#{port}: closed")
|
|
|
|
end
|
|
|
|
|
|
|
|
raise HTTPStatus::EOFError
|
|
|
|
end
|
|
|
|
|
2007-12-31 09:17:41 -05:00
|
|
|
def do_GET(req, res)
|
|
|
|
perform_proxy_request(req, res) do |http, path, header|
|
|
|
|
http.get(path, header)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def do_HEAD(req, res)
|
|
|
|
perform_proxy_request(req, res) do |http, path, header|
|
|
|
|
http.head(path, header)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def do_POST(req, res)
|
|
|
|
perform_proxy_request(req, res) do |http, path, header|
|
|
|
|
http.post(path, req.body || "", header)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
def do_OPTIONS(req, res)
|
|
|
|
res['allow'] = "GET,HEAD,POST,OPTIONS,CONNECT"
|
|
|
|
end
|
2007-12-31 09:17:41 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2008-06-04 05:37:38 -04:00
|
|
|
# Some header fields should not be transferred.
|
2007-12-31 09:17:41 -05:00
|
|
|
HopByHop = %w( connection keep-alive proxy-authenticate upgrade
|
|
|
|
proxy-authorization te trailers transfer-encoding )
|
|
|
|
ShouldNotTransfer = %w( set-cookie proxy-connection )
|
|
|
|
def split_field(f) f ? f.split(/,\s+/).collect{|i| i.downcase } : [] end
|
|
|
|
|
|
|
|
def choose_header(src, dst)
|
|
|
|
connections = split_field(src['connection'])
|
|
|
|
src.each{|key, value|
|
|
|
|
key = key.downcase
|
|
|
|
if HopByHop.member?(key) || # RFC2616: 13.5.1
|
|
|
|
connections.member?(key) || # RFC2616: 14.10
|
|
|
|
ShouldNotTransfer.member?(key) # pragmatics
|
|
|
|
@logger.debug("choose_header: `#{key}: #{value}'")
|
|
|
|
next
|
|
|
|
end
|
|
|
|
dst[key] = value
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
# Net::HTTP is stupid about the multiple header fields.
|
|
|
|
# Here is workaround:
|
|
|
|
def set_cookie(src, dst)
|
|
|
|
if str = src['set-cookie']
|
|
|
|
cookies = []
|
|
|
|
str.split(/,\s*/).each{|token|
|
|
|
|
if /^[^=]+;/o =~ token
|
|
|
|
cookies[-1] << ", " << token
|
|
|
|
elsif /=/o =~ token
|
|
|
|
cookies << token
|
|
|
|
else
|
|
|
|
cookies[-1] << ", " << token
|
|
|
|
end
|
|
|
|
}
|
|
|
|
dst.cookies.replace(cookies)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_via(h)
|
|
|
|
if @config[:ProxyVia]
|
|
|
|
if h['via']
|
|
|
|
h['via'] << ", " << @via
|
|
|
|
else
|
|
|
|
h['via'] = @via
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def setup_proxy_header(req, res)
|
|
|
|
# Choose header fields to transfer
|
|
|
|
header = Hash.new
|
|
|
|
choose_header(req, header)
|
|
|
|
set_via(header)
|
|
|
|
return header
|
|
|
|
end
|
|
|
|
|
|
|
|
def setup_upstream_proxy_authentication(req, res, header)
|
|
|
|
if upstream = proxy_uri(req, res)
|
|
|
|
if upstream.userinfo
|
|
|
|
header['proxy-authorization'] =
|
2016-01-09 19:35:43 -05:00
|
|
|
"Basic " + [upstream.userinfo].pack("m0")
|
2007-12-31 09:17:41 -05:00
|
|
|
end
|
|
|
|
return upstream
|
|
|
|
end
|
|
|
|
return FakeProxyURI
|
|
|
|
end
|
|
|
|
|
|
|
|
def perform_proxy_request(req, res)
|
|
|
|
uri = req.request_uri
|
|
|
|
path = uri.path.dup
|
|
|
|
path << "?" << uri.query if uri.query
|
|
|
|
header = setup_proxy_header(req, res)
|
|
|
|
upstream = setup_upstream_proxy_authentication(req, res, header)
|
|
|
|
response = nil
|
|
|
|
|
|
|
|
http = Net::HTTP.new(uri.host, uri.port, upstream.host, upstream.port)
|
|
|
|
http.start do
|
|
|
|
if @config[:ProxyTimeout]
|
2009-03-05 22:56:38 -05:00
|
|
|
################################## these issues are
|
2013-12-04 09:06:52 -05:00
|
|
|
http.open_timeout = 30 # secs # necessary (maybe because
|
2007-12-31 09:17:41 -05:00
|
|
|
http.read_timeout = 60 # secs # Ruby's bug, but why?)
|
|
|
|
##################################
|
|
|
|
end
|
|
|
|
response = yield(http, path, header)
|
|
|
|
end
|
|
|
|
|
2008-06-04 05:37:38 -04:00
|
|
|
# Persistent connection requirements are mysterious for me.
|
2007-12-31 09:17:41 -05:00
|
|
|
# So I will close the connection in every response.
|
|
|
|
res['proxy-connection'] = "close"
|
|
|
|
res['connection'] = "close"
|
|
|
|
|
|
|
|
# Convert Net::HTTP::HTTPResponse to WEBrick::HTTPResponse
|
|
|
|
res.status = response.code.to_i
|
|
|
|
choose_header(response, res)
|
|
|
|
set_cookie(response, res)
|
|
|
|
set_via(res)
|
|
|
|
res.body = response.body
|
|
|
|
end
|
2013-01-25 20:12:54 -05:00
|
|
|
|
|
|
|
# :stopdoc:
|
2003-07-23 12:51:36 -04:00
|
|
|
end
|
|
|
|
end
|