From eb1f1e4465bc3358e63d6b9ce05da22ba158492a Mon Sep 17 00:00:00 2001 From: Jamis Buck Date: Sat, 31 Jan 2009 21:37:28 -0700 Subject: Allow local port to be specified to Gateway#open --- CHANGELOG.rdoc | 4 ++++ lib/net/ssh/gateway.rb | 21 +++++++++++++-------- test/gateway_test.rb | 8 +++++++- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.rdoc b/CHANGELOG.rdoc index 4bf62fb..c8d37b2 100644 --- a/CHANGELOG.rdoc +++ b/CHANGELOG.rdoc @@ -1,3 +1,7 @@ +=== (unreleased) + +* Allow local port to be specified [Will Klancnik] + === 1.0.0 / 1 May 2008 * (no changes since the preview release) diff --git a/lib/net/ssh/gateway.rb b/lib/net/ssh/gateway.rb index 897291f..9742db9 100644 --- a/lib/net/ssh/gateway.rb +++ b/lib/net/ssh/gateway.rb @@ -111,25 +111,28 @@ class Net::SSH::Gateway # port = gateway.open('host', 80) # # ... # gateway.close(port) - def open(host, port) + # + # If +local_port+ is not specified, the next available port will be used. + def open(host, port, local_port=nil) ensure_open! - local_port = next_port + actual_local_port = local_port || next_port @session_mutex.synchronize do - @session.forward.local(local_port, host, port) + @session.forward.local(actual_local_port, host, port) end if block_given? begin - yield local_port + yield actual_local_port ensure - close(local_port) + close(actual_local_port) end else - return local_port + return actual_local_port end rescue Errno::EADDRINUSE + raise if local_port # if a local port was explicitly requested, bubble the error up retry end @@ -186,7 +189,9 @@ class Net::SSH::Gateway @thread = Thread.new do while @active - @session_mutex.synchronize { @session.process(0.1) } + @session_mutex.synchronize do + @session.process(0.1) + end end end end @@ -200,4 +205,4 @@ class Net::SSH::Gateway port end end -end \ No newline at end of file +end diff --git a/test/gateway_test.rb b/test/gateway_test.rb index 6194886..8d7fac7 100644 --- a/test/gateway_test.rb +++ b/test/gateway_test.rb @@ -27,6 +27,12 @@ class GatewayTest < Test::Unit::TestCase assert_equal [65000, "app1", 22], gateway_session.forward.active_locals[65000] end + def test_open_with_explicit_local_port_should_use_that_port + gateway_session, gateway = new_gateway + assert_equal 8181, gateway.open("app1", 22, 8181) + assert_equal [8181, "app1", 22], gateway_session.forward.active_locals[8181] + end + def test_ssh_should_return_connection_when_no_block_is_given gateway_session, gateway = new_gateway expect_connect_to("127.0.0.1", "user", :port => 65535).returns(result = mock("session")) @@ -113,4 +119,4 @@ class GatewayTest < Test::Unit::TestCase @looping = false end end -end \ No newline at end of file +end -- cgit v1.2.1