Class: PublishToWeb::Tunnel
- Inherits:
-
Object
- Object
- PublishToWeb::Tunnel
- Defined in:
- lib/publish_to_web/tunnel.rb
Instance Attribute Summary collapse
-
#bind_host ⇒ Object
readonly
Returns the value of attribute bind_host.
-
#forward_port ⇒ Object
readonly
Returns the value of attribute forward_port.
-
#identity ⇒ Object
readonly
Returns the value of attribute identity.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#proxy_host ⇒ Object
readonly
Returns the value of attribute proxy_host.
-
#proxy_port ⇒ Object
readonly
Returns the value of attribute proxy_port.
-
#proxy_user ⇒ Object
readonly
Returns the value of attribute proxy_user.
-
#remote_port ⇒ Object
readonly
Returns the value of attribute remote_port.
Instance Method Summary collapse
-
#initialize(proxy_host:, proxy_user:, proxy_port:, identity:, bind_host:, remote_port:, forward_port:, logger:) ⇒ Tunnel
constructor
A new instance of Tunnel.
- #local_port ⇒ Object
- #running? ⇒ Boolean
- #ssh_options ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(proxy_host:, proxy_user:, proxy_port:, identity:, bind_host:, remote_port:, forward_port:, logger:) ⇒ Tunnel
Returns a new instance of Tunnel.
5 6 7 8 9 10 11 12 13 14 |
# File 'lib/publish_to_web/tunnel.rb', line 5 def initialize(proxy_host:, proxy_user:, proxy_port:, identity:, bind_host:, remote_port:, forward_port:, logger:) @proxy_host = proxy_host @proxy_user = proxy_user @proxy_port = proxy_port @identity = identity @bind_host = bind_host @remote_port = remote_port @forward_port = forward_port @logger = logger end |
Instance Attribute Details
#bind_host ⇒ Object (readonly)
Returns the value of attribute bind_host.
3 4 5 |
# File 'lib/publish_to_web/tunnel.rb', line 3 def bind_host @bind_host end |
#forward_port ⇒ Object (readonly)
Returns the value of attribute forward_port.
3 4 5 |
# File 'lib/publish_to_web/tunnel.rb', line 3 def forward_port @forward_port end |
#identity ⇒ Object (readonly)
Returns the value of attribute identity.
3 4 5 |
# File 'lib/publish_to_web/tunnel.rb', line 3 def identity @identity end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
3 4 5 |
# File 'lib/publish_to_web/tunnel.rb', line 3 def logger @logger end |
#proxy_host ⇒ Object (readonly)
Returns the value of attribute proxy_host.
3 4 5 |
# File 'lib/publish_to_web/tunnel.rb', line 3 def proxy_host @proxy_host end |
#proxy_port ⇒ Object (readonly)
Returns the value of attribute proxy_port.
3 4 5 |
# File 'lib/publish_to_web/tunnel.rb', line 3 def proxy_port @proxy_port end |
#proxy_user ⇒ Object (readonly)
Returns the value of attribute proxy_user.
3 4 5 |
# File 'lib/publish_to_web/tunnel.rb', line 3 def proxy_user @proxy_user end |
#remote_port ⇒ Object (readonly)
Returns the value of attribute remote_port.
3 4 5 |
# File 'lib/publish_to_web/tunnel.rb', line 3 def remote_port @remote_port end |
Instance Method Details
#local_port ⇒ Object
35 36 37 38 39 40 |
# File 'lib/publish_to_web/tunnel.rb', line 35 def local_port server = TCPServer.new('127.0.0.1', 0) server.addr[1] ensure server.close end |
#running? ⇒ Boolean
46 47 48 |
# File 'lib/publish_to_web/tunnel.rb', line 46 def running? @running end |
#ssh_options ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/publish_to_web/tunnel.rb', line 16 def ||= { keepalive: true, keepalive_interval: 5, paranoid: false, # ExitOnForwardFailure ?? use_agent: false, user_known_hosts_file: "/dev/null", port: proxy_port, key_data: [identity], # We need to make another logger here because verbose: :warn # will change the log level on the logger - we want to keep # the info messages from the client in general but avoid the # low-level noise from net/ssh logger: PublishToWeb.create_logger, verbose: :warn } end |
#start ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/publish_to_web/tunnel.rb', line 50 def start Net::SSH.start proxy_host, proxy_user, do |ssh| ssh.forward.remote forward_port, bind_host, remote_port do |real_remote_port| # Indicate to our caller that we have established the connection successfully yield if block_given? logger.info "Established remote forwarding at port #{real_remote_port}" end ssh.forward.local(local_port, bind_host, 8765).tap do |real_local_port| logger.info "Established local forwarding at port #{real_local_port}" end logger.info "Entering keepalive loop" @running = true ssh.loop { @running } end end |
#stop ⇒ Object
42 43 44 |
# File 'lib/publish_to_web/tunnel.rb', line 42 def stop @running = false end |