-
Notifications
You must be signed in to change notification settings - Fork 21
Support both variants of Net::SSH::Service::Forward#open() #12
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
base: master
Are you sure you want to change the base?
Changes from all commits
6f90bf3
473d59b
dbef24a
7843869
d5f1e89
cd13956
26d6165
1c8e0e3
413ffc5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,6 @@ | |
/tmp/ | ||
*.swp | ||
.DS_Store | ||
|
||
.byebug_history | ||
*.gem |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
net-ssh-gateway | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -99,37 +99,32 @@ def shutdown! | |
# # ... | ||
# gateway.close(port) | ||
# | ||
# This function takes variable arguments. See comments in the case statement | ||
# for details. | ||
# | ||
# The +local_host+ parameter specifies which network interface to bind to locally, | ||
# and defaults to "127.0.0.1" if omitted. | ||
# If +local_port+ is not specified, the next available port will be used. | ||
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) | ||
end | ||
|
||
if block_given? | ||
begin | ||
yield actual_local_port | ||
ensure | ||
close(actual_local_port) | ||
end | ||
def open(*args) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to keep documentation in a single place. Also it probably makes sense to follow the argument options what we have in net-ssh eg local_host in front https://github.com/net-ssh/net-ssh/blob/master/lib/net/ssh/service/forward.rb and not change that. Or go with keyword arguments. |
||
case args.size | ||
when 2 # open(host, port) | ||
open3(*args[0,2], nil) | ||
when 3 # open(host, port, local_port) | ||
open3(*args) | ||
when 4 # open(host, port, local_host, local_port) | ||
open4(*args) | ||
else | ||
return actual_local_port | ||
raise ArgumentError, "Expecting 2..4 arguments, but got #{args.size} instead." | ||
end | ||
rescue Errno::EADDRINUSE | ||
raise if local_port # if a local port was explicitly requested, bubble the error up | ||
retry | ||
end | ||
|
||
# Cancels port-forwarding over an open port that was previously opened via | ||
# #open. | ||
def close(port) | ||
def close(port, bind_address="127.0.0.1") | ||
ensure_open! | ||
|
||
@session_mutex.synchronize do | ||
@session.forward.cancel_local(port) | ||
@session.forward.cancel_local(port, bind_address) | ||
end | ||
end | ||
|
||
|
@@ -193,4 +188,67 @@ def next_port | |
port | ||
end | ||
end | ||
|
||
# Opens a new port on the local host and forwards it to the given host/port | ||
# via the gateway host. If a block is given, the newly allocated port | ||
# number will be yielded to the block, and the port automatically closed | ||
# (see #close) when the block finishes. Otherwise, the port number will be | ||
# returned, and the caller is responsible for closing the port (#close). | ||
# | ||
# gateway.open('host', 80) do |port| | ||
# # ... | ||
# end | ||
# | ||
# port = gateway.open('host', 80) | ||
# # ... | ||
# gateway.close(port) | ||
# | ||
# Unlike open4(), this method always binds to the loopback network | ||
# interface # of "127.0.0.1". | ||
# | ||
# If +local_port+ is not specified, the next available port will be used. | ||
def open3(host, port, local_port=nil) | ||
open4(host, port, "127.0.0.1", local_port) | ||
end | ||
|
||
# Opens a new port on the local host and forwards it to the given host/port | ||
# via the gateway host. If a block is given, the newly allocated port | ||
# number will be yielded to the block, and the port automatically closed | ||
# (see #close) when the block finishes. Otherwise, the port number will be | ||
# returned, and the caller is responsible for closing the port (#close). | ||
# | ||
# gateway.open('host', 80) do |port| | ||
# # ... | ||
# end | ||
# | ||
# port = gateway.open('host', 80) | ||
# # ... | ||
# gateway.close(port) | ||
# | ||
# Unlike open3(), this method specifies which network interface to bind to | ||
# locally via the +local_host+ parameter. | ||
# | ||
# If +local_port+ is not specified, the next available port will be used. | ||
def open4(host, port, local_host, local_port=nil) | ||
ensure_open! | ||
|
||
actual_local_port = local_port || next_port | ||
|
||
@session_mutex.synchronize do | ||
@session.forward.local(local_host, actual_local_port, host, port) | ||
end | ||
|
||
if block_given? | ||
begin | ||
yield actual_local_port | ||
ensure | ||
close(actual_local_port) | ||
end | ||
else | ||
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 | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
module Net | ||
module SSH | ||
class Gateway | ||
VERSION = "2.0.0" | ||
VERSION = "2.1.0" | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's add that to .gitignore it's not needed to everyone