Class: Falcon::Controller::Proxy

Inherits:
Serve
  • Object
show all
Defined in:
lib/falcon/controller/proxy.rb

Overview

A controller for proxying requests.

Constant Summary collapse

DEFAULT_SESSION_ID =

The default SSL session identifier.

"falcon"

Instance Method Summary collapse

Methods inherited from Serve

#create_container, #setup, #stop

Constructor Details

#initialize(command, session_id: DEFAULT_SESSION_ID, **options) ⇒ Proxy

Initialize the proxy controller.



41
42
43
44
45
46
# File 'lib/falcon/controller/proxy.rb', line 41

def initialize(command, session_id: DEFAULT_SESSION_ID, **options)
	super(command, **options)
	
	@session_id = session_id
	@hosts = {}
end

Instance Method Details

#endpointObject

The endpoint the server will bind to.



96
97
98
99
100
101
# File 'lib/falcon/controller/proxy.rb', line 96

def endpoint
	@command.endpoint.with(
		ssl_context: self.ssl_context,
		reuse_address: true,
	)
end

#host_context(socket, hostname) ⇒ Object

Look up the host context for the given hostname, and update the socket hostname if necessary.



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/falcon/controller/proxy.rb', line 61

def host_context(socket, hostname)
	if host = @hosts[hostname]
		Console.logger.debug(self) {"Resolving #{hostname} -> #{host}"}
		
		socket.hostname = hostname
		
		return host.ssl_context
	else
		Console.logger.warn(self) {"Unable to resolve #{hostname}!"}
		
		return nil
	end
end

#load_appObject

Load the Middleware::Proxy application with the specified hosts.



49
50
51
# File 'lib/falcon/controller/proxy.rb', line 49

def load_app
	return Middleware::Proxy.new(Middleware::BadRequest, @hosts)
end

#nameObject

The name of the controller which is used for the process title.



54
55
56
# File 'lib/falcon/controller/proxy.rb', line 54

def name
	"Falcon Proxy Server"
end

#ssl_contextObject

Generate an SSL context which delegates to #host_context to multiplex based on hostname.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/falcon/controller/proxy.rb', line 76

def ssl_context
	@server_context ||= OpenSSL::SSL::SSLContext.new.tap do |context|
		context.servername_cb = Proc.new do |socket, hostname|
			self.host_context(socket, hostname)
		end
		
		context.session_id_context = @session_id
		
		context.ssl_version = :TLSv1_2_server
		
		context.set_params(
			ciphers: TLS::SERVER_CIPHERS,
			verify_mode: OpenSSL::SSL::VERIFY_NONE,
		)
		
		context.setup
	end
end

#startObject

Builds a map of host redirections.



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/falcon/controller/proxy.rb', line 104

def start
	configuration = @command.configuration
	
	services = Services.new(configuration)
	
	@hosts = {}
	
	services.each do |service|
		if service.is_a?(Service::Proxy)
			Console.logger.info(self) {"Proxying #{service.authority} to #{service.endpoint}"}
			@hosts[service.authority] = service
			
			# Pre-cache the ssl contexts:
			# It seems some OpenSSL objects don't like event-driven I/O.
			service.ssl_context
		end
	end
	
	super
end