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.



24
25
26
27
28
29
# File 'lib/falcon/controller/proxy.rb', line 24

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.



79
80
81
82
83
84
# File 'lib/falcon/controller/proxy.rb', line 79

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.



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/falcon/controller/proxy.rb', line 44

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.



32
33
34
# File 'lib/falcon/controller/proxy.rb', line 32

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

#nameObject

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



37
38
39
# File 'lib/falcon/controller/proxy.rb', line 37

def name
	"Falcon Proxy Server"
end

#ssl_contextObject

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



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

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.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/falcon/controller/proxy.rb', line 87

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