Add proper gem dependencies for opening the connection properly

This commit is contained in:
R. Tyler Croy 2013-03-10 17:06:09 -07:00
parent 320f90dbac
commit c5723be5b3
5 changed files with 72 additions and 58 deletions

View File

@ -1,5 +1,6 @@
#!/usr/bin/env ruby
# Copyright (c) 2010 Jeff Lindsay
# Copyright (c) 2013 R. Tyler Croy
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
@ -24,16 +25,16 @@
require 'rubygems'
require 'optparse'
require 'localtunnel'
require 'passageway'
key = nil
options = OptionParser.new do |o|
o.banner =
"Usage: localtunnel [options] <localport>\n\n" +
o.banner =
"Usage: passageway [options] <localport>\n\n" +
"The file .localtunnel_callback, if it exists in the current working directory,\n"+
"will be executed with the tunnel endpoint as the first argument once the \n" +
"tunnel is started\n\n"
o.on("-k", "--key FILE", "upload a public key for authentication") do |k|
key = File.exist?(k.to_s) ? File.open(k).read : nil
end
@ -47,6 +48,6 @@ unless local_port.to_i.between?(1, 65535)
exit
end
t = LocalTunnel::Tunnel.new(local_port, key)
t = Passageway::Client.new(local_port, key)
t.register_tunnel
t.start_tunnel

View File

@ -5,68 +5,69 @@ require 'net/http'
require 'uri'
require 'json'
require 'localtunnel/net_ssh_gateway_patch'
require 'passageway/net_ssh_gateway_patch'
module LocalTunnel; end
module Passageway
class Client
class LocalTunnel::Tunnel
SHELL_HOOK_FILE = "./.localtunnel_callback"
SHELL_HOOK_FILE = "./.localtunnel_callback"
attr_accessor :port, :key, :host
attr_accessor :port, :key, :host
def initialize(port, key)
@port = port
@key = key
@host = ""
end
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))
def initialize(port, key)
@port = port
@key = key
@host = ""
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
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 "#{File.expand_path(SHELL_HOOK_FILE)} ""#{tunnel['host']}"""
if !$?.success?
puts " An error occurred executing the callback hook #{SHELL_HOOK_FILE}"
puts " (Make sure it is executable)"
end
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
puts " Port #{port} is now publicly accessible from http://#{tunnel['host']} ..."
begin
sleep 1 while true
rescue Interrupt
gateway.close_remote(rp, rh)
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
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 "#{File.expand_path(SHELL_HOOK_FILE)} ""#{tunnel['host']}"""
if !$?.success?
puts " An error occurred executing the callback hook #{SHELL_HOOK_FILE}"
puts " (Make sure it is executable)"
end
end
puts " Port #{port} is now publicly accessible from http://#{tunnel['host']} ..."
begin
sleep 1 while true
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.pub'} #{port}\n\n"
puts " Don't have a key? Check out http://bit.ly/createsshkey"
exit
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.pub'} #{port}\n\n"
puts " Don't have a key? Check out http://bit.ly/createsshkey"
exit
end
end

View File

@ -20,4 +20,8 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "bundler", "~> 1.3"
spec.add_development_dependency "rake"
spec.add_dependency 'json'
spec.add_dependency 'net-ssh'
spec.add_dependency 'net-ssh-gateway'
end

6
spec/client_spec.rb Normal file
View File

@ -0,0 +1,6 @@
require 'spec_helper'
require 'passageway/client'
describe Passageway::Client do
end

View File

@ -0,0 +1,2 @@
$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')