Class: Falcon::Hosts

Inherits:
Object
  • Object
show all
Defined in:
lib/falcon/hosts.rb

Constant Summary collapse

DEFAULT_ALPN_PROTOCOLS =
['h2', 'http/1.1'].freeze

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ Hosts

Returns a new instance of Hosts.



99
100
101
102
103
104
105
106
107
# File 'lib/falcon/hosts.rb', line 99

def initialize(configuration)
  @named = {}
  @server_context = nil
  @server_endpoint = nil
  
  configuration.each do |environment|
    add(Host.new(environment))
  end
end

Instance Method Details

#add(host) ⇒ Object



149
150
151
# File 'lib/falcon/hosts.rb', line 149

def add(host)
  @named[host.authority] = host
end

#each(&block) ⇒ Object



109
110
111
# File 'lib/falcon/hosts.rb', line 109

def each(&block)
  @named.each(&block)
end

#endpointObject



113
114
115
116
117
118
119
# File 'lib/falcon/hosts.rb', line 113

def endpoint
  @server_endpoint ||= Async::HTTP::URLEndpoint.parse(
    'https://[::]',
    ssl_context: self.ssl_context,
    reuse_address: true
  )
end

#host_context(socket, hostname) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/falcon/hosts.rb', line 135

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

#proxyObject



153
154
155
# File 'lib/falcon/hosts.rb', line 153

def proxy
  Proxy.new(Falcon::BadRequest, @named)
end

#redirection(secure_endpoint) ⇒ Object



157
158
159
# File 'lib/falcon/hosts.rb', line 157

def redirection(secure_endpoint)
  Redirection.new(Falcon::BadRequest, @named, secure_endpoint)
end

#run(container = Async::Container::Forked.new, **options) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/falcon/hosts.rb', line 161

def run(container = Async::Container::Forked.new, **options)
  @named.each do |name, host|
    host.run(container)
  end
  
  secure_endpoint = Async::HTTP::URLEndpoint.parse(options[:bind_secure], ssl_context: self.ssl_context)
  insecure_endpoint = Async::HTTP::URLEndpoint.parse(options[:bind_insecure])
  
  container.run(count: 1, name: "Falcon Proxy") do |task, instance|
    proxy = self.proxy
    
    proxy_server = Falcon::Server.new(proxy, secure_endpoint)
    
    proxy_server.run
  end
  
  container.run(count: 1, name: "Falcon Redirector") do |task, instance|
    redirection = self.redirection(secure_endpoint)
    
    redirection_server = Falcon::Server.new(redirection, insecure_endpoint)
    
    redirection_server.run
  end
  
  return container
end

#ssl_contextObject



121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/falcon/hosts.rb', line 121

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 = "falcon"
    context.alpn_protocols = DEFAULT_ALPN_PROTOCOLS
    context.set_params
    
    context.setup
  end
end