Class: SshTunnels::Tunnel
- Inherits:
-
Object
- Object
- SshTunnels::Tunnel
- Defined in:
- lib/ssh_tunnels/tunnel.rb
Overview
SSH Tunnel
Instance Attribute Summary collapse
-
#error ⇒ Object
readonly
Returns the value of attribute error.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #active? ⇒ Boolean
-
#initialize(name, user, config, gateway, passphrase) ⇒ Tunnel
constructor
A new instance of Tunnel.
- #open ⇒ Object
- #shutdown ⇒ Object
- #to_s ⇒ Object
- #toggle ⇒ Object
Constructor Details
#initialize(name, user, config, gateway, passphrase) ⇒ Tunnel
Returns a new instance of Tunnel.
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/ssh_tunnels/tunnel.rb', line 8 def initialize(name, user, config, gateway, passphrase) @name = name @user = user @config = config @passphrase = passphrase @gateway = gateway @session = nil @thread = nil @active = false end |
Instance Attribute Details
#error ⇒ Object (readonly)
Returns the value of attribute error.
6 7 8 |
# File 'lib/ssh_tunnels/tunnel.rb', line 6 def error @error end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/ssh_tunnels/tunnel.rb', line 6 def name @name end |
Instance Method Details
#active? ⇒ Boolean
44 45 46 |
# File 'lib/ssh_tunnels/tunnel.rb', line 44 def active? @active && @thread&.alive? end |
#open ⇒ Object
34 35 36 37 38 39 40 41 42 |
# File 'lib/ssh_tunnels/tunnel.rb', line 34 def open @session = Net::SSH.start(@gateway.fetch('host'), @gateway.fetch('user', @user), ) forward_local @active = true @thread = Thread.new { @session.loop(0.001) { @active } } rescue StandardError shutdown raise end |
#shutdown ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/ssh_tunnels/tunnel.rb', line 48 def shutdown @active = false @thread&.join @session&.close @session = nil @thread = nil end |
#to_s ⇒ Object
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/ssh_tunnels/tunnel.rb', line 19 def to_s base = if local_host "#{local_host}:#{local_port}:#{remote_host}:#{remote_port}" else "#{local_port}:#{remote_host}:#{remote_port}" end return base unless @error "#{base} (#{@error})" end |
#toggle ⇒ Object
30 31 32 |
# File 'lib/ssh_tunnels/tunnel.rb', line 30 def toggle active? ? shutdown : open end |