Class: LocalTunnel::Tunnel

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tunnel_host, reflected_host, reflected_port, key) ⇒ Tunnel

Returns a new instance of Tunnel.



16
17
18
19
20
21
22
# File 'lib/localtunnel/tunnel.rb', line 16

def initialize(tunnel_host, reflected_host, reflected_port, key)
  @tunnel_host = tunnel_host
  @reflected_host = reflected_host
  @reflected_port = reflected_port
  @key  = key
  @host = ""
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



14
15
16
# File 'lib/localtunnel/tunnel.rb', line 14

def host
  @host
end

#keyObject

Returns the value of attribute key.



14
15
16
# File 'lib/localtunnel/tunnel.rb', line 14

def key
  @key
end

#reflected_hostObject

Returns the value of attribute reflected_host.



14
15
16
# File 'lib/localtunnel/tunnel.rb', line 14

def reflected_host
  @reflected_host
end

#reflected_portObject

Returns the value of attribute reflected_port.



14
15
16
# File 'lib/localtunnel/tunnel.rb', line 14

def reflected_port
  @reflected_port
end

#tunnel_hostObject

Returns the value of attribute tunnel_host.



14
15
16
# File 'lib/localtunnel/tunnel.rb', line 14

def tunnel_host
  @tunnel_host
end

Instance Method Details

#register_tunnel(key = @key) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/localtunnel/tunnel.rb', line 24

def register_tunnel(key=@key)
  url = URI.parse("http://#{tunnel_host}/")
  if key
    resp = JSON.parse(Net::HTTP.post_form(url, {"key" => key}).body)
  else
    resp = JSON.parse(Net::HTTP.get(url))
  end
  if resp.has_key? 'error'
    puts "   [Error] #{resp['error']}"
    exit
  end
  @ssh_host = resp['host'].split(':').first
  @tunnel = resp
  return resp
rescue
  puts "   [Error] Unable to register tunnel. Perhaps service is down?"
  exit
end

#start_tunnel(mogotest) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/localtunnel/tunnel.rb', line 43

def start_tunnel(mogotest)
  tunnel = @tunnel
  gateway = Net::SSH::Gateway.new(@ssh_host, tunnel['user'])
  gateway.open_remote(reflected_port.to_i, reflected_host, tunnel['through_port'].to_i) do |rp,rh|
    puts "   You're good to go. Any tests you run against '#{mogotest.test_host}' on Mogotest will now access the site running on #{reflected_host}:#{reflected_port}."
    begin
      sleep 1 while true
    rescue Interrupt
      gateway.close_remote(rp, rh)
    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 "   mogotest -k #{possible_key ? possible_key : '~/path/to/key'} #{mogotest.api_key} #{mogotest.test_host} #{port}"
  exit
end