Class: LocalTunnel::Tunnel

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

Defined Under Namespace

Classes: TunnelApp

Constant Summary collapse

SHELL_HOOK_FILE =
"./.localtunnel_callback"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(port, key) ⇒ Tunnel

Returns a new instance of Tunnel.



20
21
22
23
24
# File 'lib/localtunnel/tunnel.rb', line 20

def initialize(port, key)
  @port = port
  @key  = key
  @host = ""
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



18
19
20
# File 'lib/localtunnel/tunnel.rb', line 18

def host
  @host
end

#keyObject

Returns the value of attribute key.



18
19
20
# File 'lib/localtunnel/tunnel.rb', line 18

def key
  @key
end

#portObject

Returns the value of attribute port.



18
19
20
# File 'lib/localtunnel/tunnel.rb', line 18

def port
  @port
end

Instance Method Details

#register_tunnel(key = @key) ⇒ Object



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

def register_tunnel(key=@key)
  url = URI.parse("http://open.localtunnel.com/")
  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
  @host = resp['host'].split(':').first
  @tunnel = resp
  return resp
rescue
  puts "   [Error] Unable to register tunnel. Perhaps service is down?"
  exit
end

#start_tunnelObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/localtunnel/tunnel.rb', line 45

def start_tunnel
  port = @port
  tunnel = @tunnel
  gateway = Net::SSH::Gateway.new(@host, tunnel['user'], :auth_methods => %w{ publickey })
  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'
    if File.exists?(File.expand_path(SHELL_HOOK_FILE))
      system "#{SHELL_HOOK_FILE} ""#{tunnel['host']}""" if File.exists?(File.expand_path(SHELL_HOOK_FILE))
      if !$?.success?
        puts "   An error occurred executing the callback hook #{SHELL_HOOK_FILE}"
        puts "   (Make sure it is executable)"
      end
    end
    TunnelApp.tunnel = tunnel['host']
    pid = fork {
      TunnelApp.run!
    }
    puts "   Port #{port} is now publicly accessible from http://#{tunnel['host']} ..."
    begin
      sleep 1 while true
    rescue Interrupt
      Process.kill('KILL', pid)
      Process.waitpid(pid)
      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