Revision 0eb81790
Added by Dmitri Dolguikh almost 9 years ago
lib/proxy/plugin.rb | ||
---|---|---|
end
|
||
|
||
def enabled_plugins
|
||
plugins = @@enabled.select {|name, instance| instance.is_a?(::Proxy::Plugin)}
|
||
plugins.values
|
||
@@enabled.values.select {|instance| instance.is_a?(::Proxy::Plugin)}
|
||
end
|
||
|
||
def find_provider(provider_name)
|
||
providers = @@enabled.select {|name, instance| instance.is_a?(::Proxy::Provider)}
|
||
provider = providers[provider_name.to_sym]
|
||
raise ::Proxy::PluginProviderNotFound, "Provider '#{provider_name}' could not be found" unless provider
|
||
provider = @@enabled[provider_name.to_sym]
|
||
raise ::Proxy::PluginProviderNotFound, "Provider '#{provider_name}' could not be found" if provider.nil? || !provider.is_a?(::Proxy::Provider)
|
||
provider
|
||
end
|
||
end
|
lib/smart_proxy.rb | ||
---|---|---|
logger.warn "Missing SSL setup, https is disabled."
|
||
nil
|
||
else
|
||
begin
|
||
app = Rack::Builder.new do
|
||
::Proxy::Plugins.enabled_plugins.each {|p| instance_eval(p.https_rackup)}
|
||
end
|
||
|
||
Rack::Server.new(
|
||
:app => app,
|
||
:server => :webrick,
|
||
:Host => SETTINGS.bind_host,
|
||
:Port => SETTINGS.https_port,
|
||
:SSLEnable => true,
|
||
:SSLVerifyClient => OpenSSL::SSL::VERIFY_PEER,
|
||
:SSLPrivateKey => OpenSSL::PKey::RSA.new(File.read(SETTINGS.ssl_private_key)),
|
||
:SSLCertificate => OpenSSL::X509::Certificate.new(File.read(SETTINGS.ssl_certificate)),
|
||
:SSLCACertificateFile => SETTINGS.ssl_ca_file,
|
||
:daemonize => false,
|
||
:pid => SETTINGS.daemon ? pid_path : nil)
|
||
rescue => e
|
||
logger.error "Unable to access the SSL keys. Are the values correct in settings.yml and do permissions allow reading?: #{e}"
|
||
nil
|
||
app = Rack::Builder.new do
|
||
::Proxy::Plugins.enabled_plugins.each {|p| instance_eval(p.https_rackup)}
|
||
end
|
||
|
||
Rack::Server.new(
|
||
:app => app,
|
||
:server => :webrick,
|
||
:Host => SETTINGS.bind_host,
|
||
:Port => SETTINGS.https_port,
|
||
:SSLEnable => true,
|
||
:SSLVerifyClient => OpenSSL::SSL::VERIFY_PEER,
|
||
:SSLPrivateKey => load_ssl_private_key(SETTINGS.ssl_private_key),
|
||
:SSLCertificate => load_ssl_certificate(SETTINGS.ssl_certificate),
|
||
:SSLCACertificateFile => SETTINGS.ssl_ca_file,
|
||
:daemonize => false,
|
||
:pid => SETTINGS.daemon ? pid_path : nil)
|
||
end
|
||
end
|
||
|
||
def load_ssl_private_key(path)
|
||
OpenSSL::PKey::RSA.new(File.read(path))
|
||
rescue Exception => e
|
||
logger.error "Unable to load private SSL key. Are the values correct in settings.yml and do permissions allow reading?: #{e}"
|
||
raise e
|
||
end
|
||
|
||
def load_ssl_certificate(path)
|
||
OpenSSL::X509::Certificate.new(File.read(path))
|
||
rescue Exception => e
|
||
logger.error "Unable to load SSL certificate. Are the values correct in settings.yml and do permissions allow reading?: #{e}"
|
||
raise e
|
||
end
|
||
|
||
def launch
|
||
::Proxy::Plugins.configure_loaded_plugins
|
||
|
||
create_pid_dir
|
||
http_app = http_app()
|
||
https_app = https_app()
|
||
|
||
if http_app.nil? && https_app.nil?
|
||
logger.error("Both http and https are disabled, unable to start.")
|
||
exit(1)
|
||
end
|
||
raise Exception.new("Both http and https are disabled, unable to start.") if http_app.nil? && https_app.nil?
|
||
|
||
Process.daemon if SETTINGS.daemon
|
||
|
||
... | ... | |
end
|
||
|
||
(t1 || t2).join
|
||
rescue SignalException => e
|
||
# This is to prevent the exception handler below from catching SignalException exceptions.
|
||
logger.info("Caught #{e}. Exiting")
|
||
raise
|
||
rescue SystemExit
|
||
# do nothing. This is to prevent the exception handler below from catching SystemExit exceptions.
|
||
raise
|
||
rescue Exception => e
|
||
logger.error("Error during startup, terminating. #{e}")
|
||
logger.debug("#{e}:#{e.backtrace.join("\n")}")
|
||
|
||
puts "Errors detected on startup, see log for details. Exiting."
|
||
exit(1)
|
||
end
|
||
end
|
||
end
|
test/plugin_test.rb | ||
---|---|---|
require 'test_helper'
|
||
|
||
class Proxy::Plugins
|
||
def self.reset
|
||
@@enabled = {}
|
||
end
|
||
end
|
||
|
||
class Proxy::Plugin
|
||
class << self
|
||
attr_writer :settings
|
||
... | ... | |
end
|
||
|
||
class PluginTest < Test::Unit::TestCase
|
||
def setup
|
||
::Proxy::Plugins.reset
|
||
end
|
||
|
||
class TestPlugin1 < Proxy::Plugin; end
|
||
def test_log_used_default_settings
|
||
plugin = TestPlugin1.new
|
||
... | ... | |
assert_equal ':a: a, :b: b, :enabled: false', plugin.log_used_default_settings
|
||
end
|
||
|
||
class TestPlugin1A < Proxy::Plugin; plugin :test_plugin_1a, "1.0"; default_settings :enabled => true; end
|
||
class TestPlugin1B < Proxy::Plugin; end
|
||
class TestPlugin1C < Proxy::Provider; plugin :test_plugin_1c, "1.0", :factory => nil; default_settings :enabled => true; end
|
||
def test_enabled_plugins
|
||
TestPlugin1A.new.configure_plugin
|
||
TestPlugin1C.new.configure_plugin
|
||
|
||
assert_equal 1, Proxy::Plugins.enabled_plugins.size
|
||
assert Proxy::Plugins.enabled_plugins.first.is_a?(PluginTest::TestPlugin1A)
|
||
end
|
||
|
||
class TestPlugin1D < Proxy::Plugin; plugin :test_plugin_1d, "1.0"; default_settings :enabled => true; end
|
||
class TestPlugin1E < Proxy::Provider; plugin :test_plugin_1e, "1.0", :factory => nil; default_settings :enabled => true; end
|
||
class TestPlugin1F < Proxy::Provider; plugin :test_plugin_1f, "1.0", :factory => nil; default_settings :enabled => true; end
|
||
def test_find_provider
|
||
TestPlugin1E.new.configure_plugin
|
||
TestPlugin1F.new.configure_plugin
|
||
|
||
assert Proxy::Plugins.find_provider(:test_plugin_1e).is_a?(PluginTest::TestPlugin1E)
|
||
end
|
||
|
||
def test_find_provider_should_raise_exception_if_no_provider_exists
|
||
assert_raises ::Proxy::PluginProviderNotFound do
|
||
Proxy::Plugins.find_provider(:nonexistent)
|
||
end
|
||
end
|
||
|
||
class TestPlugin1G < Proxy::Plugin; plugin :test_plugin_1g, "1.0"; default_settings :enabled => true; end
|
||
def test_find_provider_should_raise_exception_if_provider_is_of_wrong_class
|
||
TestPlugin1G.new.configure_plugin
|
||
|
||
assert_equal 1, Proxy::Plugins.enabled_plugins.size
|
||
assert_raises ::Proxy::PluginProviderNotFound do
|
||
Proxy::Plugins.find_provider(:test_plugin_1g)
|
||
end
|
||
end
|
||
|
||
class TestPlugin2 < Proxy::Plugin; plugin :test2, '1.0'; end
|
||
def test_http_rackup_returns_empty_string_with_missing_rackup_path
|
||
assert_equal "", TestPlugin2.new.http_rackup
|
Also available in: Unified diff
fixes #11098: fixed ruby 1.8-specific issues around plugin and provider searches