Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes problem when local_port is set to zero #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions lib/net/ssh/gateway.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ def active?
@active
end

# Returns the gateway host being used for the tunnel
def host?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be just host no question mark

@session.transport.host
end

# Shuts down the gateway by closing all forwarded ports and then closing
# the gateway's SSH session.
def shutdown!
Expand Down Expand Up @@ -118,13 +123,22 @@ def shutdown!
# gateway.close(port)
#
# If +local_port+ is not specified, the next available port will be used.
#
# If local_port is zero then the next available local port will automatically
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't think we need 0 and nil to have different behaviour. Using the ephemeral port with 0 is the right way even is local_port is nil (not specified), and next_port should be deleted.

# be assigned during socket create, and assigned port stored in actual_local_port.
# This is useful if you want to start more than one gateway session at once and want
# to avoid local port clashes based on next_port selection process
def open(host, port, local_port=nil)
ensure_open!

actual_local_port = local_port || next_port

@session_mutex.synchronize do
@session.forward.local(actual_local_port, host, port)
if local_port.nil?
@session.forward.local(actual_local_port, host, port)
else
actual_local_port = @session.forward.local(actual_local_port, host, port)
end
end

if block_given?
Expand Down Expand Up @@ -211,4 +225,4 @@ def next_port
port
end
end
end
end