Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add ssl-support and tests

* Adds -p and -c command line params for private key and cert.
* Passes them to the handler
* A seperate test setup
  • Loading branch information...
commit e46af1c01dc12f4fcddf06edd1fa5cbe4bacd452 1 parent 31532e9
@skade skade authored
View
16 bin/slanger
@@ -34,6 +34,16 @@ OptionParser.new do |opts|
options[:websocket_host], options[:websocket_port] = p.split(':')
end
+ opts.on '-p', '--private_key_file FILE', "Private key file for SSL transport" do |p|
+ options[:tls_options] ||= {}
+ options[:tls_options][:private_key_file] = p
+ end
+
+ opts.on '-c', '--cert_chain_file FILE', "Certificate chain file for SSL transport" do |p|
+ options[:tls_options] ||= {}
+ options[:tls_options][:cert_chain_file] = p
+ end
+
opts.on "-v", "--[no-]verbose", "Run verbosely" do |v|
options[:debug] = v
end
@@ -43,6 +53,12 @@ end.parse!
raise RuntimeError.new "--#{parameter} STRING is a required argument. Use your Pusher #{parameter}." unless options[parameter.to_sym]
end
+if options[:tls_options]
+ [:cert_chain_file, :private_key_file].each do |param|
+ raise RuntimeError.new "Both --cert_chain_file and --private_key_file need to be specified" unless options[:tls_options][param]
+ end
+end
+
EM.run do
File.tap { |f| require f.expand_path(f.join(f.dirname(__FILE__),'..', 'slanger.rb')) }
Slanger::Config.load options
View
14 lib/slanger/web_socket_server.rb
@@ -5,7 +5,19 @@ module Slanger
module WebSocketServer
def run
EM.run do
- EM::WebSocket.start host: Slanger::Config[:websocket_host], port: Slanger::Config[:websocket_port], debug: Slanger::Config[:debug], app_key: Slanger::Config[:app_key] do |ws|
+ options = {
+ host: Slanger::Config[:websocket_host],
+ port: Slanger::Config[:websocket_port],
+ debug: Slanger::Config[:debug],
+ app_key: Slanger::Config[:app_key]
+ }
+
+ if Slanger::Config[:tls_options]
+ options.merge! secure: true,
+ tls_options: Slanger::Config[:tls_options]
+ end
+
+ EM::WebSocket.start options do |ws|
# Keep track of handler instance in instance of EM::Connection to ensure a unique handler instance is used per connection.
ws.class_eval { attr_accessor :connection_handler }
# Delegate connection management to handler instance.
View
69 spec/integration/ssl_spec.rb
@@ -0,0 +1,69 @@
+require 'bundler/setup'
+
+require 'active_support/json'
+require 'active_support/core_ext/hash'
+require 'eventmachine'
+require 'em-http-request'
+require 'pusher'
+require 'thin'
+require './spec/spec_helper'
+
+describe 'Integration' do
+ let(:errback) { Proc.new { fail 'cannot connect to slanger. your box might be too slow. try increasing sleep value in the before block' } }
+
+ before(:each) do
+ # Fork service. Our integration tests MUST block the main thread because we want to wait for i/o to finish.
+ @server_pid = EM.fork_reactor do
+ require File.expand_path(File.dirname(__FILE__) + '/../../slanger.rb')
+ Thin::Logging.silent = true
+
+ Slanger::Config.load host: '0.0.0.0',
+ api_port: '4567',
+ websocket_port: '8080',
+ app_key: '765ec374ae0a69f4ce44',
+ secret: 'your-pusher-secret',
+ cert_chain_file: 'spec/server.crt',
+ private_key_file: 'spec/server.key'
+
+ Slanger::Service.run
+ end
+ # Give Slanger a chance to start
+ sleep 0.6
+ end
+
+ after(:each) do
+ # Ensure Slanger is properly stopped. No orphaned processes allowed!
+ Process.kill 'SIGKILL', @server_pid
+ Process.wait @server_pid
+ end
+
+ before :all do
+ Pusher.tap do |p|
+ p.host = '0.0.0.0'
+ p.port = 4567
+ p.app_id = 'your-pusher-app-id'
+ p.secret = 'your-pusher-secret'
+ p.key = '765ec374ae0a69f4ce44'
+ end
+ end
+
+ describe 'regular channels:' do
+ it 'pushes messages to interested websocket connections' do
+ messages = em_stream do |websocket, messages|
+ websocket.callback do
+ websocket.send({ event: 'pusher:subscribe', data: { channel: 'MY_CHANNEL'} }.to_json)
+ end if messages.one?
+
+ if messages.length < 3
+ Pusher['MY_CHANNEL'].trigger_async 'an_event', { some: 'data' }
+ else
+ EM.stop
+ end
+
+ end
+
+ messages.should have_attributes connection_established: true, id_present: true,
+ last_event: 'an_event', last_data: { some: 'data' }.to_json
+ end
+ end
+end
View
12 spec/server.crt
@@ -0,0 +1,12 @@
+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CzBkAy53RkTFcBk74MYgECQQDMyAMT0DhlRBqD
+4vrPF+GZ+rMYpeTjuNDOZphIwzxpv70uyh2RNg+7F6U91Qxz6vpbIkz8Zf4TgdwM
+u/rroktxAkEAwO6wyzidm2yrPMPtnwxDIYnH/ETdNraa3JyHsjXsQwGAIG80+hCv
+QfCA/LmvNOm/Mpe1EyiAeI1/YJp4a2xwIQJBAKysFpQ1ZehVtbnxwaSwMWXiE/Q7
+pjYyl7cCoXPxVFai+8WhXa8dE8Shmo75v2dbAsGnuZy177jJLiB6vYjFL7ECQQCI
+Zri7lLVo8zVFasgO0F6N0ZmAMzeqvQNTwZ72UcVNwjvRso3j1fPyTJUFGEpUwIWa
+wUMV3mal1HQf2lYUrL/BAkBBtXqOLFHINHUmffdRSV/2HECYXHazb6lAnL8nnQX0
+vin2ujCli9mcYWnrY7zwlXdAxgQv5Q2ByQT9Fd8S7FjA
+-----END RSA PRIVATE KEY-----
Please sign in to comment.
Something went wrong with that request. Please try again.