Skip to content
Browse files

switching to json payload

  • Loading branch information...
1 parent acf3390 commit 23aa84eadfbce1606179812134ecf2598a2385d6 @progrium progrium committed May 8, 2010
Showing with 15 additions and 9 deletions.
  1. +10 −8 bin/localtunnel
  2. +5 −1 server.py
View
18 bin/localtunnel
@@ -5,6 +5,7 @@ require 'net/ssh/gateway'
require 'net/http'
require 'uri'
require 'optparse'
+require 'json'
require 'lib/gateway'
@@ -26,18 +27,17 @@ key = File.exist?(options[:key].to_s) ? File.open(key_file).read : nil
def register_tunnel(key=nil)
url = URI.parse("http://open.localtunnel.com/")
if key
- return Net::HTTP.post_form(url, {"key" => key}).body
+ return JSON.parse(Net::HTTP.post_form(url, {"key" => key}).body)
else
- return Net::HTTP.get(url)
+ return JSON.parse(Net::HTTP.get(url))
end
end
def start_tunnel(port, tunnel)
- remote_port, host = tunnel.split(':')
- user, host = host.split('@')
- gateway = Net::SSH::Gateway.new(host, user)
- gateway.open_remote(port.to_i, '127.0.0.1', remote_port.to_i) do |a,b|
- puts "http://#{host} now tunnels to local port #{port}..."
+ 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 |a,b|
+ puts tunnel['banner'] if tunnel.has_key? 'banner'
+ puts "Local port #{port} is now accessible from http://#{tunnel['host']} ..."
while true
sleep 1
end
@@ -47,7 +47,9 @@ end
begin
start_tunnel(local_port, register_tunnel(key))
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."
+ puts "upload a public key using the -k option. Try this:\n"
+ puts "localtunnel -k #{possible_key ? possible_key : '~/path/to/key'} #{local_port}"
end
View
6 server.py
@@ -4,10 +4,12 @@
import sys, time
import urlparse
import socket
+import simplejson
SSH_USER = 'localtunnel'
AUTHORIZED_KEYS = '/home/localtunnel/.ssh/authorized_keys'
PORT_RANGE = [9000, 9100]
+BANNER = "This localtunnel service is brought to you by Twilio."
def port_available(port):
try:
@@ -59,12 +61,14 @@ def register_tunnel(self, superhost, key=None):
name = self.find_tunnel_name()
port = self.find_tunnel_port()
self.tunnels[name] = port
- return "%s:%s@%s.%s" % (port, self.user, name, superhost)
+ return simplejson.dumps(
+ dict(through_port=port, user=self.user, host='%s.%s' % (name, superhost), banner=BANNER))
def render(self, request):
host = request.getHeader('host')
name, superhost = host.split('.', 1)
if host.startswith('open.'):
+ request.setHeader('Content-Type', 'application/json')
return self.register_tunnel(superhost, request.args.get('key', [None])[0])
else:
if not name in self.tunnels: return "Not found"

0 comments on commit 23aa84e

Please sign in to comment.
Something went wrong with that request. Please try again.