Skip to content
Browse files

pick a random port for tunnel if none are available

Change-Id: I65b49019ad67091c554adbaaf1401b5d92e1cb00
  • Loading branch information...
1 parent 1648e21 commit 7b363fcc8e74fc24973a0a9c57df4a824b2e111c @vito vito committed Jan 4, 2012
Showing with 11 additions and 5 deletions.
  1. +0 −3 lib/cli/commands/services.rb
  2. +11 −2 lib/cli/tunnel_helper.rb
View
3 lib/cli/commands/services.rb
@@ -114,9 +114,6 @@ def tunnel(service=nil, client_name=nil)
err "Unknown service '#{service}'" unless info
- # TODO: rather than default to a particular port, we should get
- # the defaults based on the service name.. i.e. known services should
- # have known local default ports for this side of the tunnel.
port = pick_tunnel_port(@options[:port] || 10000)
raise VMC::Client::AuthError unless client.logged_in?
View
13 lib/cli/tunnel_helper.rb
@@ -210,12 +210,21 @@ def pick_tunnel_port(port)
begin
TCPSocket.open('localhost', port)
port += 1
- rescue => e
+ rescue
return port
end
end
- err "Could not pick a port between #{original} and #{original + PORT_RANGE - 1}"
+ grab_ephemeral_port
+ end
+
+ def grab_ephemeral_port
+ socket = TCPServer.new('0.0.0.0', 0)
+ socket.setsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEADDR, true)
+ Socket.do_not_reverse_lookup = true
+ port = socket.addr[1]
+ socket.close
+ return port
end
def wait_for_tunnel_start(port)

0 comments on commit 7b363fc

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