Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add tls support (client and server side).

  • Loading branch information...
commit 72a8fb79929fc632b895512c6326d05f1740c0bc 1 parent a87597a
@jeremyellison authored
Showing with 67 additions and 19 deletions.
  1. +67 −19 bin/em-websocket-proxy
View
86 bin/em-websocket-proxy
@@ -6,6 +6,8 @@ require 'eventmachine'
require 'em-websocket'
options = {}
+tls_options = {}
+
OptionParser.new do |opts|
opts.banner = "Usage: em-websocket-proxy [options]"
@@ -20,6 +22,29 @@ OptionParser.new do |opts|
opts.on("-q", "--remote-port [PORT]", "Port to proxy to") do |port|
options[:remote_port] = port
end
+
+ opts.on("-k", "--key [KEY]", "Private Key") do |key|
+ puts key
+ tls_options[:private_key_file] = "/Users/jeremy/twotoasters/AutonetMobile/test-server/cert/server.key"
+ end
+
+ opts.on("-c", "--cert [CERT]", "Certificate") do |cert|
+ tls_options[:cert_chain_file] = "/Users/jeremy/twotoasters/AutonetMobile/test-server/cert/server.crt"
+ end
+
+ opts.on("-t", "--use-tls", "Use TLS for all connections") do |t|
+ options[:use_tls_remote] = true
+ options[:use_tls_local] = true
+ end
+
+ opts.on("--use-tls-remote", "Use TLS for the connection to the remote-host") do
+ options[:use_tls_remote] = true
+ end
+
+ opts.on("--use-tls-local", "Use TLS for the local connection. Requires crt and key.") do
+ options[:use_tls_local] = true
+ end
+
end.parse!
required_opts = [:port, :remote_host, :remote_port]
@@ -30,31 +55,53 @@ required_opts.each do |opt|
end
end
-EventMachine.run {
+# This is so that options is not out of scope in post_init.
+@@options = options
+
+ws_options = {:host => "0.0.0.0", :port => options[:port]}
+if (options[:use_tls_local])
+ ws_options[:tls_options] = tls_options
+ ws_options[:secure] = true
+end
- class Server < EventMachine::Connection
- def initialize(input, output, server_close, client_close)
- @input = input
- @output = output
- @server_close = server_close
- @client_close = client_close
+class Server < EventMachine::Connection
+ def initialize(input, output, server_close, client_close)
+ super
+ @input = input
+ @output = output
+ @server_close = server_close
+ @client_close = client_close
- @input_sid = @input.subscribe { |msg| send_data msg }
- @client_close_sid = @client_close.subscribe { |msg| close_connection }
- end
+ @input_sid = @input.subscribe { |msg| send_data msg }
+ @client_close_sid = @client_close.subscribe { |msg| close_connection }
+ end
- def receive_data(data)
- @output.push(data)
+ def post_init
+ if (@@options[:use_tls_remote])
+ puts "Starting TLS"
+ start_tls
end
+ end
- def unbind
- @server_close.push("exit")
- @input.unsubscribe(@input_sid)
- @client_close.unsubscribe(@client_close_sid)
- end
- end
+ def receive_data(data)
+ @output.push(data)
+ end
+
+ def unbind
+ @server_close.push("exit")
+ @input.unsubscribe(@input_sid)
+ @client_close.unsubscribe(@client_close_sid)
+ end
+
+ def ssl_handshake_completed
+ puts get_peer_cert
+ puts "Handshake Complete"
+ end
+end
+
+EventMachine.run {
- EventMachine::WebSocket.start(:host => "0.0.0.0", :port => options[:port]) do |ws|
+ EventMachine::WebSocket.start(ws_options) do |ws|
ws.onopen {
output = EM::Channel.new
input = EM::Channel.new
@@ -63,6 +110,7 @@ EventMachine.run {
output_sid = output.subscribe { |msg| ws.send msg }
server_close_sid = server_close.subscribe { |msg| ws.close_connection }
+
EventMachine::connect options[:remote_host], options[:remote_port], Server, input, output, server_close, client_close
ws.onmessage { |msg| input.push(msg)}

0 comments on commit 72a8fb7

Please sign in to comment.
Something went wrong with that request. Please try again.