Class: LocalTunnel::Tunnel
- Inherits:
-
Object
- Object
- LocalTunnel::Tunnel
- 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
-
#host ⇒ Object
Returns the value of attribute host.
-
#key ⇒ Object
Returns the value of attribute key.
-
#port ⇒ Object
Returns the value of attribute port.
Instance Method Summary collapse
-
#initialize(port, key) ⇒ Tunnel
constructor
A new instance of Tunnel.
- #register_tunnel(key = @key) ⇒ Object
- #start_tunnel ⇒ Object
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
#host ⇒ Object
Returns the value of attribute host.
18 19 20 |
# File 'lib/localtunnel/tunnel.rb', line 18 def host @host end |
#key ⇒ Object
Returns the value of attribute key.
18 19 20 |
# File 'lib/localtunnel/tunnel.rb', line 18 def key @key end |
#port ⇒ Object
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_tunnel ⇒ Object
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.(SHELL_HOOK_FILE)) system "#{SHELL_HOOK_FILE} ""#{tunnel['host']}""" if File.exists?(File.(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.('~/.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 |