Class: LocalTunnel::Tunnel

Inherits:
Object
  • Object
show all
Defined in:
lib/localtunnel_patch.rb

Instance Method Summary collapse

Instance Method Details

#start_tunnelObject

Replace sleep call in start_tunnel with a yield to a block to be executed once the tunnel is established.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/localtunnel_patch.rb', line 7

def start_tunnel
  port = @port
  tunnel = @tunnel
  gateway = Net::SSH::Gateway.new(tunnel['host'], tunnel['user'])
  gateway.open_remote(port.to_i, '127.0.0.1', tunnel['through_port'].to_i) do |rp,rh|
    puts "   " << tunnel['banner'] if tunnel.has_key? 'banner'
    puts "   Port #{port} is now publicly accessible from http://#{tunnel['host']} ..."
    begin
      yield
    rescue Interrupt
      gateway.close_remote(rp, rh)
      exit
    end
  end
rescue Net::SSH::AuthenticationFailed
  possible_key = Dir[File.expand_path('~/.ssh/*.pub')].first
  puts "   Failed to authenticate. If this is your first tunnel, you need to"
  puts "   upload a public key using the -k option. Try this:\n\n"
  puts "   localtunnel -k #{possible_key ? possible_key : '~/path/to/key'} #{port}"
  exit
end