# frozen_string_literal: true# Copyright, 2018, by Samuel G. D. Williams. <http://www.codeotaku.com># # Permission is hereby granted, free of charge, to any person obtaining a copy# of this software and associated documentation files (the "Software"), to deal# in the Software without restriction, including without limitation the rights# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell# copies of the Software, and to permit persons to whom the Software is# furnished to do so, subject to the following conditions:# # The above copyright notice and this permission notice shall be included in# all copies or substantial portions of the Software.# # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN# THE SOFTWARE.require'async/http/client'require'protocol/http/headers'require'protocol/http/middleware'moduleFalconmoduleMiddleware# A static middleware which always returns a 400 bad request response.moduleBadRequestdefself.call(request)returnProtocol::HTTP::Response[400,{},[]]enddefself.closeendend# A HTTP middleware for proxying requests to a given set of hosts.# Typically used for implementing virtual servers.classProxy<Protocol::HTTP::MiddlewareFORWARDED='forwarded'X_FORWARDED_FOR='x-forwarded-for'X_FORWARDED_PROTO='x-forwarded-proto'VIA='via'CONNECTION='connection'# HTTP hop headers which *should* not be passed through the proxy.HOP_HEADERS=['connection','keep-alive','public','proxy-authenticate','transfer-encoding','upgrade',]# Initialize the proxy middleware.# @parameter app [Protocol::HTTP::Middleware] The middleware to use if a request can't be proxied.# @parameter hosts [Array(Service::Proxy)] The host applications to proxy to.definitialize(app,hosts)super(app)@server_context=nil@hosts=hosts@clients={}@count=0end# The number of requests that have been proxied.# @attribute [Integer]attr:count# Close all the connections to the upstream hosts.defclose@clients.each_value(&:close)superend# Establish a connection to the specified upstream endpoint.# @parameter endpoint [Async::HTTP::Endpoint]defconnect(endpoint)@clients[endpoint]||=Async::HTTP::Client.new(endpoint)end# Lookup the appropriate host for the given request.# @parameter request [Protocol::HTTP::Request]# @returns [Service::Proxy]deflookup(request)# Trailing dot and port is ignored/normalized.ifauthority=request.authority&.sub(/(\.)?(:\d+)?$/,'')return@hosts[authority]endend# Prepare the headers to be sent to an upstream host.# In particular, we delete all connection and hop headers.defprepare_headers(headers)ifconnection=headers[CONNECTION]headers.extract(connection)endheaders.extract(HOP_HEADERS)end# Prepare the request to be proxied to the specified host.# In particular, we set appropriate {VIA}, {FORWARDED}, {X_FORWARDED_FOR} and {X_FORWARDED_PROTO} headers.defprepare_request(request,host)forwarded=[]Async.logger.debug(self)do|buffer|buffer.puts"Request authority: #{request.authority}"buffer.puts"Host authority: #{host.authority}"buffer.puts"Request: #{request.method}#{request.path}#{request.version}"buffer.puts"Request headers: #{request.headers.inspect}"end# The authority of the request must match the authority of the endpoint we are proxying to, otherwise SNI and other things won't work correctly.request.authority=host.authorityifaddress=request.remote_addressrequest.headers.add(X_FORWARDED_FOR,address.ip_address)forwarded<<"for=#{address.ip_address}"endifscheme=request.schemerequest.headers.add(X_FORWARDED_PROTO,scheme)forwarded<<"proto=#{scheme}"endunlessforwarded.empty?request.headers.add(FORWARDED,forwarded.join(';'))endrequest.headers.add(VIA,"#{request.version}#{self.class}")self.prepare_headers(request.headers)returnrequestend# Proxy the request if the authority matches a specific host.# @parameter request [Protocol::HTTP::Request]defcall(request)ifhost=lookup(request)@count+=1request=self.prepare_request(request,host)client=connect(host.endpoint)client.call(request)elsesuperendrescueAsync.logger.error(self){$!}returnProtocol::HTTP::Response[502,{'content-type'=>'text/plain'},["#{$!.inspect}: #{$!.backtrace.join("\n")}"]]endendendend