Permalink
Browse files

enable https

Change-Id: I46715e3aa936ce7e22cd68435892badc40a48246
  • Loading branch information...
1 parent 07c9720 commit 0e6aa7f47f151424647f087b505dc3eb968ecc67 Patrick Bozeman committed Oct 20, 2011
@@ -5,7 +5,7 @@
module Caldecott
module Client
def self.sanitize_url(tun_url)
- tun_url = tun_url =~ /(http|ws).*/i ? tun_url : "http://#{tun_url}"
+ tun_url = tun_url =~ /(http|https|ws).*/i ? tun_url : "https://#{tun_url}"
end
def self.start(opts)
@@ -15,6 +15,7 @@ def self.start(opts)
dst_port = opts[:dst_port]
log_file = opts[:log_file]
log_level = opts[:log_level]
+ auth_token = opts[:auth_token]
trap("TERM") { stop }
trap("INT") { stop }
@@ -34,7 +35,7 @@ def self.start(opts)
conn.onopen do
log.debug "local connected"
- tun = Tunnel.start(log, tun_url, dst_host, dst_port)
+ tun = Tunnel.start(log, tun_url, dst_host, dst_port, auth_token)
end
tun.onopen do
@@ -8,8 +8,8 @@ module Client
class HttpTunnel
MAX_RETRIES = 10
- def initialize(logger, url, dst_host, dst_port)
- @log = logger
+ def initialize(logger, url, dst_host, dst_port, auth_token)
+ @log, @auth_token = logger, auth_token
@closing = false
@retries = 0
init_msg = ""
@@ -75,7 +75,7 @@ def start(base_uri, init_msg)
parsed_uri.path = '/tunnels'
@log.debug "post #{parsed_uri.to_s}"
- req = EM::HttpRequest.new(parsed_uri.to_s).post :body => init_msg
+ req = EM::HttpRequest.new(parsed_uri.to_s).post :body => init_msg, :head => { "Auth-Token" => @auth_token }
req.callback do
@log.debug "post #{parsed_uri.to_s} #{req.response_header.status}"
@@ -89,10 +89,10 @@ def start(base_uri, init_msg)
@tun_uri = parsed_uri.to_s
parsed_uri.path = resp["path_out"]
- @reader = Reader.new(@log, parsed_uri.to_s, self)
+ @reader = Reader.new(@log, parsed_uri.to_s, self, @auth_token)
parsed_uri.path = resp["path_in"]
- @writer = Writer.new(@log, parsed_uri.to_s, self)
+ @writer = Writer.new(@log, parsed_uri.to_s, self, @auth_token)
trigger_on_open
end
end
@@ -118,7 +118,7 @@ def stop
return if @tun_uri.nil?
@log.debug "delete #{@tun_uri}"
- req = EM::HttpRequest.new("#{@tun_uri}").delete
+ req = EM::HttpRequest.new("#{@tun_uri}").delete :head => { "Auth-Token" => @auth_token }
req.errback do
@log.debug "delete #{@tun_uri} error"
@@ -136,8 +136,8 @@ def stop
end
class Reader
- def initialize(log, uri, conn)
- @log, @base_uri, @conn = log, uri, conn
+ def initialize(log, uri, conn, auth_token)
+ @log, @base_uri, @conn, @auth_token = log, uri, conn
@retries = 0
@closing = false
start
@@ -156,7 +156,7 @@ def start(seq = 1)
return if @closing
uri = "#{@base_uri}/#{seq}"
@log.debug "get #{uri}"
- req = EM::HttpRequest.new(uri).get :timeout => 0
+ req = EM::HttpRequest.new(uri).get :timeout => 0, :head => { "Auth-Token" => @auth_token }
req.errback do
@log.debug "get #{uri} error"
@@ -180,8 +180,8 @@ def start(seq = 1)
end
class Writer
- def initialize(log, uri, conn)
- @log, @uri, @conn = log, uri, conn
+ def initialize(log, uri, conn, auth_token)
+ @log, @uri, @conn, @auth_token = log, uri, conn, auth_token
@retries = 0
@seq, @write_buffer = 1, ""
@closing = @writing = false
@@ -208,7 +208,7 @@ def send_data_buffered
@writing = true
uri = "#{@uri}/#{@seq}"
@log.debug "put #{uri}"
- req = EM::HttpRequest.new(uri).put :body => data
+ req = EM::HttpRequest.new(uri).put :body => data, :head => { "Auth-Token" => @auth_token }
req.errback do
@log.debug "put #{uri} error"
@@ -8,12 +8,12 @@ module Tunnel
# Note: I wanted to do this with self#new but had issues
# with getting send :initialize to figure out the right
# number of arguments
- def self.start(logger, tun_url, dst_host, dst_port)
+ def self.start(logger, tun_url, dst_host, dst_port, auth_token)
case Addressable::URI.parse(tun_url).normalized_scheme
- when "http"
- HttpTunnel.new(logger, tun_url, dst_host, dst_port)
+ when "http", "https"
+ HttpTunnel.new(logger, tun_url, dst_host, dst_port, auth_token)
when "ws"
- WebSocketTunnel.new(logger, tun_url, dst_host, dst_port)
+ WebSocketTunnel.new(logger, tun_url, dst_host, dst_port, auth_token)
else
raise "invalid url"
end
@@ -5,7 +5,7 @@
module Caldecott
module Client
class WebSocketTunnel
- def initialize(logger, url, dst_host, dst_port)
+ def initialize(logger, url, dst_host, dst_port, auth_token)
@ws = EM::HttpRequest.new("#{url}/websocket/#{dst_host}/#{dst_port}").get :timeout => 0
end
@@ -162,6 +162,10 @@ def tunnel_from_id(tun_id)
tun
end
+ before do
+ not_found if env['HTTP_AUTH_TOKEN'] != settings.auth_token
+ end
+
get '/' do
return "Caldecott Tunnel (HTTP Transport) #{VERSION}\n"
end
@@ -13,20 +13,28 @@
@auth_token = 'this_is_an_auth_token'
end
- # FIXME: we need an https and wss test
-
it 'should start an http tunnel when given a http url' do
url = 'http://tunnel.com/'
Caldecott::Client::HttpTunnel.should_receive(:new).once.with(@log, url, @host, @port, @auth_token)
Caldecott::Client::Tunnel.start(@log, url, @host, @port, @auth_token)
end
- it 'should start a websocket tunnel when given a websocket url' do
+ it 'should start an https tunnel when given a https url' do
+ url = 'https://tunnel.com/'
+ Caldecott::Client::HttpTunnel.should_receive(:new).once.with(@log, url, @host, @port, @auth_token)
+ Caldecott::Client::Tunnel.start(@log, url, @host, @port, @auth_token)
+ end
+
+ it 'should start a websocket tunnel when given a ws url' do
url = 'ws://tunnel.com/'
Caldecott::Client::WebSocketTunnel.should_receive(:new).once.with(@log, url, @host, @port, @auth_token)
Caldecott::Client::Tunnel.start(@log, url, @host, @port, @auth_token)
end
+ it 'should start a secure websocket tunnel when given a wss url' do
+ pending "full end-to-end wss testing hasn't been done yet"
+ end
+
it 'should raise an error when given an invalid url' do
lambda { Caldecott::Client::Tunnel.start(@log, 'wtf://tunnel.com/', @host, @port, @auth_token) }.should raise_exception
end

0 comments on commit 0e6aa7f

Please sign in to comment.